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 | |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 81 | #if defined(LINUXSPARC) |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 82 | |
Roland McGrath | 4db2624 | 2003-01-30 20:15:19 +0000 | [diff] [blame] | 83 | # define fpq kernel_fpq |
| 84 | # define fq kernel_fq |
| 85 | # define fpu kernel_fpu |
| 86 | # include <asm/reg.h> |
| 87 | # undef fpq |
| 88 | # undef fq |
| 89 | # undef fpu |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 90 | |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 91 | #if defined (SPARC64) |
| 92 | # define r_pc r_tpc |
| 93 | # undef PTRACE_GETREGS |
| 94 | # define PTRACE_GETREGS PTRACE_GETREGS64 |
| 95 | # undef PTRACE_SETREGS |
| 96 | # define PTRACE_SETREGS PTRACE_SETREGS64 |
| 97 | #endif /* SPARC64 */ |
| 98 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 99 | #if !defined(__GLIBC__) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 100 | |
| 101 | #include <linux/unistd.h> |
| 102 | |
| 103 | #define _hack_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\ |
| 104 | type5,arg5,syscall) \ |
| 105 | type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \ |
| 106 | { \ |
| 107 | long __res; \ |
| 108 | \ |
| 109 | __asm__ volatile ("or %%g0, %1, %%o0\n\t" \ |
| 110 | "or %%g0, %2, %%o1\n\t" \ |
| 111 | "or %%g0, %3, %%o2\n\t" \ |
| 112 | "or %%g0, %4, %%o3\n\t" \ |
| 113 | "or %%g0, %5, %%o4\n\t" \ |
| 114 | "or %%g0, %6, %%g1\n\t" \ |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 115 | #if defined (SPARC64) |
| 116 | "t 0x6d\n\t" \ |
| 117 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 118 | "t 0x10\n\t" \ |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 119 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 120 | "bcc 1f\n\t" \ |
| 121 | "or %%g0, %%o0, %0\n\t" \ |
| 122 | "sub %%g0, %%o0, %0\n\t" \ |
| 123 | "1:\n\t" \ |
| 124 | : "=r" (__res) \ |
| 125 | : "0" ((long)(arg1)),"1" ((long)(arg2)), \ |
| 126 | "2" ((long)(arg3)),"3" ((long)(arg4)),"4" ((long)(arg5)), \ |
| 127 | "i" (__NR_##syscall) \ |
| 128 | : "g1", "o0", "o1", "o2", "o3", "o4"); \ |
| 129 | if (__res>=0) \ |
| 130 | return (type) __res; \ |
| 131 | errno = -__res; \ |
| 132 | return -1; \ |
| 133 | } |
| 134 | |
| 135 | static _hack_syscall5(int,_ptrace,int,__request,int,__pid,int,__addr,int,__data,int,__addr2,ptrace) |
| 136 | |
| 137 | #define _ptrace |
| 138 | |
| 139 | #endif |
| 140 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 143 | /* macros */ |
| 144 | #ifndef MAX |
| 145 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) |
| 146 | #endif |
| 147 | #ifndef MIN |
| 148 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) |
| 149 | #endif |
| 150 | |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 151 | #if 0 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 152 | void |
| 153 | tv_tv(tv, a, b) |
| 154 | struct timeval *tv; |
| 155 | int a; |
| 156 | int b; |
| 157 | { |
| 158 | tv->tv_sec = a; |
| 159 | tv->tv_usec = b; |
| 160 | } |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 161 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 162 | |
| 163 | int |
| 164 | tv_nz(a) |
| 165 | struct timeval *a; |
| 166 | { |
| 167 | return a->tv_sec || a->tv_usec; |
| 168 | } |
| 169 | |
| 170 | int |
| 171 | tv_cmp(a, b) |
| 172 | struct timeval *a, *b; |
| 173 | { |
| 174 | if (a->tv_sec < b->tv_sec |
| 175 | || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) |
| 176 | return -1; |
| 177 | if (a->tv_sec > b->tv_sec |
| 178 | || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec)) |
| 179 | return 1; |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | double |
| 184 | tv_float(tv) |
| 185 | struct timeval *tv; |
| 186 | { |
| 187 | return tv->tv_sec + tv->tv_usec/1000000.0; |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | tv_add(tv, a, b) |
| 192 | struct timeval *tv, *a, *b; |
| 193 | { |
| 194 | tv->tv_sec = a->tv_sec + b->tv_sec; |
| 195 | tv->tv_usec = a->tv_usec + b->tv_usec; |
Roland McGrath | 58372f5 | 2007-07-24 01:38:22 +0000 | [diff] [blame] | 196 | if (tv->tv_usec >= 1000000) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 197 | tv->tv_sec++; |
| 198 | tv->tv_usec -= 1000000; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | tv_sub(tv, a, b) |
| 204 | struct timeval *tv, *a, *b; |
| 205 | { |
| 206 | tv->tv_sec = a->tv_sec - b->tv_sec; |
| 207 | tv->tv_usec = a->tv_usec - b->tv_usec; |
| 208 | if (((long) tv->tv_usec) < 0) { |
| 209 | tv->tv_sec--; |
| 210 | tv->tv_usec += 1000000; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | tv_div(tv, a, n) |
| 216 | struct timeval *tv, *a; |
| 217 | int n; |
| 218 | { |
| 219 | tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n; |
| 220 | tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000; |
| 221 | tv->tv_usec %= 1000000; |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | tv_mul(tv, a, n) |
| 226 | struct timeval *tv, *a; |
| 227 | int n; |
| 228 | { |
| 229 | tv->tv_usec = a->tv_usec * n; |
Dmitry V. Levin | fefdd97 | 2007-06-29 21:25:56 +0000 | [diff] [blame] | 230 | tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 231 | tv->tv_usec %= 1000000; |
| 232 | } |
| 233 | |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 234 | const char * |
| 235 | xlookup(const struct xlat *xlat, int val) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 236 | { |
| 237 | for (; xlat->str != NULL; xlat++) |
| 238 | if (xlat->val == val) |
| 239 | return xlat->str; |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Print entry in struct xlat table, if there. |
| 245 | */ |
| 246 | void |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 247 | printxval(const struct xlat *xlat, int val, const char *dflt) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 248 | { |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 249 | const char *str = xlookup(xlat, val); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 250 | |
| 251 | if (str) |
| 252 | tprintf("%s", str); |
| 253 | else |
| 254 | tprintf("%#x /* %s */", val, dflt); |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Interpret `xlat' as an array of flags |
| 259 | * print the entries whose bits are on in `flags' |
| 260 | * return # of flags printed. |
| 261 | */ |
| 262 | int |
| 263 | addflags(xlat, flags) |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 264 | const struct xlat *xlat; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 265 | int flags; |
| 266 | { |
| 267 | int n; |
| 268 | |
| 269 | for (n = 0; xlat->str; xlat++) { |
| 270 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 271 | tprintf("|%s", xlat->str); |
| 272 | flags &= ~xlat->val; |
| 273 | n++; |
| 274 | } |
| 275 | } |
| 276 | if (flags) { |
| 277 | tprintf("|%#x", flags); |
| 278 | n++; |
| 279 | } |
| 280 | return n; |
| 281 | } |
| 282 | |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 283 | /* |
| 284 | * Interpret `xlat' as an array of flags/ |
| 285 | * Print to static string the entries whose bits are on in `flags' |
| 286 | * Return static string. |
| 287 | */ |
| 288 | const char * |
| 289 | sprintflags(const char *prefix, const struct xlat *xlat, int flags) |
| 290 | { |
| 291 | static char outstr[1024]; |
| 292 | int found = 0; |
| 293 | |
| 294 | strcpy(outstr, prefix); |
| 295 | |
| 296 | for (; xlat->str; xlat++) { |
| 297 | if ((flags & xlat->val) == xlat->val) { |
| 298 | if (found) |
| 299 | strcat(outstr, "|"); |
| 300 | strcat(outstr, xlat->str); |
| 301 | flags &= ~xlat->val; |
| 302 | found = 1; |
| 303 | } |
| 304 | } |
| 305 | if (flags) { |
| 306 | if (found) |
| 307 | strcat(outstr, "|"); |
| 308 | sprintf(outstr + strlen(outstr), "%#x", flags); |
| 309 | } |
| 310 | |
| 311 | return outstr; |
| 312 | } |
| 313 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 314 | int |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 315 | printflags(xlat, flags, dflt) |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 316 | const struct xlat *xlat; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 317 | int flags; |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 318 | const char *dflt; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 319 | { |
| 320 | int n; |
| 321 | char *sep; |
| 322 | |
| 323 | if (flags == 0 && xlat->val == 0) { |
| 324 | tprintf("%s", xlat->str); |
| 325 | return 1; |
| 326 | } |
| 327 | |
| 328 | sep = ""; |
| 329 | for (n = 0; xlat->str; xlat++) { |
| 330 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 331 | tprintf("%s%s", sep, xlat->str); |
| 332 | flags &= ~xlat->val; |
| 333 | sep = "|"; |
| 334 | n++; |
| 335 | } |
| 336 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 337 | |
| 338 | if (n) { |
| 339 | if (flags) { |
| 340 | tprintf("%s%#x", sep, flags); |
| 341 | n++; |
| 342 | } |
| 343 | } else { |
| 344 | if (flags) { |
| 345 | tprintf("%#x", flags); |
| 346 | if (dflt) |
| 347 | tprintf(" /* %s */", dflt); |
| 348 | } else { |
| 349 | if (dflt) |
| 350 | tprintf("0"); |
| 351 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 352 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 353 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 354 | return n; |
| 355 | } |
| 356 | |
| 357 | void |
| 358 | printnum(tcp, addr, fmt) |
| 359 | struct tcb *tcp; |
| 360 | long addr; |
| 361 | char *fmt; |
| 362 | { |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 363 | long num; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 364 | |
| 365 | if (!addr) { |
| 366 | tprintf("NULL"); |
| 367 | return; |
| 368 | } |
| 369 | if (umove(tcp, addr, &num) < 0) { |
| 370 | tprintf("%#lx", addr); |
| 371 | return; |
| 372 | } |
| 373 | tprintf("["); |
| 374 | tprintf(fmt, num); |
| 375 | tprintf("]"); |
| 376 | } |
| 377 | |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 378 | void |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 379 | printnum_int(tcp, addr, fmt) |
| 380 | struct tcb *tcp; |
| 381 | long addr; |
| 382 | char *fmt; |
| 383 | { |
| 384 | int num; |
| 385 | |
| 386 | if (!addr) { |
| 387 | tprintf("NULL"); |
| 388 | return; |
| 389 | } |
| 390 | if (umove(tcp, addr, &num) < 0) { |
| 391 | tprintf("%#lx", addr); |
| 392 | return; |
| 393 | } |
| 394 | tprintf("["); |
| 395 | tprintf(fmt, num); |
| 396 | tprintf("]"); |
| 397 | } |
| 398 | |
| 399 | void |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 400 | printuid(text, uid) |
| 401 | const char *text; |
| 402 | unsigned long uid; |
| 403 | { |
| 404 | tprintf("%s", text); |
| 405 | tprintf((uid == -1) ? "%ld" : "%lu", uid); |
| 406 | } |
| 407 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 408 | static char path[MAXPATHLEN + 1]; |
| 409 | |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 410 | static int |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 411 | string_quote(const char *instr, char *outstr, int len, int size) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 412 | { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 413 | const unsigned char *ustr = (const unsigned char *) instr; |
| 414 | char *s = outstr; |
| 415 | int usehex = 0, c, i; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 416 | |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 417 | if (xflag > 1) |
| 418 | usehex = 1; |
| 419 | else if (xflag) { |
| 420 | for (i = 0; i < size; ++i) { |
| 421 | c = ustr[i]; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 422 | if (len < 0 && i == size - 2 && c != '\0') |
| 423 | ++i; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 424 | if (len < 0 && c == '\0') |
| 425 | break; |
| 426 | if (!isprint(c) && !isspace(c)) { |
| 427 | usehex = 1; |
| 428 | break; |
| 429 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 432 | |
| 433 | *s++ = '\"'; |
| 434 | |
| 435 | if (usehex) { |
| 436 | for (i = 0; i < size; ++i) { |
| 437 | c = ustr[i]; |
| 438 | if (len < 0 && c == '\0') |
| 439 | break; |
| 440 | sprintf(s, "\\x%02x", c); |
| 441 | s += 4; |
| 442 | } |
| 443 | } else { |
| 444 | for (i = 0; i < size; ++i) { |
| 445 | c = ustr[i]; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 446 | if (len < 0 && i == size - 2 && c != '\0') |
| 447 | ++i; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 448 | if (len < 0 && c == '\0') |
| 449 | break; |
| 450 | switch (c) { |
| 451 | case '\"': case '\\': |
| 452 | *s++ = '\\'; |
| 453 | *s++ = c; |
| 454 | break; |
| 455 | case '\f': |
| 456 | *s++ = '\\'; |
| 457 | *s++ = 'f'; |
| 458 | break; |
| 459 | case '\n': |
| 460 | *s++ = '\\'; |
| 461 | *s++ = 'n'; |
| 462 | break; |
| 463 | case '\r': |
| 464 | *s++ = '\\'; |
| 465 | *s++ = 'r'; |
| 466 | break; |
| 467 | case '\t': |
| 468 | *s++ = '\\'; |
| 469 | *s++ = 't'; |
| 470 | break; |
| 471 | case '\v': |
| 472 | *s++ = '\\'; |
| 473 | *s++ = 'v'; |
| 474 | break; |
| 475 | default: |
| 476 | if (isprint(c)) |
| 477 | *s++ = c; |
| 478 | else if (i + 1 < size |
| 479 | && isdigit(ustr[i + 1])) { |
| 480 | sprintf(s, "\\%03o", c); |
| 481 | s += 4; |
| 482 | } else { |
| 483 | sprintf(s, "\\%o", c); |
| 484 | s += strlen(s); |
| 485 | } |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | *s++ = '\"'; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 492 | *s = '\0'; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 493 | |
| 494 | /* Return nonzero if the string was unterminated. */ |
| 495 | return i == size; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 499 | printpathn(struct tcb *tcp, long addr, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 500 | { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 501 | if (n > sizeof path - 1) |
Roland McGrath | b15c4e4 | 2005-10-21 22:06:46 +0000 | [diff] [blame] | 502 | n = sizeof path - 1; |
| 503 | |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 504 | if (addr == 0) { |
Roland McGrath | 371ed8f | 2005-02-06 01:55:07 +0000 | [diff] [blame] | 505 | tprintf("NULL"); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 506 | return; |
| 507 | } |
| 508 | |
| 509 | path[n] = '\0'; |
| 510 | if (umovestr(tcp, addr, n + 1, path) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 511 | tprintf("%#lx", addr); |
| 512 | else { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 513 | static char outstr[4*(sizeof path - 1) + sizeof "\"...\""]; |
| 514 | int trunc = (path[n] != '\0'); |
| 515 | |
| 516 | if (trunc) |
| 517 | path[n] = '\0'; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 518 | if (string_quote(path, outstr, -1, n + 1) || trunc) |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 519 | strcat(outstr, "..."); |
| 520 | tprintf("%s", outstr); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | void |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 525 | printpath(struct tcb *tcp, long addr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 526 | { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 527 | printpathn(tcp, addr, sizeof path - 1); |
| 528 | } |
| 529 | |
| 530 | void |
| 531 | printstr(struct tcb *tcp, long addr, int len) |
| 532 | { |
| 533 | static char *str = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 534 | static char *outstr; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 535 | int size; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 536 | |
| 537 | if (!addr) { |
| 538 | tprintf("NULL"); |
| 539 | return; |
| 540 | } |
| 541 | if (!str) { |
Roland McGrath | a503dcf | 2007-08-02 02:06:26 +0000 | [diff] [blame] | 542 | if ((str = malloc(max_strlen + 1)) == NULL |
Roland McGrath | 779c466 | 2007-07-11 07:23:40 +0000 | [diff] [blame] | 543 | || (outstr = malloc(4*max_strlen |
| 544 | + sizeof "\"\"...")) == NULL) { |
Roland McGrath | 46100d0 | 2005-06-01 18:55:42 +0000 | [diff] [blame] | 545 | fprintf(stderr, "out of memory\n"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 546 | tprintf("%#lx", addr); |
| 547 | return; |
| 548 | } |
| 549 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 550 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 551 | if (len < 0) { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 552 | size = max_strlen + 1; |
| 553 | if (umovestr(tcp, addr, size, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 554 | tprintf("%#lx", addr); |
| 555 | return; |
| 556 | } |
| 557 | } |
| 558 | else { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 559 | size = MIN(len, max_strlen + 1); |
| 560 | if (umoven(tcp, addr, size, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 561 | tprintf("%#lx", addr); |
| 562 | return; |
| 563 | } |
| 564 | } |
| 565 | |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 566 | if (string_quote(str, outstr, len, size)) |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 567 | strcat(outstr, "..."); |
Roland McGrath | a503dcf | 2007-08-02 02:06:26 +0000 | [diff] [blame] | 568 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 569 | tprintf("%s", outstr); |
| 570 | } |
| 571 | |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 572 | #if HAVE_SYS_UIO_H |
| 573 | void |
| 574 | dumpiov(tcp, len, addr) |
| 575 | struct tcb * tcp; |
| 576 | int len; |
| 577 | long addr; |
| 578 | { |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 579 | #if defined(LINUX) && SUPPORTED_PERSONALITIES > 1 |
| 580 | union { |
| 581 | struct { u_int32_t base; u_int32_t len; } *iov32; |
| 582 | struct { u_int64_t base; u_int64_t len; } *iov64; |
| 583 | } iovu; |
| 584 | #define iov iovu.iov64 |
| 585 | #define sizeof_iov \ |
| 586 | (personality_wordsize[current_personality] == 4 \ |
| 587 | ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64)) |
| 588 | #define iov_iov_base(i) \ |
| 589 | (personality_wordsize[current_personality] == 4 \ |
| 590 | ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base) |
| 591 | #define iov_iov_len(i) \ |
| 592 | (personality_wordsize[current_personality] == 4 \ |
| 593 | ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len) |
| 594 | #else |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 595 | struct iovec *iov; |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 596 | #define sizeof_iov sizeof(*iov) |
| 597 | #define iov_iov_base(i) iov[i].iov_base |
| 598 | #define iov_iov_len(i) iov[i].iov_len |
| 599 | #endif |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 600 | int i; |
Roland McGrath | aa524c8 | 2005-06-01 19:22:06 +0000 | [diff] [blame] | 601 | unsigned long size; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 602 | |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 603 | size = sizeof_iov * (unsigned long) len; |
| 604 | if (size / sizeof_iov != len |
| 605 | || (iov = malloc(size)) == NULL) { |
Roland McGrath | aa524c8 | 2005-06-01 19:22:06 +0000 | [diff] [blame] | 606 | fprintf(stderr, "out of memory\n"); |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 607 | return; |
| 608 | } |
Roland McGrath | aa524c8 | 2005-06-01 19:22:06 +0000 | [diff] [blame] | 609 | if (umoven(tcp, addr, size, (char *) iov) >= 0) { |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 610 | for (i = 0; i < len; i++) { |
| 611 | /* include the buffer number to make it easy to |
| 612 | * match up the trace with the source */ |
| 613 | tprintf(" * %lu bytes in buffer %d\n", |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 614 | (unsigned long)iov_iov_len(i), i); |
| 615 | dumpstr(tcp, (long) iov_iov_base(i), |
| 616 | iov_iov_len(i)); |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | free((char *) iov); |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 620 | #undef sizeof_iov |
| 621 | #undef iov_iov_base |
| 622 | #undef iov_iov_len |
| 623 | #undef iov |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 624 | } |
| 625 | #endif |
| 626 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 627 | void |
| 628 | dumpstr(tcp, addr, len) |
| 629 | struct tcb *tcp; |
| 630 | long addr; |
| 631 | int len; |
| 632 | { |
| 633 | static int strsize = -1; |
| 634 | static unsigned char *str; |
| 635 | static char outstr[80]; |
| 636 | char *s; |
| 637 | int i, j; |
| 638 | |
| 639 | if (strsize < len) { |
| 640 | if (str) |
| 641 | free(str); |
| 642 | if ((str = malloc(len)) == NULL) { |
Roland McGrath | 46100d0 | 2005-06-01 18:55:42 +0000 | [diff] [blame] | 643 | fprintf(stderr, "out of memory\n"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 644 | return; |
| 645 | } |
| 646 | strsize = len; |
| 647 | } |
| 648 | |
| 649 | if (umoven(tcp, addr, len, (char *) str) < 0) |
| 650 | return; |
| 651 | |
| 652 | for (i = 0; i < len; i += 16) { |
| 653 | s = outstr; |
| 654 | sprintf(s, " | %05x ", i); |
| 655 | s += 9; |
| 656 | for (j = 0; j < 16; j++) { |
| 657 | if (j == 8) |
| 658 | *s++ = ' '; |
| 659 | if (i + j < len) { |
| 660 | sprintf(s, " %02x", str[i + j]); |
| 661 | s += 3; |
| 662 | } |
| 663 | else { |
| 664 | *s++ = ' '; *s++ = ' '; *s++ = ' '; |
| 665 | } |
| 666 | } |
| 667 | *s++ = ' '; *s++ = ' '; |
| 668 | for (j = 0; j < 16; j++) { |
| 669 | if (j == 8) |
| 670 | *s++ = ' '; |
| 671 | if (i + j < len) { |
| 672 | if (isprint(str[i + j])) |
| 673 | *s++ = str[i + j]; |
| 674 | else |
| 675 | *s++ = '.'; |
| 676 | } |
| 677 | else |
| 678 | *s++ = ' '; |
| 679 | } |
| 680 | tprintf("%s |\n", outstr); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | #define PAGMASK (~(PAGSIZ - 1)) |
| 685 | /* |
| 686 | * move `len' bytes of data from process `pid' |
| 687 | * at address `addr' to our space at `laddr' |
| 688 | */ |
| 689 | int |
| 690 | umoven(tcp, addr, len, laddr) |
| 691 | struct tcb *tcp; |
| 692 | long addr; |
| 693 | int len; |
| 694 | char *laddr; |
| 695 | { |
| 696 | |
| 697 | #ifdef LINUX |
| 698 | int pid = tcp->pid; |
| 699 | int n, m; |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 700 | int started = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 701 | union { |
| 702 | long val; |
| 703 | char x[sizeof(long)]; |
| 704 | } u; |
| 705 | |
| 706 | if (addr & (sizeof(long) - 1)) { |
| 707 | /* addr not a multiple of sizeof(long) */ |
| 708 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 709 | addr &= -sizeof(long); /* residue */ |
| 710 | errno = 0; |
| 711 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 712 | if (errno) { |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 713 | if (started && (errno==EPERM || errno==EIO)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 714 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 715 | return 0; |
| 716 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 717 | /* But if not started, we had a bogus address. */ |
Roland McGrath | 1c45976 | 2007-08-02 02:22:06 +0000 | [diff] [blame] | 718 | if (addr != 0 && errno != EIO) |
| 719 | perror("ptrace: umoven"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 720 | return -1; |
| 721 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 722 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 723 | memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len)); |
| 724 | addr += sizeof(long), laddr += m, len -= m; |
| 725 | } |
| 726 | while (len) { |
| 727 | errno = 0; |
| 728 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 729 | if (errno) { |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 730 | if (started && (errno==EPERM || errno==EIO)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 731 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 732 | return 0; |
| 733 | } |
Roland McGrath | 1c45976 | 2007-08-02 02:22:06 +0000 | [diff] [blame] | 734 | if (addr != 0 && errno != EIO) |
Roland McGrath | 4db2624 | 2003-01-30 20:15:19 +0000 | [diff] [blame] | 735 | perror("ptrace: umoven"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 736 | return -1; |
| 737 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 738 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 739 | memcpy(laddr, u.x, m = MIN(sizeof(long), len)); |
| 740 | addr += sizeof(long), laddr += m, len -= m; |
| 741 | } |
| 742 | #endif /* LINUX */ |
| 743 | |
| 744 | #ifdef SUNOS4 |
| 745 | int pid = tcp->pid; |
| 746 | #if 0 |
| 747 | int n, m; |
| 748 | union { |
| 749 | long val; |
| 750 | char x[sizeof(long)]; |
| 751 | } u; |
| 752 | |
| 753 | if (addr & (sizeof(long) - 1)) { |
| 754 | /* addr not a multiple of sizeof(long) */ |
| 755 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 756 | addr &= -sizeof(long); /* residue */ |
| 757 | errno = 0; |
| 758 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 759 | if (errno) { |
| 760 | perror("umoven"); |
| 761 | return -1; |
| 762 | } |
| 763 | memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len)); |
| 764 | addr += sizeof(long), laddr += m, len -= m; |
| 765 | } |
| 766 | while (len) { |
| 767 | errno = 0; |
| 768 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 769 | if (errno) { |
| 770 | perror("umoven"); |
| 771 | return -1; |
| 772 | } |
| 773 | memcpy(laddr, u.x, m = MIN(sizeof(long), len)); |
| 774 | addr += sizeof(long), laddr += m, len -= m; |
| 775 | } |
| 776 | #else /* !oldway */ |
| 777 | int n; |
| 778 | |
| 779 | while (len) { |
| 780 | n = MIN(len, PAGSIZ); |
| 781 | n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr); |
| 782 | if (ptrace(PTRACE_READDATA, pid, |
| 783 | (char *) addr, len, laddr) < 0) { |
| 784 | perror("umoven: ptrace(PTRACE_READDATA, ...)"); |
| 785 | abort(); |
| 786 | return -1; |
| 787 | } |
| 788 | len -= n; |
| 789 | addr += n; |
| 790 | laddr += n; |
| 791 | } |
| 792 | #endif /* !oldway */ |
| 793 | #endif /* SUNOS4 */ |
| 794 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 795 | #ifdef USE_PROCFS |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 796 | #ifdef HAVE_MP_PROCFS |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 797 | int fd = tcp->pfd_as; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 798 | #else |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 799 | int fd = tcp->pfd; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 800 | #endif |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 801 | lseek(fd, addr, SEEK_SET); |
| 802 | if (read(fd, laddr, len) == -1) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 803 | return -1; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 804 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | * like `umove' but make the additional effort of looking |
| 811 | * for a terminating zero byte. |
| 812 | */ |
| 813 | int |
| 814 | umovestr(tcp, addr, len, laddr) |
| 815 | struct tcb *tcp; |
| 816 | long addr; |
| 817 | int len; |
| 818 | char *laddr; |
| 819 | { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 820 | #ifdef USE_PROCFS |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 821 | #ifdef HAVE_MP_PROCFS |
| 822 | int fd = tcp->pfd_as; |
| 823 | #else |
| 824 | int fd = tcp->pfd; |
| 825 | #endif |
| 826 | /* Some systems (e.g. FreeBSD) can be upset if we read off the |
| 827 | end of valid memory, avoid this by trying to read up |
| 828 | to page boundaries. But we don't know what a page is (and |
| 829 | getpagesize(2) (if it exists) doesn't necessarily return |
| 830 | hardware page size). Assume all pages >= 1024 (a-historical |
| 831 | I know) */ |
| 832 | |
| 833 | int page = 1024; /* How to find this? */ |
| 834 | int move = page - (addr & (page - 1)); |
| 835 | int left = len; |
| 836 | |
| 837 | lseek(fd, addr, SEEK_SET); |
| 838 | |
| 839 | while (left) { |
| 840 | if (move > left) move = left; |
John Hughes | 9cecf7f | 2001-10-16 10:20:22 +0000 | [diff] [blame] | 841 | if ((move = read(fd, laddr, move)) <= 0) |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 842 | return left != len ? 0 : -1; |
| 843 | if (memchr (laddr, 0, move)) break; |
| 844 | left -= move; |
| 845 | laddr += move; |
| 846 | addr += move; |
| 847 | move = page; |
| 848 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 849 | #else /* !USE_PROCFS */ |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 850 | int started = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 851 | int pid = tcp->pid; |
| 852 | int i, n, m; |
| 853 | union { |
| 854 | long val; |
| 855 | char x[sizeof(long)]; |
| 856 | } u; |
| 857 | |
| 858 | if (addr & (sizeof(long) - 1)) { |
| 859 | /* addr not a multiple of sizeof(long) */ |
| 860 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 861 | addr &= -sizeof(long); /* residue */ |
| 862 | errno = 0; |
| 863 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
| 864 | if (errno) { |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 865 | if (started && (errno==EPERM || errno==EIO)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 866 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 867 | return 0; |
| 868 | } |
Roland McGrath | 1c45976 | 2007-08-02 02:22:06 +0000 | [diff] [blame] | 869 | if (addr != 0 && errno != EIO) |
| 870 | perror("umovestr"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 871 | return -1; |
| 872 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 873 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 874 | memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len)); |
| 875 | while (n & (sizeof(long) - 1)) |
| 876 | if (u.x[n++] == '\0') |
| 877 | return 0; |
| 878 | addr += sizeof(long), laddr += m, len -= m; |
| 879 | } |
| 880 | while (len) { |
| 881 | errno = 0; |
| 882 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
| 883 | if (errno) { |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 884 | if (started && (errno==EPERM || errno==EIO)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 885 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 886 | return 0; |
| 887 | } |
Roland McGrath | 1c45976 | 2007-08-02 02:22:06 +0000 | [diff] [blame] | 888 | if (addr != 0 && errno != EIO) |
| 889 | perror("umovestr"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 890 | return -1; |
| 891 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 892 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 893 | memcpy(laddr, u.x, m = MIN(sizeof(long), len)); |
| 894 | for (i = 0; i < sizeof(long); i++) |
| 895 | if (u.x[i] == '\0') |
| 896 | return 0; |
| 897 | |
| 898 | addr += sizeof(long), laddr += m, len -= m; |
| 899 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 900 | #endif /* !USE_PROCFS */ |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 901 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | #ifdef LINUX |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 905 | #if !defined (SPARC) && !defined(SPARC64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 906 | #define PTRACE_WRITETEXT 101 |
| 907 | #define PTRACE_WRITEDATA 102 |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 908 | #endif /* !SPARC && !SPARC64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 909 | #endif /* LINUX */ |
| 910 | |
| 911 | #ifdef SUNOS4 |
| 912 | |
| 913 | static int |
| 914 | uload(cmd, pid, addr, len, laddr) |
| 915 | int cmd; |
| 916 | int pid; |
| 917 | long addr; |
| 918 | int len; |
| 919 | char *laddr; |
| 920 | { |
| 921 | #if 0 |
| 922 | int n; |
| 923 | |
| 924 | while (len) { |
| 925 | n = MIN(len, PAGSIZ); |
| 926 | n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr); |
| 927 | if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) { |
| 928 | perror("uload: ptrace(PTRACE_WRITE, ...)"); |
| 929 | return -1; |
| 930 | } |
| 931 | len -= n; |
| 932 | addr += n; |
| 933 | laddr += n; |
| 934 | } |
| 935 | #else |
| 936 | int peek, poke; |
| 937 | int n, m; |
| 938 | union { |
| 939 | long val; |
| 940 | char x[sizeof(long)]; |
| 941 | } u; |
| 942 | |
| 943 | if (cmd == PTRACE_WRITETEXT) { |
| 944 | peek = PTRACE_PEEKTEXT; |
| 945 | poke = PTRACE_POKETEXT; |
| 946 | } |
| 947 | else { |
| 948 | peek = PTRACE_PEEKDATA; |
| 949 | poke = PTRACE_POKEDATA; |
| 950 | } |
| 951 | if (addr & (sizeof(long) - 1)) { |
| 952 | /* addr not a multiple of sizeof(long) */ |
| 953 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 954 | addr &= -sizeof(long); |
| 955 | errno = 0; |
| 956 | u.val = ptrace(peek, pid, (char *) addr, 0); |
| 957 | if (errno) { |
| 958 | perror("uload: POKE"); |
| 959 | return -1; |
| 960 | } |
| 961 | memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len)); |
| 962 | if (ptrace(poke, pid, (char *)addr, u.val) < 0) { |
| 963 | perror("uload: POKE"); |
| 964 | return -1; |
| 965 | } |
| 966 | addr += sizeof(long), laddr += m, len -= m; |
| 967 | } |
| 968 | while (len) { |
| 969 | if (len < sizeof(long)) |
| 970 | u.val = ptrace(peek, pid, (char *) addr, 0); |
| 971 | memcpy(u.x, laddr, m = MIN(sizeof(long), len)); |
| 972 | if (ptrace(poke, pid, (char *) addr, u.val) < 0) { |
| 973 | perror("uload: POKE"); |
| 974 | return -1; |
| 975 | } |
| 976 | addr += sizeof(long), laddr += m, len -= m; |
| 977 | } |
| 978 | #endif |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | int |
| 983 | tload(pid, addr, len, laddr) |
| 984 | int pid; |
| 985 | int addr, len; |
| 986 | char *laddr; |
| 987 | { |
| 988 | return uload(PTRACE_WRITETEXT, pid, addr, len, laddr); |
| 989 | } |
| 990 | |
| 991 | int |
| 992 | dload(pid, addr, len, laddr) |
| 993 | int pid; |
| 994 | int addr; |
| 995 | int len; |
| 996 | char *laddr; |
| 997 | { |
| 998 | return uload(PTRACE_WRITEDATA, pid, addr, len, laddr); |
| 999 | } |
| 1000 | |
| 1001 | #endif /* SUNOS4 */ |
| 1002 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1003 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1004 | |
| 1005 | int |
| 1006 | upeek(pid, off, res) |
| 1007 | int pid; |
| 1008 | long off; |
| 1009 | long *res; |
| 1010 | { |
| 1011 | long val; |
| 1012 | |
| 1013 | #ifdef SUNOS4_KERNEL_ARCH_KLUDGE |
| 1014 | { |
| 1015 | static int is_sun4m = -1; |
| 1016 | struct utsname name; |
| 1017 | |
| 1018 | /* Round up the usual suspects. */ |
| 1019 | if (is_sun4m == -1) { |
| 1020 | if (uname(&name) < 0) { |
| 1021 | perror("upeek: uname?"); |
| 1022 | exit(1); |
| 1023 | } |
| 1024 | is_sun4m = strcmp(name.machine, "sun4m") == 0; |
| 1025 | if (is_sun4m) { |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 1026 | extern const struct xlat struct_user_offsets[]; |
| 1027 | const struct xlat *x; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1028 | |
| 1029 | for (x = struct_user_offsets; x->str; x++) |
| 1030 | x->val += 1024; |
| 1031 | } |
| 1032 | } |
| 1033 | if (is_sun4m) |
| 1034 | off += 1024; |
| 1035 | } |
| 1036 | #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */ |
| 1037 | errno = 0; |
| 1038 | val = ptrace(PTRACE_PEEKUSER, pid, (char *) off, 0); |
| 1039 | if (val == -1 && errno) { |
Roland McGrath | 1e85cf9 | 2002-12-16 20:40:54 +0000 | [diff] [blame] | 1040 | char buf[60]; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1041 | sprintf(buf,"upeek: ptrace(PTRACE_PEEKUSER,%d,%lu,0)",pid,off); |
Roland McGrath | 1e85cf9 | 2002-12-16 20:40:54 +0000 | [diff] [blame] | 1042 | perror(buf); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1043 | return -1; |
| 1044 | } |
| 1045 | *res = val; |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1049 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1050 | |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 1051 | #if 0 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1052 | long |
| 1053 | getpc(tcp) |
| 1054 | struct tcb *tcp; |
| 1055 | { |
| 1056 | |
| 1057 | #ifdef LINUX |
| 1058 | long pc; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1059 | #if defined(I386) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1060 | if (upeek(tcp->pid, 4*EIP, &pc) < 0) |
| 1061 | return -1; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1062 | #elif defined(X86_64) |
| 1063 | if (upeek(tcp->pid, 8*RIP, &pc) < 0) |
| 1064 | return -1; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1065 | #elif defined(IA64) |
| 1066 | if (upeek(tcp->pid, PT_B0, &pc) < 0) |
| 1067 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1068 | #elif defined(ARM) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1069 | if (upeek(tcp->pid, 4*15, &pc) < 0) |
| 1070 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1071 | #elif defined(POWERPC) |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 1072 | if (upeek(tcp->pid, sizeof(unsigned long)*PT_NIP, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1073 | return -1; |
Roland McGrath | 84fa923 | 2005-06-08 18:06:22 +0000 | [diff] [blame] | 1074 | #elif defined(M68K) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1075 | if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) |
| 1076 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1077 | #elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1078 | if (upeek(tcp->pid, REG_PC, &pc) < 0) |
| 1079 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1080 | #elif defined(MIPS) |
| 1081 | if (upeek(tcp->pid, REG_EPC, &pc) < 0) |
| 1082 | return -1; |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1083 | #elif defined(SPARC) || defined(SPARC64) |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1084 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1085 | if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) |
| 1086 | return -1; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1087 | pc = regs.r_pc; |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1088 | #elif defined(S390) || defined(S390X) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1089 | if(upeek(tcp->pid,PT_PSWADDR,&pc) < 0) |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1090 | return -1; |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1091 | #elif defined(HPPA) |
| 1092 | if(upeek(tcp->pid,PT_IAOQ0,&pc) < 0) |
| 1093 | return -1; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1094 | #elif defined(SH) |
| 1095 | if (upeek(tcp->pid, 4*REG_PC ,&pc) < 0) |
| 1096 | return -1; |
Roland McGrath | f5a4777 | 2003-06-26 22:40:42 +0000 | [diff] [blame] | 1097 | #elif defined(SH64) |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1098 | if (upeek(tcp->pid, REG_PC ,&pc) < 0) |
| 1099 | return -1; |
Wichert Akkerman | 7a0b649 | 1999-12-23 15:08:17 +0000 | [diff] [blame] | 1100 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1101 | return pc; |
| 1102 | #endif /* LINUX */ |
| 1103 | |
| 1104 | #ifdef SUNOS4 |
| 1105 | /* |
| 1106 | * Return current program counter for `pid' |
| 1107 | * Assumes PC is never 0xffffffff |
| 1108 | */ |
| 1109 | struct regs regs; |
| 1110 | |
| 1111 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) { |
| 1112 | perror("getpc: ptrace(PTRACE_GETREGS, ...)"); |
| 1113 | return -1; |
| 1114 | } |
| 1115 | return regs.r_pc; |
| 1116 | #endif /* SUNOS4 */ |
| 1117 | |
| 1118 | #ifdef SVR4 |
| 1119 | /* XXX */ |
| 1120 | return 0; |
| 1121 | #endif /* SVR4 */ |
| 1122 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1123 | #ifdef FREEBSD |
| 1124 | struct reg regs; |
| 1125 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 1126 | return regs.r_eip; |
| 1127 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1128 | } |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 1129 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1130 | |
| 1131 | void |
| 1132 | printcall(tcp) |
| 1133 | struct tcb *tcp; |
| 1134 | { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1135 | #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \ |
| 1136 | sizeof(long) == 8 ? "[????????????????] " : \ |
| 1137 | NULL /* crash */) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1138 | |
| 1139 | #ifdef LINUX |
| 1140 | #ifdef I386 |
| 1141 | long eip; |
| 1142 | |
| 1143 | if (upeek(tcp->pid, 4*EIP, &eip) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1144 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1145 | return; |
| 1146 | } |
| 1147 | tprintf("[%08lx] ", eip); |
Roland McGrath | eac26fc | 2005-02-02 02:48:53 +0000 | [diff] [blame] | 1148 | |
| 1149 | #elif defined(S390) || defined(S390X) |
| 1150 | long psw; |
| 1151 | if(upeek(tcp->pid,PT_PSWADDR,&psw) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1152 | PRINTBADPC; |
Roland McGrath | eac26fc | 2005-02-02 02:48:53 +0000 | [diff] [blame] | 1153 | return; |
| 1154 | } |
| 1155 | #ifdef S390 |
| 1156 | tprintf("[%08lx] ", psw); |
| 1157 | #elif S390X |
| 1158 | tprintf("[%16lx] ", psw); |
| 1159 | #endif |
| 1160 | |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1161 | #elif defined(X86_64) |
| 1162 | long rip; |
| 1163 | |
| 1164 | if (upeek(tcp->pid, 8*RIP, &rip) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1165 | PRINTBADPC; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1166 | return; |
| 1167 | } |
| 1168 | tprintf("[%16lx] ", rip); |
Roland McGrath | ef38868 | 2003-06-03 23:28:59 +0000 | [diff] [blame] | 1169 | #elif defined(IA64) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1170 | long ip; |
| 1171 | |
| 1172 | if (upeek(tcp->pid, PT_B0, &ip) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1173 | PRINTBADPC; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1174 | return; |
| 1175 | } |
| 1176 | tprintf("[%08lx] ", ip); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1177 | #elif defined(POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1178 | long pc; |
| 1179 | |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 1180 | if (upeek(tcp->pid, sizeof(unsigned long)*PT_NIP, &pc) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1181 | tprintf ("[????????] "); |
| 1182 | return; |
| 1183 | } |
| 1184 | tprintf("[%08lx] ", pc); |
Roland McGrath | 84fa923 | 2005-06-08 18:06:22 +0000 | [diff] [blame] | 1185 | #elif defined(M68K) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1186 | long pc; |
| 1187 | |
| 1188 | if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) { |
| 1189 | tprintf ("[????????] "); |
| 1190 | return; |
| 1191 | } |
| 1192 | tprintf("[%08lx] ", pc); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1193 | #elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1194 | long pc; |
| 1195 | |
| 1196 | if (upeek(tcp->pid, REG_PC, &pc) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1197 | tprintf ("[????????????????] "); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1198 | return; |
| 1199 | } |
| 1200 | tprintf("[%08lx] ", pc); |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1201 | #elif defined(SPARC) || defined(SPARC64) |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1202 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1203 | if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1204 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1205 | return; |
| 1206 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1207 | tprintf("[%08lx] ", regs.r_pc); |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1208 | #elif defined(HPPA) |
| 1209 | long pc; |
| 1210 | |
| 1211 | if(upeek(tcp->pid,PT_IAOQ0,&pc) < 0) { |
| 1212 | tprintf ("[????????] "); |
| 1213 | return; |
| 1214 | } |
| 1215 | tprintf("[%08lx] ", pc); |
Wichert Akkerman | 75c422b | 2001-04-10 10:22:50 +0000 | [diff] [blame] | 1216 | #elif defined(MIPS) |
| 1217 | long pc; |
| 1218 | |
| 1219 | if (upeek(tcp->pid, REG_EPC, &pc) < 0) { |
| 1220 | tprintf ("[????????] "); |
| 1221 | return; |
| 1222 | } |
| 1223 | tprintf("[%08lx] ", pc); |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1224 | #elif defined(SH) |
| 1225 | long pc; |
| 1226 | |
| 1227 | if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) { |
| 1228 | tprintf ("[????????] "); |
| 1229 | return; |
| 1230 | } |
| 1231 | tprintf("[%08lx] ", pc); |
Roland McGrath | f5a4777 | 2003-06-26 22:40:42 +0000 | [diff] [blame] | 1232 | #elif defined(SH64) |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1233 | long pc; |
| 1234 | |
| 1235 | if (upeek(tcp->pid, REG_PC, &pc) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1236 | tprintf ("[????????????????] "); |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1237 | return; |
| 1238 | } |
| 1239 | tprintf("[%08lx] ", pc); |
Roland McGrath | ef38868 | 2003-06-03 23:28:59 +0000 | [diff] [blame] | 1240 | #elif defined(ARM) |
| 1241 | long pc; |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1242 | |
Roland McGrath | ef38868 | 2003-06-03 23:28:59 +0000 | [diff] [blame] | 1243 | if (upeek(tcp->pid, 4*15, &pc) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1244 | PRINTBADPC; |
Roland McGrath | ef38868 | 2003-06-03 23:28:59 +0000 | [diff] [blame] | 1245 | return; |
| 1246 | } |
| 1247 | tprintf("[%08lx] ", pc); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1248 | #endif /* !architecture */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1249 | #endif /* LINUX */ |
| 1250 | |
| 1251 | #ifdef SUNOS4 |
| 1252 | struct regs regs; |
| 1253 | |
| 1254 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) { |
| 1255 | perror("printcall: ptrace(PTRACE_GETREGS, ...)"); |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1256 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1257 | return; |
| 1258 | } |
| 1259 | tprintf("[%08x] ", regs.r_o7); |
| 1260 | #endif /* SUNOS4 */ |
| 1261 | |
| 1262 | #ifdef SVR4 |
| 1263 | /* XXX */ |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1264 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1265 | #endif |
| 1266 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1267 | #ifdef FREEBSD |
| 1268 | struct reg regs; |
| 1269 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 1270 | tprintf("[%08x] ", regs.r_eip); |
| 1271 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1274 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1275 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1276 | #if defined LINUX |
| 1277 | |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1278 | #include "syscall.h" |
| 1279 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1280 | #include <sys/syscall.h> |
| 1281 | #ifndef CLONE_PTRACE |
| 1282 | # define CLONE_PTRACE 0x00002000 |
| 1283 | #endif |
Roland McGrath | d6ff0d5 | 2008-07-18 01:09:44 +0000 | [diff] [blame] | 1284 | #ifndef CLONE_VFORK |
| 1285 | # define CLONE_VFORK 0x00004000 |
| 1286 | #endif |
Roland McGrath | 76989d7 | 2005-06-07 23:21:31 +0000 | [diff] [blame] | 1287 | #ifndef CLONE_STOPPED |
| 1288 | # define CLONE_STOPPED 0x02000000 |
| 1289 | #endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1290 | |
| 1291 | #ifdef IA64 |
| 1292 | |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1293 | /* We don't have fork()/vfork() syscalls on ia64 itself, but the ia32 |
| 1294 | subsystem has them for x86... */ |
| 1295 | #define SYS_fork 2 |
| 1296 | #define SYS_vfork 190 |
| 1297 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1298 | typedef unsigned long *arg_setup_state; |
| 1299 | |
| 1300 | static int |
| 1301 | arg_setup(struct tcb *tcp, arg_setup_state *state) |
| 1302 | { |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1303 | unsigned long cfm, sof, sol; |
| 1304 | long bsp; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1305 | |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1306 | if (ia32) { |
| 1307 | /* Satisfy a false GCC warning. */ |
| 1308 | *state = NULL; |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1309 | return 0; |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1310 | } |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1311 | |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1312 | if (upeek(tcp->pid, PT_AR_BSP, &bsp) < 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1313 | return -1; |
| 1314 | if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0) |
| 1315 | return -1; |
| 1316 | |
| 1317 | sof = (cfm >> 0) & 0x7f; |
| 1318 | sol = (cfm >> 7) & 0x7f; |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1319 | bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1320 | |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1321 | *state = (unsigned long *) bsp; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1322 | return 0; |
| 1323 | } |
| 1324 | |
| 1325 | # define arg_finish_change(tcp, state) 0 |
| 1326 | |
| 1327 | #ifdef SYS_fork |
| 1328 | static int |
| 1329 | get_arg0 (struct tcb *tcp, arg_setup_state *state, long *valp) |
| 1330 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1331 | int ret; |
| 1332 | |
| 1333 | if (ia32) |
| 1334 | ret = upeek (tcp->pid, PT_R11, valp); |
| 1335 | else |
| 1336 | ret = umoven (tcp, |
| 1337 | (unsigned long) ia64_rse_skip_regs(*state, 0), |
| 1338 | sizeof(long), (void *) valp); |
| 1339 | return ret; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | static int |
| 1343 | get_arg1 (struct tcb *tcp, arg_setup_state *state, long *valp) |
| 1344 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1345 | int ret; |
| 1346 | |
| 1347 | if (ia32) |
| 1348 | ret = upeek (tcp->pid, PT_R9, valp); |
| 1349 | else |
| 1350 | ret = umoven (tcp, |
| 1351 | (unsigned long) ia64_rse_skip_regs(*state, 1), |
| 1352 | sizeof(long), (void *) valp); |
| 1353 | return ret; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1354 | } |
| 1355 | #endif |
| 1356 | |
| 1357 | static int |
| 1358 | set_arg0 (struct tcb *tcp, arg_setup_state *state, long val) |
| 1359 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1360 | int req = PTRACE_POKEDATA; |
| 1361 | void *ap; |
| 1362 | |
| 1363 | if (ia32) { |
| 1364 | ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */ |
| 1365 | req = PTRACE_POKEUSER; |
| 1366 | } else |
| 1367 | ap = ia64_rse_skip_regs(*state, 0); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1368 | errno = 0; |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1369 | ptrace(req, tcp->pid, ap, val); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1370 | return errno ? -1 : 0; |
| 1371 | } |
| 1372 | |
| 1373 | static int |
| 1374 | set_arg1 (struct tcb *tcp, arg_setup_state *state, long val) |
| 1375 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1376 | int req = PTRACE_POKEDATA; |
| 1377 | void *ap; |
| 1378 | |
| 1379 | if (ia32) { |
| 1380 | ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */ |
| 1381 | req = PTRACE_POKEUSER; |
| 1382 | } else |
| 1383 | ap = ia64_rse_skip_regs(*state, 1); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1384 | errno = 0; |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1385 | ptrace(req, tcp->pid, ap, val); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1386 | return errno ? -1 : 0; |
| 1387 | } |
| 1388 | |
Roland McGrath | b659f87 | 2008-07-18 01:19:36 +0000 | [diff] [blame] | 1389 | /* ia64 does not return the input arguments from functions (and syscalls) |
| 1390 | according to ia64 RSE (Register Stack Engine) behavior. */ |
| 1391 | |
| 1392 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1393 | # define restore_arg1(tcp, state, val) ((void) (state), 0) |
| 1394 | |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1395 | #elif defined (SPARC) || defined (SPARC64) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1396 | |
| 1397 | typedef struct regs arg_setup_state; |
| 1398 | |
| 1399 | # define arg_setup(tcp, state) \ |
| 1400 | (ptrace (PTRACE_GETREGS, tcp->pid, (char *) (state), 0)) |
| 1401 | # define arg_finish_change(tcp, state) \ |
| 1402 | (ptrace (PTRACE_SETREGS, tcp->pid, (char *) (state), 0)) |
| 1403 | |
| 1404 | # define get_arg0(tcp, state, valp) (*(valp) = (state)->r_o0, 0) |
| 1405 | # define get_arg1(tcp, state, valp) (*(valp) = (state)->r_o1, 0) |
| 1406 | # define set_arg0(tcp, state, val) ((state)->r_o0 = (val), 0) |
| 1407 | # define set_arg1(tcp, state, val) ((state)->r_o1 = (val), 0) |
Roland McGrath | e1df47f | 2003-01-14 09:46:15 +0000 | [diff] [blame] | 1408 | # define restore_arg0(tcp, state, val) 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1409 | |
| 1410 | #else |
| 1411 | |
| 1412 | # if defined S390 || defined S390X |
Roland McGrath | 7b30822 | 2003-01-20 09:04:36 +0000 | [diff] [blame] | 1413 | /* Note: this is only true for the `clone' system call, which handles |
| 1414 | arguments specially. We could as well say that its first two arguments |
| 1415 | are swapped relative to other architectures, but that would just be |
| 1416 | another #ifdef in the calls. */ |
| 1417 | # define arg0_offset PT_GPR3 |
| 1418 | # define arg1_offset PT_ORIGGPR2 |
| 1419 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1420 | # define restore_arg1(tcp, state, val) ((void) (state), 0) |
Roland McGrath | c9dc3c1 | 2004-03-01 20:57:09 +0000 | [diff] [blame] | 1421 | # define arg0_index 1 |
| 1422 | # define arg1_index 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1423 | # elif defined (ALPHA) || defined (MIPS) |
| 1424 | # define arg0_offset REG_A0 |
| 1425 | # define arg1_offset (REG_A0+1) |
| 1426 | # elif defined (POWERPC) |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 1427 | # define arg0_offset (sizeof(unsigned long)*PT_R3) |
| 1428 | # define arg1_offset (sizeof(unsigned long)*PT_R4) |
Roland McGrath | 7b30822 | 2003-01-20 09:04:36 +0000 | [diff] [blame] | 1429 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1430 | # elif defined (HPPA) |
| 1431 | # define arg0_offset PT_GR26 |
| 1432 | # define arg1_offset (PT_GR26-4) |
Roland McGrath | 7f33cc3 | 2003-01-10 20:51:00 +0000 | [diff] [blame] | 1433 | # elif defined (X86_64) |
| 1434 | # define arg0_offset ((long)(8*(current_personality ? RBX : RDI))) |
| 1435 | # define arg1_offset ((long)(8*(current_personality ? RCX : RSI))) |
Roland McGrath | ac971c2 | 2003-03-31 01:03:33 +0000 | [diff] [blame] | 1436 | # elif defined (SH) |
| 1437 | # define arg0_offset (4*(REG_REG0+4)) |
| 1438 | # define arg1_offset (4*(REG_REG0+5)) |
Roland McGrath | f5a4777 | 2003-06-26 22:40:42 +0000 | [diff] [blame] | 1439 | # elif defined (SH64) |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1440 | /* ABI defines arg0 & 1 in r2 & r3 */ |
| 1441 | # define arg0_offset (REG_OFFSET+16) |
| 1442 | # define arg1_offset (REG_OFFSET+24) |
| 1443 | # define restore_arg0(tcp, state, val) 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1444 | # else |
| 1445 | # define arg0_offset 0 |
| 1446 | # define arg1_offset 4 |
Roland McGrath | ac971c2 | 2003-03-31 01:03:33 +0000 | [diff] [blame] | 1447 | # if defined ARM |
Roland McGrath | e1df47f | 2003-01-14 09:46:15 +0000 | [diff] [blame] | 1448 | # define restore_arg0(tcp, state, val) 0 |
| 1449 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1450 | # endif |
| 1451 | |
| 1452 | typedef int arg_setup_state; |
| 1453 | |
| 1454 | # define arg_setup(tcp, state) (0) |
| 1455 | # define arg_finish_change(tcp, state) 0 |
| 1456 | # define get_arg0(tcp, cookie, valp) \ |
| 1457 | (upeek ((tcp)->pid, arg0_offset, (valp))) |
| 1458 | # define get_arg1(tcp, cookie, valp) \ |
| 1459 | (upeek ((tcp)->pid, arg1_offset, (valp))) |
| 1460 | |
| 1461 | static int |
| 1462 | set_arg0 (struct tcb *tcp, void *cookie, long val) |
| 1463 | { |
Roland McGrath | ca85b97 | 2005-06-07 23:22:08 +0000 | [diff] [blame] | 1464 | return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | static int |
| 1468 | set_arg1 (struct tcb *tcp, void *cookie, long val) |
| 1469 | { |
| 1470 | return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val); |
| 1471 | } |
| 1472 | |
| 1473 | #endif |
| 1474 | |
Roland McGrath | e1df47f | 2003-01-14 09:46:15 +0000 | [diff] [blame] | 1475 | #ifndef restore_arg0 |
| 1476 | # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val)) |
| 1477 | #endif |
| 1478 | #ifndef restore_arg1 |
| 1479 | # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val)) |
| 1480 | #endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1481 | |
Roland McGrath | 90d0afd | 2004-03-01 21:05:16 +0000 | [diff] [blame] | 1482 | #ifndef arg0_index |
| 1483 | # define arg0_index 0 |
| 1484 | # define arg1_index 1 |
| 1485 | #endif |
| 1486 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1487 | int |
| 1488 | setbpt(tcp) |
| 1489 | struct tcb *tcp; |
| 1490 | { |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1491 | static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone }; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1492 | extern int change_syscall(struct tcb *, int); |
| 1493 | arg_setup_state state; |
| 1494 | |
| 1495 | if (tcp->flags & TCB_BPTSET) { |
| 1496 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1497 | return -1; |
| 1498 | } |
| 1499 | |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1500 | /* |
| 1501 | * It's a silly kludge to initialize this with a search at runtime. |
| 1502 | * But it's better than maintaining another magic thing in the |
| 1503 | * godforsaken tables. |
| 1504 | */ |
| 1505 | if (clone_scno[current_personality] == 0) { |
| 1506 | int i; |
| 1507 | for (i = 0; i < nsyscalls; ++i) |
| 1508 | if (sysent[i].sys_func == sys_clone) { |
| 1509 | clone_scno[current_personality] = i; |
| 1510 | break; |
| 1511 | } |
| 1512 | } |
| 1513 | |
Roland McGrath | 76989d7 | 2005-06-07 23:21:31 +0000 | [diff] [blame] | 1514 | switch (known_scno(tcp)) { |
Roland McGrath | 9383c6c | 2003-01-18 00:19:31 +0000 | [diff] [blame] | 1515 | #ifdef SYS_vfork |
| 1516 | case SYS_vfork: |
| 1517 | #endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1518 | #ifdef SYS_fork |
| 1519 | case SYS_fork: |
Roland McGrath | 9b0982b | 2003-01-18 00:21:51 +0000 | [diff] [blame] | 1520 | #endif |
| 1521 | #if defined SYS_fork || defined SYS_vfork |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1522 | if (arg_setup (tcp, &state) < 0 |
| 1523 | || get_arg0 (tcp, &state, &tcp->inst[0]) < 0 |
| 1524 | || get_arg1 (tcp, &state, &tcp->inst[1]) < 0 |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1525 | || change_syscall(tcp, clone_scno[current_personality]) < 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1526 | || set_arg0 (tcp, &state, CLONE_PTRACE|SIGCHLD) < 0 |
| 1527 | || set_arg1 (tcp, &state, 0) < 0 |
| 1528 | || arg_finish_change (tcp, &state) < 0) |
| 1529 | return -1; |
Roland McGrath | c9dc3c1 | 2004-03-01 20:57:09 +0000 | [diff] [blame] | 1530 | tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD; |
| 1531 | tcp->u_arg[arg1_index] = 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1532 | tcp->flags |= TCB_BPTSET; |
| 1533 | return 0; |
| 1534 | #endif |
| 1535 | |
| 1536 | case SYS_clone: |
| 1537 | #ifdef SYS_clone2 |
| 1538 | case SYS_clone2: |
| 1539 | #endif |
Roland McGrath | d6ff0d5 | 2008-07-18 01:09:44 +0000 | [diff] [blame] | 1540 | /* ia64 calls directly `clone (CLONE_VFORK)' contrary to |
| 1541 | x86 SYS_vfork above. Even on x86 we turn the vfork |
| 1542 | semantics into plain fork - each application must not |
| 1543 | depend on the vfork specifics according to POSIX. We |
| 1544 | would hang waiting for the parent resume otherwise. */ |
| 1545 | if ((arg_setup (tcp, &state) < 0 |
| 1546 | || set_arg0 (tcp, &state, |
| 1547 | (tcp->u_arg[arg0_index] | CLONE_PTRACE) |
| 1548 | & ~CLONE_VFORK) < 0 |
| 1549 | || arg_finish_change (tcp, &state) < 0)) |
| 1550 | return -1; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1551 | tcp->flags |= TCB_BPTSET; |
Roland McGrath | c9dc3c1 | 2004-03-01 20:57:09 +0000 | [diff] [blame] | 1552 | tcp->inst[0] = tcp->u_arg[arg0_index]; |
| 1553 | tcp->inst[1] = tcp->u_arg[arg1_index]; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1554 | return 0; |
| 1555 | |
| 1556 | default: |
| 1557 | fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n", |
| 1558 | tcp->scno, tcp->pid); |
| 1559 | break; |
| 1560 | } |
| 1561 | |
| 1562 | return -1; |
| 1563 | } |
| 1564 | |
| 1565 | int |
| 1566 | clearbpt(tcp) |
| 1567 | struct tcb *tcp; |
| 1568 | { |
| 1569 | arg_setup_state state; |
| 1570 | if (arg_setup (tcp, &state) < 0 |
Roland McGrath | e1df47f | 2003-01-14 09:46:15 +0000 | [diff] [blame] | 1571 | || restore_arg0 (tcp, &state, tcp->inst[0]) < 0 |
| 1572 | || restore_arg1 (tcp, &state, tcp->inst[1]) < 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1573 | || arg_finish_change (tcp, &state)) |
| 1574 | return -1; |
| 1575 | tcp->flags &= ~TCB_BPTSET; |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
| 1579 | #else |
| 1580 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1581 | int |
| 1582 | setbpt(tcp) |
| 1583 | struct tcb *tcp; |
| 1584 | { |
| 1585 | |
| 1586 | #ifdef LINUX |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1587 | #if defined (SPARC) || defined (SPARC64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1588 | /* We simply use the SunOS breakpoint code. */ |
| 1589 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1590 | struct regs regs; |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1591 | unsigned long inst; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1592 | #define LOOPA 0x30800000 /* ba,a 0 */ |
| 1593 | |
| 1594 | if (tcp->flags & TCB_BPTSET) { |
| 1595 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1596 | return -1; |
| 1597 | } |
| 1598 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1599 | perror("setbpt: ptrace(PTRACE_GETREGS, ...)"); |
| 1600 | return -1; |
| 1601 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1602 | tcp->baddr = regs.r_o7 + 8; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1603 | errno = 0; |
| 1604 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0); |
| 1605 | if(errno) { |
| 1606 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
| 1607 | return -1; |
| 1608 | } |
| 1609 | |
| 1610 | /* |
| 1611 | * XXX - BRUTAL MODE ON |
| 1612 | * We cannot set a real BPT in the child, since it will not be |
| 1613 | * traced at the moment it will reach the trap and would probably |
| 1614 | * die with a core dump. |
| 1615 | * Thus, we are force our way in by taking out two instructions |
| 1616 | * and insert an eternal loop instead, in expectance of the SIGSTOP |
| 1617 | * generated by out PTRACE_ATTACH. |
| 1618 | * Of cause, if we evaporate ourselves in the middle of all this... |
| 1619 | */ |
| 1620 | errno = 0; |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1621 | inst = LOOPA; |
| 1622 | #if defined (SPARC64) |
| 1623 | inst <<= 32; |
| 1624 | inst |= (tcp->inst[0] & 0xffffffffUL); |
| 1625 | #endif |
| 1626 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, inst); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1627 | if(errno) { |
| 1628 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
| 1629 | return -1; |
| 1630 | } |
| 1631 | tcp->flags |= TCB_BPTSET; |
| 1632 | |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1633 | #else /* !SPARC && !SPARC64 */ |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1634 | #ifdef IA64 |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1635 | if (ia32) { |
| 1636 | # define LOOP 0x0000feeb |
| 1637 | if (tcp->flags & TCB_BPTSET) { |
| 1638 | fprintf(stderr, "PANIC: bpt already set in pid %u\n", |
| 1639 | tcp->pid); |
| 1640 | return -1; |
| 1641 | } |
| 1642 | if (upeek(tcp->pid, PT_CR_IIP, &tcp->baddr) < 0) |
| 1643 | return -1; |
| 1644 | if (debug) |
| 1645 | fprintf(stderr, "[%d] setting bpt at %lx\n", |
| 1646 | tcp->pid, tcp->baddr); |
| 1647 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, |
| 1648 | (char *) tcp->baddr, 0); |
| 1649 | if (errno) { |
| 1650 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
| 1651 | return -1; |
| 1652 | } |
| 1653 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP); |
| 1654 | if (errno) { |
| 1655 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
| 1656 | return -1; |
| 1657 | } |
| 1658 | tcp->flags |= TCB_BPTSET; |
| 1659 | } else { |
| 1660 | /* |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1661 | * Our strategy here is to replace the bundle that |
| 1662 | * contained the clone() syscall with a bundle of the |
| 1663 | * form: |
| 1664 | * |
| 1665 | * { 1: br 1b; br 1b; br 1b } |
| 1666 | * |
| 1667 | * This ensures that the newly forked child will loop |
| 1668 | * endlessly until we've got a chance to attach to it. |
| 1669 | */ |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1670 | # define LOOP0 0x0000100000000017 |
| 1671 | # define LOOP1 0x4000000000200000 |
| 1672 | unsigned long addr, ipsr; |
| 1673 | pid_t pid; |
| 1674 | |
| 1675 | pid = tcp->pid; |
| 1676 | if (upeek(pid, PT_CR_IPSR, &ipsr) < 0) |
| 1677 | return -1; |
| 1678 | if (upeek(pid, PT_CR_IIP, &addr) < 0) |
| 1679 | return -1; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1680 | /* store "ri" in low two bits */ |
| 1681 | tcp->baddr = addr | ((ipsr >> 41) & 0x3); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1682 | |
| 1683 | errno = 0; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1684 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 0, |
| 1685 | 0); |
| 1686 | tcp->inst[1] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 8, |
| 1687 | 0); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1688 | if (errno) { |
| 1689 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
| 1690 | return -1; |
| 1691 | } |
| 1692 | |
| 1693 | errno = 0; |
| 1694 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, LOOP0); |
| 1695 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, LOOP1); |
| 1696 | if (errno) { |
| 1697 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
| 1698 | return -1; |
| 1699 | } |
| 1700 | tcp->flags |= TCB_BPTSET; |
| 1701 | } |
| 1702 | #else /* !IA64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1703 | |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1704 | #if defined (I386) || defined(X86_64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1705 | #define LOOP 0x0000feeb |
| 1706 | #elif defined (M68K) |
| 1707 | #define LOOP 0x60fe0000 |
| 1708 | #elif defined (ALPHA) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1709 | #define LOOP 0xc3ffffff |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1710 | #elif defined (POWERPC) |
Roland McGrath | 1e85cf9 | 2002-12-16 20:40:54 +0000 | [diff] [blame] | 1711 | #define LOOP 0x48000000 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1712 | #elif defined(ARM) |
Wichert Akkerman | 9a8a37f | 1999-12-24 23:19:31 +0000 | [diff] [blame] | 1713 | #define LOOP 0xEAFFFFFE |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1714 | #elif defined(MIPS) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1715 | #define LOOP 0x1000ffff |
| 1716 | #elif defined(S390) |
| 1717 | #define LOOP 0xa7f40000 /* BRC 15,0 */ |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1718 | #elif defined(S390X) |
| 1719 | #define LOOP 0xa7f4000000000000UL /* BRC 15,0 */ |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1720 | #elif defined(HPPA) |
| 1721 | #define LOOP 0xe81f1ff7 /* b,l,n <loc>,r0 */ |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1722 | #elif defined(SH) |
| 1723 | #ifdef __LITTLE_ENDIAN__ |
| 1724 | #define LOOP 0x0000affe |
| 1725 | #else |
| 1726 | #define LOOP 0xfeaf0000 |
| 1727 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1728 | #else |
| 1729 | #error unknown architecture |
| 1730 | #endif |
| 1731 | |
| 1732 | if (tcp->flags & TCB_BPTSET) { |
| 1733 | fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid); |
| 1734 | return -1; |
| 1735 | } |
| 1736 | #if defined (I386) |
| 1737 | if (upeek(tcp->pid, 4*EIP, &tcp->baddr) < 0) |
| 1738 | return -1; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1739 | #elif defined (X86_64) |
| 1740 | if (upeek(tcp->pid, 8*RIP, &tcp->baddr) < 0) |
| 1741 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1742 | #elif defined (M68K) |
| 1743 | if (upeek(tcp->pid, 4*PT_PC, &tcp->baddr) < 0) |
| 1744 | return -1; |
| 1745 | #elif defined (ALPHA) |
| 1746 | return -1; |
| 1747 | #elif defined (ARM) |
| 1748 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1749 | #elif defined (MIPS) |
| 1750 | return -1; /* FIXME: I do not know what i do - Flo */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1751 | #elif defined (POWERPC) |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 1752 | if (upeek(tcp->pid, sizeof(unsigned long)*PT_NIP, &tcp->baddr) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1753 | return -1; |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1754 | #elif defined(S390) || defined(S390X) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1755 | if (upeek(tcp->pid,PT_PSWADDR, &tcp->baddr) < 0) |
| 1756 | return -1; |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1757 | #elif defined(HPPA) |
| 1758 | if (upeek(tcp->pid, PT_IAOQ0, &tcp->baddr) < 0) |
| 1759 | return -1; |
| 1760 | tcp->baddr &= ~0x03; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1761 | #elif defined(SH) |
| 1762 | if (upeek(tcp->pid, 4*REG_PC, &tcp->baddr) < 0) |
| 1763 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1764 | #else |
| 1765 | #error unknown architecture |
| 1766 | #endif |
| 1767 | if (debug) |
| 1768 | fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr); |
| 1769 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0); |
| 1770 | if (errno) { |
| 1771 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
| 1772 | return -1; |
| 1773 | } |
| 1774 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP); |
| 1775 | if (errno) { |
| 1776 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
| 1777 | return -1; |
| 1778 | } |
| 1779 | tcp->flags |= TCB_BPTSET; |
| 1780 | |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1781 | #endif /* !IA64 */ |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1782 | #endif /* SPARC || SPARC64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1783 | #endif /* LINUX */ |
| 1784 | |
| 1785 | #ifdef SUNOS4 |
| 1786 | #ifdef SPARC /* This code is slightly sparc specific */ |
| 1787 | |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 1788 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1789 | #define BPT 0x91d02001 /* ta 1 */ |
| 1790 | #define LOOP 0x10800000 /* ba 0 */ |
| 1791 | #define LOOPA 0x30800000 /* ba,a 0 */ |
| 1792 | #define NOP 0x01000000 |
| 1793 | #if LOOPA |
| 1794 | static int loopdeloop[1] = {LOOPA}; |
| 1795 | #else |
| 1796 | static int loopdeloop[2] = {LOOP, NOP}; |
| 1797 | #endif |
| 1798 | |
| 1799 | if (tcp->flags & TCB_BPTSET) { |
| 1800 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1801 | return -1; |
| 1802 | } |
| 1803 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1804 | perror("setbpt: ptrace(PTRACE_GETREGS, ...)"); |
| 1805 | return -1; |
| 1806 | } |
| 1807 | tcp->baddr = regs.r_o7 + 8; |
| 1808 | if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr, |
| 1809 | sizeof tcp->inst, (char *)tcp->inst) < 0) { |
| 1810 | perror("setbpt: ptrace(PTRACE_READTEXT, ...)"); |
| 1811 | return -1; |
| 1812 | } |
| 1813 | |
| 1814 | /* |
| 1815 | * XXX - BRUTAL MODE ON |
| 1816 | * We cannot set a real BPT in the child, since it will not be |
| 1817 | * traced at the moment it will reach the trap and would probably |
| 1818 | * die with a core dump. |
| 1819 | * Thus, we are force our way in by taking out two instructions |
| 1820 | * and insert an eternal loop in stead, in expectance of the SIGSTOP |
| 1821 | * generated by out PTRACE_ATTACH. |
| 1822 | * Of cause, if we evaporate ourselves in the middle of all this... |
| 1823 | */ |
| 1824 | if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr, |
| 1825 | sizeof loopdeloop, (char *) loopdeloop) < 0) { |
| 1826 | perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)"); |
| 1827 | return -1; |
| 1828 | } |
| 1829 | tcp->flags |= TCB_BPTSET; |
| 1830 | |
| 1831 | #endif /* SPARC */ |
| 1832 | #endif /* SUNOS4 */ |
| 1833 | |
| 1834 | return 0; |
| 1835 | } |
| 1836 | |
| 1837 | int |
| 1838 | clearbpt(tcp) |
| 1839 | struct tcb *tcp; |
| 1840 | { |
| 1841 | |
| 1842 | #ifdef LINUX |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1843 | #if defined(I386) || defined(X86_64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1844 | long eip; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1845 | #elif defined(POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1846 | long pc; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1847 | #elif defined(M68K) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1848 | long pc; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1849 | #elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1850 | long pc; |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1851 | #elif defined(HPPA) |
| 1852 | long iaoq; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1853 | #elif defined(SH) |
| 1854 | long pc; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1855 | #endif /* architecture */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1856 | |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1857 | #if defined (SPARC) || defined (SPARC64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1858 | /* Again, we borrow the SunOS breakpoint code. */ |
| 1859 | if (!(tcp->flags & TCB_BPTSET)) { |
| 1860 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 1861 | return -1; |
| 1862 | } |
| 1863 | errno = 0; |
| 1864 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]); |
| 1865 | if(errno) { |
| 1866 | perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)"); |
| 1867 | return -1; |
| 1868 | } |
| 1869 | tcp->flags &= ~TCB_BPTSET; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1870 | #elif defined(IA64) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1871 | if (ia32) { |
| 1872 | unsigned long addr; |
| 1873 | |
| 1874 | if (debug) |
| 1875 | fprintf(stderr, "[%d] clearing bpt\n", tcp->pid); |
| 1876 | if (!(tcp->flags & TCB_BPTSET)) { |
| 1877 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 1878 | return -1; |
| 1879 | } |
| 1880 | errno = 0; |
| 1881 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]); |
| 1882 | if (errno) { |
| 1883 | perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)"); |
| 1884 | return -1; |
| 1885 | } |
| 1886 | tcp->flags &= ~TCB_BPTSET; |
| 1887 | |
| 1888 | if (upeek(tcp->pid, PT_CR_IIP, &addr) < 0) |
| 1889 | return -1; |
| 1890 | if (addr != tcp->baddr) { |
| 1891 | /* The breakpoint has not been reached yet. */ |
| 1892 | if (debug) |
| 1893 | fprintf(stderr, |
| 1894 | "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1895 | addr, tcp->baddr); |
| 1896 | return 0; |
| 1897 | } |
| 1898 | } else { |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1899 | unsigned long addr, ipsr; |
| 1900 | pid_t pid; |
| 1901 | |
| 1902 | pid = tcp->pid; |
| 1903 | |
| 1904 | if (upeek(pid, PT_CR_IPSR, &ipsr) < 0) |
| 1905 | return -1; |
| 1906 | if (upeek(pid, PT_CR_IIP, &addr) < 0) |
| 1907 | return -1; |
| 1908 | |
| 1909 | /* restore original bundle: */ |
| 1910 | errno = 0; |
| 1911 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, tcp->inst[0]); |
| 1912 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, tcp->inst[1]); |
| 1913 | if (errno) { |
| 1914 | perror("clearbpt: ptrace(PTRACE_POKETEXT, ...)"); |
| 1915 | return -1; |
| 1916 | } |
| 1917 | |
| 1918 | /* restore original "ri" in ipsr: */ |
| 1919 | ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41); |
| 1920 | errno = 0; |
| 1921 | ptrace(PTRACE_POKEUSER, pid, (char *) PT_CR_IPSR, ipsr); |
| 1922 | if (errno) { |
| 1923 | perror("clrbpt: ptrace(PTRACE_POKEUSER, ...)"); |
| 1924 | return -1; |
| 1925 | } |
| 1926 | |
| 1927 | tcp->flags &= ~TCB_BPTSET; |
| 1928 | |
| 1929 | if (addr != (tcp->baddr & ~0x3)) { |
| 1930 | /* the breakpoint has not been reached yet. */ |
| 1931 | if (debug) |
| 1932 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1933 | addr, tcp->baddr); |
| 1934 | return 0; |
| 1935 | } |
| 1936 | } |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1937 | #else /* !IA64 && !SPARC && !SPARC64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1938 | |
| 1939 | if (debug) |
| 1940 | fprintf(stderr, "[%d] clearing bpt\n", tcp->pid); |
| 1941 | if (!(tcp->flags & TCB_BPTSET)) { |
| 1942 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 1943 | return -1; |
| 1944 | } |
| 1945 | errno = 0; |
| 1946 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]); |
| 1947 | if (errno) { |
| 1948 | perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)"); |
| 1949 | return -1; |
| 1950 | } |
| 1951 | tcp->flags &= ~TCB_BPTSET; |
| 1952 | |
| 1953 | #ifdef I386 |
| 1954 | if (upeek(tcp->pid, 4*EIP, &eip) < 0) |
| 1955 | return -1; |
| 1956 | if (eip != tcp->baddr) { |
| 1957 | /* The breakpoint has not been reached yet. */ |
| 1958 | if (debug) |
| 1959 | fprintf(stderr, |
| 1960 | "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1961 | eip, tcp->baddr); |
| 1962 | return 0; |
| 1963 | } |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1964 | #elif defined(X86_64) |
| 1965 | if (upeek(tcp->pid, 8*RIP, &eip) < 0) |
| 1966 | return -1; |
| 1967 | if (eip != tcp->baddr) { |
| 1968 | /* The breakpoint has not been reached yet. */ |
| 1969 | if (debug) |
| 1970 | fprintf(stderr, |
| 1971 | "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1972 | eip, tcp->baddr); |
| 1973 | return 0; |
| 1974 | } |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 1975 | #elif defined(POWERPC) |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 1976 | if (upeek(tcp->pid, sizeof(unsigned long)*PT_NIP, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1977 | return -1; |
| 1978 | if (pc != tcp->baddr) { |
| 1979 | /* The breakpoint has not been reached yet. */ |
| 1980 | if (debug) |
| 1981 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1982 | pc, tcp->baddr); |
| 1983 | return 0; |
| 1984 | } |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1985 | #elif defined(M68K) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1986 | if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) |
| 1987 | return -1; |
| 1988 | if (pc != tcp->baddr) { |
| 1989 | /* The breakpoint has not been reached yet. */ |
| 1990 | if (debug) |
| 1991 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1992 | pc, tcp->baddr); |
| 1993 | return 0; |
| 1994 | } |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1995 | #elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1996 | if (upeek(tcp->pid, REG_PC, &pc) < 0) |
| 1997 | return -1; |
| 1998 | if (pc != tcp->baddr) { |
| 1999 | /* The breakpoint has not been reached yet. */ |
| 2000 | if (debug) |
| 2001 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2002 | pc, tcp->baddr); |
| 2003 | return 0; |
| 2004 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 2005 | #elif defined(HPPA) |
| 2006 | if (upeek(tcp->pid, PT_IAOQ0, &iaoq) < 0) |
| 2007 | return -1; |
| 2008 | iaoq &= ~0x03; |
| 2009 | if (iaoq != tcp->baddr && iaoq != tcp->baddr + 4) { |
| 2010 | /* The breakpoint has not been reached yet. */ |
| 2011 | if (debug) |
| 2012 | fprintf(stderr, "NOTE: PC not at bpt (iaoq %#lx baddr %#lx)\n", |
| 2013 | iaoq, tcp->baddr); |
| 2014 | return 0; |
| 2015 | } |
| 2016 | iaoq = tcp->baddr | 3; |
| 2017 | /* We should be pointing at a 'ldi -1000,r1' in glibc, so it is |
| 2018 | * safe to set both IAOQ0 and IAOQ1 to that so the PSW N bit |
| 2019 | * has no significant effect. |
| 2020 | */ |
| 2021 | ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ0, iaoq); |
| 2022 | ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ1, iaoq); |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 2023 | #elif defined(SH) |
| 2024 | if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) |
| 2025 | return -1; |
| 2026 | if (pc != tcp->baddr) { |
| 2027 | /* The breakpoint has not been reached yet. */ |
| 2028 | if (debug) |
| 2029 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2030 | pc, tcp->baddr); |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 2034 | #endif /* arch */ |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 2035 | #endif /* !SPARC && !SPARC64 && !IA64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2036 | #endif /* LINUX */ |
| 2037 | |
| 2038 | #ifdef SUNOS4 |
| 2039 | #ifdef SPARC |
| 2040 | |
| 2041 | #if !LOOPA |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 2042 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2043 | #endif |
| 2044 | |
| 2045 | if (!(tcp->flags & TCB_BPTSET)) { |
| 2046 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 2047 | return -1; |
| 2048 | } |
| 2049 | if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr, |
| 2050 | sizeof tcp->inst, (char *) tcp->inst) < 0) { |
| 2051 | perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)"); |
| 2052 | return -1; |
| 2053 | } |
| 2054 | tcp->flags &= ~TCB_BPTSET; |
| 2055 | |
| 2056 | #if !LOOPA |
| 2057 | /* |
| 2058 | * Since we don't have a single instruction breakpoint, we may have |
| 2059 | * to adjust the program counter after removing the our `breakpoint'. |
| 2060 | */ |
| 2061 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 2062 | perror("clearbpt: ptrace(PTRACE_GETREGS, ...)"); |
| 2063 | return -1; |
| 2064 | } |
| 2065 | if ((regs.r_pc < tcp->baddr) || |
| 2066 | (regs.r_pc > tcp->baddr + 4)) { |
| 2067 | /* The breakpoint has not been reached yet */ |
| 2068 | if (debug) |
| 2069 | fprintf(stderr, |
| 2070 | "NOTE: PC not at bpt (pc %#x baddr %#x)\n", |
| 2071 | regs.r_pc, tcp->parent->baddr); |
| 2072 | return 0; |
| 2073 | } |
| 2074 | if (regs.r_pc != tcp->baddr) |
| 2075 | if (debug) |
| 2076 | fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n", |
| 2077 | regs.r_pc, tcp->baddr); |
| 2078 | |
| 2079 | regs.r_pc = tcp->baddr; |
| 2080 | if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 2081 | perror("clearbpt: ptrace(PTRACE_SETREGS, ...)"); |
| 2082 | return -1; |
| 2083 | } |
| 2084 | #endif /* LOOPA */ |
| 2085 | #endif /* SPARC */ |
| 2086 | #endif /* SUNOS4 */ |
| 2087 | |
| 2088 | return 0; |
| 2089 | } |
| 2090 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 2091 | #endif |
| 2092 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2093 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2094 | |
| 2095 | #ifdef SUNOS4 |
| 2096 | |
| 2097 | static int |
| 2098 | getex(pid, hdr) |
| 2099 | int pid; |
| 2100 | struct exec *hdr; |
| 2101 | { |
| 2102 | int n; |
| 2103 | |
| 2104 | for (n = 0; n < sizeof *hdr; n += 4) { |
| 2105 | long res; |
| 2106 | if (upeek(pid, uoff(u_exdata) + n, &res) < 0) |
| 2107 | return -1; |
| 2108 | memcpy(((char *) hdr) + n, &res, 4); |
| 2109 | } |
| 2110 | if (debug) { |
| 2111 | fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n", |
| 2112 | hdr->a_magic, hdr->a_toolversion, hdr->a_machtype); |
| 2113 | fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n", |
| 2114 | hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry); |
| 2115 | } |
| 2116 | return 0; |
| 2117 | } |
| 2118 | |
| 2119 | int |
| 2120 | fixvfork(tcp) |
| 2121 | struct tcb *tcp; |
| 2122 | { |
| 2123 | int pid = tcp->pid; |
| 2124 | /* |
| 2125 | * Change `vfork' in a freshly exec'ed dynamically linked |
| 2126 | * executable's (internal) symbol table to plain old `fork' |
| 2127 | */ |
| 2128 | |
| 2129 | struct exec hdr; |
| 2130 | struct link_dynamic dyn; |
| 2131 | struct link_dynamic_2 ld; |
| 2132 | char *strtab, *cp; |
| 2133 | |
| 2134 | if (getex(pid, &hdr) < 0) |
| 2135 | return -1; |
| 2136 | if (!hdr.a_dynamic) |
| 2137 | return -1; |
| 2138 | |
| 2139 | if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) { |
| 2140 | fprintf(stderr, "Cannot read DYNAMIC\n"); |
| 2141 | return -1; |
| 2142 | } |
| 2143 | if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) { |
| 2144 | fprintf(stderr, "Cannot read link_dynamic_2\n"); |
| 2145 | return -1; |
| 2146 | } |
| 2147 | if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) { |
Roland McGrath | 46100d0 | 2005-06-01 18:55:42 +0000 | [diff] [blame] | 2148 | fprintf(stderr, "out of memory\n"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2149 | return -1; |
| 2150 | } |
| 2151 | if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr), |
| 2152 | (int)ld.ld_symb_size, strtab) < 0) |
| 2153 | goto err; |
| 2154 | |
| 2155 | #if 0 |
| 2156 | for (cp = strtab; cp < strtab + ld.ld_symb_size; ) { |
| 2157 | fprintf(stderr, "[symbol: %s]\n", cp); |
| 2158 | cp += strlen(cp)+1; |
| 2159 | } |
| 2160 | return 0; |
| 2161 | #endif |
| 2162 | for (cp = strtab; cp < strtab + ld.ld_symb_size; ) { |
| 2163 | if (strcmp(cp, "_vfork") == 0) { |
| 2164 | if (debug) |
| 2165 | fprintf(stderr, "fixvfork: FOUND _vfork\n"); |
| 2166 | strcpy(cp, "_fork"); |
| 2167 | break; |
| 2168 | } |
| 2169 | cp += strlen(cp)+1; |
| 2170 | } |
| 2171 | if (cp < strtab + ld.ld_symb_size) |
| 2172 | /* |
| 2173 | * Write entire symbol table back to avoid |
| 2174 | * memory alignment bugs in ptrace |
| 2175 | */ |
| 2176 | if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr), |
| 2177 | (int)ld.ld_symb_size, strtab) < 0) |
| 2178 | goto err; |
| 2179 | |
| 2180 | free(strtab); |
| 2181 | return 0; |
| 2182 | |
| 2183 | err: |
| 2184 | free(strtab); |
| 2185 | return -1; |
| 2186 | } |
| 2187 | |
| 2188 | #endif /* SUNOS4 */ |