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/user.h> |
| 36 | #include <sys/param.h> |
| 37 | #include <fcntl.h> |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 38 | #if HAVE_SYS_UIO_H |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 39 | # include <sys/uio.h> |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 40 | #endif |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 41 | |
Denys Vlasenko | 8470374 | 2012-02-25 02:38:52 +0100 | [diff] [blame] | 42 | #if defined(IA64) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 43 | # include <asm/ptrace_offsets.h> |
| 44 | # include <asm/rse.h> |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 47 | #ifdef HAVE_SYS_REG_H |
Denys Vlasenko | 8470374 | 2012-02-25 02:38:52 +0100 | [diff] [blame] | 48 | # include <sys/reg.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 49 | # define PTRACE_PEEKUSR PTRACE_PEEKUSER |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 50 | #elif defined(HAVE_LINUX_PTRACE_H) |
Denys Vlasenko | 8470374 | 2012-02-25 02:38:52 +0100 | [diff] [blame] | 51 | # undef PTRACE_SYSCALL |
Roland McGrath | ce9f074 | 2004-03-01 21:29:22 +0000 | [diff] [blame] | 52 | # ifdef HAVE_STRUCT_IA64_FPREG |
| 53 | # define ia64_fpreg XXX_ia64_fpreg |
| 54 | # endif |
| 55 | # ifdef HAVE_STRUCT_PT_ALL_USER_REGS |
| 56 | # define pt_all_user_regs XXX_pt_all_user_regs |
| 57 | # endif |
Denys Vlasenko | 8470374 | 2012-02-25 02:38:52 +0100 | [diff] [blame] | 58 | # include <linux/ptrace.h> |
Roland McGrath | ce9f074 | 2004-03-01 21:29:22 +0000 | [diff] [blame] | 59 | # undef ia64_fpreg |
| 60 | # undef pt_all_user_regs |
Wichert Akkerman | 2e2553a | 1999-05-09 00:29:58 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 63 | int |
Dmitry V. Levin | ccee169 | 2012-03-25 21:49:48 +0000 | [diff] [blame] | 64 | string_to_uint(const char *str) |
| 65 | { |
| 66 | char *error; |
| 67 | long value; |
| 68 | |
| 69 | if (!*str) |
| 70 | return -1; |
| 71 | errno = 0; |
| 72 | value = strtol(str, &error, 10); |
| 73 | if (errno || *error || value < 0 || (long)(int)value != value) |
| 74 | return -1; |
| 75 | return (int)value; |
| 76 | } |
| 77 | |
| 78 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 79 | tv_nz(struct timeval *a) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 80 | { |
| 81 | return a->tv_sec || a->tv_usec; |
| 82 | } |
| 83 | |
| 84 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 85 | tv_cmp(struct timeval *a, struct timeval *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 86 | { |
| 87 | if (a->tv_sec < b->tv_sec |
| 88 | || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) |
| 89 | return -1; |
| 90 | if (a->tv_sec > b->tv_sec |
| 91 | || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec)) |
| 92 | return 1; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | double |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 97 | tv_float(struct timeval *tv) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 98 | { |
| 99 | return tv->tv_sec + tv->tv_usec/1000000.0; |
| 100 | } |
| 101 | |
| 102 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 103 | tv_add(struct timeval *tv, struct timeval *a, struct timeval *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 104 | { |
| 105 | tv->tv_sec = a->tv_sec + b->tv_sec; |
| 106 | tv->tv_usec = a->tv_usec + b->tv_usec; |
Roland McGrath | 58372f5 | 2007-07-24 01:38:22 +0000 | [diff] [blame] | 107 | if (tv->tv_usec >= 1000000) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 108 | tv->tv_sec++; |
| 109 | tv->tv_usec -= 1000000; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 114 | tv_sub(struct timeval *tv, struct timeval *a, struct timeval *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 115 | { |
| 116 | tv->tv_sec = a->tv_sec - b->tv_sec; |
| 117 | tv->tv_usec = a->tv_usec - b->tv_usec; |
| 118 | if (((long) tv->tv_usec) < 0) { |
| 119 | tv->tv_sec--; |
| 120 | tv->tv_usec += 1000000; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 125 | tv_div(struct timeval *tv, struct timeval *a, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 126 | { |
| 127 | tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n; |
| 128 | tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000; |
| 129 | tv->tv_usec %= 1000000; |
| 130 | } |
| 131 | |
| 132 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 133 | tv_mul(struct timeval *tv, struct timeval *a, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 134 | { |
| 135 | tv->tv_usec = a->tv_usec * n; |
Dmitry V. Levin | fefdd97 | 2007-06-29 21:25:56 +0000 | [diff] [blame] | 136 | tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 137 | tv->tv_usec %= 1000000; |
| 138 | } |
| 139 | |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 140 | const char * |
| 141 | xlookup(const struct xlat *xlat, int val) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 142 | { |
| 143 | for (; xlat->str != NULL; xlat++) |
| 144 | if (xlat->val == val) |
| 145 | return xlat->str; |
| 146 | return 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 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 159 | /* |
| 160 | * Print entry in struct xlat table, if there. |
| 161 | */ |
| 162 | void |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 163 | printxval(const struct xlat *xlat, int val, const char *dflt) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 164 | { |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 165 | const char *str = xlookup(xlat, val); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 166 | |
| 167 | if (str) |
Denys Vlasenko | 5940e65 | 2011-09-01 09:55:05 +0200 | [diff] [blame] | 168 | tprints(str); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 169 | else |
| 170 | tprintf("%#x /* %s */", val, dflt); |
| 171 | } |
| 172 | |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 173 | /* |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 174 | * Print 64bit argument at position arg_no and return the index of the next |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 175 | * argument. |
| 176 | */ |
| 177 | int |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 178 | printllval(struct tcb *tcp, const char *format, int arg_no) |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 179 | { |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame^] | 180 | #if SIZEOF_LONG > 4 && SIZEOF_LONG == SIZEOF_LONG_LONG |
| 181 | # if SUPPORTED_PERSONALITIES > 1 |
| 182 | if (current_wordsize > 4) { |
| 183 | # endif |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 184 | tprintf(format, tcp->u_arg[arg_no]); |
| 185 | arg_no++; |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame^] | 186 | # if SUPPORTED_PERSONALITIES > 1 |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 187 | } else { |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame^] | 188 | # if defined(AARCH64) || defined(POWERPC64) |
Dmitry V. Levin | a55b6b1 | 2013-05-03 22:27:45 +0000 | [diff] [blame] | 189 | /* Align arg_no to the next even number. */ |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 190 | arg_no = (arg_no + 1) & 0xe; |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame^] | 191 | # endif |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 192 | tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1])); |
| 193 | arg_no += 2; |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 194 | } |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame^] | 195 | # endif /* SUPPORTED_PERSONALITIES */ |
| 196 | #elif SIZEOF_LONG > 4 |
| 197 | # error Unsupported configuration: SIZEOF_LONG > 4 && SIZEOF_LONG_LONG > SIZEOF_LONG |
| 198 | #elif defined LINUX_MIPSN32 |
| 199 | tprintf(format, tcp->ext_arg[arg_no]); |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 200 | arg_no++; |
Dmitry V. Levin | 0b46883 | 2013-05-02 08:41:27 +0000 | [diff] [blame] | 201 | #elif defined X32 |
| 202 | if (current_personality == 0) { |
| 203 | tprintf(format, tcp->ext_arg[arg_no]); |
| 204 | arg_no++; |
| 205 | } else { |
| 206 | tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1])); |
| 207 | arg_no += 2; |
| 208 | } |
Denys Vlasenko | c9d0fc0 | 2013-02-17 22:41:33 +0100 | [diff] [blame] | 209 | #else |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame^] | 210 | # if defined(ARM) || defined(POWERPC) |
Dmitry V. Levin | a55b6b1 | 2013-05-03 22:27:45 +0000 | [diff] [blame] | 211 | /* Align arg_no to the next even number. */ |
| 212 | arg_no = (arg_no + 1) & 0xe; |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame^] | 213 | # endif |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 214 | tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1])); |
| 215 | arg_no += 2; |
Denys Vlasenko | c9d0fc0 | 2013-02-17 22:41:33 +0100 | [diff] [blame] | 216 | #endif |
Dmitry V. Levin | 7a498be | 2013-05-04 19:51:57 +0000 | [diff] [blame^] | 217 | |
Chris Metcalf | 879dddd | 2013-03-01 10:41:02 +0100 | [diff] [blame] | 218 | return arg_no; |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 219 | } |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 220 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 221 | /* |
| 222 | * Interpret `xlat' as an array of flags |
| 223 | * print the entries whose bits are on in `flags' |
| 224 | * return # of flags printed. |
| 225 | */ |
Denys Vlasenko | 4924dbd | 2011-08-19 18:06:46 +0200 | [diff] [blame] | 226 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 227 | addflags(const struct xlat *xlat, int flags) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 228 | { |
Denys Vlasenko | 4924dbd | 2011-08-19 18:06:46 +0200 | [diff] [blame] | 229 | for (; xlat->str; xlat++) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 230 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 231 | tprintf("|%s", xlat->str); |
| 232 | flags &= ~xlat->val; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | if (flags) { |
| 236 | tprintf("|%#x", flags); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 237 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 240 | /* |
Denys Vlasenko | 4924dbd | 2011-08-19 18:06:46 +0200 | [diff] [blame] | 241 | * Interpret `xlat' as an array of flags. |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 242 | * Print to static string the entries whose bits are on in `flags' |
| 243 | * Return static string. |
| 244 | */ |
| 245 | const char * |
| 246 | sprintflags(const char *prefix, const struct xlat *xlat, int flags) |
| 247 | { |
| 248 | static char outstr[1024]; |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 249 | char *outptr; |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 250 | int found = 0; |
| 251 | |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 252 | outptr = stpcpy(outstr, prefix); |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 253 | |
| 254 | for (; xlat->str; xlat++) { |
| 255 | if ((flags & xlat->val) == xlat->val) { |
| 256 | if (found) |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 257 | *outptr++ = '|'; |
| 258 | outptr = stpcpy(outptr, xlat->str); |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 259 | found = 1; |
Denys Vlasenko | 4f3df07 | 2012-01-29 22:38:35 +0100 | [diff] [blame] | 260 | flags &= ~xlat->val; |
| 261 | if (!flags) |
| 262 | break; |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | if (flags) { |
| 266 | if (found) |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 267 | *outptr++ = '|'; |
| 268 | outptr += sprintf(outptr, "%#x", flags); |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return outstr; |
| 272 | } |
| 273 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 274 | int |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 275 | printflags(const struct xlat *xlat, int flags, const char *dflt) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 276 | { |
| 277 | int n; |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 278 | const char *sep; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 279 | |
| 280 | if (flags == 0 && xlat->val == 0) { |
Denys Vlasenko | 5940e65 | 2011-09-01 09:55:05 +0200 | [diff] [blame] | 281 | tprints(xlat->str); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | sep = ""; |
| 286 | for (n = 0; xlat->str; xlat++) { |
| 287 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 288 | tprintf("%s%s", sep, xlat->str); |
| 289 | flags &= ~xlat->val; |
| 290 | sep = "|"; |
| 291 | n++; |
| 292 | } |
| 293 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 294 | |
| 295 | if (n) { |
| 296 | if (flags) { |
| 297 | tprintf("%s%#x", sep, flags); |
| 298 | n++; |
| 299 | } |
| 300 | } else { |
| 301 | if (flags) { |
| 302 | tprintf("%#x", flags); |
| 303 | if (dflt) |
| 304 | tprintf(" /* %s */", dflt); |
| 305 | } else { |
| 306 | if (dflt) |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 307 | tprints("0"); |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 308 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 309 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 310 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 311 | return n; |
| 312 | } |
| 313 | |
| 314 | void |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 315 | printnum(struct tcb *tcp, long addr, const char *fmt) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 316 | { |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 317 | long num; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 318 | |
| 319 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 320 | tprints("NULL"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 321 | return; |
| 322 | } |
| 323 | if (umove(tcp, addr, &num) < 0) { |
| 324 | tprintf("%#lx", addr); |
| 325 | return; |
| 326 | } |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 327 | tprints("["); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 328 | tprintf(fmt, num); |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 329 | tprints("]"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 332 | void |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 333 | printnum_int(struct tcb *tcp, long addr, const char *fmt) |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 334 | { |
| 335 | int num; |
| 336 | |
| 337 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 338 | tprints("NULL"); |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 339 | return; |
| 340 | } |
| 341 | if (umove(tcp, addr, &num) < 0) { |
| 342 | tprintf("%#lx", addr); |
| 343 | return; |
| 344 | } |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 345 | tprints("["); |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 346 | tprintf(fmt, num); |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 347 | tprints("]"); |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void |
Dmitry V. Levin | 3138213 | 2011-03-04 05:08:02 +0300 | [diff] [blame] | 351 | printfd(struct tcb *tcp, int fd) |
| 352 | { |
Denys Vlasenko | 61ad0a4 | 2013-03-06 18:24:34 +0100 | [diff] [blame] | 353 | char path[PATH_MAX + 1]; |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 354 | |
Denys Vlasenko | 61ad0a4 | 2013-03-06 18:24:34 +0100 | [diff] [blame] | 355 | if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) |
| 356 | tprintf("%d<%s>", fd, path); |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 357 | else |
| 358 | tprintf("%d", fd); |
Dmitry V. Levin | 3138213 | 2011-03-04 05:08:02 +0300 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 362 | printuid(const char *text, unsigned long uid) |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 363 | { |
Denys Vlasenko | 5940e65 | 2011-09-01 09:55:05 +0200 | [diff] [blame] | 364 | tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid); |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 367 | /* |
| 368 | * Quote string `instr' of length `size' |
| 369 | * Write up to (3 + `size' * 4) bytes to `outstr' buffer. |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 370 | * If `len' is -1, treat `instr' as a NUL-terminated string |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 371 | * and quote at most (`size' - 1) bytes. |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 372 | * |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 373 | * Returns 0 if len == -1 and NUL was seen, 1 otherwise. |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 374 | * Note that if len >= 0, always returns 1. |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 375 | */ |
Mike Frysinger | ebee04c | 2012-04-17 22:19:31 -0400 | [diff] [blame] | 376 | int |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 377 | string_quote(const char *instr, char *outstr, long len, int size) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 378 | { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 379 | const unsigned char *ustr = (const unsigned char *) instr; |
| 380 | char *s = outstr; |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 381 | int usehex, c, i, eol; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 382 | |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 383 | eol = 0x100; /* this can never match a char */ |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 384 | if (len == -1) { |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 385 | size--; |
| 386 | eol = '\0'; |
| 387 | } |
| 388 | |
| 389 | usehex = 0; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 390 | if (xflag > 1) |
| 391 | usehex = 1; |
| 392 | else if (xflag) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 393 | /* Check for presence of symbol which require |
| 394 | to hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 395 | for (i = 0; i < size; ++i) { |
| 396 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 397 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 398 | if (c == eol) |
| 399 | break; |
Denys Vlasenko | 5198ed4 | 2013-03-06 23:44:23 +0100 | [diff] [blame] | 400 | |
| 401 | /* Force hex unless c is printable or whitespace */ |
| 402 | if (c > 0x7e) { |
| 403 | usehex = 1; |
| 404 | break; |
| 405 | } |
| 406 | /* In ASCII isspace is only these chars: "\t\n\v\f\r". |
| 407 | * They happen to have ASCII codes 9,10,11,12,13. |
| 408 | */ |
| 409 | if (c < ' ' && (unsigned)(c - 9) >= 5) { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 410 | usehex = 1; |
| 411 | break; |
| 412 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 415 | |
| 416 | *s++ = '\"'; |
| 417 | |
| 418 | if (usehex) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 419 | /* Hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 420 | for (i = 0; i < size; ++i) { |
| 421 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 422 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 423 | if (c == eol) |
| 424 | goto asciz_ended; |
| 425 | *s++ = '\\'; |
| 426 | *s++ = 'x'; |
| 427 | *s++ = "0123456789abcdef"[c >> 4]; |
| 428 | *s++ = "0123456789abcdef"[c & 0xf]; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 429 | } |
| 430 | } else { |
| 431 | for (i = 0; i < size; ++i) { |
| 432 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 433 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 434 | if (c == eol) |
| 435 | goto asciz_ended; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 436 | switch (c) { |
| 437 | case '\"': case '\\': |
| 438 | *s++ = '\\'; |
| 439 | *s++ = c; |
| 440 | break; |
| 441 | case '\f': |
| 442 | *s++ = '\\'; |
| 443 | *s++ = 'f'; |
| 444 | break; |
| 445 | case '\n': |
| 446 | *s++ = '\\'; |
| 447 | *s++ = 'n'; |
| 448 | break; |
| 449 | case '\r': |
| 450 | *s++ = '\\'; |
| 451 | *s++ = 'r'; |
| 452 | break; |
| 453 | case '\t': |
| 454 | *s++ = '\\'; |
| 455 | *s++ = 't'; |
| 456 | break; |
| 457 | case '\v': |
| 458 | *s++ = '\\'; |
| 459 | *s++ = 'v'; |
| 460 | break; |
| 461 | default: |
Denys Vlasenko | 5198ed4 | 2013-03-06 23:44:23 +0100 | [diff] [blame] | 462 | if (c >= ' ' && c <= 0x7e) |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 463 | *s++ = c; |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 464 | else { |
| 465 | /* Print \octal */ |
| 466 | *s++ = '\\'; |
| 467 | if (i + 1 < size |
| 468 | && ustr[i + 1] >= '0' |
| 469 | && ustr[i + 1] <= '9' |
| 470 | ) { |
| 471 | /* Print \ooo */ |
| 472 | *s++ = '0' + (c >> 6); |
| 473 | *s++ = '0' + ((c >> 3) & 0x7); |
| 474 | } else { |
| 475 | /* Print \[[o]o]o */ |
| 476 | if ((c >> 3) != 0) { |
| 477 | if ((c >> 6) != 0) |
| 478 | *s++ = '0' + (c >> 6); |
| 479 | *s++ = '0' + ((c >> 3) & 0x7); |
| 480 | } |
| 481 | } |
| 482 | *s++ = '0' + (c & 0x7); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 483 | } |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | *s++ = '\"'; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 490 | *s = '\0'; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 491 | |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 492 | /* Return zero if we printed entire ASCIZ string (didn't truncate it) */ |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 493 | if (len == -1 && ustr[i] == '\0') { |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 494 | /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended') |
| 495 | * but next char is NUL. |
| 496 | */ |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | return 1; |
| 501 | |
| 502 | asciz_ended: |
| 503 | *s++ = '\"'; |
| 504 | *s = '\0'; |
| 505 | /* Return zero: we printed entire ASCIZ string (didn't truncate it) */ |
| 506 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 509 | /* |
| 510 | * Print path string specified by address `addr' and length `n'. |
| 511 | * If path length exceeds `n', append `...' to the output. |
| 512 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 513 | void |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 514 | printpathn(struct tcb *tcp, long addr, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 515 | { |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 516 | char path[MAXPATHLEN + 1]; |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 517 | int nul_seen; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 518 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 519 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 520 | tprints("NULL"); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 524 | /* Cap path length to the path buffer size */ |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 525 | if (n > sizeof path - 1) |
| 526 | n = sizeof path - 1; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 527 | |
| 528 | /* Fetch one byte more to find out whether path length > n. */ |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 529 | nul_seen = umovestr(tcp, addr, n + 1, path); |
| 530 | if (nul_seen < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 531 | tprintf("%#lx", addr); |
| 532 | else { |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 533 | char *outstr; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 534 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 535 | path[n] = '\0'; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 536 | n++; |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 537 | outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */ |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 538 | string_quote(path, outstr, -1, n); |
| 539 | tprints(outstr); |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 540 | if (!nul_seen) |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 541 | tprints("..."); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | void |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 546 | printpath(struct tcb *tcp, long addr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 547 | { |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 548 | /* Size must correspond to char path[] size in printpathn */ |
| 549 | printpathn(tcp, addr, MAXPATHLEN); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 552 | /* |
| 553 | * Print string specified by address `addr' and length `len'. |
| 554 | * If `len' < 0, treat the string as a NUL-terminated string. |
| 555 | * If string length exceeds `max_strlen', append `...' to the output. |
| 556 | */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 557 | void |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 558 | printstr(struct tcb *tcp, long addr, long len) |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 559 | { |
| 560 | static char *str = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 561 | static char *outstr; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 562 | int size; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 563 | int ellipsis; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 564 | |
| 565 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 566 | tprints("NULL"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 567 | return; |
| 568 | } |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 569 | /* Allocate static buffers if they are not allocated yet. */ |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 570 | if (!str) { |
Dmitry V. Levin | 378f9c5 | 2012-03-25 22:56:53 +0000 | [diff] [blame] | 571 | unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3; |
| 572 | |
| 573 | if (outstr_size / 4 != max_strlen) |
| 574 | die_out_of_memory(); |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 575 | str = malloc(max_strlen + 1); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 576 | if (!str) |
| 577 | die_out_of_memory(); |
Dmitry V. Levin | 378f9c5 | 2012-03-25 22:56:53 +0000 | [diff] [blame] | 578 | outstr = malloc(outstr_size); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 579 | if (!outstr) |
| 580 | die_out_of_memory(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 581 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 582 | |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 583 | if (len == -1) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 584 | /* |
| 585 | * Treat as a NUL-terminated string: fetch one byte more |
| 586 | * because string_quote() quotes one byte less. |
| 587 | */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 588 | size = max_strlen + 1; |
| 589 | if (umovestr(tcp, addr, size, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 590 | tprintf("%#lx", addr); |
| 591 | return; |
| 592 | } |
| 593 | } |
| 594 | else { |
Denys Vlasenko | b5d43b8 | 2012-04-28 14:58:35 +0200 | [diff] [blame] | 595 | size = max_strlen; |
| 596 | if (size > (unsigned long)len) |
| 597 | size = (unsigned long)len; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 598 | if (umoven(tcp, addr, size, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 599 | tprintf("%#lx", addr); |
| 600 | return; |
| 601 | } |
| 602 | } |
| 603 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 604 | /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str |
| 605 | * or we were requested to print more than -s NUM chars)... |
| 606 | */ |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 607 | ellipsis = (string_quote(str, outstr, len, size) && |
| 608 | (len < 0 || len > max_strlen)); |
Roland McGrath | a503dcf | 2007-08-02 02:06:26 +0000 | [diff] [blame] | 609 | |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 610 | tprints(outstr); |
| 611 | if (ellipsis) |
| 612 | tprints("..."); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 613 | } |
| 614 | |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 615 | #if HAVE_SYS_UIO_H |
| 616 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 617 | dumpiov(struct tcb *tcp, int len, long addr) |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 618 | { |
Denys Vlasenko | 8470374 | 2012-02-25 02:38:52 +0100 | [diff] [blame] | 619 | #if SUPPORTED_PERSONALITIES > 1 |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 620 | union { |
| 621 | struct { u_int32_t base; u_int32_t len; } *iov32; |
| 622 | struct { u_int64_t base; u_int64_t len; } *iov64; |
| 623 | } iovu; |
| 624 | #define iov iovu.iov64 |
| 625 | #define sizeof_iov \ |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 626 | (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64)) |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 627 | #define iov_iov_base(i) \ |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 628 | (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] | 629 | #define iov_iov_len(i) \ |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 630 | (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] | 631 | #else |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 632 | struct iovec *iov; |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 633 | #define sizeof_iov sizeof(*iov) |
| 634 | #define iov_iov_base(i) iov[i].iov_base |
| 635 | #define iov_iov_len(i) iov[i].iov_len |
| 636 | #endif |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 637 | int i; |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 638 | unsigned size; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 639 | |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 640 | size = sizeof_iov * len; |
| 641 | /* Assuming no sane program has millions of iovs */ |
| 642 | if ((unsigned)len > 1024*1024 /* insane or negative size? */ |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 643 | || (iov = malloc(size)) == NULL) { |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 644 | fprintf(stderr, "Out of memory\n"); |
| 645 | return; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 646 | } |
Roland McGrath | aa524c8 | 2005-06-01 19:22:06 +0000 | [diff] [blame] | 647 | if (umoven(tcp, addr, size, (char *) iov) >= 0) { |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 648 | for (i = 0; i < len; i++) { |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 649 | /* include the buffer number to make it easy to |
| 650 | * match up the trace with the source */ |
| 651 | tprintf(" * %lu bytes in buffer %d\n", |
| 652 | (unsigned long)iov_iov_len(i), i); |
| 653 | dumpstr(tcp, (long) iov_iov_base(i), |
| 654 | iov_iov_len(i)); |
| 655 | } |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 656 | } |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 657 | free(iov); |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 658 | #undef sizeof_iov |
| 659 | #undef iov_iov_base |
| 660 | #undef iov_iov_len |
| 661 | #undef iov |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 662 | } |
| 663 | #endif |
| 664 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 665 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 666 | dumpstr(struct tcb *tcp, long addr, int len) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 667 | { |
| 668 | static int strsize = -1; |
| 669 | static unsigned char *str; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 670 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 671 | char outbuf[ |
| 672 | ( |
| 673 | (sizeof( |
| 674 | "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx " |
| 675 | "1234567890123456") + /*in case I'm off by few:*/ 4) |
| 676 | /*align to 8 to make memset easier:*/ + 7) & -8 |
| 677 | ]; |
| 678 | const unsigned char *src; |
| 679 | int i; |
| 680 | |
| 681 | memset(outbuf, ' ', sizeof(outbuf)); |
| 682 | |
| 683 | if (strsize < len + 16) { |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 684 | free(str); |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 685 | str = malloc(len + 16); |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 686 | if (!str) { |
| 687 | strsize = -1; |
| 688 | fprintf(stderr, "Out of memory\n"); |
| 689 | return; |
| 690 | } |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 691 | strsize = len + 16; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | if (umoven(tcp, addr, len, (char *) str) < 0) |
| 695 | return; |
| 696 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 697 | /* Space-pad to 16 bytes */ |
| 698 | i = len; |
| 699 | while (i & 0xf) |
| 700 | str[i++] = ' '; |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 701 | |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 702 | i = 0; |
| 703 | src = str; |
| 704 | while (i < len) { |
| 705 | char *dst = outbuf; |
| 706 | /* Hex dump */ |
| 707 | do { |
| 708 | if (i < len) { |
| 709 | *dst++ = "0123456789abcdef"[*src >> 4]; |
| 710 | *dst++ = "0123456789abcdef"[*src & 0xf]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 711 | } |
| 712 | else { |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 713 | *dst++ = ' '; |
| 714 | *dst++ = ' '; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 715 | } |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 716 | dst++; /* space is there by memset */ |
| 717 | i++; |
| 718 | if ((i & 7) == 0) |
| 719 | dst++; /* space is there by memset */ |
| 720 | src++; |
| 721 | } while (i & 0xf); |
| 722 | /* ASCII dump */ |
| 723 | i -= 16; |
| 724 | src -= 16; |
| 725 | do { |
| 726 | if (*src >= ' ' && *src < 0x7f) |
| 727 | *dst++ = *src; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 728 | else |
Denys Vlasenko | 7632580 | 2013-02-22 14:47:39 +0100 | [diff] [blame] | 729 | *dst++ = '.'; |
| 730 | src++; |
| 731 | } while (++i & 0xf); |
| 732 | *dst = '\0'; |
Denys Vlasenko | f90979b | 2013-02-22 15:00:11 +0100 | [diff] [blame] | 733 | tprintf(" | %05x %s |\n", i - 16, outbuf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Mike Frysinger | 612659e | 2012-02-14 14:38:28 +0100 | [diff] [blame] | 737 | #ifdef HAVE_PROCESS_VM_READV |
| 738 | /* C library supports this, but the kernel might not. */ |
| 739 | static bool process_vm_readv_not_supported = 0; |
| 740 | #else |
| 741 | |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 742 | /* Need to do this since process_vm_readv() is not yet available in libc. |
| 743 | * When libc is be updated, only "static bool process_vm_readv_not_supported" |
| 744 | * line should remain. |
| 745 | */ |
| 746 | #if !defined(__NR_process_vm_readv) |
| 747 | # if defined(I386) |
| 748 | # define __NR_process_vm_readv 347 |
| 749 | # elif defined(X86_64) |
| 750 | # define __NR_process_vm_readv 310 |
| 751 | # elif defined(POWERPC) |
| 752 | # define __NR_process_vm_readv 351 |
| 753 | # endif |
| 754 | #endif |
| 755 | |
| 756 | #if defined(__NR_process_vm_readv) |
| 757 | static bool process_vm_readv_not_supported = 0; |
Mike Frysinger | 24ee60b | 2012-05-04 19:37:29 -0400 | [diff] [blame] | 758 | /* Have to avoid duplicating with the C library headers. */ |
| 759 | static ssize_t strace_process_vm_readv(pid_t pid, |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 760 | const struct iovec *lvec, |
| 761 | unsigned long liovcnt, |
| 762 | const struct iovec *rvec, |
| 763 | unsigned long riovcnt, |
| 764 | unsigned long flags) |
| 765 | { |
| 766 | return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags); |
| 767 | } |
Mike Frysinger | 24ee60b | 2012-05-04 19:37:29 -0400 | [diff] [blame] | 768 | #define process_vm_readv strace_process_vm_readv |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 769 | #else |
| 770 | static bool process_vm_readv_not_supported = 1; |
| 771 | # define process_vm_readv(...) (errno = ENOSYS, -1) |
| 772 | #endif |
Mike Frysinger | 612659e | 2012-02-14 14:38:28 +0100 | [diff] [blame] | 773 | |
| 774 | #endif /* end of hack */ |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 775 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 776 | #define PAGMASK (~(PAGSIZ - 1)) |
| 777 | /* |
| 778 | * move `len' bytes of data from process `pid' |
| 779 | * at address `addr' to our space at `laddr' |
| 780 | */ |
| 781 | int |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 782 | umoven(struct tcb *tcp, long addr, int len, char *laddr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 783 | { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 784 | int pid = tcp->pid; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 785 | int n, m, nread; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 786 | union { |
| 787 | long val; |
| 788 | char x[sizeof(long)]; |
| 789 | } u; |
| 790 | |
Denys Vlasenko | 2544f98 | 2013-02-19 17:39:56 +0100 | [diff] [blame] | 791 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 792 | if (current_wordsize < sizeof(addr)) |
| 793 | addr &= (1ul << 8 * current_wordsize) - 1; |
Denys Vlasenko | d2a660f | 2012-02-25 00:43:22 +0100 | [diff] [blame] | 794 | #endif |
| 795 | |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 796 | if (!process_vm_readv_not_supported) { |
| 797 | struct iovec local[1], remote[1]; |
| 798 | int r; |
| 799 | |
| 800 | local[0].iov_base = laddr; |
| 801 | remote[0].iov_base = (void*)addr; |
| 802 | local[0].iov_len = remote[0].iov_len = len; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 803 | r = process_vm_readv(pid, local, 1, remote, 1, 0); |
Ben Noordhuis | 1d58fe9 | 2013-02-26 12:24:25 +0100 | [diff] [blame] | 804 | if (r == len) |
| 805 | return 0; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 806 | if (r >= 0) { |
| 807 | error_msg("umoven: short read (%d < %d) @0x%lx", |
| 808 | r, len, addr); |
| 809 | return -1; |
| 810 | } |
| 811 | switch (errno) { |
| 812 | case ENOSYS: |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 813 | process_vm_readv_not_supported = 1; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 814 | break; |
| 815 | case ESRCH: |
| 816 | /* the process is gone */ |
| 817 | return -1; |
| 818 | case EFAULT: case EIO: case EPERM: |
| 819 | /* address space is inaccessible */ |
| 820 | return -1; |
| 821 | default: |
| 822 | /* all the rest is strange and should be reported */ |
Denys Vlasenko | 905e8e0 | 2013-02-26 12:30:09 +0100 | [diff] [blame] | 823 | perror_msg("process_vm_readv"); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 824 | return -1; |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 825 | } |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 826 | } |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 827 | |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 828 | nread = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 829 | if (addr & (sizeof(long) - 1)) { |
| 830 | /* addr not a multiple of sizeof(long) */ |
| 831 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 832 | addr &= -sizeof(long); /* residue */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 833 | errno = 0; |
| 834 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 835 | switch (errno) { |
| 836 | case 0: |
| 837 | break; |
| 838 | case ESRCH: case EINVAL: |
| 839 | /* these could be seen if the process is gone */ |
| 840 | return -1; |
| 841 | case EFAULT: case EIO: case EPERM: |
| 842 | /* address space is inaccessible */ |
| 843 | return -1; |
| 844 | default: |
| 845 | /* all the rest is strange and should be reported */ |
| 846 | perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", |
| 847 | pid, addr); |
| 848 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 849 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 850 | m = MIN(sizeof(long) - n, len); |
| 851 | memcpy(laddr, &u.x[n], m); |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 852 | addr += sizeof(long); |
| 853 | laddr += m; |
| 854 | nread += m; |
| 855 | len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 856 | } |
| 857 | while (len) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 858 | errno = 0; |
| 859 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 860 | switch (errno) { |
| 861 | case 0: |
| 862 | break; |
| 863 | case ESRCH: case EINVAL: |
| 864 | /* these could be seen if the process is gone */ |
| 865 | return -1; |
| 866 | case EFAULT: case EIO: case EPERM: |
| 867 | /* address space is inaccessible */ |
| 868 | if (nread) { |
| 869 | perror_msg("umoven: short read (%d < %d) @0x%lx", |
| 870 | nread, nread + len, addr - nread); |
| 871 | } |
| 872 | return -1; |
| 873 | default: |
| 874 | /* all the rest is strange and should be reported */ |
| 875 | perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", |
| 876 | pid, addr); |
| 877 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 878 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 879 | m = MIN(sizeof(long), len); |
| 880 | memcpy(laddr, u.x, m); |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 881 | addr += sizeof(long); |
| 882 | laddr += m; |
| 883 | nread += m; |
| 884 | len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 885 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 886 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | /* |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 891 | * Like `umove' but make the additional effort of looking |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 892 | * for a terminating zero byte. |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 893 | * |
| 894 | * Returns < 0 on error, > 0 if NUL was seen, |
| 895 | * (TODO if useful: return count of bytes including NUL), |
| 896 | * else 0 if len bytes were read but no NUL byte seen. |
| 897 | * |
| 898 | * Note: there is no guarantee we won't overwrite some bytes |
| 899 | * in laddr[] _after_ terminating NUL (but, of course, |
| 900 | * we never write past laddr[len-1]). |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 901 | */ |
| 902 | int |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 903 | umovestr(struct tcb *tcp, long addr, int len, char *laddr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 904 | { |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 905 | #if SIZEOF_LONG == 4 |
| 906 | const unsigned long x01010101 = 0x01010101ul; |
| 907 | const unsigned long x80808080 = 0x80808080ul; |
| 908 | #elif SIZEOF_LONG == 8 |
| 909 | const unsigned long x01010101 = 0x0101010101010101ul; |
| 910 | const unsigned long x80808080 = 0x8080808080808080ul; |
| 911 | #else |
| 912 | # error SIZEOF_LONG > 8 |
| 913 | #endif |
| 914 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 915 | int pid = tcp->pid; |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 916 | int n, m, nread; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 917 | union { |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 918 | unsigned long val; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 919 | char x[sizeof(long)]; |
| 920 | } u; |
| 921 | |
Denys Vlasenko | 2544f98 | 2013-02-19 17:39:56 +0100 | [diff] [blame] | 922 | #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4 |
Denys Vlasenko | 9fd4f96 | 2012-03-19 09:36:42 +0100 | [diff] [blame] | 923 | if (current_wordsize < sizeof(addr)) |
| 924 | addr &= (1ul << 8 * current_wordsize) - 1; |
Dmitry V. Levin | 856c7ed | 2011-12-26 20:12:02 +0000 | [diff] [blame] | 925 | #endif |
| 926 | |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 927 | nread = 0; |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 928 | if (!process_vm_readv_not_supported) { |
| 929 | struct iovec local[1], remote[1]; |
| 930 | |
| 931 | local[0].iov_base = laddr; |
| 932 | remote[0].iov_base = (void*)addr; |
| 933 | |
| 934 | while (len > 0) { |
| 935 | int end_in_page; |
| 936 | int r; |
| 937 | int chunk_len; |
| 938 | |
| 939 | /* Don't read kilobytes: most strings are short */ |
| 940 | chunk_len = len; |
| 941 | if (chunk_len > 256) |
| 942 | chunk_len = 256; |
| 943 | /* Don't cross pages. I guess otherwise we can get EFAULT |
| 944 | * and fail to notice that terminating NUL lies |
| 945 | * in the existing (first) page. |
| 946 | * (I hope there aren't arches with pages < 4K) |
| 947 | */ |
| 948 | end_in_page = ((addr + chunk_len) & 4095); |
| 949 | r = chunk_len - end_in_page; |
| 950 | if (r > 0) /* if chunk_len > end_in_page */ |
| 951 | chunk_len = r; /* chunk_len -= end_in_page */ |
| 952 | |
| 953 | local[0].iov_len = remote[0].iov_len = chunk_len; |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 954 | r = process_vm_readv(pid, local, 1, remote, 1, 0); |
| 955 | if (r > 0) { |
| 956 | if (memchr(local[0].iov_base, '\0', r)) |
| 957 | return 1; |
| 958 | local[0].iov_base += r; |
| 959 | remote[0].iov_base += r; |
| 960 | len -= r; |
| 961 | nread += r; |
| 962 | continue; |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 963 | } |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 964 | switch (errno) { |
| 965 | case ENOSYS: |
| 966 | process_vm_readv_not_supported = 1; |
| 967 | goto vm_readv_didnt_work; |
| 968 | case ESRCH: |
| 969 | /* the process is gone */ |
| 970 | return -1; |
| 971 | case EFAULT: case EIO: case EPERM: |
| 972 | /* address space is inaccessible */ |
| 973 | if (nread) { |
| 974 | perror_msg("umovestr: short read (%d < %d) @0x%lx", |
| 975 | nread, nread + len, addr); |
| 976 | } |
| 977 | return -1; |
| 978 | default: |
| 979 | /* all the rest is strange and should be reported */ |
| 980 | perror_msg("process_vm_readv"); |
| 981 | return -1; |
| 982 | } |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 983 | } |
| 984 | return 0; |
| 985 | } |
| 986 | vm_readv_didnt_work: |
| 987 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 988 | if (addr & (sizeof(long) - 1)) { |
| 989 | /* addr not a multiple of sizeof(long) */ |
| 990 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 991 | addr &= -sizeof(long); /* residue */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 992 | errno = 0; |
| 993 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 994 | switch (errno) { |
| 995 | case 0: |
| 996 | break; |
| 997 | case ESRCH: case EINVAL: |
| 998 | /* these could be seen if the process is gone */ |
| 999 | return -1; |
| 1000 | case EFAULT: case EIO: case EPERM: |
| 1001 | /* address space is inaccessible */ |
| 1002 | return -1; |
| 1003 | default: |
| 1004 | /* all the rest is strange and should be reported */ |
| 1005 | perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", |
| 1006 | pid, addr); |
| 1007 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1008 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1009 | m = MIN(sizeof(long) - n, len); |
| 1010 | memcpy(laddr, &u.x[n], m); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1011 | while (n & (sizeof(long) - 1)) |
| 1012 | if (u.x[n++] == '\0') |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 1013 | return 1; |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1014 | addr += sizeof(long); |
| 1015 | laddr += m; |
| 1016 | nread += m; |
| 1017 | len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1018 | } |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1019 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1020 | while (len) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1021 | errno = 0; |
| 1022 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
Dmitry V. Levin | 9700592 | 2013-02-26 21:16:22 +0000 | [diff] [blame] | 1023 | switch (errno) { |
| 1024 | case 0: |
| 1025 | break; |
| 1026 | case ESRCH: case EINVAL: |
| 1027 | /* these could be seen if the process is gone */ |
| 1028 | return -1; |
| 1029 | case EFAULT: case EIO: case EPERM: |
| 1030 | /* address space is inaccessible */ |
| 1031 | if (nread) { |
| 1032 | perror_msg("umovestr: short read (%d < %d) @0x%lx", |
| 1033 | nread, nread + len, addr - nread); |
| 1034 | } |
| 1035 | return -1; |
| 1036 | default: |
| 1037 | /* all the rest is strange and should be reported */ |
| 1038 | perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", |
| 1039 | pid, addr); |
| 1040 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1041 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1042 | m = MIN(sizeof(long), len); |
| 1043 | memcpy(laddr, u.x, m); |
Denys Vlasenko | 1694092 | 2013-03-01 18:52:59 +0100 | [diff] [blame] | 1044 | /* "If a NUL char exists in this word" */ |
| 1045 | if ((u.val - x01010101) & ~u.val & x80808080) |
| 1046 | return 1; |
| 1047 | addr += sizeof(long); |
| 1048 | laddr += m; |
| 1049 | nread += m; |
| 1050 | len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1051 | } |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 1052 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1055 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1056 | upeek(struct tcb *tcp, long off, long *res) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1057 | { |
| 1058 | long val; |
| 1059 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1060 | errno = 0; |
Denys Vlasenko | 114aefd | 2012-03-08 12:13:44 +0100 | [diff] [blame] | 1061 | val = ptrace(PTRACE_PEEKUSER, tcp->pid, (char *) off, 0); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1062 | if (val == -1 && errno) { |
| 1063 | if (errno != ESRCH) { |
Denys Vlasenko | 4c65c44 | 2012-03-08 11:54:10 +0100 | [diff] [blame] | 1064 | perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", tcp->pid, off); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1065 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1066 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1067 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1068 | *res = val; |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
Denys Vlasenko | d27809c | 2013-02-12 12:50:10 +0100 | [diff] [blame] | 1072 | /* Note! On new kernels (about 2.5.46+), we use PTRACE_O_TRACECLONE |
| 1073 | * and PTRACE_O_TRACE[V]FORK for tracing children. |
| 1074 | * If you are adding a new arch which is only supported by newer kernels, |
| 1075 | * you most likely don't need to add any code below |
| 1076 | * beside a dummy "return 0" block in change_syscall(). |
| 1077 | */ |
| 1078 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1079 | /* |
| 1080 | * These #if's are huge, please indent them correctly. |
| 1081 | * It's easy to get confused otherwise. |
| 1082 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1083 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1084 | #include "syscall.h" |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1085 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1086 | #ifndef CLONE_PTRACE |
| 1087 | # define CLONE_PTRACE 0x00002000 |
| 1088 | #endif |
| 1089 | #ifndef CLONE_VFORK |
| 1090 | # define CLONE_VFORK 0x00004000 |
| 1091 | #endif |
| 1092 | #ifndef CLONE_VM |
| 1093 | # define CLONE_VM 0x00000100 |
| 1094 | #endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1095 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1096 | #ifdef IA64 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1097 | |
| 1098 | typedef unsigned long *arg_setup_state; |
| 1099 | |
| 1100 | static int |
| 1101 | arg_setup(struct tcb *tcp, arg_setup_state *state) |
| 1102 | { |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1103 | unsigned long cfm, sof, sol; |
| 1104 | long bsp; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1105 | |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1106 | if (ia32) { |
| 1107 | /* Satisfy a false GCC warning. */ |
| 1108 | *state = NULL; |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1109 | return 0; |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1110 | } |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1111 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1112 | if (upeek(tcp, PT_AR_BSP, &bsp) < 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1113 | return -1; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1114 | if (upeek(tcp, PT_CFM, (long *) &cfm) < 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1115 | return -1; |
| 1116 | |
| 1117 | sof = (cfm >> 0) & 0x7f; |
| 1118 | sol = (cfm >> 7) & 0x7f; |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1119 | bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1120 | |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1121 | *state = (unsigned long *) bsp; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1125 | # define arg_finish_change(tcp, state) 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1126 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1127 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1128 | get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1129 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1130 | int ret; |
| 1131 | |
| 1132 | if (ia32) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1133 | ret = upeek(tcp, PT_R11, valp); |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1134 | else |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1135 | ret = umoven(tcp, |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1136 | (unsigned long) ia64_rse_skip_regs(*state, 0), |
| 1137 | sizeof(long), (void *) valp); |
| 1138 | return ret; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1142 | get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1143 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1144 | int ret; |
| 1145 | |
| 1146 | if (ia32) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1147 | ret = upeek(tcp, PT_R9, valp); |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1148 | else |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1149 | ret = umoven(tcp, |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1150 | (unsigned long) ia64_rse_skip_regs(*state, 1), |
| 1151 | sizeof(long), (void *) valp); |
| 1152 | return ret; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1153 | } |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1154 | |
| 1155 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1156 | set_arg0(struct tcb *tcp, arg_setup_state *state, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1157 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1158 | int req = PTRACE_POKEDATA; |
| 1159 | void *ap; |
| 1160 | |
| 1161 | if (ia32) { |
| 1162 | ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */ |
| 1163 | req = PTRACE_POKEUSER; |
| 1164 | } else |
| 1165 | ap = ia64_rse_skip_regs(*state, 0); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1166 | errno = 0; |
| 1167 | ptrace(req, tcp->pid, ap, val); |
| 1168 | return errno ? -1 : 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1172 | set_arg1(struct tcb *tcp, arg_setup_state *state, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1173 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1174 | int req = PTRACE_POKEDATA; |
| 1175 | void *ap; |
| 1176 | |
| 1177 | if (ia32) { |
| 1178 | ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */ |
| 1179 | req = PTRACE_POKEUSER; |
| 1180 | } else |
| 1181 | ap = ia64_rse_skip_regs(*state, 1); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1182 | errno = 0; |
| 1183 | ptrace(req, tcp->pid, ap, val); |
| 1184 | return errno ? -1 : 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Roland McGrath | b659f87 | 2008-07-18 01:19:36 +0000 | [diff] [blame] | 1187 | /* ia64 does not return the input arguments from functions (and syscalls) |
| 1188 | according to ia64 RSE (Register Stack Engine) behavior. */ |
| 1189 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1190 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1191 | # define restore_arg1(tcp, state, val) ((void) (state), 0) |
Roland McGrath | b659f87 | 2008-07-18 01:19:36 +0000 | [diff] [blame] | 1192 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1193 | #elif defined(SPARC) || defined(SPARC64) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1194 | |
Denys Vlasenko | ce7d953 | 2013-02-05 16:36:13 +0100 | [diff] [blame] | 1195 | # if defined(SPARC64) |
| 1196 | # undef PTRACE_GETREGS |
| 1197 | # define PTRACE_GETREGS PTRACE_GETREGS64 |
| 1198 | # undef PTRACE_SETREGS |
| 1199 | # define PTRACE_SETREGS PTRACE_SETREGS64 |
| 1200 | # endif |
| 1201 | |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1202 | typedef struct pt_regs arg_setup_state; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1203 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1204 | # define arg_setup(tcp, state) \ |
| 1205 | (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0)) |
| 1206 | # define arg_finish_change(tcp, state) \ |
| 1207 | (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0)) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1208 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1209 | # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0) |
| 1210 | # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0) |
| 1211 | # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0) |
| 1212 | # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0) |
| 1213 | # define restore_arg0(tcp, state, val) 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1214 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1215 | #else /* other architectures */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1216 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1217 | # if defined S390 || defined S390X |
Roland McGrath | 7b30822 | 2003-01-20 09:04:36 +0000 | [diff] [blame] | 1218 | /* Note: this is only true for the `clone' system call, which handles |
| 1219 | arguments specially. We could as well say that its first two arguments |
| 1220 | are swapped relative to other architectures, but that would just be |
| 1221 | another #ifdef in the calls. */ |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1222 | # define arg0_offset PT_GPR3 |
| 1223 | # define arg1_offset PT_ORIGGPR2 |
| 1224 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1225 | # define restore_arg1(tcp, state, val) ((void) (state), 0) |
| 1226 | # define arg0_index 1 |
| 1227 | # define arg1_index 0 |
| 1228 | # elif defined(ALPHA) || defined(MIPS) |
| 1229 | # define arg0_offset REG_A0 |
| 1230 | # define arg1_offset (REG_A0+1) |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1231 | # elif defined(POWERPC) |
| 1232 | # define arg0_offset (sizeof(unsigned long)*PT_R3) |
| 1233 | # define arg1_offset (sizeof(unsigned long)*PT_R4) |
| 1234 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1235 | # elif defined(HPPA) |
| 1236 | # define arg0_offset PT_GR26 |
| 1237 | # define arg1_offset (PT_GR26-4) |
H.J. Lu | 35be581 | 2012-04-16 13:00:01 +0200 | [diff] [blame] | 1238 | # elif defined(X86_64) || defined(X32) |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1239 | # define arg0_offset ((long)(8*(current_personality ? RBX : RDI))) |
| 1240 | # define arg1_offset ((long)(8*(current_personality ? RCX : RSI))) |
| 1241 | # elif defined(SH) |
| 1242 | # define arg0_offset (4*(REG_REG0+4)) |
| 1243 | # define arg1_offset (4*(REG_REG0+5)) |
| 1244 | # elif defined(SH64) |
| 1245 | /* ABI defines arg0 & 1 in r2 & r3 */ |
| 1246 | # define arg0_offset (REG_OFFSET+16) |
| 1247 | # define arg1_offset (REG_OFFSET+24) |
| 1248 | # define restore_arg0(tcp, state, val) 0 |
| 1249 | # elif defined CRISV10 || defined CRISV32 |
| 1250 | # define arg0_offset (4*PT_R11) |
| 1251 | # define arg1_offset (4*PT_ORIG_R10) |
| 1252 | # define restore_arg0(tcp, state, val) 0 |
| 1253 | # define restore_arg1(tcp, state, val) 0 |
| 1254 | # define arg0_index 1 |
| 1255 | # define arg1_index 0 |
| 1256 | # else |
| 1257 | # define arg0_offset 0 |
| 1258 | # define arg1_offset 4 |
| 1259 | # if defined ARM |
| 1260 | # define restore_arg0(tcp, state, val) 0 |
| 1261 | # endif |
| 1262 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1263 | |
| 1264 | typedef int arg_setup_state; |
| 1265 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1266 | # define arg_setup(tcp, state) (0) |
| 1267 | # define arg_finish_change(tcp, state) 0 |
| 1268 | # define get_arg0(tcp, cookie, valp) (upeek((tcp), arg0_offset, (valp))) |
| 1269 | # define get_arg1(tcp, cookie, valp) (upeek((tcp), arg1_offset, (valp))) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1270 | |
| 1271 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1272 | set_arg0(struct tcb *tcp, void *cookie, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1273 | { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1274 | return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1278 | set_arg1(struct tcb *tcp, void *cookie, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1279 | { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1280 | return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1283 | #endif /* architectures */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1284 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1285 | #ifndef restore_arg0 |
| 1286 | # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val)) |
| 1287 | #endif |
| 1288 | #ifndef restore_arg1 |
| 1289 | # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val)) |
| 1290 | #endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1291 | |
Denys Vlasenko | 2d1e90f | 2012-02-25 02:46:14 +0100 | [diff] [blame] | 1292 | #ifndef arg0_index |
| 1293 | # define arg0_index 0 |
| 1294 | # define arg1_index 1 |
| 1295 | #endif |
Roland McGrath | 90d0afd | 2004-03-01 21:05:16 +0000 | [diff] [blame] | 1296 | |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1297 | static int |
| 1298 | change_syscall(struct tcb *tcp, arg_setup_state *state, int new) |
| 1299 | { |
| 1300 | #if defined(I386) |
| 1301 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_EAX * 4), new) < 0) |
| 1302 | return -1; |
| 1303 | return 0; |
Denys Vlasenko | bf357fc | 2013-02-12 13:06:51 +0100 | [diff] [blame] | 1304 | #elif defined(X86_64) |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1305 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_RAX * 8), new) < 0) |
| 1306 | return -1; |
| 1307 | return 0; |
Denys Vlasenko | bf357fc | 2013-02-12 13:06:51 +0100 | [diff] [blame] | 1308 | #elif defined(X32) |
| 1309 | /* setbpt/clearbpt never used: */ |
| 1310 | /* X32 is only supported since about linux-3.0.30 */ |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1311 | #elif defined(POWERPC) |
| 1312 | if (ptrace(PTRACE_POKEUSER, tcp->pid, |
| 1313 | (char*)(sizeof(unsigned long)*PT_R0), new) < 0) |
| 1314 | return -1; |
| 1315 | return 0; |
| 1316 | #elif defined(S390) || defined(S390X) |
| 1317 | /* s390 linux after 2.4.7 has a hook in entry.S to allow this */ |
| 1318 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GPR2), new) < 0) |
| 1319 | return -1; |
| 1320 | return 0; |
| 1321 | #elif defined(M68K) |
| 1322 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_ORIG_D0), new) < 0) |
| 1323 | return -1; |
| 1324 | return 0; |
| 1325 | #elif defined(SPARC) || defined(SPARC64) |
| 1326 | state->u_regs[U_REG_G1] = new; |
| 1327 | return 0; |
| 1328 | #elif defined(MIPS) |
| 1329 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), new) < 0) |
| 1330 | return -1; |
| 1331 | return 0; |
| 1332 | #elif defined(ALPHA) |
| 1333 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), new) < 0) |
| 1334 | return -1; |
| 1335 | return 0; |
| 1336 | #elif defined(AVR32) |
Denys Vlasenko | d27809c | 2013-02-12 12:50:10 +0100 | [diff] [blame] | 1337 | /* setbpt/clearbpt never used: */ |
| 1338 | /* AVR32 is only supported since about linux-2.6.19 */ |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1339 | #elif defined(BFIN) |
Denys Vlasenko | d27809c | 2013-02-12 12:50:10 +0100 | [diff] [blame] | 1340 | /* setbpt/clearbpt never used: */ |
| 1341 | /* Blackfin is only supported since about linux-2.6.23 */ |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1342 | #elif defined(IA64) |
| 1343 | if (ia32) { |
| 1344 | switch (new) { |
| 1345 | case 2: |
| 1346 | break; /* x86 SYS_fork */ |
| 1347 | case SYS_clone: |
| 1348 | new = 120; |
| 1349 | break; |
| 1350 | default: |
| 1351 | fprintf(stderr, "%s: unexpected syscall %d\n", |
| 1352 | __FUNCTION__, new); |
| 1353 | return -1; |
| 1354 | } |
| 1355 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R1), new) < 0) |
| 1356 | return -1; |
| 1357 | } else if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R15), new) < 0) |
| 1358 | return -1; |
| 1359 | return 0; |
| 1360 | #elif defined(HPPA) |
| 1361 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR20), new) < 0) |
| 1362 | return -1; |
| 1363 | return 0; |
| 1364 | #elif defined(SH) |
| 1365 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*(REG_REG0+3)), new) < 0) |
| 1366 | return -1; |
| 1367 | return 0; |
| 1368 | #elif defined(SH64) |
| 1369 | /* Top half of reg encodes the no. of args n as 0x1n. |
| 1370 | Assume 0 args as kernel never actually checks... */ |
| 1371 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_SYSCALL), |
| 1372 | 0x100000 | new) < 0) |
| 1373 | return -1; |
| 1374 | return 0; |
| 1375 | #elif defined(CRISV10) || defined(CRISV32) |
| 1376 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_R9), new) < 0) |
| 1377 | return -1; |
| 1378 | return 0; |
| 1379 | #elif defined(ARM) |
| 1380 | /* Some kernels support this, some (pre-2.6.16 or so) don't. */ |
| 1381 | # ifndef PTRACE_SET_SYSCALL |
| 1382 | # define PTRACE_SET_SYSCALL 23 |
| 1383 | # endif |
| 1384 | if (ptrace(PTRACE_SET_SYSCALL, tcp->pid, 0, new & 0xffff) != 0) |
| 1385 | return -1; |
| 1386 | return 0; |
Denys Vlasenko | bf357fc | 2013-02-12 13:06:51 +0100 | [diff] [blame] | 1387 | #elif defined(AARCH64) |
| 1388 | /* setbpt/clearbpt never used: */ |
| 1389 | /* AARCH64 is only supported since about linux-3.0.31 */ |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1390 | #elif defined(TILE) |
Denys Vlasenko | d27809c | 2013-02-12 12:50:10 +0100 | [diff] [blame] | 1391 | /* setbpt/clearbpt never used: */ |
| 1392 | /* Tilera CPUs are only supported since about linux-2.6.34 */ |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1393 | #elif defined(MICROBLAZE) |
Denys Vlasenko | a86696b | 2013-02-12 16:07:54 +0100 | [diff] [blame] | 1394 | /* setbpt/clearbpt never used: */ |
| 1395 | /* microblaze is only supported since about linux-2.6.30 */ |
Christian Svensson | 492f81f | 2013-02-14 13:26:27 +0100 | [diff] [blame] | 1396 | #elif defined(OR1K) |
| 1397 | /* never reached; OR1K is only supported by kernels since 3.1.0. */ |
James Hogan | 5f999a8 | 2013-02-22 14:44:10 +0000 | [diff] [blame] | 1398 | #elif defined(METAG) |
| 1399 | /* setbpt/clearbpt never used: */ |
| 1400 | /* Meta is only supported since linux-3.7 */ |
Chris Zankel | 8f636ed | 2013-03-25 10:22:07 -0700 | [diff] [blame] | 1401 | #elif defined(XTENSA) |
| 1402 | /* setbpt/clearbpt never used: */ |
| 1403 | /* Xtensa is only supported since linux 2.6.13 */ |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1404 | #else |
| 1405 | #warning Do not know how to handle change_syscall for this architecture |
| 1406 | #endif /* architecture */ |
| 1407 | return -1; |
| 1408 | } |
| 1409 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1410 | int |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1411 | setbpt(struct tcb *tcp) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1412 | { |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1413 | static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone }; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1414 | arg_setup_state state; |
| 1415 | |
| 1416 | if (tcp->flags & TCB_BPTSET) { |
| 1417 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1418 | return -1; |
| 1419 | } |
| 1420 | |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1421 | /* |
| 1422 | * It's a silly kludge to initialize this with a search at runtime. |
| 1423 | * But it's better than maintaining another magic thing in the |
| 1424 | * godforsaken tables. |
| 1425 | */ |
| 1426 | if (clone_scno[current_personality] == 0) { |
| 1427 | int i; |
| 1428 | for (i = 0; i < nsyscalls; ++i) |
| 1429 | if (sysent[i].sys_func == sys_clone) { |
| 1430 | clone_scno[current_personality] = i; |
| 1431 | break; |
| 1432 | } |
| 1433 | } |
| 1434 | |
Denys Vlasenko | 74ec14f | 2013-02-21 16:13:47 +0100 | [diff] [blame] | 1435 | if (tcp->s_ent->sys_func == sys_fork || |
| 1436 | tcp->s_ent->sys_func == sys_vfork) { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1437 | if (arg_setup(tcp, &state) < 0 |
| 1438 | || get_arg0(tcp, &state, &tcp->inst[0]) < 0 |
| 1439 | || get_arg1(tcp, &state, &tcp->inst[1]) < 0 |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1440 | || change_syscall(tcp, &state, |
| 1441 | clone_scno[current_personality]) < 0 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1442 | || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0 |
| 1443 | || set_arg1(tcp, &state, 0) < 0 |
| 1444 | || arg_finish_change(tcp, &state) < 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1445 | return -1; |
Roland McGrath | c9dc3c1 | 2004-03-01 20:57:09 +0000 | [diff] [blame] | 1446 | tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD; |
| 1447 | tcp->u_arg[arg1_index] = 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1448 | tcp->flags |= TCB_BPTSET; |
| 1449 | return 0; |
Dmitry V. Levin | 0c66151 | 2012-02-20 21:17:58 +0000 | [diff] [blame] | 1450 | } |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1451 | |
Denys Vlasenko | 74ec14f | 2013-02-21 16:13:47 +0100 | [diff] [blame] | 1452 | if (tcp->s_ent->sys_func == sys_clone) { |
Jan Kratochvil | 8fc9575 | 2008-08-06 21:43:35 +0000 | [diff] [blame] | 1453 | /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)' |
Dmitry V. Levin | 0c66151 | 2012-02-20 21:17:58 +0000 | [diff] [blame] | 1454 | contrary to x86 vfork above. Even on x86 we turn the |
Jan Kratochvil | 8fc9575 | 2008-08-06 21:43:35 +0000 | [diff] [blame] | 1455 | vfork semantics into plain fork - each application must not |
| 1456 | depend on the vfork specifics according to POSIX. We would |
| 1457 | hang waiting for the parent resume otherwise. We need to |
| 1458 | clear also CLONE_VM but only in the CLONE_VFORK case as |
| 1459 | otherwise we would break pthread_create. */ |
| 1460 | |
Denys Vlasenko | c133bf0 | 2011-06-23 21:57:54 +0200 | [diff] [blame] | 1461 | long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE); |
| 1462 | if (new_arg0 & CLONE_VFORK) |
| 1463 | new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM); |
| 1464 | if (arg_setup(tcp, &state) < 0 |
| 1465 | || set_arg0(tcp, &state, new_arg0) < 0 |
| 1466 | || arg_finish_change(tcp, &state) < 0) |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1467 | return -1; |
Roland McGrath | c9dc3c1 | 2004-03-01 20:57:09 +0000 | [diff] [blame] | 1468 | tcp->inst[0] = tcp->u_arg[arg0_index]; |
| 1469 | tcp->inst[1] = tcp->u_arg[arg1_index]; |
Denys Vlasenko | 55980f5 | 2012-05-14 16:40:28 +0200 | [diff] [blame] | 1470 | tcp->flags |= TCB_BPTSET; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1471 | return 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Dmitry V. Levin | 0c66151 | 2012-02-20 21:17:58 +0000 | [diff] [blame] | 1474 | fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n", |
| 1475 | tcp->scno, tcp->pid); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1476 | return -1; |
| 1477 | } |
| 1478 | |
| 1479 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1480 | clearbpt(struct tcb *tcp) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1481 | { |
| 1482 | arg_setup_state state; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1483 | if (arg_setup(tcp, &state) < 0 |
James Hogan | 05eb905 | 2012-11-29 17:37:37 +0000 | [diff] [blame] | 1484 | || change_syscall(tcp, &state, tcp->scno) < 0 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1485 | || restore_arg0(tcp, &state, tcp->inst[0]) < 0 |
| 1486 | || restore_arg1(tcp, &state, tcp->inst[1]) < 0 |
| 1487 | || arg_finish_change(tcp, &state)) |
Denys Vlasenko | c133bf0 | 2011-06-23 21:57:54 +0200 | [diff] [blame] | 1488 | if (errno != ESRCH) |
| 1489 | return -1; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1490 | tcp->flags &= ~TCB_BPTSET; |
| 1491 | return 0; |
| 1492 | } |