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