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