Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1 | /* |
| 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> |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 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> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 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. |
| 21 | * |
| 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. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | #include "defs.h" |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 35 | #include <sys/param.h> |
| 36 | #include <fcntl.h> |
Mike Frysinger | 54646b8 | 2015-08-19 13:29:27 -0400 | [diff] [blame] | 37 | #include <stdarg.h> |
Dmitry V. Levin | d93c4e8 | 2015-06-17 20:09:13 +0000 | [diff] [blame] | 38 | #ifdef HAVE_SYS_XATTR_H |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 39 | # include <sys/xattr.h> |
| 40 | #endif |
Dmitry V. Levin | b2fa2be | 2014-11-21 20:46:16 +0000 | [diff] [blame] | 41 | #include <sys/uio.h> |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 42 | |
Dmitry V. Levin | 5503dd2 | 2015-02-13 02:12:14 +0000 | [diff] [blame] | 43 | #include "regs.h" |
Dmitry V. Levin | 7211dbc | 2015-02-28 12:20:21 +0000 | [diff] [blame] | 44 | #include "ptrace.h" |
Wichert Akkerman | 2e2553a | 1999-05-09 00:29:58 +0000 | [diff] [blame] | 45 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 46 | int |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 47 | string_to_uint(const char *str) |
| 48 | { |
| 49 | char *error; |
| 50 | long value; |
| 51 | |
| 52 | if (!*str) |
| 53 | return -1; |
| 54 | errno = 0; |
| 55 | value = strtol(str, &error, 10); |
| 56 | if (errno || *error || value < 0 || (long)(int)value != value) |
| 57 | return -1; |
| 58 | return (int)value; |
| 59 | } |
| 60 | |
| 61 | int |
Dmitry V. Levin | 447db45 | 2014-05-29 17:59:01 +0000 | [diff] [blame] | 62 | tv_nz(const struct timeval *a) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 63 | { |
| 64 | return a->tv_sec || a->tv_usec; |
| 65 | } |
| 66 | |
| 67 | int |
Dmitry V. Levin | 447db45 | 2014-05-29 17:59:01 +0000 | [diff] [blame] | 68 | tv_cmp(const struct timeval *a, const struct timeval *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 69 | { |
| 70 | if (a->tv_sec < b->tv_sec |
| 71 | || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) |
| 72 | return -1; |
| 73 | if (a->tv_sec > b->tv_sec |
| 74 | || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec)) |
| 75 | return 1; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | double |
Dmitry V. Levin | 447db45 | 2014-05-29 17:59:01 +0000 | [diff] [blame] | 80 | tv_float(const struct timeval *tv) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 81 | { |
| 82 | return tv->tv_sec + tv->tv_usec/1000000.0; |
| 83 | } |
| 84 | |
| 85 | void |
Dmitry V. Levin | 447db45 | 2014-05-29 17:59:01 +0000 | [diff] [blame] | 86 | tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 87 | { |
| 88 | tv->tv_sec = a->tv_sec + b->tv_sec; |
| 89 | tv->tv_usec = a->tv_usec + b->tv_usec; |
Roland McGrath | 58372f5 | 2007-07-24 01:38:22 +0000 | [diff] [blame] | 90 | if (tv->tv_usec >= 1000000) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 91 | tv->tv_sec++; |
| 92 | tv->tv_usec -= 1000000; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
Dmitry V. Levin | 447db45 | 2014-05-29 17:59:01 +0000 | [diff] [blame] | 97 | tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 98 | { |
| 99 | tv->tv_sec = a->tv_sec - b->tv_sec; |
| 100 | tv->tv_usec = a->tv_usec - b->tv_usec; |
| 101 | if (((long) tv->tv_usec) < 0) { |
| 102 | tv->tv_sec--; |
| 103 | tv->tv_usec += 1000000; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
Dmitry V. Levin | 447db45 | 2014-05-29 17:59:01 +0000 | [diff] [blame] | 108 | tv_div(struct timeval *tv, const struct timeval *a, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 109 | { |
| 110 | tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n; |
| 111 | tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000; |
| 112 | tv->tv_usec %= 1000000; |
| 113 | } |
| 114 | |
| 115 | void |
Dmitry V. Levin | 447db45 | 2014-05-29 17:59:01 +0000 | [diff] [blame] | 116 | tv_mul(struct timeval *tv, const struct timeval *a, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 117 | { |
| 118 | tv->tv_usec = a->tv_usec * n; |
Dmitry V. Levin | fefdd97 | 2007-06-29 21:25:56 +0000 | [diff] [blame] | 119 | tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 120 | tv->tv_usec %= 1000000; |
| 121 | } |
| 122 | |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 123 | const char * |
Dmitry V. Levin | c0db59b | 2016-05-14 21:55:35 +0000 | [diff] [blame] | 124 | xlookup(const struct xlat *xlat, const uint64_t val) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 125 | { |
| 126 | for (; xlat->str != NULL; xlat++) |
| 127 | if (xlat->val == val) |
| 128 | return xlat->str; |
| 129 | return NULL; |
| 130 | } |
| 131 | |
Dmitry V. Levin | 4176d53 | 2014-09-21 22:42:45 +0000 | [diff] [blame] | 132 | static int |
| 133 | xlat_bsearch_compare(const void *a, const void *b) |
| 134 | { |
Dmitry V. Levin | 43242e6 | 2016-04-28 18:37:54 +0000 | [diff] [blame] | 135 | const uint64_t val1 = *(const uint64_t *) a; |
| 136 | const uint64_t val2 = ((const struct xlat *) b)->val; |
Dmitry V. Levin | 4176d53 | 2014-09-21 22:42:45 +0000 | [diff] [blame] | 137 | return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0; |
| 138 | } |
| 139 | |
| 140 | const char * |
Dmitry V. Levin | 43242e6 | 2016-04-28 18:37:54 +0000 | [diff] [blame] | 141 | xlat_search(const struct xlat *xlat, const size_t nmemb, const uint64_t val) |
Dmitry V. Levin | 4176d53 | 2014-09-21 22:42:45 +0000 | [diff] [blame] | 142 | { |
| 143 | const struct xlat *e = |
Dmitry V. Levin | 43242e6 | 2016-04-28 18:37:54 +0000 | [diff] [blame] | 144 | bsearch((const void*) &val, |
Dmitry V. Levin | 4176d53 | 2014-09-21 22:42:45 +0000 | [diff] [blame] | 145 | xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare); |
| 146 | |
| 147 | return e ? e->str : NULL; |
| 148 | } |
| 149 | |
Denys Vlasenko | 0a295bc | 2011-09-01 16:31:48 +0200 | [diff] [blame] | 150 | #if !defined HAVE_STPCPY |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 151 | char * |
| 152 | stpcpy(char *dst, const char *src) |
| 153 | { |
| 154 | while ((*dst = *src++) != '\0') |
| 155 | dst++; |
| 156 | return dst; |
| 157 | } |
Denys Vlasenko | 0a295bc | 2011-09-01 16:31:48 +0200 | [diff] [blame] | 158 | #endif |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 159 | |
Denys Vlasenko | b338f2d | 2013-11-09 20:40:31 +0100 | [diff] [blame] | 160 | /* Find a next bit which is set. |
| 161 | * Starts testing at cur_bit. |
| 162 | * Returns -1 if no more bits are set. |
| 163 | * |
| 164 | * We never touch bytes we don't need to. |
| 165 | * On big-endian, array is assumed to consist of |
| 166 | * current_wordsize wide words: for example, is current_wordsize is 4, |
| 167 | * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence. |
| 168 | * On little-endian machines, word size is immaterial. |
| 169 | */ |
| 170 | int |
| 171 | next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits) |
| 172 | { |
| 173 | const unsigned endian = 1; |
| 174 | int little_endian = *(char*)&endian; |
| 175 | |
| 176 | const uint8_t *array = bit_array; |
| 177 | unsigned pos = cur_bit / 8; |
| 178 | unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1; |
| 179 | |
| 180 | for (;;) { |
| 181 | uint8_t bitmask; |
| 182 | uint8_t cur_byte; |
| 183 | |
| 184 | if (cur_bit >= size_bits) |
| 185 | return -1; |
| 186 | cur_byte = array[pos ^ pos_xor_mask]; |
| 187 | if (cur_byte == 0) { |
| 188 | cur_bit = (cur_bit + 8) & (-8); |
| 189 | pos++; |
| 190 | continue; |
| 191 | } |
| 192 | bitmask = 1 << (cur_bit & 7); |
| 193 | for (;;) { |
| 194 | if (cur_byte & bitmask) |
| 195 | return cur_bit; |
| 196 | cur_bit++; |
| 197 | if (cur_bit >= size_bits) |
| 198 | return -1; |
| 199 | bitmask <<= 1; |
| 200 | /* This check *can't be* optimized out: */ |
| 201 | if (bitmask == 0) |
| 202 | break; |
| 203 | } |
| 204 | pos++; |
| 205 | } |
| 206 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 207 | /* |
| 208 | * Print entry in struct xlat table, if there. |
| 209 | */ |
| 210 | void |
Dmitry V. Levin | c132810 | 2016-04-28 18:42:10 +0000 | [diff] [blame] | 211 | printxvals(const uint64_t val, const char *dflt, const struct xlat *xlat, ...) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 212 | { |
Mike Frysinger | 54646b8 | 2015-08-19 13:29:27 -0400 | [diff] [blame] | 213 | va_list args; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 214 | |
Mike Frysinger | 54646b8 | 2015-08-19 13:29:27 -0400 | [diff] [blame] | 215 | va_start(args, xlat); |
| 216 | for (; xlat; xlat = va_arg(args, const struct xlat *)) { |
| 217 | const char *str = xlookup(xlat, val); |
| 218 | |
| 219 | if (str) { |
| 220 | tprints(str); |
| 221 | va_end(args); |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | /* No hits -- print raw # instead. */ |
Dmitry V. Levin | c132810 | 2016-04-28 18:42:10 +0000 | [diff] [blame] | 226 | tprintf("%#" PRIx64 " /* %s */", val, dflt); |
Mike Frysinger | 54646b8 | 2015-08-19 13:29:27 -0400 | [diff] [blame] | 227 | |
| 228 | va_end(args); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 231 | /* |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 232 | * Fetch 64bit argument at position arg_no and |
| 233 | * return the index of the next argument. |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 234 | */ |
| 235 | int |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 236 | getllval(struct tcb *tcp, unsigned long long *val, int arg_no) |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 237 | { |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 238 | #if SIZEOF_LONG > 4 && SIZEOF_LONG == SIZEOF_LONG_LONG |
| 239 | # if SUPPORTED_PERSONALITIES > 1 |
Dmitry V. Levin | 6974c91 | 2015-11-26 18:25:34 +0000 | [diff] [blame] | 240 | # ifdef X86_64 |
| 241 | if (current_personality != 1) { |
| 242 | # else |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 243 | if (current_wordsize > 4) { |
Dmitry V. Levin | 6974c91 | 2015-11-26 18:25:34 +0000 | [diff] [blame] | 244 | # endif |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 245 | # endif |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 246 | *val = tcp->u_arg[arg_no]; |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 247 | arg_no++; |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 248 | # if SUPPORTED_PERSONALITIES > 1 |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 249 | } else { |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 250 | # if defined(AARCH64) || defined(POWERPC64) |
Dmitry V. Levin | 3c49b02 | 2014-08-07 00:07:28 +0000 | [diff] [blame] | 251 | /* Align arg_no to the next even number. */ |
| 252 | arg_no = (arg_no + 1) & 0xe; |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 253 | # endif /* AARCH64 || POWERPC64 */ |
| 254 | *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]); |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 255 | arg_no += 2; |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 256 | } |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 257 | # endif /* SUPPORTED_PERSONALITIES > 1 */ |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 258 | #elif SIZEOF_LONG > 4 |
| 259 | # error Unsupported configuration: SIZEOF_LONG > 4 && SIZEOF_LONG_LONG > SIZEOF_LONG |
| 260 | #elif defined LINUX_MIPSN32 |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 261 | *val = tcp->ext_arg[arg_no]; |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 262 | arg_no++; |
Dmitry V. Levin | 0b46883 | 2013-05-02 08:41:27 +0000 | [diff] [blame] | 263 | #elif defined X32 |
| 264 | if (current_personality == 0) { |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 265 | *val = tcp->ext_arg[arg_no]; |
Dmitry V. Levin | 0b46883 | 2013-05-02 08:41:27 +0000 | [diff] [blame] | 266 | arg_no++; |
| 267 | } else { |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 268 | *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]); |
Dmitry V. Levin | 0b46883 | 2013-05-02 08:41:27 +0000 | [diff] [blame] | 269 | arg_no += 2; |
| 270 | } |
Denys Vlasenko | c9d0fc0 | 2013-02-17 22:41:33 +0100 | [diff] [blame] | 271 | #else |
Dmitry V. Levin | 8e096c4 | 2013-05-06 18:23:01 +0000 | [diff] [blame] | 272 | # if defined __ARM_EABI__ || \ |
| 273 | defined LINUX_MIPSO32 || \ |
| 274 | defined POWERPC || \ |
| 275 | defined XTENSA |
Dmitry V. Levin | 3c49b02 | 2014-08-07 00:07:28 +0000 | [diff] [blame] | 276 | /* Align arg_no to the next even number. */ |
| 277 | arg_no = (arg_no + 1) & 0xe; |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 278 | # endif |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 279 | *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]); |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 280 | arg_no += 2; |
Denys Vlasenko | c9d0fc0 | 2013-02-17 22:41:33 +0100 | [diff] [blame] | 281 | #endif |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 282 | |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 283 | return arg_no; |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 284 | } |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 285 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 286 | /* |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 287 | * Print 64bit argument at position arg_no and |
| 288 | * return the index of the next argument. |
| 289 | */ |
| 290 | int |
| 291 | printllval(struct tcb *tcp, const char *format, int arg_no) |
| 292 | { |
| 293 | unsigned long long val = 0; |
| 294 | |
| 295 | arg_no = getllval(tcp, &val, arg_no); |
| 296 | tprintf(format, val); |
| 297 | return arg_no; |
| 298 | } |
| 299 | |
| 300 | /* |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 301 | * Interpret `xlat' as an array of flags |
| 302 | * print the entries whose bits are on in `flags' |
| 303 | * return # of flags printed. |
| 304 | */ |
Denys Vlasenko | 4924dbd | 2011-08-19 18:06:46 +0200 | [diff] [blame] | 305 | void |
Dmitry V. Levin | c9146eb | 2016-04-28 18:19:27 +0000 | [diff] [blame] | 306 | addflags(const struct xlat *xlat, uint64_t flags) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 307 | { |
Denys Vlasenko | 4924dbd | 2011-08-19 18:06:46 +0200 | [diff] [blame] | 308 | for (; xlat->str; xlat++) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 309 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 310 | tprintf("|%s", xlat->str); |
| 311 | flags &= ~xlat->val; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | if (flags) { |
Dmitry V. Levin | c9146eb | 2016-04-28 18:19:27 +0000 | [diff] [blame] | 315 | tprintf("|%#" PRIx64, flags); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 316 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 319 | /* |
Denys Vlasenko | 4924dbd | 2011-08-19 18:06:46 +0200 | [diff] [blame] | 320 | * Interpret `xlat' as an array of flags. |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 321 | * Print to static string the entries whose bits are on in `flags' |
| 322 | * Return static string. |
| 323 | */ |
| 324 | const char * |
Dmitry V. Levin | 4811023 | 2016-05-15 14:26:03 +0000 | [diff] [blame] | 325 | sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags) |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 326 | { |
| 327 | static char outstr[1024]; |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 328 | char *outptr; |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 329 | int found = 0; |
| 330 | |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 331 | outptr = stpcpy(outstr, prefix); |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 332 | |
Dmitry V. Levin | 71af115 | 2015-11-15 20:44:13 +0000 | [diff] [blame] | 333 | if (flags == 0 && xlat->val == 0 && xlat->str) { |
| 334 | strcpy(outptr, xlat->str); |
| 335 | return outstr; |
| 336 | } |
| 337 | |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 338 | for (; xlat->str; xlat++) { |
Dmitry V. Levin | 71af115 | 2015-11-15 20:44:13 +0000 | [diff] [blame] | 339 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 340 | if (found) |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 341 | *outptr++ = '|'; |
| 342 | outptr = stpcpy(outptr, xlat->str); |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 343 | found = 1; |
Denys Vlasenko | 4f3df07 | 2012-01-29 22:38:35 +0100 | [diff] [blame] | 344 | flags &= ~xlat->val; |
| 345 | if (!flags) |
| 346 | break; |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | if (flags) { |
| 350 | if (found) |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 351 | *outptr++ = '|'; |
Dmitry V. Levin | 01c910c | 2016-04-28 18:49:36 +0000 | [diff] [blame] | 352 | outptr += sprintf(outptr, "%#" PRIx64, flags); |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return outstr; |
| 356 | } |
| 357 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 358 | int |
Dmitry V. Levin | 00beed6 | 2016-04-28 18:55:18 +0000 | [diff] [blame] | 359 | printflags64(const struct xlat *xlat, uint64_t flags, const char *dflt) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 360 | { |
| 361 | int n; |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 362 | const char *sep; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 363 | |
Mike Frysinger | 79bddff | 2015-10-31 00:47:59 -0400 | [diff] [blame] | 364 | if (flags == 0 && xlat->val == 0 && xlat->str) { |
Denys Vlasenko | 5940e65 | 2011-09-01 09:55:05 +0200 | [diff] [blame] | 365 | tprints(xlat->str); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 366 | return 1; |
| 367 | } |
| 368 | |
| 369 | sep = ""; |
| 370 | for (n = 0; xlat->str; xlat++) { |
| 371 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 372 | tprintf("%s%s", sep, xlat->str); |
| 373 | flags &= ~xlat->val; |
| 374 | sep = "|"; |
| 375 | n++; |
| 376 | } |
| 377 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 378 | |
| 379 | if (n) { |
| 380 | if (flags) { |
Dmitry V. Levin | 00beed6 | 2016-04-28 18:55:18 +0000 | [diff] [blame] | 381 | tprintf("%s%#" PRIx64, sep, flags); |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 382 | n++; |
| 383 | } |
| 384 | } else { |
| 385 | if (flags) { |
Dmitry V. Levin | 00beed6 | 2016-04-28 18:55:18 +0000 | [diff] [blame] | 386 | tprintf("%#" PRIx64, flags); |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 387 | if (dflt) |
| 388 | tprintf(" /* %s */", dflt); |
| 389 | } else { |
| 390 | if (dflt) |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 391 | tprints("0"); |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 392 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 393 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 394 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 395 | return n; |
| 396 | } |
| 397 | |
| 398 | void |
Dmitry V. Levin | 332a326 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 399 | printaddr(const long addr) |
| 400 | { |
| 401 | if (!addr) |
| 402 | tprints("NULL"); |
| 403 | else |
| 404 | tprintf("%#lx", addr); |
| 405 | } |
| 406 | |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 407 | #define DEF_PRINTNUM(name, type) \ |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 408 | bool \ |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 409 | printnum_ ## name(struct tcb *tcp, const long addr, const char *fmt) \ |
| 410 | { \ |
| 411 | type num; \ |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 412 | if (umove_or_printaddr(tcp, addr, &num)) \ |
| 413 | return false; \ |
| 414 | tprints("["); \ |
| 415 | tprintf(fmt, num); \ |
| 416 | tprints("]"); \ |
| 417 | return true; \ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 420 | #define DEF_PRINTPAIR(name, type) \ |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 421 | bool \ |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 422 | printpair_ ## name(struct tcb *tcp, const long addr, const char *fmt) \ |
| 423 | { \ |
| 424 | type pair[2]; \ |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 425 | if (umove_or_printaddr(tcp, addr, &pair)) \ |
| 426 | return false; \ |
| 427 | tprints("["); \ |
| 428 | tprintf(fmt, pair[0]); \ |
| 429 | tprints(", "); \ |
| 430 | tprintf(fmt, pair[1]); \ |
| 431 | tprints("]"); \ |
| 432 | return true; \ |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 435 | DEF_PRINTNUM(int, int) |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 436 | DEF_PRINTPAIR(int, int) |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 437 | DEF_PRINTNUM(short, short) |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 438 | DEF_PRINTNUM(int64, uint64_t) |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 439 | DEF_PRINTPAIR(int64, uint64_t) |
Dmitry V. Levin | 2479ef0 | 2015-08-18 14:58:27 +0000 | [diff] [blame] | 440 | |
| 441 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 442 | bool |
Dmitry V. Levin | 2479ef0 | 2015-08-18 14:58:27 +0000 | [diff] [blame] | 443 | printnum_long_int(struct tcb *tcp, const long addr, |
| 444 | const char *fmt_long, const char *fmt_int) |
| 445 | { |
| 446 | if (current_wordsize > sizeof(int)) { |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 447 | return printnum_int64(tcp, addr, fmt_long); |
Dmitry V. Levin | 2479ef0 | 2015-08-18 14:58:27 +0000 | [diff] [blame] | 448 | } else { |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 449 | return printnum_int(tcp, addr, fmt_int); |
Dmitry V. Levin | 2479ef0 | 2015-08-18 14:58:27 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 452 | #endif |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 453 | |
Dmitry V. Levin | b1a01b8 | 2014-12-06 03:53:16 +0000 | [diff] [blame] | 454 | const char * |
| 455 | sprinttime(time_t t) |
| 456 | { |
| 457 | struct tm *tmp; |
Dmitry V. Levin | d4a9d83 | 2015-01-08 15:08:16 +0000 | [diff] [blame] | 458 | static char buf[sizeof(int) * 3 * 6]; |
Dmitry V. Levin | b1a01b8 | 2014-12-06 03:53:16 +0000 | [diff] [blame] | 459 | |
| 460 | if (t == 0) { |
| 461 | strcpy(buf, "0"); |
| 462 | return buf; |
| 463 | } |
| 464 | tmp = localtime(&t); |
| 465 | if (tmp) |
| 466 | snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d", |
| 467 | tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, |
| 468 | tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 469 | else |
| 470 | snprintf(buf, sizeof buf, "%lu", (unsigned long) t); |
| 471 | |
| 472 | return buf; |
| 473 | } |
| 474 | |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 475 | static char * |
| 476 | getfdproto(struct tcb *tcp, int fd, char *buf, unsigned bufsize) |
| 477 | { |
Dmitry V. Levin | d93c4e8 | 2015-06-17 20:09:13 +0000 | [diff] [blame] | 478 | #ifdef HAVE_SYS_XATTR_H |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 479 | ssize_t r; |
| 480 | char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3]; |
| 481 | |
| 482 | if (fd < 0) |
| 483 | return NULL; |
| 484 | |
| 485 | sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd); |
| 486 | r = getxattr(path, "system.sockprotoname", buf, bufsize - 1); |
| 487 | if (r <= 0) |
| 488 | return NULL; |
| 489 | else { |
| 490 | /* |
| 491 | * This is a protection for the case when the kernel |
| 492 | * side does not append a null byte to the buffer. |
| 493 | */ |
| 494 | buf[r] = '\0'; |
| 495 | return buf; |
| 496 | } |
| 497 | #else |
| 498 | return NULL; |
| 499 | #endif |
| 500 | } |
| 501 | |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 502 | void |
Dmitry V. Levin | 3138213 | 2011-03-04 05:08:02 +0300 | [diff] [blame] | 503 | printfd(struct tcb *tcp, int fd) |
| 504 | { |
Denys Vlasenko | 61ad0a4 | 2013-03-06 18:24:34 +0100 | [diff] [blame] | 505 | char path[PATH_MAX + 1]; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 506 | if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) { |
| 507 | static const char socket_prefix[] = "socket:["; |
| 508 | const size_t socket_prefix_len = sizeof(socket_prefix) - 1; |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 509 | const size_t path_len = strlen(path); |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 510 | |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 511 | tprintf("%d<", fd); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 512 | if (show_fd_path > 1 && |
| 513 | strncmp(path, socket_prefix, socket_prefix_len) == 0 && |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 514 | path[path_len - 1] == ']') { |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 515 | unsigned long inode = |
Dmitry V. Levin | ea8b8e3 | 2016-01-23 16:35:02 +0000 | [diff] [blame] | 516 | strtoul(path + socket_prefix_len, NULL, 10); |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 517 | |
| 518 | if (!print_sockaddr_by_inode_cached(inode)) { |
| 519 | char buf[256]; |
| 520 | const char *proto = |
| 521 | getfdproto(tcp, fd, buf, sizeof(buf)); |
| 522 | if (!print_sockaddr_by_inode(inode, proto)) |
| 523 | tprints(path); |
| 524 | } |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 525 | } else { |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 526 | print_quoted_string(path, path_len, |
| 527 | QUOTE_OMIT_LEADING_TRAILING_QUOTES); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 528 | } |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 529 | tprints(">"); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 530 | } else |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 531 | tprintf("%d", fd); |
Dmitry V. Levin | 3138213 | 2011-03-04 05:08:02 +0300 | [diff] [blame] | 532 | } |
| 533 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 534 | /* |
| 535 | * Quote string `instr' of length `size' |
| 536 | * Write up to (3 + `size' * 4) bytes to `outstr' buffer. |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 537 | * |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 538 | * If QUOTE_0_TERMINATED `style' flag is set, |
| 539 | * treat `instr' as a NUL-terminated string, |
| 540 | * checking up to (`size' + 1) bytes of `instr'. |
| 541 | * |
| 542 | * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set, |
| 543 | * do not add leading and trailing quoting symbols. |
| 544 | * |
| 545 | * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise. |
| 546 | * Note that if QUOTE_0_TERMINATED is not set, always returns 1. |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 547 | */ |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 548 | int |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 549 | string_quote(const char *instr, char *outstr, const unsigned int size, |
| 550 | const unsigned int style) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 551 | { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 552 | const unsigned char *ustr = (const unsigned char *) instr; |
| 553 | char *s = outstr; |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 554 | unsigned int i; |
| 555 | int usehex, c, eol; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 556 | |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 557 | if (style & QUOTE_0_TERMINATED) |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 558 | eol = '\0'; |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 559 | else |
| 560 | eol = 0x100; /* this can never match a char */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 561 | |
| 562 | usehex = 0; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 563 | if (xflag > 1) |
| 564 | usehex = 1; |
| 565 | else if (xflag) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 566 | /* Check for presence of symbol which require |
| 567 | to hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 568 | for (i = 0; i < size; ++i) { |
| 569 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 570 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 571 | if (c == eol) |
| 572 | break; |
Denys Vlasenko | 5198ed4 | 2013-03-06 23:44:23 +0100 | [diff] [blame] | 573 | |
| 574 | /* Force hex unless c is printable or whitespace */ |
| 575 | if (c > 0x7e) { |
| 576 | usehex = 1; |
| 577 | break; |
| 578 | } |
| 579 | /* In ASCII isspace is only these chars: "\t\n\v\f\r". |
| 580 | * They happen to have ASCII codes 9,10,11,12,13. |
| 581 | */ |
| 582 | if (c < ' ' && (unsigned)(c - 9) >= 5) { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 583 | usehex = 1; |
| 584 | break; |
| 585 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 588 | |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 589 | if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES)) |
| 590 | *s++ = '\"'; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 591 | |
| 592 | if (usehex) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 593 | /* Hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 594 | for (i = 0; i < size; ++i) { |
| 595 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 596 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 597 | if (c == eol) |
| 598 | goto asciz_ended; |
| 599 | *s++ = '\\'; |
| 600 | *s++ = 'x'; |
| 601 | *s++ = "0123456789abcdef"[c >> 4]; |
| 602 | *s++ = "0123456789abcdef"[c & 0xf]; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 603 | } |
| 604 | } else { |
| 605 | for (i = 0; i < size; ++i) { |
| 606 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 607 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 608 | if (c == eol) |
| 609 | goto asciz_ended; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 610 | switch (c) { |
| 611 | case '\"': case '\\': |
| 612 | *s++ = '\\'; |
| 613 | *s++ = c; |
| 614 | break; |
| 615 | case '\f': |
| 616 | *s++ = '\\'; |
| 617 | *s++ = 'f'; |
| 618 | break; |
| 619 | case '\n': |
| 620 | *s++ = '\\'; |
| 621 | *s++ = 'n'; |
| 622 | break; |
| 623 | case '\r': |
| 624 | *s++ = '\\'; |
| 625 | *s++ = 'r'; |
| 626 | break; |
| 627 | case '\t': |
| 628 | *s++ = '\\'; |
| 629 | *s++ = 't'; |
| 630 | break; |
| 631 | case '\v': |
| 632 | *s++ = '\\'; |
| 633 | *s++ = 'v'; |
| 634 | break; |
| 635 | default: |
Denys Vlasenko | 5198ed4 | 2013-03-06 23:44:23 +0100 | [diff] [blame] | 636 | if (c >= ' ' && c <= 0x7e) |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 637 | *s++ = c; |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 638 | else { |
| 639 | /* Print \octal */ |
| 640 | *s++ = '\\'; |
| 641 | if (i + 1 < size |
| 642 | && ustr[i + 1] >= '0' |
| 643 | && ustr[i + 1] <= '9' |
| 644 | ) { |
| 645 | /* Print \ooo */ |
| 646 | *s++ = '0' + (c >> 6); |
| 647 | *s++ = '0' + ((c >> 3) & 0x7); |
| 648 | } else { |
| 649 | /* Print \[[o]o]o */ |
| 650 | if ((c >> 3) != 0) { |
| 651 | if ((c >> 6) != 0) |
| 652 | *s++ = '0' + (c >> 6); |
| 653 | *s++ = '0' + ((c >> 3) & 0x7); |
| 654 | } |
| 655 | } |
| 656 | *s++ = '0' + (c & 0x7); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 657 | } |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 663 | if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES)) |
| 664 | *s++ = '\"'; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 665 | *s = '\0'; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 666 | |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 667 | /* Return zero if we printed entire ASCIZ string (didn't truncate it) */ |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 668 | if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') { |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 669 | /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended') |
| 670 | * but next char is NUL. |
| 671 | */ |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | return 1; |
| 676 | |
| 677 | asciz_ended: |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 678 | if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES)) |
| 679 | *s++ = '\"'; |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 680 | *s = '\0'; |
| 681 | /* Return zero: we printed entire ASCIZ string (didn't truncate it) */ |
| 682 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 685 | #ifndef ALLOCA_CUTOFF |
| 686 | # define ALLOCA_CUTOFF 4032 |
| 687 | #endif |
| 688 | #define use_alloca(n) ((n) <= ALLOCA_CUTOFF) |
| 689 | |
| 690 | /* |
| 691 | * Quote string `str' of length `size' and print the result. |
| 692 | * |
| 693 | * If QUOTE_0_TERMINATED `style' flag is set, |
| 694 | * treat `str' as a NUL-terminated string and |
| 695 | * quote at most (`size' - 1) bytes. |
| 696 | * |
| 697 | * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set, |
| 698 | * do not add leading and trailing quoting symbols. |
| 699 | * |
| 700 | * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise. |
| 701 | * Note that if QUOTE_0_TERMINATED is not set, always returns 1. |
| 702 | */ |
| 703 | int |
| 704 | print_quoted_string(const char *str, unsigned int size, |
| 705 | const unsigned int style) |
| 706 | { |
| 707 | char *buf; |
| 708 | char *outstr; |
| 709 | unsigned int alloc_size; |
| 710 | int rc; |
| 711 | |
| 712 | if (size && style & QUOTE_0_TERMINATED) |
| 713 | --size; |
| 714 | |
| 715 | alloc_size = 4 * size; |
| 716 | if (alloc_size / 4 != size) { |
| 717 | error_msg("Out of memory"); |
| 718 | tprints("???"); |
| 719 | return -1; |
| 720 | } |
| 721 | alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2); |
| 722 | |
| 723 | if (use_alloca(alloc_size)) { |
| 724 | outstr = alloca(alloc_size); |
| 725 | buf = NULL; |
| 726 | } else { |
| 727 | outstr = buf = malloc(alloc_size); |
| 728 | if (!buf) { |
| 729 | error_msg("Out of memory"); |
| 730 | tprints("???"); |
| 731 | return -1; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | rc = string_quote(str, outstr, size, style); |
| 736 | tprints(outstr); |
| 737 | |
| 738 | free(buf); |
| 739 | return rc; |
| 740 | } |
| 741 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 742 | /* |
| 743 | * Print path string specified by address `addr' and length `n'. |
| 744 | * If path length exceeds `n', append `...' to the output. |
| 745 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 746 | void |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 747 | printpathn(struct tcb *tcp, long addr, unsigned int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 748 | { |
Dmitry V. Levin | 025b358 | 2014-11-21 22:28:34 +0000 | [diff] [blame] | 749 | char path[PATH_MAX + 1]; |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 750 | int nul_seen; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 751 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 752 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 753 | tprints("NULL"); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 754 | return; |
| 755 | } |
| 756 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 757 | /* Cap path length to the path buffer size */ |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 758 | if (n > sizeof path - 1) |
| 759 | n = sizeof path - 1; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 760 | |
| 761 | /* Fetch one byte more to find out whether path length > n. */ |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 762 | nul_seen = umovestr(tcp, addr, n + 1, path); |
| 763 | if (nul_seen < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 764 | tprintf("%#lx", addr); |
| 765 | else { |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 766 | path[n++] = '\0'; |
| 767 | print_quoted_string(path, n, QUOTE_0_TERMINATED); |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 768 | if (!nul_seen) |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 769 | tprints("..."); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | void |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 774 | printpath(struct tcb *tcp, long addr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 775 | { |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 776 | /* Size must correspond to char path[] size in printpathn */ |
Dmitry V. Levin | 025b358 | 2014-11-21 22:28:34 +0000 | [diff] [blame] | 777 | printpathn(tcp, addr, PATH_MAX); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 780 | /* |
| 781 | * Print string specified by address `addr' and length `len'. |
| 782 | * If `len' < 0, treat the string as a NUL-terminated string. |
| 783 | * If string length exceeds `max_strlen', append `...' to the output. |
| 784 | */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 785 | void |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 786 | printstr(struct tcb *tcp, long addr, long len) |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 787 | { |
| 788 | static char *str = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 789 | static char *outstr; |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 790 | unsigned int size; |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 791 | unsigned int style; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 792 | int ellipsis; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 793 | |
| 794 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 795 | tprints("NULL"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 796 | return; |
| 797 | } |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 798 | /* Allocate static buffers if they are not allocated yet. */ |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 799 | if (!str) { |
Dmitry V. Levin | 378f9c5 | 2012-03-25 22:56:53 +0000 | [diff] [blame] | 800 | unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3; |
| 801 | |
| 802 | if (outstr_size / 4 != max_strlen) |
| 803 | die_out_of_memory(); |
Dmitry V. Levin | 3e9d71f | 2015-05-25 20:41:02 +0000 | [diff] [blame] | 804 | str = xmalloc(max_strlen + 1); |
| 805 | outstr = xmalloc(outstr_size); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 806 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 807 | |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 808 | size = max_strlen; |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 809 | if (len == -1) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 810 | /* |
| 811 | * Treat as a NUL-terminated string: fetch one byte more |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 812 | * because string_quote may look one byte ahead. |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 813 | */ |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 814 | if (umovestr(tcp, addr, size + 1, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 815 | tprintf("%#lx", addr); |
| 816 | return; |
| 817 | } |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 818 | style = QUOTE_0_TERMINATED; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 819 | } |
| 820 | else { |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 821 | if (size > (unsigned long)len) |
| 822 | size = (unsigned long)len; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 823 | if (umoven(tcp, addr, size, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 824 | tprintf("%#lx", addr); |
| 825 | return; |
| 826 | } |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 827 | style = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 830 | /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str |
| 831 | * or we were requested to print more than -s NUM chars)... |
| 832 | */ |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 833 | ellipsis = (string_quote(str, outstr, size, style) && |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 834 | (len < 0 || (unsigned long) len > max_strlen)); |
Roland McGrath | a503dcf | 2007-08-02 02:06:26 +0000 | [diff] [blame] | 835 | |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 836 | tprints(outstr); |
| 837 | if (ellipsis) |
| 838 | tprints("..."); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 839 | } |
| 840 | |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 841 | void |
Dmitry V. Levin | 05a0af6 | 2016-01-20 00:17:02 +0000 | [diff] [blame] | 842 | dumpiov_upto(struct tcb *tcp, int len, long addr, unsigned long data_size) |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 843 | { |
Denys Vlasenko | 8470374 | 2012-02-25 02:38:52 +0100 | [diff] [blame] | 844 | #if SUPPORTED_PERSONALITIES > 1 |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 845 | union { |
Dmitry V. Levin | 0894194 | 2016-01-16 22:50:09 +0000 | [diff] [blame] | 846 | struct { uint32_t base; uint32_t len; } *iov32; |
| 847 | struct { uint64_t base; uint64_t len; } *iov64; |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 848 | } iovu; |
| 849 | #define iov iovu.iov64 |
| 850 | #define sizeof_iov \ |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 851 | (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64)) |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 852 | #define iov_iov_base(i) \ |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 853 | (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base) |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 854 | #define iov_iov_len(i) \ |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 855 | (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len) |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 856 | #else |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 857 | struct iovec *iov; |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 858 | #define sizeof_iov sizeof(*iov) |
| 859 | #define iov_iov_base(i) iov[i].iov_base |
| 860 | #define iov_iov_len(i) iov[i].iov_len |
| 861 | #endif |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 862 | int i; |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 863 | unsigned size; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 864 | |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 865 | size = sizeof_iov * len; |
| 866 | /* Assuming no sane program has millions of iovs */ |
| 867 | if ((unsigned)len > 1024*1024 /* insane or negative size? */ |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 868 | || (iov = malloc(size)) == NULL) { |
Dmitry V. Levin | df38991 | 2015-05-25 23:33:31 +0300 | [diff] [blame] | 869 | error_msg("Out of memory"); |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 870 | return; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 871 | } |
Denys Vlasenko | 7e69ed9 | 2015-03-21 19:50:53 +0100 | [diff] [blame] | 872 | if (umoven(tcp, addr, size, iov) >= 0) { |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 873 | for (i = 0; i < len; i++) { |
Dmitry V. Levin | 05a0af6 | 2016-01-20 00:17:02 +0000 | [diff] [blame] | 874 | unsigned long iov_len = iov_iov_len(i); |
| 875 | if (iov_len > data_size) |
| 876 | iov_len = data_size; |
| 877 | if (!iov_len) |
| 878 | break; |
| 879 | data_size -= iov_len; |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 880 | /* include the buffer number to make it easy to |
| 881 | * match up the trace with the source */ |
Dmitry V. Levin | 05a0af6 | 2016-01-20 00:17:02 +0000 | [diff] [blame] | 882 | tprintf(" * %lu bytes in buffer %d\n", iov_len, i); |
| 883 | dumpstr(tcp, (long) iov_iov_base(i), iov_len); |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 884 | } |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 885 | } |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 886 | free(iov); |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 887 | #undef sizeof_iov |
| 888 | #undef iov_iov_base |
| 889 | #undef iov_iov_len |
| 890 | #undef iov |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 891 | } |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 892 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 893 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 894 | dumpstr(struct tcb *tcp, long addr, int len) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 895 | { |
| 896 | static int strsize = -1; |
| 897 | static unsigned char *str; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 898 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 899 | char outbuf[ |
| 900 | ( |
| 901 | (sizeof( |
| 902 | "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx " |
| 903 | "1234567890123456") + /*in case I'm off by few:*/ 4) |
| 904 | /*align to 8 to make memset easier:*/ + 7) & -8 |
| 905 | ]; |
| 906 | const unsigned char *src; |
| 907 | int i; |
| 908 | |
| 909 | memset(outbuf, ' ', sizeof(outbuf)); |
| 910 | |
| 911 | if (strsize < len + 16) { |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 912 | free(str); |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 913 | str = malloc(len + 16); |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 914 | if (!str) { |
| 915 | strsize = -1; |
Dmitry V. Levin | df38991 | 2015-05-25 23:33:31 +0300 | [diff] [blame] | 916 | error_msg("Out of memory"); |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 917 | return; |
| 918 | } |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 919 | strsize = len + 16; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Denys Vlasenko | 7e69ed9 | 2015-03-21 19:50:53 +0100 | [diff] [blame] | 922 | if (umoven(tcp, addr, len, str) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 923 | return; |
| 924 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 925 | /* Space-pad to 16 bytes */ |
| 926 | i = len; |
| 927 | while (i & 0xf) |
| 928 | str[i++] = ' '; |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 929 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 930 | i = 0; |
| 931 | src = str; |
| 932 | while (i < len) { |
| 933 | char *dst = outbuf; |
| 934 | /* Hex dump */ |
| 935 | do { |
| 936 | if (i < len) { |
| 937 | *dst++ = "0123456789abcdef"[*src >> 4]; |
| 938 | *dst++ = "0123456789abcdef"[*src & 0xf]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 939 | } |
| 940 | else { |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 941 | *dst++ = ' '; |
| 942 | *dst++ = ' '; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 943 | } |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 944 | dst++; /* space is there by memset */ |
| 945 | i++; |
| 946 | if ((i & 7) == 0) |
| 947 | dst++; /* space is there by memset */ |
| 948 | src++; |
| 949 | } while (i & 0xf); |
| 950 | /* ASCII dump */ |
| 951 | i -= 16; |
| 952 | src -= 16; |
| 953 | do { |
| 954 | if (*src >= ' ' && *src < 0x7f) |
| 955 | *dst++ = *src; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 956 | else |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 957 | *dst++ = '.'; |
| 958 | src++; |
| 959 | } while (++i & 0xf); |
| 960 | *dst = '\0'; |
Denys Vlasenko | f90979b | 2013-02-22 15:00:11 +0100 | [diff] [blame] | 961 | tprintf(" | %05x %s |\n", i - 16, outbuf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | |
Mike Frysinger | 612659e | 2012-02-14 14:38:28 +0100 | [diff] [blame] | 965 | #ifdef HAVE_PROCESS_VM_READV |
| 966 | /* C library supports this, but the kernel might not. */ |
| 967 | static bool process_vm_readv_not_supported = 0; |
| 968 | #else |
| 969 | |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 970 | /* Need to do this since process_vm_readv() is not yet available in libc. |
| 971 | * When libc is be updated, only "static bool process_vm_readv_not_supported" |
| 972 | * line should remain. |
| 973 | */ |
| 974 | #if !defined(__NR_process_vm_readv) |
| 975 | # if defined(I386) |
| 976 | # define __NR_process_vm_readv 347 |
| 977 | # elif defined(X86_64) |
| 978 | # define __NR_process_vm_readv 310 |
| 979 | # elif defined(POWERPC) |
| 980 | # define __NR_process_vm_readv 351 |
| 981 | # endif |
| 982 | #endif |
| 983 | |
| 984 | #if defined(__NR_process_vm_readv) |
| 985 | static bool process_vm_readv_not_supported = 0; |
Mike Frysinger | 24ee60b | 2012-05-04 19:37:29 -0400 | [diff] [blame] | 986 | /* Have to avoid duplicating with the C library headers. */ |
| 987 | static ssize_t strace_process_vm_readv(pid_t pid, |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 988 | const struct iovec *lvec, |
| 989 | unsigned long liovcnt, |
| 990 | const struct iovec *rvec, |
| 991 | unsigned long riovcnt, |
| 992 | unsigned long flags) |
| 993 | { |
| 994 | return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags); |
| 995 | } |
Mike Frysinger | 24ee60b | 2012-05-04 19:37:29 -0400 | [diff] [blame] | 996 | #define process_vm_readv strace_process_vm_readv |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 997 | #else |
| 998 | static bool process_vm_readv_not_supported = 1; |
| 999 | # define process_vm_readv(...) (errno = ENOSYS, -1) |
| 1000 | #endif |
Mike Frysinger | 612659e | 2012-02-14 14:38:28 +0100 | [diff] [blame] | 1001 | |
| 1002 | #endif /* end of hack */ |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1003 | |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1004 | static ssize_t |
| 1005 | vm_read_mem(pid_t pid, void *laddr, long raddr, size_t len) |
| 1006 | { |
| 1007 | const struct iovec local = { |
| 1008 | .iov_base = laddr, |
| 1009 | .iov_len = len |
| 1010 | }; |
| 1011 | const struct iovec remote = { |
| 1012 | .iov_base = (void *) raddr, |
| 1013 | .iov_len = len |
| 1014 | }; |
| 1015 | |
| 1016 | return process_vm_readv(pid, &local, 1, &remote, 1, 0); |
| 1017 | } |
| 1018 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1019 | /* |
| 1020 | * move `len' bytes of data from process `pid' |
Denys Vlasenko | 7e69ed9 | 2015-03-21 19:50:53 +0100 | [diff] [blame] | 1021 | * at address `addr' to our space at `our_addr' |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1022 | */ |
| 1023 | int |
Denys Vlasenko | 7e69ed9 | 2015-03-21 19:50:53 +0100 | [diff] [blame] | 1024 | umoven(struct tcb *tcp, long addr, unsigned int len, void *our_addr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1025 | { |
Denys Vlasenko | 7e69ed9 | 2015-03-21 19:50:53 +0100 | [diff] [blame] | 1026 | char *laddr = our_addr; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1027 | int pid = tcp->pid; |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1028 | unsigned int n, m, nread; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1029 | union { |
| 1030 | long val; |
| 1031 | char x[sizeof(long)]; |
| 1032 | } u; |
| 1033 | |
Denys Vlasenko | 2544f98 | 2013-02-19 17:39:56 +0100 | [diff] [blame] | 1034 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 1035 | if (current_wordsize < sizeof(addr)) |
| 1036 | addr &= (1ul << 8 * current_wordsize) - 1; |
Denys Vlasenko | d2a660f | 2012-02-25 00:43:22 +0100 | [diff] [blame] | 1037 | #endif |
| 1038 | |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1039 | if (!process_vm_readv_not_supported) { |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1040 | int r = vm_read_mem(pid, laddr, addr, len); |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1041 | if ((unsigned int) r == len) |
Ben Noordhuis | 1d58fe9 | 2013-02-26 12:24:25 +0100 | [diff] [blame] | 1042 | return 0; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1043 | if (r >= 0) { |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1044 | error_msg("umoven: short read (%u < %u) @0x%lx", |
| 1045 | (unsigned int) r, len, addr); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1046 | return -1; |
| 1047 | } |
| 1048 | switch (errno) { |
| 1049 | case ENOSYS: |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1050 | process_vm_readv_not_supported = 1; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1051 | break; |
Dmitry V. Levin | b2893c9 | 2015-03-30 15:21:55 +0000 | [diff] [blame] | 1052 | case EPERM: |
| 1053 | /* operation not permitted, try PTRACE_PEEKDATA */ |
| 1054 | break; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1055 | case ESRCH: |
| 1056 | /* the process is gone */ |
| 1057 | return -1; |
Dmitry V. Levin | b2893c9 | 2015-03-30 15:21:55 +0000 | [diff] [blame] | 1058 | case EFAULT: case EIO: |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1059 | /* address space is inaccessible */ |
| 1060 | return -1; |
| 1061 | default: |
| 1062 | /* all the rest is strange and should be reported */ |
Denys Vlasenko | 905e8e0 | 2013-02-26 12:30:09 +0100 | [diff] [blame] | 1063 | perror_msg("process_vm_readv"); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1064 | return -1; |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1065 | } |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1066 | } |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1067 | |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1068 | nread = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1069 | if (addr & (sizeof(long) - 1)) { |
| 1070 | /* addr not a multiple of sizeof(long) */ |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1071 | n = addr & (sizeof(long) - 1); /* residue */ |
| 1072 | addr &= -sizeof(long); /* aligned address */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1073 | errno = 0; |
| 1074 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1075 | switch (errno) { |
| 1076 | case 0: |
| 1077 | break; |
| 1078 | case ESRCH: case EINVAL: |
| 1079 | /* these could be seen if the process is gone */ |
| 1080 | return -1; |
| 1081 | case EFAULT: case EIO: case EPERM: |
| 1082 | /* address space is inaccessible */ |
| 1083 | return -1; |
| 1084 | default: |
| 1085 | /* all the rest is strange and should be reported */ |
| 1086 | perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", |
| 1087 | pid, addr); |
| 1088 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1089 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1090 | m = MIN(sizeof(long) - n, len); |
| 1091 | memcpy(laddr, &u.x[n], m); |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1092 | addr += sizeof(long); |
| 1093 | laddr += m; |
| 1094 | nread += m; |
| 1095 | len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1096 | } |
| 1097 | while (len) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1098 | errno = 0; |
| 1099 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1100 | switch (errno) { |
| 1101 | case 0: |
| 1102 | break; |
| 1103 | case ESRCH: case EINVAL: |
| 1104 | /* these could be seen if the process is gone */ |
| 1105 | return -1; |
| 1106 | case EFAULT: case EIO: case EPERM: |
| 1107 | /* address space is inaccessible */ |
| 1108 | if (nread) { |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1109 | perror_msg("umoven: short read (%u < %u) @0x%lx", |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1110 | nread, nread + len, addr - nread); |
| 1111 | } |
| 1112 | return -1; |
| 1113 | default: |
| 1114 | /* all the rest is strange and should be reported */ |
| 1115 | perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", |
| 1116 | pid, addr); |
| 1117 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1118 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1119 | m = MIN(sizeof(long), len); |
| 1120 | memcpy(laddr, u.x, m); |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1121 | addr += sizeof(long); |
| 1122 | laddr += m; |
| 1123 | nread += m; |
| 1124 | len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1125 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1126 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1127 | return 0; |
| 1128 | } |
| 1129 | |
Dmitry V. Levin | 332a326 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 1130 | int |
| 1131 | umoven_or_printaddr(struct tcb *tcp, const long addr, const unsigned int len, |
| 1132 | void *our_addr) |
| 1133 | { |
| 1134 | if (!addr) { |
| 1135 | tprints("NULL"); |
| 1136 | return -1; |
| 1137 | } |
Dmitry V. Levin | 61b7989 | 2015-07-14 22:03:55 +0000 | [diff] [blame] | 1138 | if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) || |
Dmitry V. Levin | 332a326 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 1139 | umoven(tcp, addr, len, our_addr) < 0) { |
| 1140 | tprintf("%#lx", addr); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1146 | /* |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 1147 | * Like `umove' but make the additional effort of looking |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1148 | * for a terminating zero byte. |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 1149 | * |
| 1150 | * Returns < 0 on error, > 0 if NUL was seen, |
| 1151 | * (TODO if useful: return count of bytes including NUL), |
| 1152 | * else 0 if len bytes were read but no NUL byte seen. |
| 1153 | * |
| 1154 | * Note: there is no guarantee we won't overwrite some bytes |
| 1155 | * in laddr[] _after_ terminating NUL (but, of course, |
| 1156 | * we never write past laddr[len-1]). |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1157 | */ |
| 1158 | int |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1159 | umovestr(struct tcb *tcp, long addr, unsigned int len, char *laddr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1160 | { |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1161 | #if SIZEOF_LONG == 4 |
| 1162 | const unsigned long x01010101 = 0x01010101ul; |
| 1163 | const unsigned long x80808080 = 0x80808080ul; |
| 1164 | #elif SIZEOF_LONG == 8 |
| 1165 | const unsigned long x01010101 = 0x0101010101010101ul; |
| 1166 | const unsigned long x80808080 = 0x8080808080808080ul; |
| 1167 | #else |
| 1168 | # error SIZEOF_LONG > 8 |
| 1169 | #endif |
| 1170 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1171 | int pid = tcp->pid; |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1172 | unsigned int n, m, nread; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1173 | union { |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1174 | unsigned long val; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1175 | char x[sizeof(long)]; |
| 1176 | } u; |
| 1177 | |
Denys Vlasenko | 2544f98 | 2013-02-19 17:39:56 +0100 | [diff] [blame] | 1178 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 1179 | if (current_wordsize < sizeof(addr)) |
| 1180 | addr &= (1ul << 8 * current_wordsize) - 1; |
Dmitry V. Levin | 856c7ed | 2011-12-26 20:12:02 +0000 | [diff] [blame] | 1181 | #endif |
| 1182 | |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1183 | nread = 0; |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1184 | if (!process_vm_readv_not_supported) { |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1185 | const size_t page_size = get_pagesize(); |
| 1186 | const size_t page_mask = page_size - 1; |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1187 | |
| 1188 | while (len > 0) { |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1189 | unsigned int chunk_len; |
| 1190 | unsigned int end_in_page; |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1191 | |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1192 | /* |
| 1193 | * Don't cross pages, otherwise we can get EFAULT |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1194 | * and fail to notice that terminating NUL lies |
| 1195 | * in the existing (first) page. |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1196 | */ |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1197 | chunk_len = len > page_size ? page_size : len; |
| 1198 | end_in_page = (addr + chunk_len) & page_mask; |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1199 | if (chunk_len > end_in_page) /* crosses to the next page */ |
| 1200 | chunk_len -= end_in_page; |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1201 | |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1202 | int r = vm_read_mem(pid, laddr, addr, chunk_len); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1203 | if (r > 0) { |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1204 | if (memchr(laddr, '\0', r)) |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1205 | return 1; |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1206 | addr += r; |
| 1207 | laddr += r; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1208 | nread += r; |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1209 | len -= r; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1210 | continue; |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1211 | } |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1212 | switch (errno) { |
| 1213 | case ENOSYS: |
| 1214 | process_vm_readv_not_supported = 1; |
| 1215 | goto vm_readv_didnt_work; |
| 1216 | case ESRCH: |
| 1217 | /* the process is gone */ |
| 1218 | return -1; |
Dmitry V. Levin | b2893c9 | 2015-03-30 15:21:55 +0000 | [diff] [blame] | 1219 | case EPERM: |
| 1220 | /* operation not permitted, try PTRACE_PEEKDATA */ |
| 1221 | if (!nread) |
| 1222 | goto vm_readv_didnt_work; |
| 1223 | /* fall through */ |
| 1224 | case EFAULT: case EIO: |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1225 | /* address space is inaccessible */ |
| 1226 | if (nread) { |
| 1227 | perror_msg("umovestr: short read (%d < %d) @0x%lx", |
Dmitry V. Levin | ea1fea6 | 2015-03-31 19:45:08 +0000 | [diff] [blame] | 1228 | nread, nread + len, addr - nread); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1229 | } |
| 1230 | return -1; |
| 1231 | default: |
| 1232 | /* all the rest is strange and should be reported */ |
| 1233 | perror_msg("process_vm_readv"); |
| 1234 | return -1; |
| 1235 | } |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1236 | } |
| 1237 | return 0; |
| 1238 | } |
| 1239 | vm_readv_didnt_work: |
| 1240 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1241 | if (addr & (sizeof(long) - 1)) { |
| 1242 | /* addr not a multiple of sizeof(long) */ |
Dmitry V. Levin | 97e5996 | 2015-01-14 08:05:45 +0000 | [diff] [blame] | 1243 | n = addr & (sizeof(long) - 1); /* residue */ |
| 1244 | addr &= -sizeof(long); /* aligned address */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1245 | errno = 0; |
| 1246 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1247 | switch (errno) { |
| 1248 | case 0: |
| 1249 | break; |
| 1250 | case ESRCH: case EINVAL: |
| 1251 | /* these could be seen if the process is gone */ |
| 1252 | return -1; |
| 1253 | case EFAULT: case EIO: case EPERM: |
| 1254 | /* address space is inaccessible */ |
| 1255 | return -1; |
| 1256 | default: |
| 1257 | /* all the rest is strange and should be reported */ |
| 1258 | perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", |
| 1259 | pid, addr); |
| 1260 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1261 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1262 | m = MIN(sizeof(long) - n, len); |
| 1263 | memcpy(laddr, &u.x[n], m); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1264 | while (n & (sizeof(long) - 1)) |
| 1265 | if (u.x[n++] == '\0') |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 1266 | return 1; |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1267 | addr += sizeof(long); |
| 1268 | laddr += m; |
| 1269 | nread += m; |
| 1270 | len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1271 | } |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1272 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1273 | while (len) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1274 | errno = 0; |
| 1275 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1276 | switch (errno) { |
| 1277 | case 0: |
| 1278 | break; |
| 1279 | case ESRCH: case EINVAL: |
| 1280 | /* these could be seen if the process is gone */ |
| 1281 | return -1; |
| 1282 | case EFAULT: case EIO: case EPERM: |
| 1283 | /* address space is inaccessible */ |
| 1284 | if (nread) { |
| 1285 | perror_msg("umovestr: short read (%d < %d) @0x%lx", |
| 1286 | nread, nread + len, addr - nread); |
| 1287 | } |
| 1288 | return -1; |
| 1289 | default: |
| 1290 | /* all the rest is strange and should be reported */ |
| 1291 | perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", |
| 1292 | pid, addr); |
| 1293 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1294 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1295 | m = MIN(sizeof(long), len); |
| 1296 | memcpy(laddr, u.x, m); |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1297 | /* "If a NUL char exists in this word" */ |
| 1298 | if ((u.val - x01010101) & ~u.val & x80808080) |
| 1299 | return 1; |
| 1300 | addr += sizeof(long); |
| 1301 | laddr += m; |
| 1302 | nread += m; |
| 1303 | len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1304 | } |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 1305 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1306 | } |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1307 | |
| 1308 | /* |
| 1309 | * Iteratively fetch and print up to nmemb elements of elem_size size |
| 1310 | * from the array that starts at tracee's address start_addr. |
| 1311 | * |
| 1312 | * Array elements are being fetched to the address specified by elem_buf. |
| 1313 | * |
| 1314 | * The fetcher callback function specified by umoven_func should follow |
| 1315 | * the same semantics as umoven_or_printaddr function. |
| 1316 | * |
| 1317 | * The printer callback function specified by print_func is expected |
| 1318 | * to print something; if it returns false, no more iterations will be made. |
| 1319 | * |
| 1320 | * The pointer specified by opaque_data is passed to each invocation |
| 1321 | * of print_func callback function. |
| 1322 | * |
| 1323 | * This function prints: |
| 1324 | * - "NULL", if start_addr is NULL; |
| 1325 | * - "[]", if nmemb is 0; |
| 1326 | * - start_addr, if nmemb * elem_size overflows or wraps around; |
| 1327 | * - nothing, if the first element cannot be fetched |
| 1328 | * (if umoven_func returns non-zero), but it is assumed that |
| 1329 | * umoven_func has printed the address it failed to fetch data from; |
| 1330 | * - elements of the array, delimited by ", ", with the array itself |
| 1331 | * enclosed with [] brackets. |
| 1332 | * |
| 1333 | * If abbrev(tcp) is true, then |
| 1334 | * - the maximum number of elements printed equals to max_strlen; |
| 1335 | * - "..." is printed instead of max_strlen+1 element |
| 1336 | * and no more iterations will be made. |
| 1337 | * |
| 1338 | * This function returns true only if |
| 1339 | * - umoven_func has been called at least once AND |
| 1340 | * - umoven_func has not returned false. |
| 1341 | */ |
| 1342 | bool |
| 1343 | print_array(struct tcb *tcp, |
| 1344 | const unsigned long start_addr, |
| 1345 | const size_t nmemb, |
| 1346 | void *const elem_buf, |
| 1347 | const size_t elem_size, |
| 1348 | int (*const umoven_func)(struct tcb *, |
| 1349 | long, |
| 1350 | unsigned int, |
| 1351 | void *), |
| 1352 | bool (*const print_func)(struct tcb *, |
| 1353 | void *elem_buf, |
| 1354 | size_t elem_size, |
| 1355 | void *opaque_data), |
| 1356 | void *const opaque_data) |
| 1357 | { |
| 1358 | if (!start_addr) { |
| 1359 | tprints("NULL"); |
| 1360 | return false; |
| 1361 | } |
| 1362 | |
| 1363 | if (!nmemb) { |
| 1364 | tprints("[]"); |
| 1365 | return false; |
| 1366 | } |
| 1367 | |
| 1368 | const size_t size = nmemb * elem_size; |
| 1369 | const unsigned long end_addr = start_addr + size; |
| 1370 | |
| 1371 | if (end_addr <= start_addr || size / elem_size != nmemb) { |
| 1372 | printaddr(start_addr); |
| 1373 | return false; |
| 1374 | } |
| 1375 | |
| 1376 | const unsigned long abbrev_end = |
| 1377 | (abbrev(tcp) && max_strlen < nmemb) ? |
| 1378 | start_addr + elem_size * max_strlen : end_addr; |
| 1379 | unsigned long cur; |
| 1380 | |
| 1381 | for (cur = start_addr; cur < end_addr; cur += elem_size) { |
| 1382 | if (cur != start_addr) |
| 1383 | tprints(", "); |
| 1384 | |
| 1385 | if (umoven_func(tcp, cur, elem_size, elem_buf)) |
| 1386 | break; |
| 1387 | |
| 1388 | if (cur == start_addr) |
| 1389 | tprints("["); |
| 1390 | |
| 1391 | if (cur >= abbrev_end) { |
| 1392 | tprints("..."); |
| 1393 | cur = end_addr; |
| 1394 | break; |
| 1395 | } |
| 1396 | |
| 1397 | if (!print_func(tcp, elem_buf, elem_size, opaque_data)) { |
| 1398 | cur = end_addr; |
| 1399 | break; |
| 1400 | } |
| 1401 | } |
| 1402 | if (cur != start_addr) |
| 1403 | tprints("]"); |
| 1404 | |
| 1405 | return cur >= end_addr; |
| 1406 | } |