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