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. |
| 32 | * |
| 33 | * $Id$ |
| 34 | */ |
| 35 | |
| 36 | #include "defs.h" |
| 37 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 38 | #include <signal.h> |
| 39 | #include <sys/syscall.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 40 | #include <sys/user.h> |
| 41 | #include <sys/param.h> |
| 42 | #include <fcntl.h> |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 43 | #if HAVE_SYS_UIO_H |
| 44 | #include <sys/uio.h> |
| 45 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 46 | #ifdef SUNOS4 |
| 47 | #include <machine/reg.h> |
| 48 | #include <a.out.h> |
| 49 | #include <link.h> |
| 50 | #endif /* SUNOS4 */ |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 51 | |
Wichert Akkerman | 43a7482 | 2000-06-27 17:33:32 +0000 | [diff] [blame] | 52 | #if defined(linux) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1)) |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 53 | #include <linux/ptrace.h> |
Roland McGrath | 1e85cf9 | 2002-12-16 20:40:54 +0000 | [diff] [blame] | 54 | #endif |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 55 | |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 56 | #if defined(LINUX) && defined(IA64) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 57 | # include <asm/ptrace_offsets.h> |
| 58 | # include <asm/rse.h> |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 61 | #ifdef HAVE_SYS_REG_H |
| 62 | #include <sys/reg.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 63 | # define PTRACE_PEEKUSR PTRACE_PEEKUSER |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 64 | #elif defined(HAVE_LINUX_PTRACE_H) |
| 65 | #undef PTRACE_SYSCALL |
Roland McGrath | ce9f074 | 2004-03-01 21:29:22 +0000 | [diff] [blame] | 66 | # ifdef HAVE_STRUCT_IA64_FPREG |
| 67 | # define ia64_fpreg XXX_ia64_fpreg |
| 68 | # endif |
| 69 | # ifdef HAVE_STRUCT_PT_ALL_USER_REGS |
| 70 | # define pt_all_user_regs XXX_pt_all_user_regs |
| 71 | # endif |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 72 | #include <linux/ptrace.h> |
Roland McGrath | ce9f074 | 2004-03-01 21:29:22 +0000 | [diff] [blame] | 73 | # undef ia64_fpreg |
| 74 | # undef pt_all_user_regs |
Wichert Akkerman | 2e2553a | 1999-05-09 00:29:58 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 77 | #ifdef SUNOS4_KERNEL_ARCH_KLUDGE |
| 78 | #include <sys/utsname.h> |
| 79 | #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */ |
| 80 | |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 81 | #if defined(LINUXSPARC) && defined (SPARC64) |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 82 | # undef PTRACE_GETREGS |
| 83 | # define PTRACE_GETREGS PTRACE_GETREGS64 |
| 84 | # undef PTRACE_SETREGS |
| 85 | # define PTRACE_SETREGS PTRACE_SETREGS64 |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 86 | #endif |
| 87 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 88 | /* macros */ |
| 89 | #ifndef MAX |
| 90 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) |
| 91 | #endif |
| 92 | #ifndef MIN |
| 93 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) |
| 94 | #endif |
| 95 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 96 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 97 | tv_nz(struct timeval *a) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 98 | { |
| 99 | return a->tv_sec || a->tv_usec; |
| 100 | } |
| 101 | |
| 102 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 103 | tv_cmp(struct timeval *a, struct timeval *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 104 | { |
| 105 | if (a->tv_sec < b->tv_sec |
| 106 | || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) |
| 107 | return -1; |
| 108 | if (a->tv_sec > b->tv_sec |
| 109 | || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec)) |
| 110 | return 1; |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | double |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 115 | tv_float(struct timeval *tv) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 116 | { |
| 117 | return tv->tv_sec + tv->tv_usec/1000000.0; |
| 118 | } |
| 119 | |
| 120 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 121 | tv_add(struct timeval *tv, struct timeval *a, struct timeval *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 122 | { |
| 123 | tv->tv_sec = a->tv_sec + b->tv_sec; |
| 124 | tv->tv_usec = a->tv_usec + b->tv_usec; |
Roland McGrath | 58372f5 | 2007-07-24 01:38:22 +0000 | [diff] [blame] | 125 | if (tv->tv_usec >= 1000000) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 126 | tv->tv_sec++; |
| 127 | tv->tv_usec -= 1000000; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 132 | tv_sub(struct timeval *tv, struct timeval *a, struct timeval *b) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 133 | { |
| 134 | tv->tv_sec = a->tv_sec - b->tv_sec; |
| 135 | tv->tv_usec = a->tv_usec - b->tv_usec; |
| 136 | if (((long) tv->tv_usec) < 0) { |
| 137 | tv->tv_sec--; |
| 138 | tv->tv_usec += 1000000; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 143 | tv_div(struct timeval *tv, struct timeval *a, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 144 | { |
| 145 | tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n; |
| 146 | tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000; |
| 147 | tv->tv_usec %= 1000000; |
| 148 | } |
| 149 | |
| 150 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 151 | tv_mul(struct timeval *tv, struct timeval *a, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 152 | { |
| 153 | tv->tv_usec = a->tv_usec * n; |
Dmitry V. Levin | fefdd97 | 2007-06-29 21:25:56 +0000 | [diff] [blame] | 154 | tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 155 | tv->tv_usec %= 1000000; |
| 156 | } |
| 157 | |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 158 | const char * |
| 159 | xlookup(const struct xlat *xlat, int val) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 160 | { |
| 161 | for (; xlat->str != NULL; xlat++) |
| 162 | if (xlat->val == val) |
| 163 | return xlat->str; |
| 164 | return NULL; |
| 165 | } |
| 166 | |
Denys Vlasenko | 0a295bc | 2011-09-01 16:31:48 +0200 | [diff] [blame] | 167 | #if !defined HAVE_STPCPY |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 168 | char * |
| 169 | stpcpy(char *dst, const char *src) |
| 170 | { |
| 171 | while ((*dst = *src++) != '\0') |
| 172 | dst++; |
| 173 | return dst; |
| 174 | } |
Denys Vlasenko | 0a295bc | 2011-09-01 16:31:48 +0200 | [diff] [blame] | 175 | #endif |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 176 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 177 | /* |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 178 | * Generic ptrace wrapper which tracks ESRCH errors |
| 179 | * by setting tcp->ptrace_errno to ESRCH. |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 180 | * |
| 181 | * We assume that ESRCH indicates likely process death (SIGKILL?), |
| 182 | * modulo bugs where process somehow ended up not stopped. |
| 183 | * Unfortunately kernel uses ESRCH for that case too. Oh well. |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 184 | * |
| 185 | * Currently used by upeek() only. |
| 186 | * TODO: use this in all other ptrace() calls while decoding. |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 187 | */ |
Denys Vlasenko | 4dedd56 | 2009-02-24 15:17:53 +0000 | [diff] [blame] | 188 | long |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 189 | do_ptrace(int request, struct tcb *tcp, void *addr, void *data) |
Denys Vlasenko | 4dedd56 | 2009-02-24 15:17:53 +0000 | [diff] [blame] | 190 | { |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 191 | long l; |
| 192 | |
| 193 | errno = 0; |
Mike Frysinger | 09a13c2 | 2009-10-07 01:13:39 -0400 | [diff] [blame] | 194 | l = ptrace(request, tcp->pid, addr, (long) data); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 195 | /* Non-ESRCH errors might be our invalid reg/mem accesses, |
| 196 | * we do not record them. */ |
| 197 | if (errno == ESRCH) |
| 198 | tcp->ptrace_errno = ESRCH; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 199 | return l; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Used when we want to unblock stopped traced process. |
| 204 | * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL. |
| 205 | * Returns 0 on success or if error was ESRCH |
| 206 | * (presumably process was killed while we talk to it). |
| 207 | * Otherwise prints error message and returns -1. |
| 208 | */ |
| 209 | int |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 210 | ptrace_restart(int op, struct tcb *tcp, int sig) |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 211 | { |
| 212 | int err; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 213 | const char *msg; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 214 | |
| 215 | errno = 0; |
Mike Frysinger | 09a13c2 | 2009-10-07 01:13:39 -0400 | [diff] [blame] | 216 | ptrace(op, tcp->pid, (void *) 1, (long) sig); |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 217 | err = errno; |
| 218 | if (!err || err == ESRCH) |
| 219 | return 0; |
| 220 | |
| 221 | tcp->ptrace_errno = err; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 222 | msg = "SYSCALL"; |
| 223 | if (op == PTRACE_CONT) |
| 224 | msg = "CONT"; |
| 225 | if (op == PTRACE_DETACH) |
| 226 | msg = "DETACH"; |
Denys Vlasenko | 31fa8a2 | 2012-01-29 02:01:44 +0100 | [diff] [blame^] | 227 | #ifdef PTRACE_LISTEN |
| 228 | if (op == PTRACE_LISTEN) |
| 229 | msg = "LISTEN"; |
| 230 | #endif |
Denys Vlasenko | 014ca3a | 2011-09-02 16:19:30 +0200 | [diff] [blame] | 231 | perror_msg("ptrace(PTRACE_%s,1,%d)", msg, sig); |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | /* |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 236 | * Print entry in struct xlat table, if there. |
| 237 | */ |
| 238 | void |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 239 | printxval(const struct xlat *xlat, int val, const char *dflt) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 240 | { |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 241 | const char *str = xlookup(xlat, val); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 242 | |
| 243 | if (str) |
Denys Vlasenko | 5940e65 | 2011-09-01 09:55:05 +0200 | [diff] [blame] | 244 | tprints(str); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 245 | else |
| 246 | tprintf("%#x /* %s */", val, dflt); |
| 247 | } |
| 248 | |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 249 | #if HAVE_LONG_LONG |
| 250 | /* |
| 251 | * Print 64bit argument at position llarg and return the index of the next |
| 252 | * argument. |
| 253 | */ |
| 254 | int |
| 255 | printllval(struct tcb *tcp, const char *format, int llarg) |
| 256 | { |
| 257 | # if defined(FREEBSD) \ |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 258 | || (defined(LINUX) && defined(POWERPC) && !defined(POWERPC64)) \ |
Dmitry V. Levin | 7a5b08f | 2011-05-28 20:47:43 +0000 | [diff] [blame] | 259 | || defined(LINUX_MIPSO32) \ |
| 260 | || defined(__ARM_EABI__) |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 261 | /* Align 64bit argument to 64bit boundary. */ |
Denys Vlasenko | 4b08df4 | 2011-08-19 15:58:24 +0200 | [diff] [blame] | 262 | llarg = (llarg + 1) & 0x1e; |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 263 | # endif |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 264 | # if defined LINUX && (defined X86_64 || defined POWERPC64) |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 265 | if (current_personality == 0) { |
| 266 | tprintf(format, tcp->u_arg[llarg]); |
| 267 | llarg++; |
| 268 | } else { |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 269 | # ifdef POWERPC64 |
| 270 | /* Align 64bit argument to 64bit boundary. */ |
Denys Vlasenko | 4b08df4 | 2011-08-19 15:58:24 +0200 | [diff] [blame] | 271 | llarg = (llarg + 1) & 0x1e; |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 272 | # endif |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 273 | tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1])); |
| 274 | llarg += 2; |
| 275 | } |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 276 | # elif defined IA64 || defined ALPHA |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 277 | tprintf(format, tcp->u_arg[llarg]); |
| 278 | llarg++; |
| 279 | # elif defined LINUX_MIPSN32 |
| 280 | tprintf(format, tcp->ext_arg[llarg]); |
| 281 | llarg++; |
| 282 | # else |
| 283 | tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1])); |
| 284 | llarg += 2; |
| 285 | # endif |
| 286 | return llarg; |
| 287 | } |
| 288 | #endif |
| 289 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 290 | /* |
| 291 | * Interpret `xlat' as an array of flags |
| 292 | * print the entries whose bits are on in `flags' |
| 293 | * return # of flags printed. |
| 294 | */ |
Denys Vlasenko | 4924dbd | 2011-08-19 18:06:46 +0200 | [diff] [blame] | 295 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 296 | addflags(const struct xlat *xlat, int flags) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 297 | { |
Denys Vlasenko | 4924dbd | 2011-08-19 18:06:46 +0200 | [diff] [blame] | 298 | for (; xlat->str; xlat++) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 299 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 300 | tprintf("|%s", xlat->str); |
| 301 | flags &= ~xlat->val; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | if (flags) { |
| 305 | tprintf("|%#x", flags); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 306 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 309 | /* |
Denys Vlasenko | 4924dbd | 2011-08-19 18:06:46 +0200 | [diff] [blame] | 310 | * Interpret `xlat' as an array of flags. |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 311 | * Print to static string the entries whose bits are on in `flags' |
| 312 | * Return static string. |
| 313 | */ |
| 314 | const char * |
| 315 | sprintflags(const char *prefix, const struct xlat *xlat, int flags) |
| 316 | { |
| 317 | static char outstr[1024]; |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 318 | char *outptr; |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 319 | int found = 0; |
| 320 | |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 321 | outptr = stpcpy(outstr, prefix); |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 322 | |
| 323 | for (; xlat->str; xlat++) { |
| 324 | if ((flags & xlat->val) == xlat->val) { |
| 325 | if (found) |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 326 | *outptr++ = '|'; |
| 327 | outptr = stpcpy(outptr, xlat->str); |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 328 | flags &= ~xlat->val; |
| 329 | found = 1; |
| 330 | } |
| 331 | } |
| 332 | if (flags) { |
| 333 | if (found) |
Denys Vlasenko | 5284557 | 2011-08-31 12:07:38 +0200 | [diff] [blame] | 334 | *outptr++ = '|'; |
| 335 | outptr += sprintf(outptr, "%#x", flags); |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return outstr; |
| 339 | } |
| 340 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 341 | int |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 342 | printflags(const struct xlat *xlat, int flags, const char *dflt) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 343 | { |
| 344 | int n; |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 345 | const char *sep; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 346 | |
| 347 | if (flags == 0 && xlat->val == 0) { |
Denys Vlasenko | 5940e65 | 2011-09-01 09:55:05 +0200 | [diff] [blame] | 348 | tprints(xlat->str); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | sep = ""; |
| 353 | for (n = 0; xlat->str; xlat++) { |
| 354 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 355 | tprintf("%s%s", sep, xlat->str); |
| 356 | flags &= ~xlat->val; |
| 357 | sep = "|"; |
| 358 | n++; |
| 359 | } |
| 360 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 361 | |
| 362 | if (n) { |
| 363 | if (flags) { |
| 364 | tprintf("%s%#x", sep, flags); |
| 365 | n++; |
| 366 | } |
| 367 | } else { |
| 368 | if (flags) { |
| 369 | tprintf("%#x", flags); |
| 370 | if (dflt) |
| 371 | tprintf(" /* %s */", dflt); |
| 372 | } else { |
| 373 | if (dflt) |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 374 | tprints("0"); |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 375 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 376 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 377 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 378 | return n; |
| 379 | } |
| 380 | |
| 381 | void |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 382 | printnum(struct tcb *tcp, long addr, const char *fmt) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 383 | { |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 384 | long num; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 385 | |
| 386 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 387 | tprints("NULL"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | if (umove(tcp, addr, &num) < 0) { |
| 391 | tprintf("%#lx", addr); |
| 392 | return; |
| 393 | } |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 394 | tprints("["); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 395 | tprintf(fmt, num); |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 396 | tprints("]"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 399 | void |
Dmitry V. Levin | 30145dd | 2010-09-06 22:08:24 +0000 | [diff] [blame] | 400 | printnum_int(struct tcb *tcp, long addr, const char *fmt) |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 401 | { |
| 402 | int num; |
| 403 | |
| 404 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 405 | tprints("NULL"); |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 406 | return; |
| 407 | } |
| 408 | if (umove(tcp, addr, &num) < 0) { |
| 409 | tprintf("%#lx", addr); |
| 410 | return; |
| 411 | } |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 412 | tprints("["); |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 413 | tprintf(fmt, num); |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 414 | tprints("]"); |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void |
Dmitry V. Levin | 3138213 | 2011-03-04 05:08:02 +0300 | [diff] [blame] | 418 | printfd(struct tcb *tcp, int fd) |
| 419 | { |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 420 | const char *p; |
| 421 | |
| 422 | if (show_fd_path && (p = getfdpath(tcp, fd))) |
| 423 | tprintf("%d<%s>", fd, p); |
| 424 | else |
| 425 | tprintf("%d", fd); |
Dmitry V. Levin | 3138213 | 2011-03-04 05:08:02 +0300 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 429 | printuid(const char *text, unsigned long uid) |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 430 | { |
Denys Vlasenko | 5940e65 | 2011-09-01 09:55:05 +0200 | [diff] [blame] | 431 | tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid); |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 434 | /* |
| 435 | * Quote string `instr' of length `size' |
| 436 | * Write up to (3 + `size' * 4) bytes to `outstr' buffer. |
| 437 | * If `len' < 0, treat `instr' as a NUL-terminated string |
| 438 | * and quote at most (`size' - 1) bytes. |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 439 | * |
| 440 | * Returns 0 if len < 0 and NUL was seen, 1 otherwise. |
| 441 | * Note that if len >= 0, always returns 1. |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 442 | */ |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 443 | static int |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 444 | string_quote(const char *instr, char *outstr, int len, int size) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 445 | { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 446 | const unsigned char *ustr = (const unsigned char *) instr; |
| 447 | char *s = outstr; |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 448 | int usehex, c, i, eol; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 449 | |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 450 | eol = 0x100; /* this can never match a char */ |
| 451 | if (len < 0) { |
| 452 | size--; |
| 453 | eol = '\0'; |
| 454 | } |
| 455 | |
| 456 | usehex = 0; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 457 | if (xflag > 1) |
| 458 | usehex = 1; |
| 459 | else if (xflag) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 460 | /* Check for presence of symbol which require |
| 461 | to hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 462 | for (i = 0; i < size; ++i) { |
| 463 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 464 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 465 | if (c == eol) |
| 466 | break; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 467 | if (!isprint(c) && !isspace(c)) { |
| 468 | usehex = 1; |
| 469 | break; |
| 470 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 473 | |
| 474 | *s++ = '\"'; |
| 475 | |
| 476 | if (usehex) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 477 | /* Hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 478 | for (i = 0; i < size; ++i) { |
| 479 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 480 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 481 | if (c == eol) |
| 482 | goto asciz_ended; |
| 483 | *s++ = '\\'; |
| 484 | *s++ = 'x'; |
| 485 | *s++ = "0123456789abcdef"[c >> 4]; |
| 486 | *s++ = "0123456789abcdef"[c & 0xf]; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 487 | } |
| 488 | } else { |
| 489 | for (i = 0; i < size; ++i) { |
| 490 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 491 | /* Check for NUL-terminated string. */ |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 492 | if (c == eol) |
| 493 | goto asciz_ended; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 494 | switch (c) { |
| 495 | case '\"': case '\\': |
| 496 | *s++ = '\\'; |
| 497 | *s++ = c; |
| 498 | break; |
| 499 | case '\f': |
| 500 | *s++ = '\\'; |
| 501 | *s++ = 'f'; |
| 502 | break; |
| 503 | case '\n': |
| 504 | *s++ = '\\'; |
| 505 | *s++ = 'n'; |
| 506 | break; |
| 507 | case '\r': |
| 508 | *s++ = '\\'; |
| 509 | *s++ = 'r'; |
| 510 | break; |
| 511 | case '\t': |
| 512 | *s++ = '\\'; |
| 513 | *s++ = 't'; |
| 514 | break; |
| 515 | case '\v': |
| 516 | *s++ = '\\'; |
| 517 | *s++ = 'v'; |
| 518 | break; |
| 519 | default: |
| 520 | if (isprint(c)) |
| 521 | *s++ = c; |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 522 | else { |
| 523 | /* Print \octal */ |
| 524 | *s++ = '\\'; |
| 525 | if (i + 1 < size |
| 526 | && ustr[i + 1] >= '0' |
| 527 | && ustr[i + 1] <= '9' |
| 528 | ) { |
| 529 | /* Print \ooo */ |
| 530 | *s++ = '0' + (c >> 6); |
| 531 | *s++ = '0' + ((c >> 3) & 0x7); |
| 532 | } else { |
| 533 | /* Print \[[o]o]o */ |
| 534 | if ((c >> 3) != 0) { |
| 535 | if ((c >> 6) != 0) |
| 536 | *s++ = '0' + (c >> 6); |
| 537 | *s++ = '0' + ((c >> 3) & 0x7); |
| 538 | } |
| 539 | } |
| 540 | *s++ = '0' + (c & 0x7); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 541 | } |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | *s++ = '\"'; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 548 | *s = '\0'; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 549 | |
Denys Vlasenko | 8778bff | 2011-08-31 12:22:56 +0200 | [diff] [blame] | 550 | /* Return zero if we printed entire ASCIZ string (didn't truncate it) */ |
| 551 | if (len < 0 && ustr[i] == '\0') { |
| 552 | /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended') |
| 553 | * but next char is NUL. |
| 554 | */ |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | return 1; |
| 559 | |
| 560 | asciz_ended: |
| 561 | *s++ = '\"'; |
| 562 | *s = '\0'; |
| 563 | /* Return zero: we printed entire ASCIZ string (didn't truncate it) */ |
| 564 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 567 | /* |
| 568 | * Print path string specified by address `addr' and length `n'. |
| 569 | * If path length exceeds `n', append `...' to the output. |
| 570 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 571 | void |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 572 | printpathn(struct tcb *tcp, long addr, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 573 | { |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 574 | char path[MAXPATHLEN + 1]; |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 575 | int nul_seen; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 576 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 577 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 578 | tprints("NULL"); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 579 | return; |
| 580 | } |
| 581 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 582 | /* Cap path length to the path buffer size */ |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 583 | if (n > sizeof path - 1) |
| 584 | n = sizeof path - 1; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 585 | |
| 586 | /* Fetch one byte more to find out whether path length > n. */ |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 587 | nul_seen = umovestr(tcp, addr, n + 1, path); |
| 588 | if (nul_seen < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 589 | tprintf("%#lx", addr); |
| 590 | else { |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 591 | char *outstr; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 592 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 593 | path[n] = '\0'; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 594 | n++; |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 595 | outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */ |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 596 | string_quote(path, outstr, -1, n); |
| 597 | tprints(outstr); |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 598 | if (!nul_seen) |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 599 | tprints("..."); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | void |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 604 | printpath(struct tcb *tcp, long addr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 605 | { |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 606 | /* Size must correspond to char path[] size in printpathn */ |
| 607 | printpathn(tcp, addr, MAXPATHLEN); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 610 | /* |
| 611 | * Print string specified by address `addr' and length `len'. |
| 612 | * If `len' < 0, treat the string as a NUL-terminated string. |
| 613 | * If string length exceeds `max_strlen', append `...' to the output. |
| 614 | */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 615 | void |
| 616 | printstr(struct tcb *tcp, long addr, int len) |
| 617 | { |
| 618 | static char *str = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 619 | static char *outstr; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 620 | int size; |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 621 | int ellipsis; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 622 | |
| 623 | if (!addr) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 624 | tprints("NULL"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 625 | return; |
| 626 | } |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 627 | /* Allocate static buffers if they are not allocated yet. */ |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 628 | if (!str) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 629 | str = malloc(max_strlen + 1); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 630 | if (!str) |
| 631 | die_out_of_memory(); |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 632 | outstr = malloc(4 * max_strlen + /*for quotes and NUL:*/ 3); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 633 | if (!outstr) |
| 634 | die_out_of_memory(); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 635 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 636 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 637 | if (len < 0) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 638 | /* |
| 639 | * Treat as a NUL-terminated string: fetch one byte more |
| 640 | * because string_quote() quotes one byte less. |
| 641 | */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 642 | size = max_strlen + 1; |
| 643 | if (umovestr(tcp, addr, size, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 644 | tprintf("%#lx", addr); |
| 645 | return; |
| 646 | } |
| 647 | } |
| 648 | else { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 649 | size = MIN(len, max_strlen); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 650 | if (umoven(tcp, addr, size, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 651 | tprintf("%#lx", addr); |
| 652 | return; |
| 653 | } |
| 654 | } |
| 655 | |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 656 | /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str |
| 657 | * or we were requested to print more than -s NUM chars)... |
| 658 | */ |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 659 | ellipsis = (string_quote(str, outstr, len, size) && |
| 660 | (len < 0 || len > max_strlen)); |
Roland McGrath | a503dcf | 2007-08-02 02:06:26 +0000 | [diff] [blame] | 661 | |
Denys Vlasenko | b3c52cf | 2012-01-19 17:20:23 +0100 | [diff] [blame] | 662 | tprints(outstr); |
| 663 | if (ellipsis) |
| 664 | tprints("..."); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 665 | } |
| 666 | |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 667 | #if HAVE_SYS_UIO_H |
| 668 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 669 | dumpiov(struct tcb *tcp, int len, long addr) |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 670 | { |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 671 | #if defined(LINUX) && SUPPORTED_PERSONALITIES > 1 |
| 672 | union { |
| 673 | struct { u_int32_t base; u_int32_t len; } *iov32; |
| 674 | struct { u_int64_t base; u_int64_t len; } *iov64; |
| 675 | } iovu; |
| 676 | #define iov iovu.iov64 |
| 677 | #define sizeof_iov \ |
| 678 | (personality_wordsize[current_personality] == 4 \ |
| 679 | ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64)) |
| 680 | #define iov_iov_base(i) \ |
| 681 | (personality_wordsize[current_personality] == 4 \ |
| 682 | ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base) |
| 683 | #define iov_iov_len(i) \ |
| 684 | (personality_wordsize[current_personality] == 4 \ |
| 685 | ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len) |
| 686 | #else |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 687 | struct iovec *iov; |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 688 | #define sizeof_iov sizeof(*iov) |
| 689 | #define iov_iov_base(i) iov[i].iov_base |
| 690 | #define iov_iov_len(i) iov[i].iov_len |
| 691 | #endif |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 692 | int i; |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 693 | unsigned size; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 694 | |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 695 | size = sizeof_iov * len; |
| 696 | /* Assuming no sane program has millions of iovs */ |
| 697 | if ((unsigned)len > 1024*1024 /* insane or negative size? */ |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 698 | || (iov = malloc(size)) == NULL) { |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 699 | fprintf(stderr, "Out of memory\n"); |
| 700 | return; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 701 | } |
Roland McGrath | aa524c8 | 2005-06-01 19:22:06 +0000 | [diff] [blame] | 702 | if (umoven(tcp, addr, size, (char *) iov) >= 0) { |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 703 | for (i = 0; i < len; i++) { |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 704 | /* include the buffer number to make it easy to |
| 705 | * match up the trace with the source */ |
| 706 | tprintf(" * %lu bytes in buffer %d\n", |
| 707 | (unsigned long)iov_iov_len(i), i); |
| 708 | dumpstr(tcp, (long) iov_iov_base(i), |
| 709 | iov_iov_len(i)); |
| 710 | } |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 711 | } |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 712 | free(iov); |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 713 | #undef sizeof_iov |
| 714 | #undef iov_iov_base |
| 715 | #undef iov_iov_len |
| 716 | #undef iov |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 717 | } |
| 718 | #endif |
| 719 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 720 | void |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 721 | dumpstr(struct tcb *tcp, long addr, int len) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 722 | { |
| 723 | static int strsize = -1; |
| 724 | static unsigned char *str; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 725 | char *s; |
| 726 | int i, j; |
| 727 | |
| 728 | if (strsize < len) { |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 729 | free(str); |
| 730 | str = malloc(len); |
Denys Vlasenko | 79a79ea | 2011-09-01 16:35:44 +0200 | [diff] [blame] | 731 | if (!str) { |
| 732 | strsize = -1; |
| 733 | fprintf(stderr, "Out of memory\n"); |
| 734 | return; |
| 735 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 736 | strsize = len; |
| 737 | } |
| 738 | |
| 739 | if (umoven(tcp, addr, len, (char *) str) < 0) |
| 740 | return; |
| 741 | |
| 742 | for (i = 0; i < len; i += 16) { |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 743 | char outstr[80]; |
| 744 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 745 | s = outstr; |
| 746 | sprintf(s, " | %05x ", i); |
| 747 | s += 9; |
| 748 | for (j = 0; j < 16; j++) { |
| 749 | if (j == 8) |
| 750 | *s++ = ' '; |
| 751 | if (i + j < len) { |
| 752 | sprintf(s, " %02x", str[i + j]); |
| 753 | s += 3; |
| 754 | } |
| 755 | else { |
| 756 | *s++ = ' '; *s++ = ' '; *s++ = ' '; |
| 757 | } |
| 758 | } |
| 759 | *s++ = ' '; *s++ = ' '; |
| 760 | for (j = 0; j < 16; j++) { |
| 761 | if (j == 8) |
| 762 | *s++ = ' '; |
| 763 | if (i + j < len) { |
| 764 | if (isprint(str[i + j])) |
| 765 | *s++ = str[i + j]; |
| 766 | else |
| 767 | *s++ = '.'; |
| 768 | } |
| 769 | else |
| 770 | *s++ = ' '; |
| 771 | } |
| 772 | tprintf("%s |\n", outstr); |
| 773 | } |
| 774 | } |
| 775 | |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 776 | |
| 777 | /* Need to do this since process_vm_readv() is not yet available in libc. |
| 778 | * When libc is be updated, only "static bool process_vm_readv_not_supported" |
| 779 | * line should remain. |
| 780 | */ |
| 781 | #if !defined(__NR_process_vm_readv) |
| 782 | # if defined(I386) |
| 783 | # define __NR_process_vm_readv 347 |
| 784 | # elif defined(X86_64) |
| 785 | # define __NR_process_vm_readv 310 |
| 786 | # elif defined(POWERPC) |
| 787 | # define __NR_process_vm_readv 351 |
| 788 | # endif |
| 789 | #endif |
| 790 | |
| 791 | #if defined(__NR_process_vm_readv) |
| 792 | static bool process_vm_readv_not_supported = 0; |
| 793 | static ssize_t process_vm_readv(pid_t pid, |
| 794 | const struct iovec *lvec, |
| 795 | unsigned long liovcnt, |
| 796 | const struct iovec *rvec, |
| 797 | unsigned long riovcnt, |
| 798 | unsigned long flags) |
| 799 | { |
| 800 | return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags); |
| 801 | } |
| 802 | #else |
| 803 | static bool process_vm_readv_not_supported = 1; |
| 804 | # define process_vm_readv(...) (errno = ENOSYS, -1) |
| 805 | #endif |
| 806 | /* end of hack */ |
| 807 | |
| 808 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 809 | #define PAGMASK (~(PAGSIZ - 1)) |
| 810 | /* |
| 811 | * move `len' bytes of data from process `pid' |
| 812 | * at address `addr' to our space at `laddr' |
| 813 | */ |
| 814 | int |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 815 | umoven(struct tcb *tcp, long addr, int len, char *laddr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 816 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 817 | #ifdef LINUX |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 818 | int pid = tcp->pid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 819 | int n, m; |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 820 | int started; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 821 | union { |
| 822 | long val; |
| 823 | char x[sizeof(long)]; |
| 824 | } u; |
| 825 | |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 826 | if (!process_vm_readv_not_supported) { |
| 827 | struct iovec local[1], remote[1]; |
| 828 | int r; |
| 829 | |
| 830 | local[0].iov_base = laddr; |
| 831 | remote[0].iov_base = (void*)addr; |
| 832 | local[0].iov_len = remote[0].iov_len = len; |
| 833 | r = process_vm_readv(pid, |
| 834 | local, 1, |
| 835 | remote, 1, |
| 836 | /*flags:*/ 0 |
| 837 | ); |
| 838 | if (r < 0) { |
| 839 | if (errno == ENOSYS) |
| 840 | process_vm_readv_not_supported = 1; |
Denys Vlasenko | 2945639 | 2012-01-28 02:49:48 +0100 | [diff] [blame] | 841 | else if (errno != EINVAL) /* EINVAL is seen if process is gone */ |
| 842 | /* strange... */ |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 843 | perror("process_vm_readv"); |
| 844 | goto vm_readv_didnt_work; |
| 845 | } |
| 846 | return r; |
| 847 | } |
| 848 | vm_readv_didnt_work: |
| 849 | |
Dmitry V. Levin | 856c7ed | 2011-12-26 20:12:02 +0000 | [diff] [blame] | 850 | #if SUPPORTED_PERSONALITIES > 1 |
| 851 | if (personality_wordsize[current_personality] < sizeof(addr)) |
| 852 | addr &= (1ul << 8 * personality_wordsize[current_personality]) - 1; |
| 853 | #endif |
| 854 | |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 855 | started = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 856 | if (addr & (sizeof(long) - 1)) { |
| 857 | /* addr not a multiple of sizeof(long) */ |
| 858 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 859 | addr &= -sizeof(long); /* residue */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 860 | errno = 0; |
| 861 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 862 | if (errno) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 863 | /* But if not started, we had a bogus address. */ |
| 864 | if (addr != 0 && errno != EIO && errno != ESRCH) |
| 865 | perror("ptrace: umoven"); |
| 866 | return -1; |
| 867 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 868 | started = 1; |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 869 | m = MIN(sizeof(long) - n, len); |
| 870 | memcpy(laddr, &u.x[n], m); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 871 | addr += sizeof(long), laddr += m, len -= m; |
| 872 | } |
| 873 | while (len) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 874 | errno = 0; |
| 875 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 876 | if (errno) { |
| 877 | if (started && (errno==EPERM || errno==EIO)) { |
| 878 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 879 | return 0; |
| 880 | } |
| 881 | if (addr != 0 && errno != EIO && errno != ESRCH) |
| 882 | perror("ptrace: umoven"); |
| 883 | return -1; |
| 884 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 885 | started = 1; |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 886 | m = MIN(sizeof(long), len); |
| 887 | memcpy(laddr, u.x, m); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 888 | addr += sizeof(long), laddr += m, len -= m; |
| 889 | } |
| 890 | #endif /* LINUX */ |
| 891 | |
| 892 | #ifdef SUNOS4 |
| 893 | int pid = tcp->pid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 894 | int n; |
| 895 | |
| 896 | while (len) { |
| 897 | n = MIN(len, PAGSIZ); |
| 898 | n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 899 | if (ptrace(PTRACE_READDATA, pid, |
| 900 | (char *) addr, len, laddr) < 0) { |
| 901 | if (errno != ESRCH) { |
| 902 | perror("umoven: ptrace(PTRACE_READDATA, ...)"); |
| 903 | abort(); |
| 904 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 905 | return -1; |
| 906 | } |
| 907 | len -= n; |
| 908 | addr += n; |
| 909 | laddr += n; |
| 910 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 911 | #endif /* SUNOS4 */ |
| 912 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 913 | #ifdef USE_PROCFS |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 914 | #ifdef HAVE_MP_PROCFS |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 915 | int fd = tcp->pfd_as; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 916 | #else |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 917 | int fd = tcp->pfd; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 918 | #endif |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 919 | lseek(fd, addr, SEEK_SET); |
| 920 | if (read(fd, laddr, len) == -1) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 921 | return -1; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 922 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | /* |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 928 | * Like `umove' but make the additional effort of looking |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 929 | * for a terminating zero byte. |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 930 | * |
| 931 | * Returns < 0 on error, > 0 if NUL was seen, |
| 932 | * (TODO if useful: return count of bytes including NUL), |
| 933 | * else 0 if len bytes were read but no NUL byte seen. |
| 934 | * |
| 935 | * Note: there is no guarantee we won't overwrite some bytes |
| 936 | * in laddr[] _after_ terminating NUL (but, of course, |
| 937 | * we never write past laddr[len-1]). |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 938 | */ |
| 939 | int |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 940 | umovestr(struct tcb *tcp, long addr, int len, char *laddr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 941 | { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 942 | #ifdef USE_PROCFS |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 943 | # ifdef HAVE_MP_PROCFS |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 944 | int fd = tcp->pfd_as; |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 945 | # else |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 946 | int fd = tcp->pfd; |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 947 | # endif |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 948 | /* Some systems (e.g. FreeBSD) can be upset if we read off the |
| 949 | end of valid memory, avoid this by trying to read up |
| 950 | to page boundaries. But we don't know what a page is (and |
| 951 | getpagesize(2) (if it exists) doesn't necessarily return |
| 952 | hardware page size). Assume all pages >= 1024 (a-historical |
| 953 | I know) */ |
| 954 | |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 955 | int page = 1024; /* How to find this? */ |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 956 | int move = page - (addr & (page - 1)); |
| 957 | int left = len; |
| 958 | |
| 959 | lseek(fd, addr, SEEK_SET); |
| 960 | |
| 961 | while (left) { |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 962 | if (move > left) |
| 963 | move = left; |
| 964 | move = read(fd, laddr, move); |
| 965 | if (move <= 0) |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 966 | return left != len ? 0 : -1; |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 967 | if (memchr(laddr, 0, move)) |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 968 | return 1; |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 969 | left -= move; |
| 970 | laddr += move; |
| 971 | addr += move; |
| 972 | move = page; |
| 973 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 974 | return 0; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 975 | #else /* !USE_PROCFS */ |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 976 | int started; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 977 | int pid = tcp->pid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 978 | int i, n, m; |
| 979 | union { |
| 980 | long val; |
| 981 | char x[sizeof(long)]; |
| 982 | } u; |
| 983 | |
Dmitry V. Levin | 856c7ed | 2011-12-26 20:12:02 +0000 | [diff] [blame] | 984 | #if SUPPORTED_PERSONALITIES > 1 |
| 985 | if (personality_wordsize[current_personality] < sizeof(addr)) |
| 986 | addr &= (1ul << 8 * personality_wordsize[current_personality]) - 1; |
| 987 | #endif |
| 988 | |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 989 | if (!process_vm_readv_not_supported) { |
| 990 | struct iovec local[1], remote[1]; |
| 991 | |
| 992 | local[0].iov_base = laddr; |
| 993 | remote[0].iov_base = (void*)addr; |
| 994 | |
| 995 | while (len > 0) { |
| 996 | int end_in_page; |
| 997 | int r; |
| 998 | int chunk_len; |
| 999 | |
| 1000 | /* Don't read kilobytes: most strings are short */ |
| 1001 | chunk_len = len; |
| 1002 | if (chunk_len > 256) |
| 1003 | chunk_len = 256; |
| 1004 | /* Don't cross pages. I guess otherwise we can get EFAULT |
| 1005 | * and fail to notice that terminating NUL lies |
| 1006 | * in the existing (first) page. |
| 1007 | * (I hope there aren't arches with pages < 4K) |
| 1008 | */ |
| 1009 | end_in_page = ((addr + chunk_len) & 4095); |
| 1010 | r = chunk_len - end_in_page; |
| 1011 | if (r > 0) /* if chunk_len > end_in_page */ |
| 1012 | chunk_len = r; /* chunk_len -= end_in_page */ |
| 1013 | |
| 1014 | local[0].iov_len = remote[0].iov_len = chunk_len; |
| 1015 | r = process_vm_readv(pid, |
| 1016 | local, 1, |
| 1017 | remote, 1, |
| 1018 | /*flags:*/ 0 |
| 1019 | ); |
| 1020 | if (r < 0) { |
| 1021 | if (errno == ENOSYS) |
| 1022 | process_vm_readv_not_supported = 1; |
Denys Vlasenko | 2945639 | 2012-01-28 02:49:48 +0100 | [diff] [blame] | 1023 | else if (errno != EINVAL) /* EINVAL is seen if process is gone */ |
| 1024 | /* strange... */ |
Denys Vlasenko | 3af224c | 2012-01-28 01:46:33 +0100 | [diff] [blame] | 1025 | perror("process_vm_readv"); |
| 1026 | goto vm_readv_didnt_work; |
| 1027 | } |
| 1028 | if (memchr(local[0].iov_base, '\0', r)) |
| 1029 | return 1; |
| 1030 | local[0].iov_base += r; |
| 1031 | remote[0].iov_base += r; |
| 1032 | len -= r; |
| 1033 | } |
| 1034 | return 0; |
| 1035 | } |
| 1036 | vm_readv_didnt_work: |
| 1037 | |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1038 | started = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1039 | if (addr & (sizeof(long) - 1)) { |
| 1040 | /* addr not a multiple of sizeof(long) */ |
| 1041 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 1042 | addr &= -sizeof(long); /* residue */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1043 | errno = 0; |
| 1044 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
| 1045 | if (errno) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1046 | if (addr != 0 && errno != EIO && errno != ESRCH) |
| 1047 | perror("umovestr"); |
| 1048 | return -1; |
| 1049 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 1050 | started = 1; |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1051 | m = MIN(sizeof(long) - n, len); |
| 1052 | memcpy(laddr, &u.x[n], m); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1053 | while (n & (sizeof(long) - 1)) |
| 1054 | if (u.x[n++] == '\0') |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 1055 | return 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1056 | addr += sizeof(long), laddr += m, len -= m; |
| 1057 | } |
| 1058 | while (len) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1059 | errno = 0; |
| 1060 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
| 1061 | if (errno) { |
| 1062 | if (started && (errno==EPERM || errno==EIO)) { |
| 1063 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 1064 | return 0; |
| 1065 | } |
| 1066 | if (addr != 0 && errno != EIO && errno != ESRCH) |
| 1067 | perror("umovestr"); |
| 1068 | return -1; |
| 1069 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 1070 | started = 1; |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1071 | m = MIN(sizeof(long), len); |
| 1072 | memcpy(laddr, u.x, m); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1073 | for (i = 0; i < sizeof(long); i++) |
| 1074 | if (u.x[i] == '\0') |
Denys Vlasenko | 6cecba5 | 2012-01-20 11:56:00 +0100 | [diff] [blame] | 1075 | return 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1076 | addr += sizeof(long), laddr += m, len -= m; |
| 1077 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1078 | #endif /* !USE_PROCFS */ |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 1079 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | #ifdef LINUX |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1083 | # if !defined (SPARC) && !defined(SPARC64) |
| 1084 | # define PTRACE_WRITETEXT 101 |
| 1085 | # define PTRACE_WRITEDATA 102 |
| 1086 | # endif /* !SPARC && !SPARC64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1087 | #endif /* LINUX */ |
| 1088 | |
| 1089 | #ifdef SUNOS4 |
| 1090 | |
| 1091 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1092 | uload(int cmd, int pid, long addr, int len, char *laddr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1093 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1094 | int peek, poke; |
| 1095 | int n, m; |
| 1096 | union { |
| 1097 | long val; |
| 1098 | char x[sizeof(long)]; |
| 1099 | } u; |
| 1100 | |
| 1101 | if (cmd == PTRACE_WRITETEXT) { |
| 1102 | peek = PTRACE_PEEKTEXT; |
| 1103 | poke = PTRACE_POKETEXT; |
| 1104 | } |
| 1105 | else { |
| 1106 | peek = PTRACE_PEEKDATA; |
| 1107 | poke = PTRACE_POKEDATA; |
| 1108 | } |
| 1109 | if (addr & (sizeof(long) - 1)) { |
| 1110 | /* addr not a multiple of sizeof(long) */ |
| 1111 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 1112 | addr &= -sizeof(long); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1113 | errno = 0; |
| 1114 | u.val = ptrace(peek, pid, (char *) addr, 0); |
| 1115 | if (errno) { |
| 1116 | perror("uload: POKE"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1117 | return -1; |
| 1118 | } |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1119 | m = MIN(sizeof(long) - n, len); |
| 1120 | memcpy(&u.x[n], laddr, m); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1121 | if (ptrace(poke, pid, (char *)addr, u.val) < 0) { |
| 1122 | perror("uload: POKE"); |
| 1123 | return -1; |
| 1124 | } |
| 1125 | addr += sizeof(long), laddr += m, len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1126 | } |
| 1127 | while (len) { |
| 1128 | if (len < sizeof(long)) |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1129 | u.val = ptrace(peek, pid, (char *) addr, 0); |
Denys Vlasenko | a47e6b9 | 2012-01-21 04:01:56 +0100 | [diff] [blame] | 1130 | m = MIN(sizeof(long), len); |
| 1131 | memcpy(u.x, laddr, m); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1132 | if (ptrace(poke, pid, (char *) addr, u.val) < 0) { |
| 1133 | perror("uload: POKE"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1134 | return -1; |
| 1135 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1136 | addr += sizeof(long), laddr += m, len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1137 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1138 | return 0; |
| 1139 | } |
| 1140 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1141 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1142 | tload(int pid, int addr, int len, char *laddr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1143 | { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1144 | return uload(PTRACE_WRITETEXT, pid, addr, len, laddr); |
| 1145 | } |
| 1146 | |
| 1147 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1148 | dload(int pid, int addr, int len, char *laddr) |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1149 | { |
| 1150 | return uload(PTRACE_WRITEDATA, pid, addr, len, laddr); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | #endif /* SUNOS4 */ |
| 1154 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1155 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1156 | |
| 1157 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1158 | upeek(struct tcb *tcp, long off, long *res) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1159 | { |
| 1160 | long val; |
| 1161 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1162 | # ifdef SUNOS4_KERNEL_ARCH_KLUDGE |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1163 | { |
| 1164 | static int is_sun4m = -1; |
| 1165 | struct utsname name; |
| 1166 | |
| 1167 | /* Round up the usual suspects. */ |
| 1168 | if (is_sun4m == -1) { |
| 1169 | if (uname(&name) < 0) { |
| 1170 | perror("upeek: uname?"); |
| 1171 | exit(1); |
| 1172 | } |
| 1173 | is_sun4m = strcmp(name.machine, "sun4m") == 0; |
| 1174 | if (is_sun4m) { |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 1175 | const struct xlat *x; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1176 | |
| 1177 | for (x = struct_user_offsets; x->str; x++) |
| 1178 | x->val += 1024; |
| 1179 | } |
| 1180 | } |
| 1181 | if (is_sun4m) |
| 1182 | off += 1024; |
| 1183 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1184 | # endif /* SUNOS4_KERNEL_ARCH_KLUDGE */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1185 | errno = 0; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1186 | val = do_ptrace(PTRACE_PEEKUSER, tcp, (char *) off, 0); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1187 | if (val == -1 && errno) { |
| 1188 | if (errno != ESRCH) { |
| 1189 | char buf[60]; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1190 | sprintf(buf, "upeek: ptrace(PTRACE_PEEKUSER,%d,%lu,0)", tcp->pid, off); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1191 | perror(buf); |
| 1192 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1193 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1194 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1195 | *res = val; |
| 1196 | return 0; |
| 1197 | } |
| 1198 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1199 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1200 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1201 | void |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1202 | printcall(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1203 | { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1204 | #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \ |
| 1205 | sizeof(long) == 8 ? "[????????????????] " : \ |
| 1206 | NULL /* crash */) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1207 | |
| 1208 | #ifdef LINUX |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1209 | # ifdef I386 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1210 | long eip; |
| 1211 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1212 | if (upeek(tcp, 4*EIP, &eip) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1213 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1214 | return; |
| 1215 | } |
| 1216 | tprintf("[%08lx] ", eip); |
Roland McGrath | eac26fc | 2005-02-02 02:48:53 +0000 | [diff] [blame] | 1217 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1218 | # elif defined(S390) || defined(S390X) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1219 | long psw; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1220 | if (upeek(tcp, PT_PSWADDR, &psw) < 0) { |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1221 | PRINTBADPC; |
| 1222 | return; |
| 1223 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1224 | # ifdef S390 |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1225 | tprintf("[%08lx] ", psw); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1226 | # elif S390X |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1227 | tprintf("[%16lx] ", psw); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1228 | # endif |
Roland McGrath | eac26fc | 2005-02-02 02:48:53 +0000 | [diff] [blame] | 1229 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1230 | # elif defined(X86_64) |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1231 | long rip; |
| 1232 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1233 | if (upeek(tcp, 8*RIP, &rip) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1234 | PRINTBADPC; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1235 | return; |
| 1236 | } |
| 1237 | tprintf("[%16lx] ", rip); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1238 | # elif defined(IA64) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1239 | long ip; |
| 1240 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1241 | if (upeek(tcp, PT_B0, &ip) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1242 | PRINTBADPC; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1243 | return; |
| 1244 | } |
| 1245 | tprintf("[%08lx] ", ip); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1246 | # elif defined(POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1247 | long pc; |
| 1248 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1249 | if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) { |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 1250 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1251 | return; |
| 1252 | } |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 1253 | # ifdef POWERPC64 |
| 1254 | tprintf("[%016lx] ", pc); |
| 1255 | # else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1256 | tprintf("[%08lx] ", pc); |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 1257 | # endif |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1258 | # elif defined(M68K) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1259 | long pc; |
| 1260 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1261 | if (upeek(tcp, 4*PT_PC, &pc) < 0) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 1262 | tprints("[????????] "); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1263 | return; |
| 1264 | } |
| 1265 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1266 | # elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1267 | long pc; |
| 1268 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1269 | if (upeek(tcp, REG_PC, &pc) < 0) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 1270 | tprints("[????????????????] "); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1271 | return; |
| 1272 | } |
| 1273 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1274 | # elif defined(SPARC) || defined(SPARC64) |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1275 | struct pt_regs regs; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1276 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1277 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1278 | return; |
| 1279 | } |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1280 | # if defined(SPARC64) |
| 1281 | tprintf("[%08lx] ", regs.tpc); |
| 1282 | # else |
| 1283 | tprintf("[%08lx] ", regs.pc); |
| 1284 | # endif |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1285 | # elif defined(HPPA) |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1286 | long pc; |
| 1287 | |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1288 | if (upeek(tcp, PT_IAOQ0, &pc) < 0) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 1289 | tprints("[????????] "); |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1290 | return; |
| 1291 | } |
| 1292 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1293 | # elif defined(MIPS) |
Wichert Akkerman | 75c422b | 2001-04-10 10:22:50 +0000 | [diff] [blame] | 1294 | long pc; |
| 1295 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1296 | if (upeek(tcp, REG_EPC, &pc) < 0) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 1297 | tprints("[????????] "); |
Wichert Akkerman | 75c422b | 2001-04-10 10:22:50 +0000 | [diff] [blame] | 1298 | return; |
| 1299 | } |
| 1300 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1301 | # elif defined(SH) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1302 | long pc; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1303 | |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1304 | if (upeek(tcp, 4*REG_PC, &pc) < 0) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 1305 | tprints("[????????] "); |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1306 | return; |
| 1307 | } |
| 1308 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1309 | # elif defined(SH64) |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1310 | long pc; |
| 1311 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1312 | if (upeek(tcp, REG_PC, &pc) < 0) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 1313 | tprints("[????????????????] "); |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1317 | # elif defined(ARM) |
Roland McGrath | ef38868 | 2003-06-03 23:28:59 +0000 | [diff] [blame] | 1318 | long pc; |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1319 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1320 | if (upeek(tcp, 4*15, &pc) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1321 | PRINTBADPC; |
Roland McGrath | ef38868 | 2003-06-03 23:28:59 +0000 | [diff] [blame] | 1322 | return; |
| 1323 | } |
| 1324 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1325 | # elif defined(AVR32) |
| 1326 | long pc; |
| 1327 | |
| 1328 | if (upeek(tcp, REG_PC, &pc) < 0) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 1329 | tprints("[????????] "); |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1330 | return; |
| 1331 | } |
| 1332 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1333 | # elif defined(BFIN) |
Dmitry V. Levin | 87ea1f4 | 2008-11-10 22:21:41 +0000 | [diff] [blame] | 1334 | long pc; |
| 1335 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1336 | if (upeek(tcp, PT_PC, &pc) < 0) { |
Dmitry V. Levin | 87ea1f4 | 2008-11-10 22:21:41 +0000 | [diff] [blame] | 1337 | PRINTBADPC; |
| 1338 | return; |
| 1339 | } |
| 1340 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1341 | #elif defined(CRISV10) |
| 1342 | long pc; |
| 1343 | |
Edgar E. Iglesias | eeb9ce3 | 2009-10-05 14:41:02 +0000 | [diff] [blame] | 1344 | if (upeek(tcp, 4*PT_IRP, &pc) < 0) { |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1345 | PRINTBADPC; |
| 1346 | return; |
| 1347 | } |
| 1348 | tprintf("[%08lx] ", pc); |
| 1349 | #elif defined(CRISV32) |
| 1350 | long pc; |
| 1351 | |
Edgar E. Iglesias | eeb9ce3 | 2009-10-05 14:41:02 +0000 | [diff] [blame] | 1352 | if (upeek(tcp, 4*PT_ERP, &pc) < 0) { |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1353 | PRINTBADPC; |
| 1354 | return; |
| 1355 | } |
| 1356 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1357 | # endif /* architecture */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1358 | #endif /* LINUX */ |
| 1359 | |
| 1360 | #ifdef SUNOS4 |
| 1361 | struct regs regs; |
| 1362 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1363 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) { |
| 1364 | perror("printcall: ptrace(PTRACE_GETREGS, ...)"); |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1365 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1366 | return; |
| 1367 | } |
| 1368 | tprintf("[%08x] ", regs.r_o7); |
| 1369 | #endif /* SUNOS4 */ |
| 1370 | |
| 1371 | #ifdef SVR4 |
| 1372 | /* XXX */ |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1373 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1374 | #endif |
| 1375 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1376 | #ifdef FREEBSD |
| 1377 | struct reg regs; |
| 1378 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 1379 | tprintf("[%08x] ", regs.r_eip); |
| 1380 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1383 | |
| 1384 | /* |
| 1385 | * These #if's are huge, please indent them correctly. |
| 1386 | * It's easy to get confused otherwise. |
| 1387 | */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1388 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1389 | |
Denys Vlasenko | e7c9024 | 2011-06-22 00:09:25 +0200 | [diff] [blame] | 1390 | # ifdef LINUX |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1391 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1392 | # include "syscall.h" |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1393 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1394 | # include <sys/syscall.h> |
| 1395 | # ifndef CLONE_PTRACE |
| 1396 | # define CLONE_PTRACE 0x00002000 |
| 1397 | # endif |
| 1398 | # ifndef CLONE_VFORK |
| 1399 | # define CLONE_VFORK 0x00004000 |
| 1400 | # endif |
| 1401 | # ifndef CLONE_VM |
| 1402 | # define CLONE_VM 0x00000100 |
| 1403 | # endif |
| 1404 | # ifndef CLONE_STOPPED |
| 1405 | # define CLONE_STOPPED 0x02000000 |
| 1406 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1407 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1408 | # ifdef IA64 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1409 | |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1410 | /* We don't have fork()/vfork() syscalls on ia64 itself, but the ia32 |
| 1411 | subsystem has them for x86... */ |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1412 | # define SYS_fork 2 |
| 1413 | # define SYS_vfork 190 |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1414 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1415 | typedef unsigned long *arg_setup_state; |
| 1416 | |
| 1417 | static int |
| 1418 | arg_setup(struct tcb *tcp, arg_setup_state *state) |
| 1419 | { |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1420 | unsigned long cfm, sof, sol; |
| 1421 | long bsp; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1422 | |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1423 | if (ia32) { |
| 1424 | /* Satisfy a false GCC warning. */ |
| 1425 | *state = NULL; |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1426 | return 0; |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1427 | } |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1428 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1429 | if (upeek(tcp, PT_AR_BSP, &bsp) < 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1430 | return -1; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1431 | if (upeek(tcp, PT_CFM, (long *) &cfm) < 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1432 | return -1; |
| 1433 | |
| 1434 | sof = (cfm >> 0) & 0x7f; |
| 1435 | sol = (cfm >> 7) & 0x7f; |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1436 | bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1437 | |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1438 | *state = (unsigned long *) bsp; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1439 | return 0; |
| 1440 | } |
| 1441 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1442 | # define arg_finish_change(tcp, state) 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1443 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1444 | # ifdef SYS_fork |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1445 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1446 | get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1447 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1448 | int ret; |
| 1449 | |
| 1450 | if (ia32) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1451 | ret = upeek(tcp, PT_R11, valp); |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1452 | else |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1453 | ret = umoven(tcp, |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1454 | (unsigned long) ia64_rse_skip_regs(*state, 0), |
| 1455 | sizeof(long), (void *) valp); |
| 1456 | return ret; |
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 | get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1461 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1462 | int ret; |
| 1463 | |
| 1464 | if (ia32) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1465 | ret = upeek(tcp, PT_R9, valp); |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1466 | else |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1467 | ret = umoven(tcp, |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1468 | (unsigned long) ia64_rse_skip_regs(*state, 1), |
| 1469 | sizeof(long), (void *) valp); |
| 1470 | return ret; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1471 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1472 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1473 | |
| 1474 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1475 | set_arg0(struct tcb *tcp, arg_setup_state *state, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1476 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1477 | int req = PTRACE_POKEDATA; |
| 1478 | void *ap; |
| 1479 | |
| 1480 | if (ia32) { |
| 1481 | ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */ |
| 1482 | req = PTRACE_POKEUSER; |
| 1483 | } else |
| 1484 | ap = ia64_rse_skip_regs(*state, 0); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1485 | errno = 0; |
| 1486 | ptrace(req, tcp->pid, ap, val); |
| 1487 | return errno ? -1 : 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1491 | set_arg1(struct tcb *tcp, arg_setup_state *state, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1492 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1493 | int req = PTRACE_POKEDATA; |
| 1494 | void *ap; |
| 1495 | |
| 1496 | if (ia32) { |
| 1497 | ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */ |
| 1498 | req = PTRACE_POKEUSER; |
| 1499 | } else |
| 1500 | ap = ia64_rse_skip_regs(*state, 1); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1501 | errno = 0; |
| 1502 | ptrace(req, tcp->pid, ap, val); |
| 1503 | return errno ? -1 : 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Roland McGrath | b659f87 | 2008-07-18 01:19:36 +0000 | [diff] [blame] | 1506 | /* ia64 does not return the input arguments from functions (and syscalls) |
| 1507 | according to ia64 RSE (Register Stack Engine) behavior. */ |
| 1508 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1509 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1510 | # define restore_arg1(tcp, state, val) ((void) (state), 0) |
Roland McGrath | b659f87 | 2008-07-18 01:19:36 +0000 | [diff] [blame] | 1511 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1512 | # elif defined (SPARC) || defined (SPARC64) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1513 | |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1514 | typedef struct pt_regs arg_setup_state; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1515 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1516 | # define arg_setup(tcp, state) \ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1517 | (ptrace(PTRACE_GETREGS, tcp->pid, (char *) (state), 0)) |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1518 | # define arg_finish_change(tcp, state) \ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1519 | (ptrace(PTRACE_SETREGS, tcp->pid, (char *) (state), 0)) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1520 | |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1521 | # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0) |
| 1522 | # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0) |
| 1523 | # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0) |
| 1524 | # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0) |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1525 | # define restore_arg0(tcp, state, val) 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1526 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1527 | # else /* other architectures */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1528 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1529 | # if defined S390 || defined S390X |
Roland McGrath | 7b30822 | 2003-01-20 09:04:36 +0000 | [diff] [blame] | 1530 | /* Note: this is only true for the `clone' system call, which handles |
| 1531 | arguments specially. We could as well say that its first two arguments |
| 1532 | are swapped relative to other architectures, but that would just be |
| 1533 | another #ifdef in the calls. */ |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1534 | # define arg0_offset PT_GPR3 |
| 1535 | # define arg1_offset PT_ORIGGPR2 |
| 1536 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1537 | # define restore_arg1(tcp, state, val) ((void) (state), 0) |
| 1538 | # define arg0_index 1 |
| 1539 | # define arg1_index 0 |
| 1540 | # elif defined (ALPHA) || defined (MIPS) |
| 1541 | # define arg0_offset REG_A0 |
| 1542 | # define arg1_offset (REG_A0+1) |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1543 | # elif defined (AVR32) |
| 1544 | # define arg0_offset (REG_R12) |
| 1545 | # define arg1_offset (REG_R11) |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1546 | # elif defined (POWERPC) |
| 1547 | # define arg0_offset (sizeof(unsigned long)*PT_R3) |
| 1548 | # define arg1_offset (sizeof(unsigned long)*PT_R4) |
| 1549 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1550 | # elif defined (HPPA) |
| 1551 | # define arg0_offset PT_GR26 |
| 1552 | # define arg1_offset (PT_GR26-4) |
| 1553 | # elif defined (X86_64) |
| 1554 | # define arg0_offset ((long)(8*(current_personality ? RBX : RDI))) |
| 1555 | # define arg1_offset ((long)(8*(current_personality ? RCX : RSI))) |
| 1556 | # elif defined (SH) |
| 1557 | # define arg0_offset (4*(REG_REG0+4)) |
| 1558 | # define arg1_offset (4*(REG_REG0+5)) |
| 1559 | # elif defined (SH64) |
| 1560 | /* ABI defines arg0 & 1 in r2 & r3 */ |
| 1561 | # define arg0_offset (REG_OFFSET+16) |
| 1562 | # define arg1_offset (REG_OFFSET+24) |
| 1563 | # define restore_arg0(tcp, state, val) 0 |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1564 | # elif defined CRISV10 || defined CRISV32 |
| 1565 | # define arg0_offset (4*PT_R11) |
| 1566 | # define arg1_offset (4*PT_ORIG_R10) |
| 1567 | # define restore_arg0(tcp, state, val) 0 |
| 1568 | # define restore_arg1(tcp, state, val) 0 |
| 1569 | # define arg0_index 1 |
| 1570 | # define arg1_index 0 |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1571 | # else |
| 1572 | # define arg0_offset 0 |
| 1573 | # define arg1_offset 4 |
| 1574 | # if defined ARM |
| 1575 | # define restore_arg0(tcp, state, val) 0 |
| 1576 | # endif |
| 1577 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1578 | |
| 1579 | typedef int arg_setup_state; |
| 1580 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1581 | # define arg_setup(tcp, state) (0) |
| 1582 | # define arg_finish_change(tcp, state) 0 |
| 1583 | # define get_arg0(tcp, cookie, valp) \ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1584 | (upeek((tcp), arg0_offset, (valp))) |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1585 | # define get_arg1(tcp, cookie, valp) \ |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1586 | (upeek((tcp), arg1_offset, (valp))) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1587 | |
| 1588 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1589 | set_arg0(struct tcb *tcp, void *cookie, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1590 | { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1591 | return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1595 | set_arg1(struct tcb *tcp, void *cookie, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1596 | { |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1597 | return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1600 | # endif /* architectures */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1601 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1602 | # ifndef restore_arg0 |
| 1603 | # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val)) |
| 1604 | # endif |
| 1605 | # ifndef restore_arg1 |
| 1606 | # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val)) |
| 1607 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1608 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1609 | # ifndef arg0_index |
| 1610 | # define arg0_index 0 |
| 1611 | # define arg1_index 1 |
| 1612 | # endif |
Roland McGrath | 90d0afd | 2004-03-01 21:05:16 +0000 | [diff] [blame] | 1613 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1614 | int |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1615 | setbpt(struct tcb *tcp) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1616 | { |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1617 | static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone }; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1618 | arg_setup_state state; |
| 1619 | |
| 1620 | if (tcp->flags & TCB_BPTSET) { |
| 1621 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1622 | return -1; |
| 1623 | } |
| 1624 | |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1625 | /* |
| 1626 | * It's a silly kludge to initialize this with a search at runtime. |
| 1627 | * But it's better than maintaining another magic thing in the |
| 1628 | * godforsaken tables. |
| 1629 | */ |
| 1630 | if (clone_scno[current_personality] == 0) { |
| 1631 | int i; |
| 1632 | for (i = 0; i < nsyscalls; ++i) |
| 1633 | if (sysent[i].sys_func == sys_clone) { |
| 1634 | clone_scno[current_personality] = i; |
| 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | |
Roland McGrath | 76989d7 | 2005-06-07 23:21:31 +0000 | [diff] [blame] | 1639 | switch (known_scno(tcp)) { |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1640 | # ifdef SYS_vfork |
Roland McGrath | 9383c6c | 2003-01-18 00:19:31 +0000 | [diff] [blame] | 1641 | case SYS_vfork: |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1642 | # endif |
| 1643 | # ifdef SYS_fork |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1644 | case SYS_fork: |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1645 | # endif |
| 1646 | # if defined SYS_fork || defined SYS_vfork |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1647 | if (arg_setup(tcp, &state) < 0 |
| 1648 | || get_arg0(tcp, &state, &tcp->inst[0]) < 0 |
| 1649 | || get_arg1(tcp, &state, &tcp->inst[1]) < 0 |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1650 | || change_syscall(tcp, clone_scno[current_personality]) < 0 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1651 | || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0 |
| 1652 | || set_arg1(tcp, &state, 0) < 0 |
| 1653 | || arg_finish_change(tcp, &state) < 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1654 | return -1; |
Roland McGrath | c9dc3c1 | 2004-03-01 20:57:09 +0000 | [diff] [blame] | 1655 | tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD; |
| 1656 | tcp->u_arg[arg1_index] = 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1657 | tcp->flags |= TCB_BPTSET; |
| 1658 | return 0; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1659 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1660 | |
Denys Vlasenko | c133bf0 | 2011-06-23 21:57:54 +0200 | [diff] [blame] | 1661 | case SYS_clone: ; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1662 | # ifdef SYS_clone2 |
Denys Vlasenko | c133bf0 | 2011-06-23 21:57:54 +0200 | [diff] [blame] | 1663 | case SYS_clone2: ; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1664 | # endif |
Jan Kratochvil | 8fc9575 | 2008-08-06 21:43:35 +0000 | [diff] [blame] | 1665 | /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)' |
| 1666 | contrary to x86 SYS_vfork above. Even on x86 we turn the |
| 1667 | vfork semantics into plain fork - each application must not |
| 1668 | depend on the vfork specifics according to POSIX. We would |
| 1669 | hang waiting for the parent resume otherwise. We need to |
| 1670 | clear also CLONE_VM but only in the CLONE_VFORK case as |
| 1671 | otherwise we would break pthread_create. */ |
| 1672 | |
Denys Vlasenko | c133bf0 | 2011-06-23 21:57:54 +0200 | [diff] [blame] | 1673 | long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE); |
| 1674 | if (new_arg0 & CLONE_VFORK) |
| 1675 | new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM); |
| 1676 | if (arg_setup(tcp, &state) < 0 |
| 1677 | || set_arg0(tcp, &state, new_arg0) < 0 |
| 1678 | || arg_finish_change(tcp, &state) < 0) |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1679 | return -1; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1680 | tcp->flags |= TCB_BPTSET; |
Roland McGrath | c9dc3c1 | 2004-03-01 20:57:09 +0000 | [diff] [blame] | 1681 | tcp->inst[0] = tcp->u_arg[arg0_index]; |
| 1682 | tcp->inst[1] = tcp->u_arg[arg1_index]; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1683 | return 0; |
| 1684 | |
| 1685 | default: |
| 1686 | fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n", |
| 1687 | tcp->scno, tcp->pid); |
| 1688 | break; |
| 1689 | } |
| 1690 | |
| 1691 | return -1; |
| 1692 | } |
| 1693 | |
| 1694 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1695 | clearbpt(struct tcb *tcp) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1696 | { |
| 1697 | arg_setup_state state; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1698 | if (arg_setup(tcp, &state) < 0 |
| 1699 | || restore_arg0(tcp, &state, tcp->inst[0]) < 0 |
| 1700 | || restore_arg1(tcp, &state, tcp->inst[1]) < 0 |
| 1701 | || arg_finish_change(tcp, &state)) |
Denys Vlasenko | c133bf0 | 2011-06-23 21:57:54 +0200 | [diff] [blame] | 1702 | if (errno != ESRCH) |
| 1703 | return -1; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1704 | tcp->flags &= ~TCB_BPTSET; |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1708 | # else /* !defined LINUX */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1709 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1710 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1711 | setbpt(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1712 | { |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1713 | # ifdef SUNOS4 |
| 1714 | # ifdef SPARC /* This code is slightly sparc specific */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1715 | |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 1716 | struct regs regs; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1717 | # define BPT 0x91d02001 /* ta 1 */ |
| 1718 | # define LOOP 0x10800000 /* ba 0 */ |
| 1719 | # define LOOPA 0x30800000 /* ba,a 0 */ |
| 1720 | # define NOP 0x01000000 |
| 1721 | # if LOOPA |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1722 | static int loopdeloop[1] = {LOOPA}; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1723 | # else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1724 | static int loopdeloop[2] = {LOOP, NOP}; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1725 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1726 | |
| 1727 | if (tcp->flags & TCB_BPTSET) { |
| 1728 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1729 | return -1; |
| 1730 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1731 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1732 | perror("setbpt: ptrace(PTRACE_GETREGS, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1733 | return -1; |
| 1734 | } |
| 1735 | tcp->baddr = regs.r_o7 + 8; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1736 | if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr, |
| 1737 | sizeof tcp->inst, (char *)tcp->inst) < 0) { |
| 1738 | perror("setbpt: ptrace(PTRACE_READTEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1739 | return -1; |
| 1740 | } |
| 1741 | |
| 1742 | /* |
| 1743 | * XXX - BRUTAL MODE ON |
| 1744 | * We cannot set a real BPT in the child, since it will not be |
| 1745 | * traced at the moment it will reach the trap and would probably |
| 1746 | * die with a core dump. |
| 1747 | * Thus, we are force our way in by taking out two instructions |
| 1748 | * and insert an eternal loop in stead, in expectance of the SIGSTOP |
| 1749 | * generated by out PTRACE_ATTACH. |
| 1750 | * Of cause, if we evaporate ourselves in the middle of all this... |
| 1751 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1752 | if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr, |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1753 | sizeof loopdeloop, (char *) loopdeloop) < 0) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1754 | perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1755 | return -1; |
| 1756 | } |
| 1757 | tcp->flags |= TCB_BPTSET; |
| 1758 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1759 | # endif /* SPARC */ |
| 1760 | # endif /* SUNOS4 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1761 | |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1766 | clearbpt(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1767 | { |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1768 | # ifdef SUNOS4 |
| 1769 | # ifdef SPARC |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1770 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1771 | # if !LOOPA |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 1772 | struct regs regs; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1773 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1774 | |
| 1775 | if (!(tcp->flags & TCB_BPTSET)) { |
| 1776 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 1777 | return -1; |
| 1778 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1779 | if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr, |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1780 | sizeof tcp->inst, (char *) tcp->inst) < 0) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1781 | perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1782 | return -1; |
| 1783 | } |
| 1784 | tcp->flags &= ~TCB_BPTSET; |
| 1785 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1786 | # if !LOOPA |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1787 | /* |
| 1788 | * Since we don't have a single instruction breakpoint, we may have |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1789 | * to adjust the program counter after removing our `breakpoint'. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1790 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1791 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1792 | perror("clearbpt: ptrace(PTRACE_GETREGS, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1793 | return -1; |
| 1794 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1795 | if ((regs.r_pc < tcp->baddr) || |
| 1796 | (regs.r_pc > tcp->baddr + 4)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1797 | /* The breakpoint has not been reached yet */ |
| 1798 | if (debug) |
| 1799 | fprintf(stderr, |
| 1800 | "NOTE: PC not at bpt (pc %#x baddr %#x)\n", |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1801 | regs.r_pc, tcp->baddr); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1802 | return 0; |
| 1803 | } |
| 1804 | if (regs.r_pc != tcp->baddr) |
| 1805 | if (debug) |
| 1806 | fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n", |
| 1807 | regs.r_pc, tcp->baddr); |
| 1808 | |
| 1809 | regs.r_pc = tcp->baddr; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1810 | if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1811 | perror("clearbpt: ptrace(PTRACE_SETREGS, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1812 | return -1; |
| 1813 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1814 | # endif /* LOOPA */ |
| 1815 | # endif /* SPARC */ |
| 1816 | # endif /* SUNOS4 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1817 | |
| 1818 | return 0; |
| 1819 | } |
| 1820 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1821 | # endif /* !defined LINUX */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1822 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1823 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1824 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1825 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1826 | #ifdef SUNOS4 |
| 1827 | |
| 1828 | static int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1829 | getex(struct tcb *tcp, struct exec *hdr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1830 | { |
| 1831 | int n; |
| 1832 | |
| 1833 | for (n = 0; n < sizeof *hdr; n += 4) { |
| 1834 | long res; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1835 | if (upeek(tcp, uoff(u_exdata) + n, &res) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1836 | return -1; |
| 1837 | memcpy(((char *) hdr) + n, &res, 4); |
| 1838 | } |
| 1839 | if (debug) { |
| 1840 | fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n", |
| 1841 | hdr->a_magic, hdr->a_toolversion, hdr->a_machtype); |
| 1842 | fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n", |
| 1843 | hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry); |
| 1844 | } |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1849 | fixvfork(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1850 | { |
| 1851 | int pid = tcp->pid; |
| 1852 | /* |
| 1853 | * Change `vfork' in a freshly exec'ed dynamically linked |
| 1854 | * executable's (internal) symbol table to plain old `fork' |
| 1855 | */ |
| 1856 | |
| 1857 | struct exec hdr; |
| 1858 | struct link_dynamic dyn; |
| 1859 | struct link_dynamic_2 ld; |
| 1860 | char *strtab, *cp; |
| 1861 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1862 | if (getex(tcp, &hdr) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1863 | return -1; |
| 1864 | if (!hdr.a_dynamic) |
| 1865 | return -1; |
| 1866 | |
| 1867 | if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) { |
| 1868 | fprintf(stderr, "Cannot read DYNAMIC\n"); |
| 1869 | return -1; |
| 1870 | } |
| 1871 | if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) { |
| 1872 | fprintf(stderr, "Cannot read link_dynamic_2\n"); |
| 1873 | return -1; |
| 1874 | } |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 1875 | strtab = malloc((unsigned)ld.ld_symb_size); |
Denys Vlasenko | 1d46ba5 | 2011-08-31 14:00:02 +0200 | [diff] [blame] | 1876 | if (!strtab) |
| 1877 | die_out_of_memory(); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1878 | if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr), |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1879 | (int)ld.ld_symb_size, strtab) < 0) |
| 1880 | goto err; |
| 1881 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1882 | for (cp = strtab; cp < strtab + ld.ld_symb_size; ) { |
| 1883 | if (strcmp(cp, "_vfork") == 0) { |
| 1884 | if (debug) |
| 1885 | fprintf(stderr, "fixvfork: FOUND _vfork\n"); |
| 1886 | strcpy(cp, "_fork"); |
| 1887 | break; |
| 1888 | } |
| 1889 | cp += strlen(cp)+1; |
| 1890 | } |
| 1891 | if (cp < strtab + ld.ld_symb_size) |
| 1892 | /* |
| 1893 | * Write entire symbol table back to avoid |
| 1894 | * memory alignment bugs in ptrace |
| 1895 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1896 | if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr), |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1897 | (int)ld.ld_symb_size, strtab) < 0) |
| 1898 | goto err; |
| 1899 | |
| 1900 | free(strtab); |
| 1901 | return 0; |
| 1902 | |
| 1903 | err: |
| 1904 | free(strtab); |
| 1905 | return -1; |
| 1906 | } |
| 1907 | |
| 1908 | #endif /* SUNOS4 */ |