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> |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 9 | * Copyright (c) 1999-2018 The strace developers. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 10 | * All rights reserved. |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * 3. The name of the author may not be used to endorse or promote products |
| 21 | * derived from this software without specific prior written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 24 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 25 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 26 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #include "defs.h" |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 36 | #include <limits.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 37 | #include <fcntl.h> |
Mike Frysinger | 54646b8 | 2015-08-19 13:29:27 -0400 | [diff] [blame] | 38 | #include <stdarg.h> |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 39 | #include <sys/stat.h> |
| 40 | #include <sys/sysmacros.h> |
Dmitry V. Levin | d93c4e8 | 2015-06-17 20:09:13 +0000 | [diff] [blame] | 41 | #ifdef HAVE_SYS_XATTR_H |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 42 | # include <sys/xattr.h> |
| 43 | #endif |
Dmitry V. Levin | b2fa2be | 2014-11-21 20:46:16 +0000 | [diff] [blame] | 44 | #include <sys/uio.h> |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 45 | |
| 46 | #include "largefile_wrappers.h" |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 47 | #include "xlat.h" |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 48 | #include "xstring.h" |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 49 | |
| 50 | int |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 51 | ts_nz(const struct timespec *a) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 52 | { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 53 | return a->tv_sec || a->tv_nsec; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | int |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 57 | ts_cmp(const struct timespec *a, const struct timespec *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 58 | { |
| 59 | if (a->tv_sec < b->tv_sec |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 60 | || (a->tv_sec == b->tv_sec && a->tv_nsec < b->tv_nsec)) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 61 | return -1; |
| 62 | if (a->tv_sec > b->tv_sec |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 63 | || (a->tv_sec == b->tv_sec && a->tv_nsec > b->tv_nsec)) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 64 | return 1; |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | double |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 69 | ts_float(const struct timespec *tv) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 70 | { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 71 | return tv->tv_sec + tv->tv_nsec/1000000000.0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 75 | ts_add(struct timespec *tv, const struct timespec *a, const struct timespec *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 76 | { |
| 77 | tv->tv_sec = a->tv_sec + b->tv_sec; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 78 | tv->tv_nsec = a->tv_nsec + b->tv_nsec; |
| 79 | if (tv->tv_nsec >= 1000000000) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 80 | tv->tv_sec++; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 81 | tv->tv_nsec -= 1000000000; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | void |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 86 | ts_sub(struct timespec *tv, const struct timespec *a, const struct timespec *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 87 | { |
| 88 | tv->tv_sec = a->tv_sec - b->tv_sec; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 89 | tv->tv_nsec = a->tv_nsec - b->tv_nsec; |
| 90 | if (tv->tv_nsec < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 91 | tv->tv_sec--; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 92 | tv->tv_nsec += 1000000000; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 97 | ts_div(struct timespec *tv, const struct timespec *a, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 98 | { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 99 | long long nsec = (a->tv_sec % n * 1000000000LL + a->tv_nsec + n / 2) / n; |
| 100 | tv->tv_sec = a->tv_sec / n + nsec / 1000000000; |
| 101 | tv->tv_nsec = nsec % 1000000000; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 105 | ts_mul(struct timespec *tv, const struct timespec *a, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 106 | { |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 107 | long long nsec = a->tv_nsec * n; |
| 108 | tv->tv_sec = a->tv_sec * n + nsec / 1000000000; |
| 109 | tv->tv_nsec = nsec % 1000000000; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Denys Vlasenko | 0a295bc | 2011-09-01 16:31:48 +0200 | [diff] [blame] | 112 | #if !defined HAVE_STPCPY |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 113 | char * |
| 114 | stpcpy(char *dst, const char *src) |
| 115 | { |
| 116 | while ((*dst = *src++) != '\0') |
| 117 | dst++; |
| 118 | return dst; |
| 119 | } |
Denys Vlasenko | 0a295bc | 2011-09-01 16:31:48 +0200 | [diff] [blame] | 120 | #endif |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 121 | |
Denys Vlasenko | b338f2d | 2013-11-09 20:40:31 +0100 | [diff] [blame] | 122 | /* Find a next bit which is set. |
| 123 | * Starts testing at cur_bit. |
| 124 | * Returns -1 if no more bits are set. |
| 125 | * |
| 126 | * We never touch bytes we don't need to. |
| 127 | * On big-endian, array is assumed to consist of |
| 128 | * current_wordsize wide words: for example, is current_wordsize is 4, |
| 129 | * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence. |
| 130 | * On little-endian machines, word size is immaterial. |
| 131 | */ |
| 132 | int |
| 133 | next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits) |
| 134 | { |
| 135 | const unsigned endian = 1; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 136 | int little_endian = *(char *) (void *) &endian; |
Denys Vlasenko | b338f2d | 2013-11-09 20:40:31 +0100 | [diff] [blame] | 137 | |
| 138 | const uint8_t *array = bit_array; |
| 139 | unsigned pos = cur_bit / 8; |
| 140 | unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1; |
| 141 | |
| 142 | for (;;) { |
| 143 | uint8_t bitmask; |
| 144 | uint8_t cur_byte; |
| 145 | |
| 146 | if (cur_bit >= size_bits) |
| 147 | return -1; |
| 148 | cur_byte = array[pos ^ pos_xor_mask]; |
| 149 | if (cur_byte == 0) { |
| 150 | cur_bit = (cur_bit + 8) & (-8); |
| 151 | pos++; |
| 152 | continue; |
| 153 | } |
| 154 | bitmask = 1 << (cur_bit & 7); |
| 155 | for (;;) { |
| 156 | if (cur_byte & bitmask) |
| 157 | return cur_bit; |
| 158 | cur_bit++; |
| 159 | if (cur_bit >= size_bits) |
| 160 | return -1; |
| 161 | bitmask <<= 1; |
| 162 | /* This check *can't be* optimized out: */ |
| 163 | if (bitmask == 0) |
| 164 | break; |
| 165 | } |
| 166 | pos++; |
| 167 | } |
| 168 | } |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 169 | |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 170 | /* |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 171 | * Fetch 64bit argument at position arg_no and |
| 172 | * return the index of the next argument. |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 173 | */ |
| 174 | int |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 175 | getllval(struct tcb *tcp, unsigned long long *val, int arg_no) |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 176 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 177 | #if SIZEOF_KERNEL_LONG_T > 4 |
| 178 | # ifndef current_klongsize |
| 179 | if (current_klongsize < SIZEOF_KERNEL_LONG_T) { |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 180 | # if defined(AARCH64) || defined(POWERPC64) |
Dmitry V. Levin | 3c49b02 | 2014-08-07 00:07:28 +0000 | [diff] [blame] | 181 | /* Align arg_no to the next even number. */ |
| 182 | arg_no = (arg_no + 1) & 0xe; |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 183 | # endif /* AARCH64 || POWERPC64 */ |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 184 | *val = ULONG_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] | 185 | arg_no += 2; |
Dmitry V. Levin | b0c5113 | 2016-06-17 16:12:13 +0000 | [diff] [blame] | 186 | } else |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 187 | # endif /* !current_klongsize */ |
Dmitry V. Levin | b0c5113 | 2016-06-17 16:12:13 +0000 | [diff] [blame] | 188 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 189 | *val = tcp->u_arg[arg_no]; |
Dmitry V. Levin | b0c5113 | 2016-06-17 16:12:13 +0000 | [diff] [blame] | 190 | arg_no++; |
Dmitry V. Levin | 0b46883 | 2013-05-02 08:41:27 +0000 | [diff] [blame] | 191 | } |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 192 | #else /* SIZEOF_KERNEL_LONG_T == 4 */ |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 193 | # if defined __ARM_EABI__ \ |
| 194 | || defined LINUX_MIPSO32 \ |
| 195 | || defined POWERPC \ |
| 196 | || defined XTENSA |
Dmitry V. Levin | 3c49b02 | 2014-08-07 00:07:28 +0000 | [diff] [blame] | 197 | /* Align arg_no to the next even number. */ |
| 198 | arg_no = (arg_no + 1) & 0xe; |
Eugene Syromyatnikov | 714a162 | 2016-08-20 17:02:55 +0300 | [diff] [blame] | 199 | # elif defined SH |
| 200 | /* |
| 201 | * The SH4 ABI does allow long longs in odd-numbered registers, but |
| 202 | * does not allow them to be split between registers and memory - and |
| 203 | * there are only four argument registers for normal functions. As a |
| 204 | * result, pread, for example, takes an extra padding argument before |
| 205 | * the offset. This was changed late in the 2.4 series (around 2.4.20). |
| 206 | */ |
| 207 | if (arg_no == 3) |
| 208 | arg_no++; |
| 209 | # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */ |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 210 | *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]); |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 211 | arg_no += 2; |
Denys Vlasenko | c9d0fc0 | 2013-02-17 22:41:33 +0100 | [diff] [blame] | 212 | #endif |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame] | 213 | |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 214 | return arg_no; |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 215 | } |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 216 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 217 | /* |
Dmitry V. Levin | 1ea6473 | 2015-01-10 00:08:58 +0000 | [diff] [blame] | 218 | * Print 64bit argument at position arg_no and |
| 219 | * return the index of the next argument. |
| 220 | */ |
| 221 | int |
| 222 | printllval(struct tcb *tcp, const char *format, int arg_no) |
| 223 | { |
| 224 | unsigned long long val = 0; |
| 225 | |
| 226 | arg_no = getllval(tcp, &val, arg_no); |
| 227 | tprintf(format, val); |
| 228 | return arg_no; |
| 229 | } |
| 230 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 231 | void |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 232 | printaddr64(const uint64_t addr) |
Dmitry V. Levin | 332a326 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 233 | { |
| 234 | if (!addr) |
| 235 | tprints("NULL"); |
| 236 | else |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 237 | tprintf("%#" PRIx64, addr); |
| 238 | } |
| 239 | |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 240 | #define DEF_PRINTNUM(name, type) \ |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 241 | bool \ |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 242 | printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \ |
| 243 | const char *const fmt) \ |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 244 | { \ |
| 245 | type num; \ |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 246 | if (umove_or_printaddr(tcp, addr, &num)) \ |
| 247 | return false; \ |
| 248 | tprints("["); \ |
| 249 | tprintf(fmt, num); \ |
| 250 | tprints("]"); \ |
| 251 | return true; \ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 254 | #define DEF_PRINTNUM_ADDR(name, type) \ |
| 255 | bool \ |
| 256 | printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \ |
| 257 | { \ |
| 258 | type num; \ |
| 259 | if (umove_or_printaddr(tcp, addr, &num)) \ |
| 260 | return false; \ |
| 261 | tprints("["); \ |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 262 | printaddr64(num); \ |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 263 | tprints("]"); \ |
| 264 | return true; \ |
| 265 | } |
| 266 | |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 267 | #define DEF_PRINTPAIR(name, type) \ |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 268 | bool \ |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 269 | printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \ |
| 270 | const char *const fmt) \ |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 271 | { \ |
| 272 | type pair[2]; \ |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 273 | if (umove_or_printaddr(tcp, addr, &pair)) \ |
| 274 | return false; \ |
| 275 | tprints("["); \ |
| 276 | tprintf(fmt, pair[0]); \ |
| 277 | tprints(", "); \ |
| 278 | tprintf(fmt, pair[1]); \ |
| 279 | tprints("]"); \ |
| 280 | return true; \ |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 283 | DEF_PRINTNUM(int, int) |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 284 | DEF_PRINTNUM_ADDR(int, unsigned int) |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 285 | DEF_PRINTPAIR(int, int) |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 286 | DEF_PRINTNUM(short, short) |
Dmitry V. Levin | c88163e | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 287 | DEF_PRINTNUM(int64, uint64_t) |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 288 | DEF_PRINTNUM_ADDR(int64, uint64_t) |
Dmitry V. Levin | 69127a3 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 289 | DEF_PRINTPAIR(int64, uint64_t) |
Dmitry V. Levin | 2479ef0 | 2015-08-18 14:58:27 +0000 | [diff] [blame] | 290 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 291 | #ifndef current_wordsize |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 292 | bool |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 293 | printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr, |
| 294 | const char *const fmt_long, const char *const fmt_int) |
Dmitry V. Levin | 2479ef0 | 2015-08-18 14:58:27 +0000 | [diff] [blame] | 295 | { |
| 296 | if (current_wordsize > sizeof(int)) { |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 297 | return printnum_int64(tcp, addr, fmt_long); |
Dmitry V. Levin | 2479ef0 | 2015-08-18 14:58:27 +0000 | [diff] [blame] | 298 | } else { |
Dmitry V. Levin | d77f669 | 2015-08-18 21:57:27 +0000 | [diff] [blame] | 299 | return printnum_int(tcp, addr, fmt_int); |
Dmitry V. Levin | 2479ef0 | 2015-08-18 14:58:27 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 302 | |
| 303 | bool |
| 304 | printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr) |
| 305 | { |
| 306 | if (current_wordsize > sizeof(int)) { |
| 307 | return printnum_addr_int64(tcp, addr); |
| 308 | } else { |
| 309 | return printnum_addr_int(tcp, addr); |
| 310 | } |
| 311 | } |
| 312 | #endif /* !current_wordsize */ |
| 313 | |
| 314 | #ifndef current_klongsize |
| 315 | bool |
| 316 | printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr) |
| 317 | { |
| 318 | if (current_klongsize > sizeof(int)) { |
| 319 | return printnum_addr_int64(tcp, addr); |
| 320 | } else { |
| 321 | return printnum_addr_int(tcp, addr); |
| 322 | } |
| 323 | } |
| 324 | #endif /* !current_klongsize */ |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 325 | |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 326 | /** |
| 327 | * Prints time to a (static internal) buffer and returns pointer to it. |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 328 | * Returns NULL if the provided time specification is not correct. |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 329 | * |
| 330 | * @param sec Seconds since epoch. |
| 331 | * @param part_sec Amount of second parts since the start of a second. |
| 332 | * @param max_part_sec Maximum value of a valid part_sec. |
| 333 | * @param width 1 + floor(log10(max_part_sec)). |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 334 | * @return Pointer to a statically allocated string on success, |
| 335 | * NULL on error. |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 336 | */ |
| 337 | static const char * |
| 338 | sprinttime_ex(const long long sec, const unsigned long long part_sec, |
| 339 | const unsigned int max_part_sec, const int width) |
Dmitry V. Levin | b1a01b8 | 2014-12-06 03:53:16 +0000 | [diff] [blame] | 340 | { |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 341 | static char buf[sizeof(int) * 3 * 6 + sizeof(part_sec) * 3 |
| 342 | + sizeof("+0000")]; |
Dmitry V. Levin | b1a01b8 | 2014-12-06 03:53:16 +0000 | [diff] [blame] | 343 | |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 344 | if ((sec == 0 && part_sec == 0) || part_sec > max_part_sec) |
| 345 | return NULL; |
| 346 | |
| 347 | time_t t = (time_t) sec; |
| 348 | struct tm *tmp = (sec == t) ? localtime(&t) : NULL; |
| 349 | if (!tmp) |
| 350 | return NULL; |
| 351 | |
| 352 | size_t pos = strftime(buf, sizeof(buf), "%FT%T", tmp); |
| 353 | if (!pos) |
| 354 | return NULL; |
| 355 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 356 | if (part_sec > 0) |
| 357 | pos += xsnprintf(buf + pos, sizeof(buf) - pos, ".%0*llu", |
| 358 | width, part_sec); |
Dmitry V. Levin | b1a01b8 | 2014-12-06 03:53:16 +0000 | [diff] [blame] | 359 | |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 360 | return strftime(buf + pos, sizeof(buf) - pos, "%z", tmp) ? buf : NULL; |
| 361 | } |
| 362 | |
| 363 | const char * |
| 364 | sprinttime(long long sec) |
| 365 | { |
| 366 | return sprinttime_ex(sec, 0, 0, 0); |
| 367 | } |
| 368 | |
| 369 | const char * |
| 370 | sprinttime_usec(long long sec, unsigned long long usec) |
| 371 | { |
| 372 | return sprinttime_ex(sec, usec, 999999, 6); |
| 373 | } |
| 374 | |
| 375 | const char * |
| 376 | sprinttime_nsec(long long sec, unsigned long long nsec) |
| 377 | { |
| 378 | return sprinttime_ex(sec, nsec, 999999999, 9); |
Dmitry V. Levin | b1a01b8 | 2014-12-06 03:53:16 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Fabien Siron | 2850f74 | 2016-07-06 15:49:22 +0000 | [diff] [blame] | 381 | enum sock_proto |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 382 | getfdproto(struct tcb *tcp, int fd) |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 383 | { |
Dmitry V. Levin | d93c4e8 | 2015-06-17 20:09:13 +0000 | [diff] [blame] | 384 | #ifdef HAVE_SYS_XATTR_H |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 385 | size_t bufsize = 256; |
| 386 | char buf[bufsize]; |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 387 | ssize_t r; |
| 388 | char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3]; |
| 389 | |
| 390 | if (fd < 0) |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 391 | return SOCK_PROTO_UNKNOWN; |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 392 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 393 | xsprintf(path, "/proc/%u/fd/%u", tcp->pid, fd); |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 394 | r = getxattr(path, "system.sockprotoname", buf, bufsize - 1); |
| 395 | if (r <= 0) |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 396 | return SOCK_PROTO_UNKNOWN; |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 397 | else { |
| 398 | /* |
| 399 | * This is a protection for the case when the kernel |
| 400 | * side does not append a null byte to the buffer. |
| 401 | */ |
| 402 | buf[r] = '\0'; |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 403 | |
| 404 | return get_proto_by_name(buf); |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 405 | } |
| 406 | #else |
Fabien Siron | 802cc28 | 2016-06-17 16:29:53 +0000 | [diff] [blame] | 407 | return SOCK_PROTO_UNKNOWN; |
Masatake YAMATO | f548067 | 2014-11-22 19:03:33 +0900 | [diff] [blame] | 408 | #endif |
| 409 | } |
| 410 | |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 411 | unsigned long |
| 412 | getfdinode(struct tcb *tcp, int fd) |
| 413 | { |
| 414 | char path[PATH_MAX + 1]; |
| 415 | |
| 416 | if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) { |
| 417 | const char *str = STR_STRIP_PREFIX(path, "socket:["); |
| 418 | |
| 419 | if (str != path) { |
| 420 | const size_t str_len = strlen(str); |
| 421 | if (str_len && str[str_len - 1] == ']') |
| 422 | return strtoul(str, NULL, 10); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 429 | static bool |
| 430 | printsocket(struct tcb *tcp, int fd, const char *path) |
| 431 | { |
| 432 | const char *str = STR_STRIP_PREFIX(path, "socket:["); |
| 433 | size_t len; |
| 434 | unsigned long inode; |
| 435 | |
| 436 | return (str != path) |
| 437 | && (len = strlen(str)) |
| 438 | && (str[len - 1] == ']') |
| 439 | && (inode = strtoul(str, NULL, 10)) |
| 440 | && print_sockaddr_by_inode(tcp, fd, inode); |
| 441 | } |
| 442 | |
| 443 | static bool |
| 444 | printdev(struct tcb *tcp, int fd, const char *path) |
| 445 | { |
| 446 | struct_stat st; |
| 447 | |
| 448 | if (path[0] != '/') |
| 449 | return false; |
| 450 | |
| 451 | if (stat_file(path, &st)) { |
| 452 | debug_func_perror_msg("stat(\"%s\")", path); |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | switch (st.st_mode & S_IFMT) { |
| 457 | case S_IFBLK: |
| 458 | case S_IFCHR: |
| 459 | print_quoted_string_ex(path, strlen(path), |
| 460 | QUOTE_OMIT_LEADING_TRAILING_QUOTES, |
| 461 | "<>"); |
| 462 | tprintf("<%s %u:%u>", |
| 463 | S_ISBLK(st.st_mode)? "block" : "char", |
| 464 | major(st.st_rdev), minor(st.st_rdev)); |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | return false; |
| 469 | } |
| 470 | |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 471 | void |
Dmitry V. Levin | 3138213 | 2011-03-04 05:08:02 +0300 | [diff] [blame] | 472 | printfd(struct tcb *tcp, int fd) |
| 473 | { |
Denys Vlasenko | 61ad0a4 | 2013-03-06 18:24:34 +0100 | [diff] [blame] | 474 | char path[PATH_MAX + 1]; |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 475 | if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) { |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 476 | tprintf("%d<", fd); |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 477 | if (show_fd_path <= 1 |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 478 | || (!printsocket(tcp, fd, path) |
| 479 | && !printdev(tcp, fd, path))) { |
| 480 | print_quoted_string_ex(path, strlen(path), |
| 481 | QUOTE_OMIT_LEADING_TRAILING_QUOTES, "<>"); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 482 | } |
Dmitry V. Levin | c723599 | 2015-01-24 19:51:39 +0000 | [diff] [blame] | 483 | tprints(">"); |
Dmitry V. Levin | 2f6510c | 2014-08-21 03:17:48 +0000 | [diff] [blame] | 484 | } else |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 485 | tprintf("%d", fd); |
Dmitry V. Levin | 3138213 | 2011-03-04 05:08:02 +0300 | [diff] [blame] | 486 | } |
| 487 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 488 | /* |
| 489 | * Quote string `instr' of length `size' |
| 490 | * Write up to (3 + `size' * 4) bytes to `outstr' buffer. |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 491 | * |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 492 | * `escape_chars' specifies characters (in addition to characters with |
| 493 | * codes 0..31, 127..255, single and double quotes) that should be escaped. |
| 494 | * |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 495 | * If QUOTE_0_TERMINATED `style' flag is set, |
| 496 | * treat `instr' as a NUL-terminated string, |
| 497 | * checking up to (`size' + 1) bytes of `instr'. |
| 498 | * |
| 499 | * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set, |
| 500 | * do not add leading and trailing quoting symbols. |
| 501 | * |
| 502 | * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise. |
| 503 | * 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] | 504 | */ |
Dmitry V. Levin | 3c17d1b | 2016-02-01 23:14:59 +0000 | [diff] [blame] | 505 | int |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 506 | string_quote(const char *instr, char *outstr, const unsigned int size, |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 507 | const unsigned int style, const char *escape_chars) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 508 | { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 509 | const unsigned char *ustr = (const unsigned char *) instr; |
| 510 | char *s = outstr; |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 511 | unsigned int i; |
| 512 | int usehex, c, eol; |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 513 | bool escape; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 514 | |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 515 | if (style & QUOTE_0_TERMINATED) |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 516 | eol = '\0'; |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 517 | else |
| 518 | eol = 0x100; /* this can never match a char */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 519 | |
| 520 | usehex = 0; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 521 | if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 522 | usehex = 1; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 523 | } else if (xflag) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 524 | /* Check for presence of symbol which require |
| 525 | to hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 526 | for (i = 0; i < size; ++i) { |
| 527 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 528 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 529 | if (c == eol) |
| 530 | break; |
Denys Vlasenko | 5198ed4 | 2013-03-06 23:44:23 +0100 | [diff] [blame] | 531 | |
| 532 | /* Force hex unless c is printable or whitespace */ |
| 533 | if (c > 0x7e) { |
| 534 | usehex = 1; |
| 535 | break; |
| 536 | } |
| 537 | /* In ASCII isspace is only these chars: "\t\n\v\f\r". |
| 538 | * They happen to have ASCII codes 9,10,11,12,13. |
| 539 | */ |
| 540 | if (c < ' ' && (unsigned)(c - 9) >= 5) { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 541 | usehex = 1; |
| 542 | break; |
| 543 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 546 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 547 | if (style & QUOTE_EMIT_COMMENT) |
| 548 | s = stpcpy(s, " /* "); |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 549 | if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES)) |
| 550 | *s++ = '\"'; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 551 | |
| 552 | if (usehex) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 553 | /* Hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 554 | for (i = 0; i < size; ++i) { |
| 555 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 556 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 557 | if (c == eol) |
| 558 | goto asciz_ended; |
| 559 | *s++ = '\\'; |
| 560 | *s++ = 'x'; |
| 561 | *s++ = "0123456789abcdef"[c >> 4]; |
| 562 | *s++ = "0123456789abcdef"[c & 0xf]; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 563 | } |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 564 | |
| 565 | goto string_ended; |
| 566 | } |
| 567 | |
| 568 | for (i = 0; i < size; ++i) { |
| 569 | c = ustr[i]; |
| 570 | /* Check for NUL-terminated string. */ |
| 571 | if (c == eol) |
| 572 | goto asciz_ended; |
| 573 | if ((i == (size - 1)) && |
| 574 | (style & QUOTE_OMIT_TRAILING_0) && (c == '\0')) |
| 575 | goto asciz_ended; |
| 576 | switch (c) { |
| 577 | case '\"': case '\\': |
| 578 | *s++ = '\\'; |
| 579 | *s++ = c; |
| 580 | break; |
| 581 | case '\f': |
| 582 | *s++ = '\\'; |
| 583 | *s++ = 'f'; |
| 584 | break; |
| 585 | case '\n': |
| 586 | *s++ = '\\'; |
| 587 | *s++ = 'n'; |
| 588 | break; |
| 589 | case '\r': |
| 590 | *s++ = '\\'; |
| 591 | *s++ = 'r'; |
| 592 | break; |
| 593 | case '\t': |
| 594 | *s++ = '\\'; |
| 595 | *s++ = 't'; |
| 596 | break; |
| 597 | case '\v': |
| 598 | *s++ = '\\'; |
| 599 | *s++ = 'v'; |
| 600 | break; |
| 601 | default: |
| 602 | escape = (c < ' ') || (c > 0x7e); |
| 603 | |
| 604 | if (!escape && escape_chars) |
| 605 | escape = !!strchr(escape_chars, c); |
| 606 | |
| 607 | if (!escape) { |
| 608 | *s++ = c; |
| 609 | } else { |
| 610 | /* Print \octal */ |
| 611 | *s++ = '\\'; |
| 612 | if (i + 1 < size |
| 613 | && ustr[i + 1] >= '0' |
| 614 | && ustr[i + 1] <= '7' |
| 615 | ) { |
| 616 | /* Print \ooo */ |
| 617 | *s++ = '0' + (c >> 6); |
| 618 | *s++ = '0' + ((c >> 3) & 0x7); |
| 619 | } else { |
| 620 | /* Print \[[o]o]o */ |
| 621 | if ((c >> 3) != 0) { |
| 622 | if ((c >> 6) != 0) |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 623 | *s++ = '0' + (c >> 6); |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 624 | *s++ = '0' + ((c >> 3) & 0x7); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 625 | } |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 626 | } |
| 627 | *s++ = '0' + (c & 0x7); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 632 | string_ended: |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 633 | if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES)) |
| 634 | *s++ = '\"'; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 635 | if (style & QUOTE_EMIT_COMMENT) |
| 636 | s = stpcpy(s, " */"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 637 | *s = '\0'; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 638 | |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 639 | /* 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] | 640 | if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') { |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 641 | /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended') |
| 642 | * but next char is NUL. |
| 643 | */ |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | return 1; |
| 648 | |
| 649 | asciz_ended: |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 650 | if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES)) |
| 651 | *s++ = '\"'; |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 652 | if (style & QUOTE_EMIT_COMMENT) |
| 653 | s = stpcpy(s, " */"); |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 654 | *s = '\0'; |
| 655 | /* Return zero: we printed entire ASCIZ string (didn't truncate it) */ |
| 656 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 659 | #ifndef ALLOCA_CUTOFF |
| 660 | # define ALLOCA_CUTOFF 4032 |
| 661 | #endif |
| 662 | #define use_alloca(n) ((n) <= ALLOCA_CUTOFF) |
| 663 | |
| 664 | /* |
| 665 | * Quote string `str' of length `size' and print the result. |
| 666 | * |
| 667 | * If QUOTE_0_TERMINATED `style' flag is set, |
| 668 | * treat `str' as a NUL-terminated string and |
| 669 | * quote at most (`size' - 1) bytes. |
| 670 | * |
| 671 | * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set, |
| 672 | * do not add leading and trailing quoting symbols. |
| 673 | * |
| 674 | * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise. |
| 675 | * Note that if QUOTE_0_TERMINATED is not set, always returns 1. |
| 676 | */ |
| 677 | int |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 678 | print_quoted_string_ex(const char *str, unsigned int size, |
| 679 | const unsigned int style, const char *escape_chars) |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 680 | { |
| 681 | char *buf; |
| 682 | char *outstr; |
| 683 | unsigned int alloc_size; |
| 684 | int rc; |
| 685 | |
| 686 | if (size && style & QUOTE_0_TERMINATED) |
| 687 | --size; |
| 688 | |
| 689 | alloc_size = 4 * size; |
| 690 | if (alloc_size / 4 != size) { |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 691 | error_func_msg("requested %u bytes exceeds %u bytes limit", |
| 692 | size, -1U / 4); |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 693 | tprints("???"); |
| 694 | return -1; |
| 695 | } |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 696 | alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2) + |
| 697 | (style & QUOTE_EMIT_COMMENT ? 7 : 0); |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 698 | |
| 699 | if (use_alloca(alloc_size)) { |
| 700 | outstr = alloca(alloc_size); |
| 701 | buf = NULL; |
| 702 | } else { |
| 703 | outstr = buf = malloc(alloc_size); |
| 704 | if (!buf) { |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 705 | error_func_msg("memory exhausted when tried to allocate" |
| 706 | " %u bytes", alloc_size); |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 707 | tprints("???"); |
| 708 | return -1; |
| 709 | } |
| 710 | } |
| 711 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 712 | rc = string_quote(str, outstr, size, style, escape_chars); |
Dmitry V. Levin | 513e96e | 2015-01-26 01:17:08 +0000 | [diff] [blame] | 713 | tprints(outstr); |
| 714 | |
| 715 | free(buf); |
| 716 | return rc; |
| 717 | } |
| 718 | |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 719 | inline int |
| 720 | print_quoted_string(const char *str, unsigned int size, |
| 721 | const unsigned int style) |
| 722 | { |
| 723 | return print_quoted_string_ex(str, size, style, NULL); |
| 724 | } |
| 725 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 726 | /* |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 727 | * Quote a NUL-terminated string `str' of length up to `size' - 1 |
| 728 | * and print the result. |
| 729 | * |
| 730 | * Returns 0 if NUL was seen, 1 otherwise. |
| 731 | */ |
| 732 | int |
| 733 | print_quoted_cstring(const char *str, unsigned int size) |
| 734 | { |
| 735 | int unterminated = |
| 736 | print_quoted_string(str, size, QUOTE_0_TERMINATED); |
| 737 | |
| 738 | if (unterminated) |
| 739 | tprints("..."); |
| 740 | |
| 741 | return unterminated; |
| 742 | } |
| 743 | |
| 744 | /* |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 745 | * Print path string specified by address `addr' and length `n'. |
| 746 | * If path length exceeds `n', append `...' to the output. |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 747 | * |
| 748 | * Returns the result of umovenstr. |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 749 | */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 750 | int |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 751 | printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 752 | { |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 753 | char path[PATH_MAX]; |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 754 | int nul_seen; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 755 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 756 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 757 | tprints("NULL"); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 758 | return -1; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 761 | /* Cap path length to the path buffer size */ |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 762 | if (n > sizeof(path) - 1) |
| 763 | n = sizeof(path) - 1; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 764 | |
| 765 | /* Fetch one byte more to find out whether path length > n. */ |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 766 | nul_seen = umovestr(tcp, addr, n + 1, path); |
| 767 | if (nul_seen < 0) |
Dmitry V. Levin | 484326d | 2016-06-11 01:28:21 +0000 | [diff] [blame] | 768 | printaddr(addr); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 769 | else { |
Elliott Hughes | 77c3ff8 | 2017-09-08 17:11:00 -0700 | [diff] [blame] | 770 | path[n++] = !nul_seen; |
| 771 | print_quoted_cstring(path, n); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 772 | } |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 773 | |
| 774 | return nul_seen; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 777 | int |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 778 | printpath(struct tcb *const tcp, const kernel_ulong_t addr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 779 | { |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 780 | /* Size must correspond to char path[] size in printpathn */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 781 | return printpathn(tcp, addr, PATH_MAX - 1); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 784 | /* |
| 785 | * Print string specified by address `addr' and length `len'. |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 786 | * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string |
| 787 | * as a NUL-terminated string. |
| 788 | * Pass `user_style' on to `string_quote'. |
| 789 | * Append `...' to the output if either the string length exceeds `max_strlen', |
| 790 | * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'. |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 791 | * |
| 792 | * Returns the result of umovenstr if style has QUOTE_0_TERMINATED, |
| 793 | * or the result of umoven otherwise. |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 794 | */ |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 795 | int |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 796 | printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr, |
| 797 | const kernel_ulong_t len, const unsigned int user_style) |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 798 | { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 799 | static char *str; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 800 | static char *outstr; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 801 | |
Dmitry V. Levin | 3ed5d02 | 2014-09-10 13:46:04 +0000 | [diff] [blame] | 802 | unsigned int size; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 803 | unsigned int style = user_style; |
| 804 | int rc; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 805 | int ellipsis; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 806 | |
| 807 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 808 | tprints("NULL"); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 809 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 810 | } |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 811 | /* Allocate static buffers if they are not allocated yet. */ |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 812 | if (!str) { |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 813 | const unsigned int outstr_size = |
| 814 | 4 * max_strlen + /* for quotes and NUL */ 3; |
| 815 | /* |
| 816 | * We can assume that outstr_size / 4 == max_strlen |
| 817 | * since we have a guarantee that max_strlen <= -1U / 4. |
| 818 | */ |
Dmitry V. Levin | 378f9c5 | 2012-03-25 22:56:53 +0000 | [diff] [blame] | 819 | |
Dmitry V. Levin | 3e9d71f | 2015-05-25 20:41:02 +0000 | [diff] [blame] | 820 | str = xmalloc(max_strlen + 1); |
| 821 | outstr = xmalloc(outstr_size); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 822 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 823 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 824 | /* Fetch one byte more because string_quote may look one byte ahead. */ |
| 825 | size = max_strlen + 1; |
| 826 | |
| 827 | if (size > len) |
| 828 | size = len; |
| 829 | if (style & QUOTE_0_TERMINATED) |
| 830 | rc = umovestr(tcp, addr, size, str); |
| 831 | else |
| 832 | rc = umoven(tcp, addr, size, str); |
| 833 | |
| 834 | if (rc < 0) { |
| 835 | printaddr(addr); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 836 | return rc; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 839 | if (size > max_strlen) |
| 840 | size = max_strlen; |
| 841 | else |
| 842 | str[size] = '\xff'; |
Eugene Syromyatnikov | ee70a1b | 2016-10-03 21:35:45 +0300 | [diff] [blame] | 843 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 844 | /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str |
| 845 | * or we were requested to print more than -s NUM chars)... |
| 846 | */ |
Elliott Hughes | 28e98bc | 2018-06-14 16:59:04 -0700 | [diff] [blame] | 847 | ellipsis = string_quote(str, outstr, size, style, NULL) |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 848 | && len |
| 849 | && ((style & QUOTE_0_TERMINATED) |
| 850 | || len > max_strlen); |
Roland McGrath | a503dcf | 2007-08-02 02:06:26 +0000 | [diff] [blame] | 851 | |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 852 | tprints(outstr); |
| 853 | if (ellipsis) |
| 854 | tprints("..."); |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 855 | |
| 856 | return rc; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 857 | } |
| 858 | |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 859 | void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 860 | dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr, |
| 861 | kernel_ulong_t data_size) |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 862 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 863 | #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 864 | union { |
Dmitry V. Levin | 0894194 | 2016-01-16 22:50:09 +0000 | [diff] [blame] | 865 | struct { uint32_t base; uint32_t len; } *iov32; |
| 866 | struct { uint64_t base; uint64_t len; } *iov64; |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 867 | } iovu; |
| 868 | #define iov iovu.iov64 |
| 869 | #define sizeof_iov \ |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 870 | (current_wordsize == 4 ? (unsigned int) sizeof(*iovu.iov32) \ |
| 871 | : (unsigned int) sizeof(*iovu.iov64)) |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 872 | #define iov_iov_base(i) \ |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 873 | (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] | 874 | #define iov_iov_len(i) \ |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 875 | (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] | 876 | #else |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 877 | struct iovec *iov; |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 878 | #define sizeof_iov ((unsigned int) sizeof(*iov)) |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 879 | #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base) |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 880 | #define iov_iov_len(i) iov[i].iov_len |
| 881 | #endif |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 882 | int i; |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 883 | unsigned int size = sizeof_iov * len; |
| 884 | if (size / sizeof_iov != (unsigned int) len) { |
| 885 | error_func_msg("requested %u iovec elements exceeds" |
| 886 | " %u iovec limit", len, -1U / sizeof_iov); |
| 887 | return; |
| 888 | } |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 889 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 890 | iov = malloc(size); |
| 891 | if (!iov) { |
| 892 | error_func_msg("memory exhausted when tried to allocate" |
| 893 | " %u bytes", size); |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 894 | return; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 895 | } |
Denys Vlasenko | 7e69ed9 | 2015-03-21 19:50:53 +0100 | [diff] [blame] | 896 | if (umoven(tcp, addr, size, iov) >= 0) { |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 897 | for (i = 0; i < len; i++) { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 898 | kernel_ulong_t iov_len = iov_iov_len(i); |
Dmitry V. Levin | 05a0af6 | 2016-01-20 00:17:02 +0000 | [diff] [blame] | 899 | if (iov_len > data_size) |
| 900 | iov_len = data_size; |
| 901 | if (!iov_len) |
| 902 | break; |
| 903 | data_size -= iov_len; |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 904 | /* include the buffer number to make it easy to |
| 905 | * match up the trace with the source */ |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 906 | tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i); |
| 907 | dumpstr(tcp, iov_iov_base(i), iov_len); |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 908 | } |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 909 | } |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 910 | free(iov); |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 911 | #undef sizeof_iov |
| 912 | #undef iov_iov_base |
| 913 | #undef iov_iov_len |
| 914 | #undef iov |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 915 | } |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 916 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 917 | void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 918 | dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 919 | { |
| 920 | static int strsize = -1; |
| 921 | static unsigned char *str; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 922 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 923 | char outbuf[ |
| 924 | ( |
| 925 | (sizeof( |
| 926 | "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx " |
| 927 | "1234567890123456") + /*in case I'm off by few:*/ 4) |
| 928 | /*align to 8 to make memset easier:*/ + 7) & -8 |
| 929 | ]; |
| 930 | const unsigned char *src; |
| 931 | int i; |
| 932 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 933 | if ((len < 0) || (len > INT_MAX - 16)) |
| 934 | return; |
| 935 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 936 | memset(outbuf, ' ', sizeof(outbuf)); |
| 937 | |
| 938 | if (strsize < len + 16) { |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 939 | free(str); |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 940 | str = malloc(len + 16); |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 941 | if (!str) { |
| 942 | strsize = -1; |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 943 | error_func_msg("memory exhausted when tried to allocate" |
| 944 | " %zu bytes", (size_t) (len + 16)); |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 945 | return; |
| 946 | } |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 947 | strsize = len + 16; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Denys Vlasenko | 7e69ed9 | 2015-03-21 19:50:53 +0100 | [diff] [blame] | 950 | if (umoven(tcp, addr, len, str) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 951 | return; |
| 952 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 953 | /* Space-pad to 16 bytes */ |
| 954 | i = len; |
| 955 | while (i & 0xf) |
| 956 | str[i++] = ' '; |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 957 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 958 | i = 0; |
| 959 | src = str; |
| 960 | while (i < len) { |
| 961 | char *dst = outbuf; |
| 962 | /* Hex dump */ |
| 963 | do { |
| 964 | if (i < len) { |
| 965 | *dst++ = "0123456789abcdef"[*src >> 4]; |
| 966 | *dst++ = "0123456789abcdef"[*src & 0xf]; |
Elliott Hughes | dc75b01 | 2017-07-05 13:54:44 -0700 | [diff] [blame] | 967 | } else { |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 968 | *dst++ = ' '; |
| 969 | *dst++ = ' '; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 970 | } |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 971 | dst++; /* space is there by memset */ |
| 972 | i++; |
| 973 | if ((i & 7) == 0) |
| 974 | dst++; /* space is there by memset */ |
| 975 | src++; |
| 976 | } while (i & 0xf); |
| 977 | /* ASCII dump */ |
| 978 | i -= 16; |
| 979 | src -= 16; |
| 980 | do { |
| 981 | if (*src >= ' ' && *src < 0x7f) |
| 982 | *dst++ = *src; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 983 | else |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 984 | *dst++ = '.'; |
| 985 | src++; |
| 986 | } while (++i & 0xf); |
| 987 | *dst = '\0'; |
Denys Vlasenko | f90979b | 2013-02-22 15:00:11 +0100 | [diff] [blame] | 988 | tprintf(" | %05x %s |\n", i - 16, outbuf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 992 | bool |
| 993 | tfetch_mem64(struct tcb *const tcp, const uint64_t addr, |
| 994 | const unsigned int len, void *const our_addr) |
Dmitry V. Levin | 332a326 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 995 | { |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 996 | return addr && verbose(tcp) && |
| 997 | (entering(tcp) || !syserror(tcp)) && |
| 998 | !umoven(tcp, addr, len, our_addr); |
| 999 | } |
| 1000 | |
| 1001 | bool |
| 1002 | tfetch_mem64_ignore_syserror(struct tcb *const tcp, const uint64_t addr, |
| 1003 | const unsigned int len, void *const our_addr) |
| 1004 | { |
| 1005 | return addr && verbose(tcp) && |
| 1006 | !umoven(tcp, addr, len, our_addr); |
Dmitry V. Levin | 332a326 | 2015-07-05 22:09:29 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1009 | int |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1010 | umoven_or_printaddr64(struct tcb *const tcp, const uint64_t addr, |
| 1011 | const unsigned int len, void *const our_addr) |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1012 | { |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1013 | if (tfetch_mem64(tcp, addr, len, our_addr)) |
| 1014 | return 0; |
| 1015 | printaddr64(addr); |
| 1016 | return -1; |
| 1017 | } |
| 1018 | |
| 1019 | int |
| 1020 | umoven_or_printaddr64_ignore_syserror(struct tcb *const tcp, |
| 1021 | const uint64_t addr, |
| 1022 | const unsigned int len, |
| 1023 | void *const our_addr) |
| 1024 | { |
| 1025 | if (tfetch_mem64_ignore_syserror(tcp, addr, len, our_addr)) |
| 1026 | return 0; |
| 1027 | printaddr64(addr); |
| 1028 | return -1; |
| 1029 | } |
| 1030 | |
| 1031 | bool |
| 1032 | print_int32_array_member(struct tcb *tcp, void *elem_buf, size_t elem_size, |
| 1033 | void *data) |
| 1034 | { |
| 1035 | tprintf("%" PRId32, *(int32_t *) elem_buf); |
| 1036 | |
| 1037 | return true; |
| 1038 | } |
| 1039 | |
| 1040 | bool |
| 1041 | print_uint32_array_member(struct tcb *tcp, void *elem_buf, size_t elem_size, |
| 1042 | void *data) |
| 1043 | { |
| 1044 | tprintf("%" PRIu32, *(uint32_t *) elem_buf); |
| 1045 | |
| 1046 | return true; |
| 1047 | } |
| 1048 | |
| 1049 | bool |
| 1050 | print_uint64_array_member(struct tcb *tcp, void *elem_buf, size_t elem_size, |
| 1051 | void *data) |
| 1052 | { |
| 1053 | tprintf("%" PRIu64, *(uint64_t *) elem_buf); |
| 1054 | |
| 1055 | return true; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1058 | /* |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1059 | * Iteratively fetch and print up to nmemb elements of elem_size size |
| 1060 | * from the array that starts at tracee's address start_addr. |
| 1061 | * |
| 1062 | * Array elements are being fetched to the address specified by elem_buf. |
| 1063 | * |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1064 | * The fetcher callback function specified by tfetch_mem_func should follow |
| 1065 | * the same semantics as tfetch_mem function. |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1066 | * |
| 1067 | * The printer callback function specified by print_func is expected |
| 1068 | * to print something; if it returns false, no more iterations will be made. |
| 1069 | * |
| 1070 | * The pointer specified by opaque_data is passed to each invocation |
| 1071 | * of print_func callback function. |
| 1072 | * |
| 1073 | * This function prints: |
| 1074 | * - "NULL", if start_addr is NULL; |
| 1075 | * - "[]", if nmemb is 0; |
| 1076 | * - start_addr, if nmemb * elem_size overflows or wraps around; |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1077 | * - start_addr, if the first tfetch_mem_func invocation returned false; |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1078 | * - elements of the array, delimited by ", ", with the array itself |
| 1079 | * enclosed with [] brackets. |
| 1080 | * |
| 1081 | * If abbrev(tcp) is true, then |
| 1082 | * - the maximum number of elements printed equals to max_strlen; |
| 1083 | * - "..." is printed instead of max_strlen+1 element |
| 1084 | * and no more iterations will be made. |
| 1085 | * |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1086 | * This function returns true only if tfetch_mem_func has returned true |
| 1087 | * at least once. |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1088 | */ |
| 1089 | bool |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1090 | print_array_ex(struct tcb *const tcp, |
| 1091 | const kernel_ulong_t start_addr, |
| 1092 | const size_t nmemb, |
| 1093 | void *const elem_buf, |
| 1094 | const size_t elem_size, |
| 1095 | tfetch_mem_fn tfetch_mem_func, |
| 1096 | print_fn print_func, |
| 1097 | void *const opaque_data, |
| 1098 | unsigned int flags, |
| 1099 | const struct xlat *index_xlat, |
| 1100 | size_t index_xlat_size, |
| 1101 | const char *index_dflt) |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1102 | { |
| 1103 | if (!start_addr) { |
| 1104 | tprints("NULL"); |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | if (!nmemb) { |
| 1109 | tprints("[]"); |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
| 1113 | const size_t size = nmemb * elem_size; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1114 | const kernel_ulong_t end_addr = start_addr + size; |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1115 | |
| 1116 | if (end_addr <= start_addr || size / elem_size != nmemb) { |
| 1117 | printaddr(start_addr); |
| 1118 | return false; |
| 1119 | } |
| 1120 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1121 | const kernel_ulong_t abbrev_end = |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1122 | (abbrev(tcp) && max_strlen < nmemb) ? |
| 1123 | start_addr + elem_size * max_strlen : end_addr; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1124 | kernel_ulong_t cur; |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1125 | kernel_ulong_t idx = 0; |
| 1126 | enum xlat_style xlat_style = flags & XLAT_STYLE_MASK; |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1127 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1128 | for (cur = start_addr; cur < end_addr; cur += elem_size, idx++) { |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1129 | if (cur != start_addr) |
| 1130 | tprints(", "); |
| 1131 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1132 | if (!tfetch_mem_func(tcp, cur, elem_size, elem_buf)) { |
| 1133 | if (cur == start_addr) |
| 1134 | printaddr(cur); |
| 1135 | else { |
| 1136 | tprints("..."); |
| 1137 | printaddr_comment(cur); |
| 1138 | } |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1139 | break; |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1140 | } |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1141 | |
| 1142 | if (cur == start_addr) |
| 1143 | tprints("["); |
| 1144 | |
| 1145 | if (cur >= abbrev_end) { |
| 1146 | tprints("..."); |
| 1147 | cur = end_addr; |
| 1148 | break; |
| 1149 | } |
| 1150 | |
Elliott Hughes | 03a418e | 2018-06-15 13:11:40 -0700 | [diff] [blame] | 1151 | if (flags & PAF_PRINT_INDICES) { |
| 1152 | tprints("["); |
| 1153 | |
| 1154 | if (!index_xlat) { |
| 1155 | print_xlat_ex(idx, NULL, xlat_style); |
| 1156 | } else if (flags & PAF_INDEX_XLAT_VALUE_INDEXED) { |
| 1157 | printxval_indexn_ex(index_xlat, |
| 1158 | index_xlat_size, idx, |
| 1159 | index_dflt, xlat_style); |
| 1160 | } else { |
| 1161 | printxvals_ex(idx, index_dflt, xlat_style, |
| 1162 | (flags & PAF_INDEX_XLAT_SORTED) |
| 1163 | && idx ? NULL : index_xlat, |
| 1164 | NULL); |
| 1165 | } |
| 1166 | |
| 1167 | tprints("] = "); |
| 1168 | } |
| 1169 | |
Dmitry V. Levin | 72e4f7a | 2016-05-06 23:26:43 +0000 | [diff] [blame] | 1170 | if (!print_func(tcp, elem_buf, elem_size, opaque_data)) { |
| 1171 | cur = end_addr; |
| 1172 | break; |
| 1173 | } |
| 1174 | } |
| 1175 | if (cur != start_addr) |
| 1176 | tprints("]"); |
| 1177 | |
| 1178 | return cur >= end_addr; |
| 1179 | } |
Elvira Khabirova | 3c1105d | 2016-06-12 16:39:02 +0300 | [diff] [blame] | 1180 | |
| 1181 | int |
| 1182 | printargs(struct tcb *tcp) |
| 1183 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 1184 | const int n = tcp->s_ent->nargs; |
| 1185 | int i; |
| 1186 | for (i = 0; i < n; ++i) |
| 1187 | tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]); |
| 1188 | return RVAL_DECODED; |
Elvira Khabirova | 3c1105d | 2016-06-12 16:39:02 +0300 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | int |
| 1192 | printargs_u(struct tcb *tcp) |
| 1193 | { |
| 1194 | const int n = tcp->s_ent->nargs; |
| 1195 | int i; |
| 1196 | for (i = 0; i < n; ++i) |
| 1197 | tprintf("%s%u", i ? ", " : "", |
| 1198 | (unsigned int) tcp->u_arg[i]); |
| 1199 | return RVAL_DECODED; |
| 1200 | } |
| 1201 | |
| 1202 | int |
| 1203 | printargs_d(struct tcb *tcp) |
| 1204 | { |
| 1205 | const int n = tcp->s_ent->nargs; |
| 1206 | int i; |
| 1207 | for (i = 0; i < n; ++i) |
| 1208 | tprintf("%s%d", i ? ", " : "", |
| 1209 | (int) tcp->u_arg[i]); |
| 1210 | return RVAL_DECODED; |
| 1211 | } |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 1212 | |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 1213 | /* Print abnormal high bits of a kernel_ulong_t value. */ |
| 1214 | void |
| 1215 | print_abnormal_hi(const kernel_ulong_t val) |
| 1216 | { |
| 1217 | if (current_klongsize > 4) { |
| 1218 | const unsigned int hi = (unsigned int) ((uint64_t) val >> 32); |
| 1219 | if (hi) |
| 1220 | tprintf("%#x<<32|", hi); |
| 1221 | } |
| 1222 | } |
| 1223 | |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 1224 | #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64 |
| 1225 | # define open_file open64 |
| 1226 | #else |
| 1227 | # define open_file open |
| 1228 | #endif |
| 1229 | |
| 1230 | int |
Elliott Hughes | b755614 | 2018-02-20 17:03:16 -0800 | [diff] [blame] | 1231 | read_int_from_file(struct tcb *tcp, const char *const fname, int *const pvalue) |
Dmitry V. Levin | bf2698a | 2016-07-03 22:15:45 +0000 | [diff] [blame] | 1232 | { |
| 1233 | const int fd = open_file(fname, O_RDONLY); |
| 1234 | if (fd < 0) |
| 1235 | return -1; |
| 1236 | |
| 1237 | long lval; |
| 1238 | char buf[sizeof(lval) * 3]; |
| 1239 | int n = read(fd, buf, sizeof(buf) - 1); |
| 1240 | int saved_errno = errno; |
| 1241 | close(fd); |
| 1242 | |
| 1243 | if (n < 0) { |
| 1244 | errno = saved_errno; |
| 1245 | return -1; |
| 1246 | } |
| 1247 | |
| 1248 | buf[n] = '\0'; |
| 1249 | char *endptr = 0; |
| 1250 | errno = 0; |
| 1251 | lval = strtol(buf, &endptr, 10); |
| 1252 | if (!endptr || (*endptr && '\n' != *endptr) |
| 1253 | #if INT_MAX < LONG_MAX |
| 1254 | || lval > INT_MAX || lval < INT_MIN |
| 1255 | #endif |
| 1256 | || ERANGE == errno) { |
| 1257 | if (!errno) |
| 1258 | errno = EINVAL; |
| 1259 | return -1; |
| 1260 | } |
| 1261 | |
| 1262 | *pvalue = (int) lval; |
| 1263 | return 0; |
| 1264 | } |