blob: 6a095597f06f37592e83eb51937a2027ac2db902 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
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 Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * 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 Akkerman76baf7c1999-02-19 00:21:36 +00009 * 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 McGrathd81f1d92003-01-09 06:53:34 +000038#include <signal.h>
39#include <sys/syscall.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000040#include <sys/user.h>
41#include <sys/param.h>
42#include <fcntl.h>
John Hughes1d08dcf2001-07-10 13:48:44 +000043#if HAVE_SYS_UIO_H
44#include <sys/uio.h>
45#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000046#ifdef SUNOS4
47#include <machine/reg.h>
48#include <a.out.h>
49#include <link.h>
50#endif /* SUNOS4 */
Wichert Akkerman36915a11999-07-13 15:45:02 +000051
Wichert Akkerman43a74822000-06-27 17:33:32 +000052#if defined(linux) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1))
Wichert Akkerman36915a11999-07-13 15:45:02 +000053#include <linux/ptrace.h>
Roland McGrath1e85cf92002-12-16 20:40:54 +000054#endif
Wichert Akkerman36915a11999-07-13 15:45:02 +000055
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000056#if defined(LINUX) && defined(IA64)
Roland McGrathd81f1d92003-01-09 06:53:34 +000057# include <asm/ptrace_offsets.h>
58# include <asm/rse.h>
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000059#endif
60
Wichert Akkerman36915a11999-07-13 15:45:02 +000061#ifdef HAVE_SYS_REG_H
62#include <sys/reg.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000063# define PTRACE_PEEKUSR PTRACE_PEEKUSER
Wichert Akkermanfaf72222000-02-19 23:59:03 +000064#elif defined(HAVE_LINUX_PTRACE_H)
65#undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000066# 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 Akkermanfaf72222000-02-19 23:59:03 +000072#include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000073# undef ia64_fpreg
74# undef pt_all_user_regs
Wichert Akkerman2e2553a1999-05-09 00:29:58 +000075#endif
76
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000077#ifdef SUNOS4_KERNEL_ARCH_KLUDGE
78#include <sys/utsname.h>
79#endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
80
Roland McGrath6d1a65c2004-07-12 07:44:08 +000081#if defined(LINUXSPARC)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000082
Roland McGrath4db26242003-01-30 20:15:19 +000083# 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 Akkerman9ce1a631999-08-29 23:15:07 +000090
Roland McGrath6d1a65c2004-07-12 07:44:08 +000091#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 Akkerman9ce1a631999-08-29 23:15:07 +000099#if !defined(__GLIBC__)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000100
101#include <linux/unistd.h>
102
103#define _hack_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\
104 type5,arg5,syscall) \
105type 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 McGrath6d1a65c2004-07-12 07:44:08 +0000115#if defined (SPARC64)
116 "t 0x6d\n\t" \
117#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000118 "t 0x10\n\t" \
Roland McGrath6d1a65c2004-07-12 07:44:08 +0000119#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000120 "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"); \
129if (__res>=0) \
Denys Vlasenkoadedb512008-12-30 18:47:55 +0000130 return (type) __res; \
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000131errno = -__res; \
132return -1; \
133}
134
135static _hack_syscall5(int,_ptrace,int,__request,int,__pid,int,__addr,int,__data,int,__addr2,ptrace)
136
137#define _ptrace
138
139#endif
140
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000141#endif
142
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000143/* 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 McGratha4d48532005-06-08 20:45:28 +0000151#if 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000152void
153tv_tv(tv, a, b)
154struct timeval *tv;
155int a;
156int b;
157{
158 tv->tv_sec = a;
159 tv->tv_usec = b;
160}
Roland McGratha4d48532005-06-08 20:45:28 +0000161#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000162
163int
164tv_nz(a)
165struct timeval *a;
166{
167 return a->tv_sec || a->tv_usec;
168}
169
170int
171tv_cmp(a, b)
172struct 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
183double
184tv_float(tv)
185struct timeval *tv;
186{
187 return tv->tv_sec + tv->tv_usec/1000000.0;
188}
189
190void
191tv_add(tv, a, b)
192struct 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 McGrath58372f52007-07-24 01:38:22 +0000196 if (tv->tv_usec >= 1000000) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000197 tv->tv_sec++;
198 tv->tv_usec -= 1000000;
199 }
200}
201
202void
203tv_sub(tv, a, b)
204struct 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
214void
215tv_div(tv, a, n)
216struct timeval *tv, *a;
217int 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
224void
225tv_mul(tv, a, n)
226struct timeval *tv, *a;
227int n;
228{
229 tv->tv_usec = a->tv_usec * n;
Dmitry V. Levinfefdd972007-06-29 21:25:56 +0000230 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000231 tv->tv_usec %= 1000000;
232}
233
Dmitry V. Levinab9008b2007-01-11 22:05:04 +0000234const char *
235xlookup(const struct xlat *xlat, int val)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000236{
237 for (; xlat->str != NULL; xlat++)
238 if (xlat->val == val)
239 return xlat->str;
240 return NULL;
241}
242
243/*
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000244 * Generic ptrace wrapper which tracks ptrace errors
245 * by setting tcp->ptrace_errno.
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000246 *
247 * We assume that ESRCH indicates likely process death (SIGKILL?),
248 * modulo bugs where process somehow ended up not stopped.
249 * Unfortunately kernel uses ESRCH for that case too. Oh well.
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000250 */
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000251static const char *
252str_PTRACE_xxx(int request)
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000253{
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000254 const char *s;
255 static char msg[sizeof(int) * 3 + sizeof("PTRACE_<%d>")];
256
257 s = xlookup(ptrace_cmds, request);
258 if (s)
259 return s;
260 sprintf(msg, "PTRACE_<%d>", request);
261 return msg;
262}
263
264long
265do_ptrace(int request, struct tcb *tcp, void *addr, long data)
266{
267 int err;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000268 long l;
269
270 errno = 0;
271 l = ptrace(request, tcp->pid, addr, data);
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000272 err = errno;
273 if (err) {
274 tcp->ptrace_errno = err;
275 if (err != ESRCH) {
276 fprintf(stderr, "strace: ptrace(%s,%u,%p,%lu): %s\n",
277 str_PTRACE_xxx(request),
278 (int) tcp->pid, addr, data, strerror(err));
279 errno = err; /* fprintf can clobber it, restore */
280 }
281 return -1;
282 }
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000283 return l;
284}
285
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000286static long
287do_ptrace_peekdata(struct tcb *tcp, void *addr, int started)
288{
289 int err;
290 long l;
291
292 errno = 0;
293 l = ptrace(PTRACE_PEEKDATA, tcp->pid, addr, 0);
294 err = errno;
295 if (err) {
296 if (started && (err == EPERM || err == EIO)) {
297 /* Ran into 'end of memory' - not an error.
298 * NB: errno is nonzero, caller uses this to detect
299 * "end of string" condition.
300 */
301 return 0;
302 }
303 /* If error happens at first call, we have a bogus address. */
304 if (addr != NULL && err != EIO) {
305 if (err != ESRCH) {
306 fprintf(stderr, "strace: ptrace(PTRACE_PEEKDATA,%u,%p,0): %s\n",
307 (int) tcp->pid, addr, strerror(err));
308 errno = err; /* fprintf can clobber it, restore */
309 }
310 tcp->ptrace_errno = err;
311 return -1;
312 }
313 }
314 return l;
315}
316
317#ifdef SUNOS4
318static long
319do_ptrace5(int request, struct tcb *tcp, void *addr, long data, char *data2)
320{
321 int err;
322 long l;
323
324 errno = 0;
325 l = ptrace(request, tcp->pid, addr, data, data2);
326 err = errno;
327 if (err) {
328 tcp->ptrace_errno = err;
329 if (err != ESRCH) {
330 fprintf(stderr, "strace: ptrace(%s,%u,%p,%lu,%p): %s\n",
331 str_PTRACE_xxx(request),
332 (int) tcp->pid, addr, data, data2, strerror(err));
333 errno = err; /* fprintf can clobber it, restore */
334 }
335 return -1;
336 }
337 return l;
338}
339#endif
340
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000341/*
342 * Used when we want to unblock stopped traced process.
343 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
344 * Returns 0 on success or if error was ESRCH
345 * (presumably process was killed while we talk to it).
346 * Otherwise prints error message and returns -1.
347 */
348int
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000349ptrace_restart(int request, struct tcb *tcp, int sig)
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000350{
351 int err;
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000352
353 errno = 0;
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000354 ptrace(request, tcp->pid, (void *) 1, (long) sig);
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000355 err = errno;
356 if (!err || err == ESRCH)
357 return 0;
358
359 tcp->ptrace_errno = err;
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000360 fprintf(stderr, "strace: ptrace(%s,%u,1,%d): %s\n",
361 str_PTRACE_xxx(request),
362 (int)tcp->pid, sig, strerror(err));
363 errno = err; /* fprintf can clobber it, restore */
Denys Vlasenko732d1bf2008-12-17 19:21:59 +0000364 return -1;
365}
366
367/*
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000368 * Print entry in struct xlat table, if there.
369 */
370void
Dmitry V. Levinab9008b2007-01-11 22:05:04 +0000371printxval(const struct xlat *xlat, int val, const char *dflt)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000372{
Dmitry V. Levinab9008b2007-01-11 22:05:04 +0000373 const char *str = xlookup(xlat, val);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000374
375 if (str)
376 tprintf("%s", str);
377 else
378 tprintf("%#x /* %s */", val, dflt);
379}
380
381/*
382 * Interpret `xlat' as an array of flags
383 * print the entries whose bits are on in `flags'
384 * return # of flags printed.
385 */
386int
387addflags(xlat, flags)
Roland McGrathd9f816f2004-09-04 03:39:20 +0000388const struct xlat *xlat;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000389int flags;
390{
391 int n;
392
393 for (n = 0; xlat->str; xlat++) {
394 if (xlat->val && (flags & xlat->val) == xlat->val) {
395 tprintf("|%s", xlat->str);
396 flags &= ~xlat->val;
397 n++;
398 }
399 }
400 if (flags) {
401 tprintf("|%#x", flags);
402 n++;
403 }
404 return n;
405}
406
Roland McGratha6c0d8c2007-11-01 21:46:22 +0000407/*
408 * Interpret `xlat' as an array of flags/
409 * Print to static string the entries whose bits are on in `flags'
410 * Return static string.
411 */
412const char *
413sprintflags(const char *prefix, const struct xlat *xlat, int flags)
414{
415 static char outstr[1024];
416 int found = 0;
417
418 strcpy(outstr, prefix);
419
420 for (; xlat->str; xlat++) {
421 if ((flags & xlat->val) == xlat->val) {
422 if (found)
423 strcat(outstr, "|");
424 strcat(outstr, xlat->str);
425 flags &= ~xlat->val;
426 found = 1;
427 }
428 }
429 if (flags) {
430 if (found)
431 strcat(outstr, "|");
432 sprintf(outstr + strlen(outstr), "%#x", flags);
433 }
434
435 return outstr;
436}
437
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000438int
Roland McGrathb2dee132005-06-01 19:02:36 +0000439printflags(xlat, flags, dflt)
Roland McGrathd9f816f2004-09-04 03:39:20 +0000440const struct xlat *xlat;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000441int flags;
Roland McGrathb2dee132005-06-01 19:02:36 +0000442const char *dflt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000443{
444 int n;
445 char *sep;
446
447 if (flags == 0 && xlat->val == 0) {
448 tprintf("%s", xlat->str);
449 return 1;
450 }
451
452 sep = "";
453 for (n = 0; xlat->str; xlat++) {
454 if (xlat->val && (flags & xlat->val) == xlat->val) {
455 tprintf("%s%s", sep, xlat->str);
456 flags &= ~xlat->val;
457 sep = "|";
458 n++;
459 }
460 }
Roland McGrathb2dee132005-06-01 19:02:36 +0000461
462 if (n) {
463 if (flags) {
464 tprintf("%s%#x", sep, flags);
465 n++;
466 }
467 } else {
468 if (flags) {
469 tprintf("%#x", flags);
470 if (dflt)
471 tprintf(" /* %s */", dflt);
472 } else {
473 if (dflt)
474 tprintf("0");
475 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000476 }
Roland McGrathb2dee132005-06-01 19:02:36 +0000477
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000478 return n;
479}
480
481void
482printnum(tcp, addr, fmt)
483struct tcb *tcp;
484long addr;
485char *fmt;
486{
Roland McGratheb285352003-01-14 09:59:00 +0000487 long num;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000488
489 if (!addr) {
490 tprintf("NULL");
491 return;
492 }
493 if (umove(tcp, addr, &num) < 0) {
494 tprintf("%#lx", addr);
495 return;
496 }
497 tprintf("[");
498 tprintf(fmt, num);
499 tprintf("]");
500}
501
Roland McGrath6bc12202003-11-13 22:32:27 +0000502void
Roland McGrath9814a942005-07-04 23:28:10 +0000503printnum_int(tcp, addr, fmt)
504struct tcb *tcp;
505long addr;
506char *fmt;
507{
508 int num;
509
510 if (!addr) {
511 tprintf("NULL");
512 return;
513 }
514 if (umove(tcp, addr, &num) < 0) {
515 tprintf("%#lx", addr);
516 return;
517 }
518 tprintf("[");
519 tprintf(fmt, num);
520 tprintf("]");
521}
522
523void
Roland McGrath6bc12202003-11-13 22:32:27 +0000524printuid(text, uid)
525const char *text;
526unsigned long uid;
527{
528 tprintf("%s", text);
529 tprintf((uid == -1) ? "%ld" : "%lu", uid);
530}
531
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000532static char path[MAXPATHLEN + 1];
533
Dmitry V. Levina501f142008-11-10 23:19:13 +0000534/*
535 * Quote string `instr' of length `size'
536 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
537 * If `len' < 0, treat `instr' as a NUL-terminated string
538 * and quote at most (`size' - 1) bytes.
539 */
Roland McGrath6d970322007-11-01 23:53:59 +0000540static int
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000541string_quote(const char *instr, char *outstr, int len, int size)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000542{
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000543 const unsigned char *ustr = (const unsigned char *) instr;
544 char *s = outstr;
545 int usehex = 0, c, i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000546
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000547 if (xflag > 1)
548 usehex = 1;
549 else if (xflag) {
Dmitry V. Levina501f142008-11-10 23:19:13 +0000550 /* Check for presence of symbol which require
551 to hex-quote the whole string. */
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000552 for (i = 0; i < size; ++i) {
553 c = ustr[i];
Dmitry V. Levina501f142008-11-10 23:19:13 +0000554 /* Check for NUL-terminated string. */
555 if (len < 0) {
556 if (c == '\0')
557 break;
558 /* Quote at most size - 1 bytes. */
559 if (i == size - 1)
560 continue;
561 }
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000562 if (!isprint(c) && !isspace(c)) {
563 usehex = 1;
564 break;
565 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000566 }
567 }
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000568
569 *s++ = '\"';
570
571 if (usehex) {
Dmitry V. Levina501f142008-11-10 23:19:13 +0000572 /* Hex-quote the whole string. */
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000573 for (i = 0; i < size; ++i) {
574 c = ustr[i];
Dmitry V. Levina501f142008-11-10 23:19:13 +0000575 /* Check for NUL-terminated string. */
576 if (len < 0) {
577 if (c == '\0')
578 break;
579 /* Quote at most size - 1 bytes. */
580 if (i == size - 1)
581 continue;
582 }
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000583 sprintf(s, "\\x%02x", c);
584 s += 4;
585 }
586 } else {
587 for (i = 0; i < size; ++i) {
588 c = ustr[i];
Dmitry V. Levina501f142008-11-10 23:19:13 +0000589 /* Check for NUL-terminated string. */
590 if (len < 0) {
591 if (c == '\0')
592 break;
593 /* Quote at most size - 1 bytes. */
594 if (i == size - 1)
595 continue;
596 }
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000597 switch (c) {
598 case '\"': case '\\':
599 *s++ = '\\';
600 *s++ = c;
601 break;
602 case '\f':
603 *s++ = '\\';
604 *s++ = 'f';
605 break;
606 case '\n':
607 *s++ = '\\';
608 *s++ = 'n';
609 break;
610 case '\r':
611 *s++ = '\\';
612 *s++ = 'r';
613 break;
614 case '\t':
615 *s++ = '\\';
616 *s++ = 't';
617 break;
618 case '\v':
619 *s++ = '\\';
620 *s++ = 'v';
621 break;
622 default:
623 if (isprint(c))
624 *s++ = c;
625 else if (i + 1 < size
626 && isdigit(ustr[i + 1])) {
627 sprintf(s, "\\%03o", c);
628 s += 4;
629 } else {
630 sprintf(s, "\\%o", c);
631 s += strlen(s);
632 }
633 break;
634 }
635 }
636 }
637
638 *s++ = '\"';
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000639 *s = '\0';
Roland McGrath6d970322007-11-01 23:53:59 +0000640
641 /* Return nonzero if the string was unterminated. */
642 return i == size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000643}
644
Dmitry V. Levina501f142008-11-10 23:19:13 +0000645/*
646 * Print path string specified by address `addr' and length `n'.
647 * If path length exceeds `n', append `...' to the output.
648 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000649void
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000650printpathn(struct tcb *tcp, long addr, int n)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000651{
Dmitry V. Levina501f142008-11-10 23:19:13 +0000652 if (!addr) {
Roland McGrath371ed8f2005-02-06 01:55:07 +0000653 tprintf("NULL");
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000654 return;
655 }
656
Dmitry V. Levina501f142008-11-10 23:19:13 +0000657 /* Cap path length to the path buffer size,
658 and NUL-terminate the buffer. */
659 if (n > sizeof path - 1)
660 n = sizeof path - 1;
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000661 path[n] = '\0';
Dmitry V. Levina501f142008-11-10 23:19:13 +0000662
663 /* Fetch one byte more to find out whether path length > n. */
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000664 if (umovestr(tcp, addr, n + 1, path) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000665 tprintf("%#lx", addr);
666 else {
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000667 static char outstr[4*(sizeof path - 1) + sizeof "\"...\""];
668 int trunc = (path[n] != '\0');
669
670 if (trunc)
671 path[n] = '\0';
Dmitry V. Levina501f142008-11-10 23:19:13 +0000672 (void) string_quote(path, outstr, -1, n + 1);
673 if (trunc)
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000674 strcat(outstr, "...");
675 tprintf("%s", outstr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000676 }
677}
678
679void
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000680printpath(struct tcb *tcp, long addr)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000681{
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000682 printpathn(tcp, addr, sizeof path - 1);
683}
684
Dmitry V. Levina501f142008-11-10 23:19:13 +0000685/*
686 * Print string specified by address `addr' and length `len'.
687 * If `len' < 0, treat the string as a NUL-terminated string.
688 * If string length exceeds `max_strlen', append `...' to the output.
689 */
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000690void
691printstr(struct tcb *tcp, long addr, int len)
692{
693 static char *str = NULL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000694 static char *outstr;
Roland McGrath6d970322007-11-01 23:53:59 +0000695 int size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000696
697 if (!addr) {
698 tprintf("NULL");
699 return;
700 }
Dmitry V. Levina501f142008-11-10 23:19:13 +0000701 /* Allocate static buffers if they are not allocated yet. */
702 if (!str)
703 str = malloc(max_strlen + 1);
704 if (!outstr)
705 outstr = malloc(4 * max_strlen + sizeof "\"...\"");
706 if (!str || !outstr) {
707 fprintf(stderr, "out of memory\n");
708 tprintf("%#lx", addr);
709 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000710 }
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000711
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000712 if (len < 0) {
Dmitry V. Levina501f142008-11-10 23:19:13 +0000713 /*
714 * Treat as a NUL-terminated string: fetch one byte more
715 * because string_quote() quotes one byte less.
716 */
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000717 size = max_strlen + 1;
Dmitry V. Levina501f142008-11-10 23:19:13 +0000718 str[max_strlen] = '\0';
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000719 if (umovestr(tcp, addr, size, str) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000720 tprintf("%#lx", addr);
721 return;
722 }
723 }
724 else {
Dmitry V. Levina501f142008-11-10 23:19:13 +0000725 size = MIN(len, max_strlen);
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000726 if (umoven(tcp, addr, size, str) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000727 tprintf("%#lx", addr);
728 return;
729 }
730 }
731
Dmitry V. Levina501f142008-11-10 23:19:13 +0000732 if (string_quote(str, outstr, len, size) &&
733 (len < 0 || len > max_strlen))
Dmitry V. Levinbea02032007-10-08 21:48:01 +0000734 strcat(outstr, "...");
Roland McGratha503dcf2007-08-02 02:06:26 +0000735
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000736 tprintf("%s", outstr);
737}
738
John Hughes1d08dcf2001-07-10 13:48:44 +0000739#if HAVE_SYS_UIO_H
740void
741dumpiov(tcp, len, addr)
742struct tcb * tcp;
743int len;
744long addr;
745{
Dmitry V. Levin4ebb4e32006-12-13 17:08:08 +0000746#if defined(LINUX) && SUPPORTED_PERSONALITIES > 1
747 union {
748 struct { u_int32_t base; u_int32_t len; } *iov32;
749 struct { u_int64_t base; u_int64_t len; } *iov64;
750 } iovu;
751#define iov iovu.iov64
752#define sizeof_iov \
753 (personality_wordsize[current_personality] == 4 \
754 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
755#define iov_iov_base(i) \
756 (personality_wordsize[current_personality] == 4 \
757 ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base)
758#define iov_iov_len(i) \
759 (personality_wordsize[current_personality] == 4 \
760 ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len)
761#else
John Hughes1d08dcf2001-07-10 13:48:44 +0000762 struct iovec *iov;
Dmitry V. Levin4ebb4e32006-12-13 17:08:08 +0000763#define sizeof_iov sizeof(*iov)
764#define iov_iov_base(i) iov[i].iov_base
765#define iov_iov_len(i) iov[i].iov_len
766#endif
John Hughes1d08dcf2001-07-10 13:48:44 +0000767 int i;
Roland McGrathaa524c82005-06-01 19:22:06 +0000768 unsigned long size;
John Hughes1d08dcf2001-07-10 13:48:44 +0000769
Dmitry V. Levin4ebb4e32006-12-13 17:08:08 +0000770 size = sizeof_iov * (unsigned long) len;
771 if (size / sizeof_iov != len
772 || (iov = malloc(size)) == NULL) {
Roland McGrathaa524c82005-06-01 19:22:06 +0000773 fprintf(stderr, "out of memory\n");
John Hughes1d08dcf2001-07-10 13:48:44 +0000774 return;
775 }
Roland McGrathaa524c82005-06-01 19:22:06 +0000776 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
John Hughes1d08dcf2001-07-10 13:48:44 +0000777 for (i = 0; i < len; i++) {
Denys Vlasenkoadedb512008-12-30 18:47:55 +0000778 /* include the buffer number to make it easy to
779 * match up the trace with the source */
780 tprintf(" * %lu bytes in buffer %d\n",
781 (unsigned long)iov_iov_len(i), i);
782 dumpstr(tcp, (long) iov_iov_base(i),
783 iov_iov_len(i));
784 }
John Hughes1d08dcf2001-07-10 13:48:44 +0000785 }
786 free((char *) iov);
Dmitry V. Levin4ebb4e32006-12-13 17:08:08 +0000787#undef sizeof_iov
788#undef iov_iov_base
789#undef iov_iov_len
790#undef iov
John Hughes1d08dcf2001-07-10 13:48:44 +0000791}
792#endif
793
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000794void
795dumpstr(tcp, addr, len)
796struct tcb *tcp;
797long addr;
798int len;
799{
800 static int strsize = -1;
801 static unsigned char *str;
802 static char outstr[80];
803 char *s;
804 int i, j;
805
806 if (strsize < len) {
807 if (str)
808 free(str);
809 if ((str = malloc(len)) == NULL) {
Roland McGrath46100d02005-06-01 18:55:42 +0000810 fprintf(stderr, "out of memory\n");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000811 return;
812 }
813 strsize = len;
814 }
815
816 if (umoven(tcp, addr, len, (char *) str) < 0)
817 return;
818
819 for (i = 0; i < len; i += 16) {
820 s = outstr;
821 sprintf(s, " | %05x ", i);
822 s += 9;
823 for (j = 0; j < 16; j++) {
824 if (j == 8)
825 *s++ = ' ';
826 if (i + j < len) {
827 sprintf(s, " %02x", str[i + j]);
828 s += 3;
829 }
830 else {
831 *s++ = ' '; *s++ = ' '; *s++ = ' ';
832 }
833 }
834 *s++ = ' '; *s++ = ' ';
835 for (j = 0; j < 16; j++) {
836 if (j == 8)
837 *s++ = ' ';
838 if (i + j < len) {
839 if (isprint(str[i + j]))
840 *s++ = str[i + j];
841 else
842 *s++ = '.';
843 }
844 else
845 *s++ = ' ';
846 }
847 tprintf("%s |\n", outstr);
848 }
849}
850
851#define PAGMASK (~(PAGSIZ - 1))
852/*
853 * move `len' bytes of data from process `pid'
854 * at address `addr' to our space at `laddr'
855 */
856int
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +0000857umoven(struct tcb *tcp, long addr, int len, char *laddr)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000858{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000859#ifdef LINUX
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000860 int n, m;
Wichert Akkerman5daa0281999-03-15 19:49:42 +0000861 int started = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000862 union {
863 long val;
864 char x[sizeof(long)];
865 } u;
866
867 if (addr & (sizeof(long) - 1)) {
868 /* addr not a multiple of sizeof(long) */
869 n = addr - (addr & -sizeof(long)); /* residue */
870 addr &= -sizeof(long); /* residue */
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000871 u.val = do_ptrace_peekdata(tcp, (char *) addr, started);
872 if (errno)
873 return u.val; /* 0 or -1 */
Wichert Akkerman5daa0281999-03-15 19:49:42 +0000874 started = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000875 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
876 addr += sizeof(long), laddr += m, len -= m;
877 }
878 while (len) {
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000879 u.val = do_ptrace_peekdata(tcp, (char *) addr, started);
880 if (errno)
881 return u.val; /* 0 or -1 */
Wichert Akkerman5daa0281999-03-15 19:49:42 +0000882 started = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000883 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
884 addr += sizeof(long), laddr += m, len -= m;
885 }
886#endif /* LINUX */
887
888#ifdef SUNOS4
889 int pid = tcp->pid;
890#if 0
891 int n, m;
892 union {
893 long val;
894 char x[sizeof(long)];
895 } u;
896
897 if (addr & (sizeof(long) - 1)) {
898 /* addr not a multiple of sizeof(long) */
899 n = addr - (addr & -sizeof(long)); /* residue */
900 addr &= -sizeof(long); /* residue */
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000901 u.val = do_ptrace(PTRACE_PEEKDATA, tcp, (char *) addr, 0);
902 if (errno)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000903 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000904 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
905 addr += sizeof(long), laddr += m, len -= m;
906 }
907 while (len) {
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000908 u.val = do_ptrace(PTRACE_PEEKDATA, tcp, (char *) addr, 0);
909 if (errno)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000910 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000911 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
912 addr += sizeof(long), laddr += m, len -= m;
913 }
914#else /* !oldway */
915 int n;
916
917 while (len) {
918 n = MIN(len, PAGSIZ);
919 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000920 if (do_ptrace5(PTRACE_READDATA, tcp, (char *) addr, len, laddr) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000921 return -1;
922 }
923 len -= n;
924 addr += n;
925 laddr += n;
926 }
927#endif /* !oldway */
928#endif /* SUNOS4 */
929
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000930#ifdef USE_PROCFS
Wichert Akkermanea78f0f1999-11-29 15:34:02 +0000931#ifdef HAVE_MP_PROCFS
John Hughesaa09c6b2001-05-15 14:53:43 +0000932 int fd = tcp->pfd_as;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000933#else
John Hughesaa09c6b2001-05-15 14:53:43 +0000934 int fd = tcp->pfd;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000935#endif
John Hughesaa09c6b2001-05-15 14:53:43 +0000936 lseek(fd, addr, SEEK_SET);
937 if (read(fd, laddr, len) == -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000938 return -1;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000939#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000940
941 return 0;
942}
943
944/*
945 * like `umove' but make the additional effort of looking
946 * for a terminating zero byte.
947 */
948int
Denys Vlasenkoef2fbf82009-01-06 21:45:06 +0000949umovestr(struct tcb *tcp, long addr, int len, char *laddr)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000950{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000951#ifdef USE_PROCFS
John Hughesaa09c6b2001-05-15 14:53:43 +0000952#ifdef HAVE_MP_PROCFS
953 int fd = tcp->pfd_as;
954#else
955 int fd = tcp->pfd;
956#endif
957 /* Some systems (e.g. FreeBSD) can be upset if we read off the
958 end of valid memory, avoid this by trying to read up
959 to page boundaries. But we don't know what a page is (and
960 getpagesize(2) (if it exists) doesn't necessarily return
961 hardware page size). Assume all pages >= 1024 (a-historical
962 I know) */
963
964 int page = 1024; /* How to find this? */
965 int move = page - (addr & (page - 1));
966 int left = len;
967
968 lseek(fd, addr, SEEK_SET);
969
970 while (left) {
971 if (move > left) move = left;
John Hughes9cecf7f2001-10-16 10:20:22 +0000972 if ((move = read(fd, laddr, move)) <= 0)
John Hughesaa09c6b2001-05-15 14:53:43 +0000973 return left != len ? 0 : -1;
974 if (memchr (laddr, 0, move)) break;
975 left -= move;
976 laddr += move;
977 addr += move;
978 move = page;
979 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000980#else /* !USE_PROCFS */
Wichert Akkerman5daa0281999-03-15 19:49:42 +0000981 int started = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000982 int i, n, m;
983 union {
984 long val;
985 char x[sizeof(long)];
986 } u;
987
988 if (addr & (sizeof(long) - 1)) {
989 /* addr not a multiple of sizeof(long) */
990 n = addr - (addr & -sizeof(long)); /* residue */
991 addr &= -sizeof(long); /* residue */
Denys Vlasenko4dedd562009-02-24 15:17:53 +0000992 u.val = do_ptrace_peekdata(tcp, (char *)addr, started);
993 if (errno)
994 return u.val; /* 0 or -1 */
Wichert Akkerman5daa0281999-03-15 19:49:42 +0000995 started = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000996 memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len));
997 while (n & (sizeof(long) - 1))
998 if (u.x[n++] == '\0')
999 return 0;
1000 addr += sizeof(long), laddr += m, len -= m;
1001 }
1002 while (len) {
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001003 u.val = do_ptrace_peekdata(tcp, (char *)addr, started);
1004 if (errno)
1005 return u.val; /* 0 or -1 */
Wichert Akkerman5daa0281999-03-15 19:49:42 +00001006 started = 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001007 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
1008 for (i = 0; i < sizeof(long); i++)
1009 if (u.x[i] == '\0')
1010 return 0;
1011
1012 addr += sizeof(long), laddr += m, len -= m;
1013 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001014#endif /* !USE_PROCFS */
John Hughesaa09c6b2001-05-15 14:53:43 +00001015 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001016}
1017
1018#ifdef LINUX
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001019# if !defined (SPARC) && !defined(SPARC64)
1020# define PTRACE_WRITETEXT 101
1021# define PTRACE_WRITEDATA 102
1022# endif /* !SPARC && !SPARC64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001023#endif /* LINUX */
1024
1025#ifdef SUNOS4
1026
1027static int
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001028uload(int cmd, struct tcb *tcp, long addr, int len, char *laddr)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001029{
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001030# if 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001031 int n;
1032
1033 while (len) {
1034 n = MIN(len, PAGSIZ);
1035 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001036 if (do_ptrace5(cmd, tcp, (char *)addr, n, laddr) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001037 return -1;
1038 }
1039 len -= n;
1040 addr += n;
1041 laddr += n;
1042 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001043# else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001044 int peek, poke;
1045 int n, m;
1046 union {
1047 long val;
1048 char x[sizeof(long)];
1049 } u;
1050
1051 if (cmd == PTRACE_WRITETEXT) {
1052 peek = PTRACE_PEEKTEXT;
1053 poke = PTRACE_POKETEXT;
1054 }
1055 else {
1056 peek = PTRACE_PEEKDATA;
1057 poke = PTRACE_POKEDATA;
1058 }
1059 if (addr & (sizeof(long) - 1)) {
1060 /* addr not a multiple of sizeof(long) */
1061 n = addr - (addr & -sizeof(long)); /* residue */
1062 addr &= -sizeof(long);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001063 u.val = do_ptrace(peek, tcp, (char *) addr, 0);
1064 if (errno)
1065 return -1;
1066 m = MIN(sizeof(long) - n;
1067 memcpy(&u.x[n], laddr, m, len));
1068 if (do_ptrace(poke, tcp, (char *)addr, u.val) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001069 return -1;
1070 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001071 addr += sizeof(long);
1072 laddr += m;
1073 len -= m;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001074 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001075 errno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001076 while (len) {
1077 if (len < sizeof(long))
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001078 u.val = do_ptrace(peek, tcp, (char *) addr, 0);
1079 m = MIN(sizeof(long), len);
1080 memcpy(u.x, laddr, m);
1081 if (errno || do_ptrace(poke, tcp, (char *) addr, u.val) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001082 return -1;
1083 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001084 addr += sizeof(long);
1085 laddr += m;
1086 len -= m;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001087 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001088# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001089 return 0;
1090}
1091
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001092static int
1093tload(struct tcb *tcp, int addr, int len, char *laddr)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001094{
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001095 return uload(PTRACE_WRITETEXT, tcp, addr, len, laddr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001096}
1097
1098#endif /* SUNOS4 */
1099
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001100#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001101
1102int
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001103upeek(struct tcb *tcp, long off, long *res)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001104{
1105 long val;
1106
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001107# ifdef SUNOS4_KERNEL_ARCH_KLUDGE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001108 {
1109 static int is_sun4m = -1;
1110 struct utsname name;
1111
1112 /* Round up the usual suspects. */
1113 if (is_sun4m == -1) {
1114 if (uname(&name) < 0) {
1115 perror("upeek: uname?");
1116 exit(1);
1117 }
1118 is_sun4m = strcmp(name.machine, "sun4m") == 0;
1119 if (is_sun4m) {
Roland McGrathd9f816f2004-09-04 03:39:20 +00001120 const struct xlat *x;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001121
1122 for (x = struct_user_offsets; x->str; x++)
1123 x->val += 1024;
1124 }
1125 }
1126 if (is_sun4m)
1127 off += 1024;
1128 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001129# endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
Denys Vlasenko732d1bf2008-12-17 19:21:59 +00001130 val = do_ptrace(PTRACE_PEEKUSER, tcp, (char *) off, 0);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001131 if (errno)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001132 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001133 *res = val;
1134 return 0;
1135}
1136
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001137#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001138
Roland McGratha4d48532005-06-08 20:45:28 +00001139#if 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001140long
1141getpc(tcp)
1142struct tcb *tcp;
1143{
1144
1145#ifdef LINUX
1146 long pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001147# if defined(I386)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001148 if (upeek(tcp, 4*EIP, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001149 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001150# elif defined(X86_64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001151 if (upeek(tcp, 8*RIP, &pc) < 0)
Michal Ludvig0e035502002-09-23 15:41:01 +00001152 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001153# elif defined(IA64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001154 if (upeek(tcp, PT_B0, &pc) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001155 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001156# elif defined(ARM)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001157 if (upeek(tcp, 4*15, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001158 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001159# elif defined(BFIN)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001160 if (upeek(tcp, REG_PC, &pc) < 0)
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001161 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001162# elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001163 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001164 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001165# elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001166 if (upeek(tcp, 4*PT_PC, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001167 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001168# elif defined(ALPHA)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001169 if (upeek(tcp, REG_PC, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001170 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001171# elif defined(MIPS)
1172 if (upeek(tcp, REG_EPC, &pc) < 0)
1173 return -1;
1174# elif defined(SPARC) || defined(SPARC64)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001175 struct regs regs;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001176 if (do_ptrace(PTRACE_GETREGS, tcp, (char *) &regs, 0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001177 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001178 pc = regs.r_pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001179# elif defined(S390) || defined(S390X)
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001180 if (upeek(tcp, PT_PSWADDR, &pc) < 0)
Michal Ludvig10a88d02002-10-07 14:31:00 +00001181 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001182# elif defined(HPPA)
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001183 if (upeek(tcp, PT_IAOQ0, &pc) < 0)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001184 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001185# elif defined(SH)
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001186 if (upeek(tcp, 4*REG_PC, &pc) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001187 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001188# elif defined(SH64)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001189 if (upeek(tcp, REG_PC ,&pc) < 0)
1190 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001191# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001192 return pc;
1193#endif /* LINUX */
1194
1195#ifdef SUNOS4
1196 /*
1197 * Return current program counter for `pid'
1198 * Assumes PC is never 0xffffffff
1199 */
1200 struct regs regs;
1201
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001202 if (do_ptrace(PTRACE_GETREGS, tcp, (char *) &regs, 0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001203 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001204 return regs.r_pc;
1205#endif /* SUNOS4 */
1206
1207#ifdef SVR4
1208 /* XXX */
1209 return 0;
1210#endif /* SVR4 */
1211
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001212#ifdef FREEBSD
1213 struct reg regs;
1214 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
1215 return regs.r_eip;
1216#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001217}
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001218#endif /* 0 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001219
1220void
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001221printcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001222{
Roland McGrath7a918832005-02-02 20:55:23 +00001223#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
1224 sizeof(long) == 8 ? "[????????????????] " : \
1225 NULL /* crash */)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001226
1227#ifdef LINUX
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001228# ifdef I386
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001229 long eip;
1230
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001231 if (upeek(tcp, 4*EIP, &eip) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001232 PRINTBADPC;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001233 return;
1234 }
1235 tprintf("[%08lx] ", eip);
Roland McGratheac26fc2005-02-02 02:48:53 +00001236
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001237# elif defined(S390) || defined(S390X)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001238 long psw;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001239 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001240 PRINTBADPC;
1241 return;
1242 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001243# ifdef S390
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001244 tprintf("[%08lx] ", psw);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001245# elif S390X
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001246 tprintf("[%16lx] ", psw);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001247# endif
Roland McGratheac26fc2005-02-02 02:48:53 +00001248
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001249# elif defined(X86_64)
Michal Ludvig0e035502002-09-23 15:41:01 +00001250 long rip;
1251
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001252 if (upeek(tcp, 8*RIP, &rip) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001253 PRINTBADPC;
Michal Ludvig0e035502002-09-23 15:41:01 +00001254 return;
1255 }
1256 tprintf("[%16lx] ", rip);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001257# elif defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001258 long ip;
1259
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001260 if (upeek(tcp, PT_B0, &ip) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001261 PRINTBADPC;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001262 return;
1263 }
1264 tprintf("[%08lx] ", ip);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001265# elif defined(POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001266 long pc;
1267
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001268 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001269 tprintf ("[????????] ");
1270 return;
1271 }
1272 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001273# elif defined(M68K)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001274 long pc;
1275
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001276 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001277 tprintf ("[????????] ");
1278 return;
1279 }
1280 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001281# elif defined(ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001282 long pc;
1283
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001284 if (upeek(tcp, REG_PC, &pc) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001285 tprintf ("[????????????????] ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001286 return;
1287 }
1288 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001289# elif defined(SPARC) || defined(SPARC64)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001290 struct regs regs;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001291 if (do_ptrace(PTRACE_GETREGS, tcp, (char *) &regs, 0) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001292 PRINTBADPC;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001293 return;
1294 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001295 tprintf("[%08lx] ", regs.r_pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001296# elif defined(HPPA)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001297 long pc;
1298
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001299 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001300 tprintf ("[????????] ");
1301 return;
1302 }
1303 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001304# elif defined(MIPS)
Wichert Akkerman75c422b2001-04-10 10:22:50 +00001305 long pc;
1306
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001307 if (upeek(tcp, REG_EPC, &pc) < 0) {
Wichert Akkerman75c422b2001-04-10 10:22:50 +00001308 tprintf ("[????????] ");
1309 return;
1310 }
1311 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001312# elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001313 long pc;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001314
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001315 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
1316 tprintf ("[????????] ");
1317 return;
1318 }
1319 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001320# elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001321 long pc;
1322
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001323 if (upeek(tcp, REG_PC, &pc) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001324 tprintf ("[????????????????] ");
Roland McGrathe1e584b2003-06-02 19:18:58 +00001325 return;
1326 }
1327 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001328# elif defined(ARM)
Roland McGrathef388682003-06-03 23:28:59 +00001329 long pc;
Roland McGrathe1e584b2003-06-02 19:18:58 +00001330
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001331 if (upeek(tcp, 4*15, &pc) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001332 PRINTBADPC;
Roland McGrathef388682003-06-03 23:28:59 +00001333 return;
1334 }
1335 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001336# elif defined(BFIN)
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001337 long pc;
1338
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001339 if (upeek(tcp, PT_PC, &pc) < 0) {
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001340 PRINTBADPC;
1341 return;
1342 }
1343 tprintf("[%08lx] ", pc);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001344#elif defined(CRISV10)
1345 long pc;
1346
1347 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
1348 PRINTBADPC;
1349 return;
1350 }
1351 tprintf("[%08lx] ", pc);
1352#elif defined(CRISV32)
1353 long pc;
1354
1355 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
1356 PRINTBADPC;
1357 return;
1358 }
1359 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001360# endif /* architecture */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001361#endif /* LINUX */
1362
1363#ifdef SUNOS4
1364 struct regs regs;
1365
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001366 if (do_ptrace(PTRACE_GETREGS, tcp, (char *) &regs, 0) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001367 PRINTBADPC;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001368 return;
1369 }
1370 tprintf("[%08x] ", regs.r_o7);
1371#endif /* SUNOS4 */
1372
1373#ifdef SVR4
1374 /* XXX */
Roland McGrath7a918832005-02-02 20:55:23 +00001375 PRINTBADPC;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001376#endif
1377
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001378#ifdef FREEBSD
1379 struct reg regs;
1380 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
1381 tprintf("[%08x] ", regs.r_eip);
1382#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001383}
1384
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001385
1386/*
1387 * These #if's are huge, please indent them correctly.
1388 * It's easy to get confused otherwise.
1389 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001390#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001391
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001392# if defined LINUX
Roland McGrathd81f1d92003-01-09 06:53:34 +00001393
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001394# include "syscall.h"
Roland McGrath3291ef22008-05-20 00:34:34 +00001395
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001396# include <sys/syscall.h>
1397# ifndef CLONE_PTRACE
1398# define CLONE_PTRACE 0x00002000
1399# endif
1400# ifndef CLONE_VFORK
1401# define CLONE_VFORK 0x00004000
1402# endif
1403# ifndef CLONE_VM
1404# define CLONE_VM 0x00000100
1405# endif
1406# ifndef CLONE_STOPPED
1407# define CLONE_STOPPED 0x02000000
1408# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001409
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001410# ifdef IA64
Roland McGrathd81f1d92003-01-09 06:53:34 +00001411
Roland McGrath08267b82004-02-20 22:56:43 +00001412/* We don't have fork()/vfork() syscalls on ia64 itself, but the ia32
1413 subsystem has them for x86... */
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001414# define SYS_fork 2
1415# define SYS_vfork 190
Roland McGrath08267b82004-02-20 22:56:43 +00001416
Roland McGrathd81f1d92003-01-09 06:53:34 +00001417typedef unsigned long *arg_setup_state;
1418
1419static int
1420arg_setup(struct tcb *tcp, arg_setup_state *state)
1421{
Jan Kratochvil1f942712008-08-06 21:38:52 +00001422 unsigned long cfm, sof, sol;
1423 long bsp;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001424
Jan Kratochvil1f942712008-08-06 21:38:52 +00001425 if (ia32) {
1426 /* Satisfy a false GCC warning. */
1427 *state = NULL;
Roland McGrath08267b82004-02-20 22:56:43 +00001428 return 0;
Jan Kratochvil1f942712008-08-06 21:38:52 +00001429 }
Roland McGrath08267b82004-02-20 22:56:43 +00001430
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001431 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001432 return -1;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001433 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001434 return -1;
1435
1436 sof = (cfm >> 0) & 0x7f;
1437 sol = (cfm >> 7) & 0x7f;
Jan Kratochvil1f942712008-08-06 21:38:52 +00001438 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
Roland McGrathd81f1d92003-01-09 06:53:34 +00001439
Jan Kratochvil1f942712008-08-06 21:38:52 +00001440 *state = (unsigned long *) bsp;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001441 return 0;
1442}
1443
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001444# define arg_finish_change(tcp, state) 0
Roland McGrathd81f1d92003-01-09 06:53:34 +00001445
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001446# ifdef SYS_fork
Roland McGrathd81f1d92003-01-09 06:53:34 +00001447static int
1448get_arg0 (struct tcb *tcp, arg_setup_state *state, long *valp)
1449{
Roland McGrath08267b82004-02-20 22:56:43 +00001450 int ret;
1451
1452 if (ia32)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001453 ret = upeek (tcp, PT_R11, valp);
Roland McGrath08267b82004-02-20 22:56:43 +00001454 else
1455 ret = umoven (tcp,
1456 (unsigned long) ia64_rse_skip_regs(*state, 0),
1457 sizeof(long), (void *) valp);
1458 return ret;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001459}
1460
1461static int
1462get_arg1 (struct tcb *tcp, arg_setup_state *state, long *valp)
1463{
Roland McGrath08267b82004-02-20 22:56:43 +00001464 int ret;
1465
1466 if (ia32)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001467 ret = upeek (tcp, PT_R9, valp);
Roland McGrath08267b82004-02-20 22:56:43 +00001468 else
1469 ret = umoven (tcp,
1470 (unsigned long) ia64_rse_skip_regs(*state, 1),
1471 sizeof(long), (void *) valp);
1472 return ret;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001473}
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001474# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001475
1476static int
1477set_arg0 (struct tcb *tcp, arg_setup_state *state, long val)
1478{
Roland McGrath08267b82004-02-20 22:56:43 +00001479 int req = PTRACE_POKEDATA;
1480 void *ap;
1481
1482 if (ia32) {
1483 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1484 req = PTRACE_POKEUSER;
1485 } else
1486 ap = ia64_rse_skip_regs(*state, 0);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001487 if (do_ptrace(req, tcp, ap, val) < 0)
1488 return -1;
1489 return 0;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001490}
1491
1492static int
1493set_arg1 (struct tcb *tcp, arg_setup_state *state, long val)
1494{
Roland McGrath08267b82004-02-20 22:56:43 +00001495 int req = PTRACE_POKEDATA;
1496 void *ap;
1497
1498 if (ia32) {
1499 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1500 req = PTRACE_POKEUSER;
1501 } else
1502 ap = ia64_rse_skip_regs(*state, 1);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001503 if (do_ptrace(req, tcp, ap, val) < 0)
1504 return -1;
1505 return 0;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001506}
1507
Roland McGrathb659f872008-07-18 01:19:36 +00001508/* ia64 does not return the input arguments from functions (and syscalls)
1509 according to ia64 RSE (Register Stack Engine) behavior. */
1510
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001511# define restore_arg0(tcp, state, val) ((void) (state), 0)
1512# define restore_arg1(tcp, state, val) ((void) (state), 0)
Roland McGrathb659f872008-07-18 01:19:36 +00001513
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001514# elif defined (SPARC) || defined (SPARC64)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001515
1516typedef struct regs arg_setup_state;
1517
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001518# define arg_setup(tcp, state) \
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001519 (do_ptrace(PTRACE_GETREGS, tcp, (char *) (state), 0))
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001520# define arg_finish_change(tcp, state) \
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001521 (do_ptrace(PTRACE_SETREGS, tcp, (char *) (state), 0))
Roland McGrathd81f1d92003-01-09 06:53:34 +00001522
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001523# define get_arg0(tcp, state, valp) (*(valp) = (state)->r_o0, 0)
1524# define get_arg1(tcp, state, valp) (*(valp) = (state)->r_o1, 0)
1525# define set_arg0(tcp, state, val) ((state)->r_o0 = (val), 0)
1526# define set_arg1(tcp, state, val) ((state)->r_o1 = (val), 0)
1527# define restore_arg0(tcp, state, val) 0
Roland McGrathd81f1d92003-01-09 06:53:34 +00001528
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001529# else /* other architectures */
Roland McGrathd81f1d92003-01-09 06:53:34 +00001530
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001531# if defined S390 || defined S390X
Roland McGrath7b308222003-01-20 09:04:36 +00001532/* Note: this is only true for the `clone' system call, which handles
1533 arguments specially. We could as well say that its first two arguments
1534 are swapped relative to other architectures, but that would just be
1535 another #ifdef in the calls. */
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001536# define arg0_offset PT_GPR3
1537# define arg1_offset PT_ORIGGPR2
1538# define restore_arg0(tcp, state, val) ((void) (state), 0)
1539# define restore_arg1(tcp, state, val) ((void) (state), 0)
1540# define arg0_index 1
1541# define arg1_index 0
1542# elif defined (ALPHA) || defined (MIPS)
1543# define arg0_offset REG_A0
1544# define arg1_offset (REG_A0+1)
1545# elif defined (POWERPC)
1546# define arg0_offset (sizeof(unsigned long)*PT_R3)
1547# define arg1_offset (sizeof(unsigned long)*PT_R4)
1548# define restore_arg0(tcp, state, val) ((void) (state), 0)
1549# elif defined (HPPA)
1550# define arg0_offset PT_GR26
1551# define arg1_offset (PT_GR26-4)
1552# elif defined (X86_64)
1553# define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1554# define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1555# elif defined (SH)
1556# define arg0_offset (4*(REG_REG0+4))
1557# define arg1_offset (4*(REG_REG0+5))
1558# elif defined (SH64)
1559 /* ABI defines arg0 & 1 in r2 & r3 */
1560# define arg0_offset (REG_OFFSET+16)
1561# define arg1_offset (REG_OFFSET+24)
1562# define restore_arg0(tcp, state, val) 0
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001563# elif defined CRISV10 || defined CRISV32
1564# define arg0_offset (4*PT_R11)
1565# define arg1_offset (4*PT_ORIG_R10)
1566# define restore_arg0(tcp, state, val) 0
1567# define restore_arg1(tcp, state, val) 0
1568# define arg0_index 1
1569# define arg1_index 0
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001570# else
1571# define arg0_offset 0
1572# define arg1_offset 4
1573# if defined ARM
1574# define restore_arg0(tcp, state, val) 0
1575# endif
1576# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001577
1578typedef int arg_setup_state;
1579
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001580# define arg_setup(tcp, state) (0)
1581# define arg_finish_change(tcp, state) 0
1582# define get_arg0(tcp, cookie, valp) \
1583 (upeek ((tcp), arg0_offset, (valp)))
1584# define get_arg1(tcp, cookie, valp) \
1585 (upeek ((tcp), arg1_offset, (valp)))
Roland McGrathd81f1d92003-01-09 06:53:34 +00001586
1587static int
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001588set_arg0(struct tcb *tcp, void *cookie, long val)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001589{
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001590 return do_ptrace(PTRACE_POKEUSER, tcp, (char*)arg0_offset, val);
Roland McGrathd81f1d92003-01-09 06:53:34 +00001591}
1592
1593static int
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001594set_arg1(struct tcb *tcp, void *cookie, long val)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001595{
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001596 return do_ptrace(PTRACE_POKEUSER, tcp, (char*)arg1_offset, val);
Roland McGrathd81f1d92003-01-09 06:53:34 +00001597}
1598
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001599# endif /* architectures */
Roland McGrathd81f1d92003-01-09 06:53:34 +00001600
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001601# ifndef restore_arg0
1602# define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1603# endif
1604# ifndef restore_arg1
1605# define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1606# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001607
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001608# ifndef arg0_index
1609# define arg0_index 0
1610# define arg1_index 1
1611# endif
Roland McGrath90d0afd2004-03-01 21:05:16 +00001612
Roland McGrathd81f1d92003-01-09 06:53:34 +00001613int
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001614setbpt(struct tcb *tcp)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001615{
Roland McGrath3291ef22008-05-20 00:34:34 +00001616 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
Roland McGrathd81f1d92003-01-09 06:53:34 +00001617 arg_setup_state state;
1618
1619 if (tcp->flags & TCB_BPTSET) {
1620 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1621 return -1;
1622 }
1623
Roland McGrath3291ef22008-05-20 00:34:34 +00001624 /*
1625 * It's a silly kludge to initialize this with a search at runtime.
1626 * But it's better than maintaining another magic thing in the
1627 * godforsaken tables.
1628 */
1629 if (clone_scno[current_personality] == 0) {
1630 int i;
1631 for (i = 0; i < nsyscalls; ++i)
1632 if (sysent[i].sys_func == sys_clone) {
1633 clone_scno[current_personality] = i;
1634 break;
1635 }
1636 }
1637
Roland McGrath76989d72005-06-07 23:21:31 +00001638 switch (known_scno(tcp)) {
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001639# ifdef SYS_vfork
Roland McGrath9383c6c2003-01-18 00:19:31 +00001640 case SYS_vfork:
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001641# endif
1642# ifdef SYS_fork
Roland McGrathd81f1d92003-01-09 06:53:34 +00001643 case SYS_fork:
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001644# endif
1645# if defined SYS_fork || defined SYS_vfork
Roland McGrathd81f1d92003-01-09 06:53:34 +00001646 if (arg_setup (tcp, &state) < 0
1647 || get_arg0 (tcp, &state, &tcp->inst[0]) < 0
1648 || get_arg1 (tcp, &state, &tcp->inst[1]) < 0
Roland McGrath3291ef22008-05-20 00:34:34 +00001649 || change_syscall(tcp, clone_scno[current_personality]) < 0
Roland McGrathd81f1d92003-01-09 06:53:34 +00001650 || set_arg0 (tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1651 || set_arg1 (tcp, &state, 0) < 0
1652 || arg_finish_change (tcp, &state) < 0)
1653 return -1;
Roland McGrathc9dc3c12004-03-01 20:57:09 +00001654 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1655 tcp->u_arg[arg1_index] = 0;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001656 tcp->flags |= TCB_BPTSET;
1657 return 0;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001658# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001659
1660 case SYS_clone:
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001661# ifdef SYS_clone2
Roland McGrathd81f1d92003-01-09 06:53:34 +00001662 case SYS_clone2:
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001663# endif
Jan Kratochvil8fc95752008-08-06 21:43:35 +00001664 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1665 contrary to x86 SYS_vfork above. Even on x86 we turn the
1666 vfork semantics into plain fork - each application must not
1667 depend on the vfork specifics according to POSIX. We would
1668 hang waiting for the parent resume otherwise. We need to
1669 clear also CLONE_VM but only in the CLONE_VFORK case as
1670 otherwise we would break pthread_create. */
1671
Roland McGrathd6ff0d52008-07-18 01:09:44 +00001672 if ((arg_setup (tcp, &state) < 0
1673 || set_arg0 (tcp, &state,
1674 (tcp->u_arg[arg0_index] | CLONE_PTRACE)
Jan Kratochvil8fc95752008-08-06 21:43:35 +00001675 & ~(tcp->u_arg[arg0_index] & CLONE_VFORK
1676 ? CLONE_VFORK | CLONE_VM : 0)) < 0
Roland McGrathd6ff0d52008-07-18 01:09:44 +00001677 || arg_finish_change (tcp, &state) < 0))
1678 return -1;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001679 tcp->flags |= TCB_BPTSET;
Roland McGrathc9dc3c12004-03-01 20:57:09 +00001680 tcp->inst[0] = tcp->u_arg[arg0_index];
1681 tcp->inst[1] = tcp->u_arg[arg1_index];
Roland McGrathd81f1d92003-01-09 06:53:34 +00001682 return 0;
1683
1684 default:
1685 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1686 tcp->scno, tcp->pid);
1687 break;
1688 }
1689
1690 return -1;
1691}
1692
1693int
1694clearbpt(tcp)
1695struct tcb *tcp;
1696{
1697 arg_setup_state state;
1698 if (arg_setup (tcp, &state) < 0
Roland McGrathe1df47f2003-01-14 09:46:15 +00001699 || restore_arg0 (tcp, &state, tcp->inst[0]) < 0
1700 || restore_arg1 (tcp, &state, tcp->inst[1]) < 0
Roland McGrathd81f1d92003-01-09 06:53:34 +00001701 || arg_finish_change (tcp, &state))
1702 return -1;
1703 tcp->flags &= ~TCB_BPTSET;
1704 return 0;
1705}
1706
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001707# else /* !defined LINUX */
Roland McGrathd81f1d92003-01-09 06:53:34 +00001708
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001709int
1710setbpt(tcp)
1711struct tcb *tcp;
1712{
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001713# ifdef LINUX
1714 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1715# if defined (SPARC) || defined (SPARC64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001716 /* We simply use the SunOS breakpoint code. */
1717
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001718 struct regs regs;
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001719 unsigned long inst;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001720# define LOOPA 0x30800000 /* ba,a 0 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001721
1722 if (tcp->flags & TCB_BPTSET) {
1723 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1724 return -1;
1725 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001726 if (do_ptrace(PTRACE_GETREGS, tcp, (char *)&regs, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001727 return -1;
1728 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001729 tcp->baddr = regs.r_o7 + 8;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001730 tcp->inst[0] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *)tcp->baddr, 0);
1731 if (errno)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001732 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001733
1734 /*
1735 * XXX - BRUTAL MODE ON
1736 * We cannot set a real BPT in the child, since it will not be
1737 * traced at the moment it will reach the trap and would probably
1738 * die with a core dump.
1739 * Thus, we are force our way in by taking out two instructions
1740 * and insert an eternal loop instead, in expectance of the SIGSTOP
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001741 * generated by our PTRACE_ATTACH.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001742 * Of cause, if we evaporate ourselves in the middle of all this...
1743 */
1744 errno = 0;
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001745 inst = LOOPA;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001746# if defined (SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001747 inst <<= 32;
1748 inst |= (tcp->inst[0] & 0xffffffffUL);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001749# endif
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001750 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, inst) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001751 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001752 tcp->flags |= TCB_BPTSET;
1753
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001754# else /* !SPARC && !SPARC64 */
1755# ifdef IA64
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001756 if (ia32) {
1757# define LOOP 0x0000feeb
1758 if (tcp->flags & TCB_BPTSET) {
1759 fprintf(stderr, "PANIC: bpt already set in pid %u\n",
1760 tcp->pid);
1761 return -1;
1762 }
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001763 if (upeek(tcp, PT_CR_IIP, &tcp->baddr) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001764 return -1;
1765 if (debug)
1766 fprintf(stderr, "[%d] setting bpt at %lx\n",
1767 tcp->pid, tcp->baddr);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001768 tcp->inst[0] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *) tcp->baddr, 0);
1769 if (errno)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001770 return -1;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001771 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, LOOP) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001772 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001773 tcp->flags |= TCB_BPTSET;
1774 } else {
1775 /*
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001776 * Our strategy here is to replace the bundle that
1777 * contained the clone() syscall with a bundle of the
1778 * form:
1779 *
1780 * { 1: br 1b; br 1b; br 1b }
1781 *
1782 * This ensures that the newly forked child will loop
1783 * endlessly until we've got a chance to attach to it.
1784 */
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001785# define LOOP0 0x0000100000000017
1786# define LOOP1 0x4000000000200000
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001787 unsigned long addr, ipsr;
1788 pid_t pid;
1789
1790 pid = tcp->pid;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001791 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001792 return -1;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001793 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001794 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001795 /* store "ri" in low two bits */
1796 tcp->baddr = addr | ((ipsr >> 41) & 0x3);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001797
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001798 tcp->inst[0] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *) addr + 0, 0);
1799 if (!errno)
1800 tcp->inst[1] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *) addr + 8, 0);
1801 if (errno)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001802 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001803
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001804 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) addr + 0, LOOP0) < 0
1805 || do_ptrace(PTRACE_POKETEXT, tcp, (char *) addr + 8, LOOP1) < 0
1806 ) {
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001807 return -1;
1808 }
1809 tcp->flags |= TCB_BPTSET;
1810 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001811# else /* !IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001812
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001813# if defined (I386) || defined(X86_64)
1814# define LOOP 0x0000feeb
1815# elif defined (M68K)
1816# define LOOP 0x60fe0000
1817# elif defined (ALPHA)
1818# define LOOP 0xc3ffffff
1819# elif defined (POWERPC)
1820# define LOOP 0x48000000
1821# elif defined(ARM)
1822# define LOOP 0xEAFFFFFE
1823# elif defined(MIPS)
1824# define LOOP 0x1000ffff
1825# elif defined(S390)
1826# define LOOP 0xa7f40000 /* BRC 15,0 */
1827# elif defined(S390X)
1828# define LOOP 0xa7f4000000000000UL /* BRC 15,0 */
1829# elif defined(HPPA)
1830# define LOOP 0xe81f1ff7 /* b,l,n <loc>,r0 */
1831# elif defined(SH)
1832# ifdef __LITTLE_ENDIAN__
1833# define LOOP 0x0000affe
1834# else
1835# define LOOP 0xfeaf0000
1836# endif
1837# else
1838# error unknown architecture
1839# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001840
1841 if (tcp->flags & TCB_BPTSET) {
1842 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1843 return -1;
1844 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001845# if defined (I386)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001846 if (upeek(tcp, 4*EIP, &tcp->baddr) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001847 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001848# elif defined (X86_64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001849 if (upeek(tcp, 8*RIP, &tcp->baddr) < 0)
Michal Ludvig0e035502002-09-23 15:41:01 +00001850 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001851# elif defined (M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001852 if (upeek(tcp, 4*PT_PC, &tcp->baddr) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001853 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001854# elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001855 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001856# elif defined (ARM)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001857 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001858# elif defined (MIPS)
Wichert Akkermanf90da011999-10-31 21:15:38 +00001859 return -1; /* FIXME: I do not know what i do - Flo */
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001860# elif defined (POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001861 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &tcp->baddr) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001862 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001863# elif defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001864 if (upeek(tcp,PT_PSWADDR, &tcp->baddr) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001865 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001866# elif defined(HPPA)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001867 if (upeek(tcp, PT_IAOQ0, &tcp->baddr) < 0)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001868 return -1;
1869 tcp->baddr &= ~0x03;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001870# elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001871 if (upeek(tcp, 4*REG_PC, &tcp->baddr) < 0)
1872 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001873# else
1874# error unknown architecture
1875# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001876 if (debug)
1877 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001878 tcp->inst[0] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *) tcp->baddr, 0);
1879 if (errno)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001880 return -1;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001881 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, LOOP) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001882 return -1;
1883 }
1884 tcp->flags |= TCB_BPTSET;
1885
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001886# endif /* !IA64 */
1887# endif /* !SPARC && !SPARC64 */
1888# endif /* LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001889
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001890# ifdef SUNOS4
1891# ifdef SPARC /* This code is slightly sparc specific */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001892
Wichert Akkermane6f876c1999-06-22 15:28:30 +00001893 struct regs regs;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001894# define BPT 0x91d02001 /* ta 1 */
1895# define LOOP 0x10800000 /* ba 0 */
1896# define LOOPA 0x30800000 /* ba,a 0 */
1897# define NOP 0x01000000
1898# if LOOPA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001899 static int loopdeloop[1] = {LOOPA};
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001900# else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001901 static int loopdeloop[2] = {LOOP, NOP};
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001902# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001903
1904 if (tcp->flags & TCB_BPTSET) {
1905 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1906 return -1;
1907 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001908 if (do_ptrace(PTRACE_GETREGS, tcp, (char *)&regs, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001909 return -1;
1910 }
1911 tcp->baddr = regs.r_o7 + 8;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001912 if (do_ptrace5(PTRACE_READTEXT, tcp, (char *)tcp->baddr,
1913 sizeof tcp->inst, (char *)tcp->inst, "READTEXT") < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001914 return -1;
1915 }
1916
1917 /*
1918 * XXX - BRUTAL MODE ON
1919 * We cannot set a real BPT in the child, since it will not be
1920 * traced at the moment it will reach the trap and would probably
1921 * die with a core dump.
1922 * Thus, we are force our way in by taking out two instructions
1923 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1924 * generated by out PTRACE_ATTACH.
1925 * Of cause, if we evaporate ourselves in the middle of all this...
1926 */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001927 if (do_ptrace5(PTRACE_WRITETEXT, tcp, (char *) tcp->baddr,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001928 sizeof loopdeloop, (char *) loopdeloop) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001929 return -1;
1930 }
1931 tcp->flags |= TCB_BPTSET;
1932
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001933# endif /* SPARC */
1934# endif /* SUNOS4 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001935
1936 return 0;
1937}
1938
1939int
1940clearbpt(tcp)
1941struct tcb *tcp;
1942{
1943
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001944# ifdef LINUX
1945 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1946# if defined(I386) || defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001947 long eip;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001948# elif defined(POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001949 long pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001950# elif defined(M68K)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001951 long pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001952# elif defined(ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001953 long pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001954# elif defined(HPPA)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001955 long iaoq;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001956# elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001957 long pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001958# endif /* architecture */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001959
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001960# if defined (SPARC) || defined (SPARC64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001961 /* Again, we borrow the SunOS breakpoint code. */
1962 if (!(tcp->flags & TCB_BPTSET)) {
1963 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1964 return -1;
1965 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001966 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, tcp->inst[0]) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001967 return -1;
1968 }
1969 tcp->flags &= ~TCB_BPTSET;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001970# elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001971 if (ia32) {
1972 unsigned long addr;
1973
1974 if (debug)
1975 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1976 if (!(tcp->flags & TCB_BPTSET)) {
1977 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1978 return -1;
1979 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001980 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, tcp->inst[0]) < 0) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001981 return -1;
1982 }
1983 tcp->flags &= ~TCB_BPTSET;
1984
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001985 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001986 return -1;
1987 if (addr != tcp->baddr) {
1988 /* The breakpoint has not been reached yet. */
1989 if (debug)
1990 fprintf(stderr,
1991 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1992 addr, tcp->baddr);
1993 return 0;
1994 }
1995 } else {
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001996 unsigned long addr, ipsr;
1997 pid_t pid;
1998
1999 pid = tcp->pid;
2000
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002001 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00002002 return -1;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002003 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00002004 return -1;
2005
2006 /* restore original bundle: */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002007 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) addr + 0, tcp->inst[0]) < 0
2008 || do_ptrace(PTRACE_POKETEXT, tcp, (char *) addr + 8, tcp->inst[1]) < 0
2009 ) {
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00002010 return -1;
2011 }
2012
2013 /* restore original "ri" in ipsr: */
2014 ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002015 if (do_ptrace(PTRACE_POKEUSER, tcp, (char *) PT_CR_IPSR, ipsr) < 0) {
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00002016 return -1;
2017 }
2018
2019 tcp->flags &= ~TCB_BPTSET;
2020
2021 if (addr != (tcp->baddr & ~0x3)) {
2022 /* the breakpoint has not been reached yet. */
2023 if (debug)
2024 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2025 addr, tcp->baddr);
2026 return 0;
2027 }
2028 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002029# else /* !IA64 && !SPARC && !SPARC64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002030
2031 if (debug)
2032 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
2033 if (!(tcp->flags & TCB_BPTSET)) {
2034 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2035 return -1;
2036 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002037 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, tcp->inst[0]) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002038 return -1;
2039 }
2040 tcp->flags &= ~TCB_BPTSET;
2041
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002042# ifdef I386
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002043 if (upeek(tcp, 4*EIP, &eip) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002044 return -1;
2045 if (eip != tcp->baddr) {
2046 /* The breakpoint has not been reached yet. */
2047 if (debug)
2048 fprintf(stderr,
2049 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2050 eip, tcp->baddr);
2051 return 0;
2052 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002053# elif defined(X86_64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002054 if (upeek(tcp, 8*RIP, &eip) < 0)
Michal Ludvig0e035502002-09-23 15:41:01 +00002055 return -1;
2056 if (eip != tcp->baddr) {
2057 /* The breakpoint has not been reached yet. */
2058 if (debug)
2059 fprintf(stderr,
2060 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2061 eip, tcp->baddr);
2062 return 0;
2063 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002064# elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002065 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002066 return -1;
2067 if (pc != tcp->baddr) {
2068 /* The breakpoint has not been reached yet. */
2069 if (debug)
2070 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2071 pc, tcp->baddr);
2072 return 0;
2073 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002074# elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002075 if (upeek(tcp, 4*PT_PC, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002076 return -1;
2077 if (pc != tcp->baddr) {
2078 /* The breakpoint has not been reached yet. */
2079 if (debug)
2080 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2081 pc, tcp->baddr);
2082 return 0;
2083 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002084# elif defined(ALPHA)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002085 if (upeek(tcp, REG_PC, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002086 return -1;
2087 if (pc != tcp->baddr) {
2088 /* The breakpoint has not been reached yet. */
2089 if (debug)
2090 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2091 pc, tcp->baddr);
2092 return 0;
2093 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002094# elif defined(HPPA)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002095 if (upeek(tcp, PT_IAOQ0, &iaoq) < 0)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00002096 return -1;
2097 iaoq &= ~0x03;
2098 if (iaoq != tcp->baddr && iaoq != tcp->baddr + 4) {
2099 /* The breakpoint has not been reached yet. */
2100 if (debug)
2101 fprintf(stderr, "NOTE: PC not at bpt (iaoq %#lx baddr %#lx)\n",
2102 iaoq, tcp->baddr);
2103 return 0;
2104 }
2105 iaoq = tcp->baddr | 3;
2106 /* We should be pointing at a 'ldi -1000,r1' in glibc, so it is
2107 * safe to set both IAOQ0 and IAOQ1 to that so the PSW N bit
2108 * has no significant effect.
2109 */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002110 if (do_ptrace(PTRACE_POKEUSER, tcp, (void *)PT_IAOQ0, iaoq) < 0
2111 || do_ptrace(PTRACE_POKEUSER, tcp, (void *)PT_IAOQ1, iaoq) < 0
2112 ) {
2113 return -1;
2114 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002115# elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00002116 if (upeek(tcp, 4*REG_PC, &pc) < 0)
2117 return -1;
2118 if (pc != tcp->baddr) {
2119 /* The breakpoint has not been reached yet. */
2120 if (debug)
2121 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2122 pc, tcp->baddr);
2123 return 0;
2124 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00002125
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002126# endif /* arch */
2127# endif /* !SPARC && !SPARC64 && !IA64 */
2128# endif /* LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002129
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002130# ifdef SUNOS4
2131# ifdef SPARC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002132
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002133# if !LOOPA
Wichert Akkermane6f876c1999-06-22 15:28:30 +00002134 struct regs regs;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002135# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002136
2137 if (!(tcp->flags & TCB_BPTSET)) {
2138 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2139 return -1;
2140 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002141 if (do_ptrace5(PTRACE_WRITETEXT, tcp, (char *) tcp->baddr,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002142 sizeof tcp->inst, (char *) tcp->inst) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002143 return -1;
2144 }
2145 tcp->flags &= ~TCB_BPTSET;
2146
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002147# if !LOOPA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002148 /*
2149 * Since we don't have a single instruction breakpoint, we may have
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002150 * to adjust the program counter after removing our `breakpoint'.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002151 */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002152 if (do_ptrace(PTRACE_GETREGS, tcp, (char *)&regs, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002153 return -1;
2154 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002155 if ((regs.r_pc < tcp->baddr) || (regs.r_pc > tcp->baddr + 4)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002156 /* The breakpoint has not been reached yet */
2157 if (debug)
2158 fprintf(stderr,
2159 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002160 regs.r_pc, tcp->baddr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002161 return 0;
2162 }
2163 if (regs.r_pc != tcp->baddr)
2164 if (debug)
2165 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
2166 regs.r_pc, tcp->baddr);
2167
2168 regs.r_pc = tcp->baddr;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002169 if (do_ptrace(PTRACE_SETREGS, tcp, (char *)&regs, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002170 return -1;
2171 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002172# endif /* LOOPA */
2173# endif /* SPARC */
2174# endif /* SUNOS4 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002175
2176 return 0;
2177}
2178
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002179# endif /* !defined LINUX */
Roland McGrathd81f1d92003-01-09 06:53:34 +00002180
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002181#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002182
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002183
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002184#ifdef SUNOS4
2185
2186static int
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002187getex(tcp, hdr)
2188struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002189struct exec *hdr;
2190{
2191 int n;
2192
2193 for (n = 0; n < sizeof *hdr; n += 4) {
2194 long res;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002195 if (upeek(tcp, uoff(u_exdata) + n, &res) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002196 return -1;
2197 memcpy(((char *) hdr) + n, &res, 4);
2198 }
2199 if (debug) {
2200 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
2201 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
2202 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
2203 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
2204 }
2205 return 0;
2206}
2207
2208int
2209fixvfork(tcp)
2210struct tcb *tcp;
2211{
2212 int pid = tcp->pid;
2213 /*
2214 * Change `vfork' in a freshly exec'ed dynamically linked
2215 * executable's (internal) symbol table to plain old `fork'
2216 */
2217
2218 struct exec hdr;
2219 struct link_dynamic dyn;
2220 struct link_dynamic_2 ld;
2221 char *strtab, *cp;
2222
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002223 if (getex(tcp, &hdr) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002224 return -1;
2225 if (!hdr.a_dynamic)
2226 return -1;
2227
2228 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
2229 fprintf(stderr, "Cannot read DYNAMIC\n");
2230 return -1;
2231 }
2232 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
2233 fprintf(stderr, "Cannot read link_dynamic_2\n");
2234 return -1;
2235 }
2236 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
Roland McGrath46100d02005-06-01 18:55:42 +00002237 fprintf(stderr, "out of memory\n");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002238 return -1;
2239 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002240 if (umoven(tcp, (int)ld.ld_symbols + (int)N_TXTADDR(hdr),
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002241 (int)ld.ld_symb_size, strtab) < 0)
2242 goto err;
2243
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002244# if 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002245 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2246 fprintf(stderr, "[symbol: %s]\n", cp);
2247 cp += strlen(cp)+1;
2248 }
2249 return 0;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002250# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002251 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2252 if (strcmp(cp, "_vfork") == 0) {
2253 if (debug)
2254 fprintf(stderr, "fixvfork: FOUND _vfork\n");
2255 strcpy(cp, "_fork");
2256 break;
2257 }
2258 cp += strlen(cp)+1;
2259 }
2260 if (cp < strtab + ld.ld_symb_size)
2261 /*
2262 * Write entire symbol table back to avoid
2263 * memory alignment bugs in ptrace
2264 */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002265 if (tload(tcp, (int)ld.ld_symbols + (int)N_TXTADDR(hdr),
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002266 (int)ld.ld_symb_size, strtab) < 0)
2267 goto err;
2268
2269 free(strtab);
2270 return 0;
2271
2272err:
2273 free(strtab);
2274 return -1;
2275}
2276
2277#endif /* SUNOS4 */