blob: 4a17d40e011c1a46b80f4172d0c2ec51df69ffc6 [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
1221printcall(tcp)
1222struct tcb *tcp;
1223{
Roland McGrath7a918832005-02-02 20:55:23 +00001224#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
1225 sizeof(long) == 8 ? "[????????????????] " : \
1226 NULL /* crash */)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001227
1228#ifdef LINUX
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001229# ifdef I386
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001230 long eip;
1231
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001232 if (upeek(tcp, 4*EIP, &eip) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001233 PRINTBADPC;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001234 return;
1235 }
1236 tprintf("[%08lx] ", eip);
Roland McGratheac26fc2005-02-02 02:48:53 +00001237
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001238# elif defined(S390) || defined(S390X)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001239 long psw;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001240 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001241 PRINTBADPC;
1242 return;
1243 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001244# ifdef S390
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001245 tprintf("[%08lx] ", psw);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001246# elif S390X
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001247 tprintf("[%16lx] ", psw);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001248# endif
Roland McGratheac26fc2005-02-02 02:48:53 +00001249
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001250# elif defined(X86_64)
Michal Ludvig0e035502002-09-23 15:41:01 +00001251 long rip;
1252
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001253 if (upeek(tcp, 8*RIP, &rip) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001254 PRINTBADPC;
Michal Ludvig0e035502002-09-23 15:41:01 +00001255 return;
1256 }
1257 tprintf("[%16lx] ", rip);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001258# elif defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001259 long ip;
1260
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001261 if (upeek(tcp, PT_B0, &ip) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001262 PRINTBADPC;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001263 return;
1264 }
1265 tprintf("[%08lx] ", ip);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001266# elif defined(POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001267 long pc;
1268
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001269 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001270 tprintf ("[????????] ");
1271 return;
1272 }
1273 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001274# elif defined(M68K)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001275 long pc;
1276
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001277 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001278 tprintf ("[????????] ");
1279 return;
1280 }
1281 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001282# elif defined(ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001283 long pc;
1284
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001285 if (upeek(tcp, REG_PC, &pc) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001286 tprintf ("[????????????????] ");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001287 return;
1288 }
1289 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001290# elif defined(SPARC) || defined(SPARC64)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001291 struct regs regs;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001292 if (do_ptrace(PTRACE_GETREGS, tcp, (char *) &regs, 0) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001293 PRINTBADPC;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001294 return;
1295 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001296 tprintf("[%08lx] ", regs.r_pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001297# elif defined(HPPA)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001298 long pc;
1299
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001300 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001301 tprintf ("[????????] ");
1302 return;
1303 }
1304 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001305# elif defined(MIPS)
Wichert Akkerman75c422b2001-04-10 10:22:50 +00001306 long pc;
1307
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001308 if (upeek(tcp, REG_EPC, &pc) < 0) {
Wichert Akkerman75c422b2001-04-10 10:22:50 +00001309 tprintf ("[????????] ");
1310 return;
1311 }
1312 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001313# elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001314 long pc;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001315
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001316 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
1317 tprintf ("[????????] ");
1318 return;
1319 }
1320 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001321# elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001322 long pc;
1323
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001324 if (upeek(tcp, REG_PC, &pc) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001325 tprintf ("[????????????????] ");
Roland McGrathe1e584b2003-06-02 19:18:58 +00001326 return;
1327 }
1328 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001329# elif defined(ARM)
Roland McGrathef388682003-06-03 23:28:59 +00001330 long pc;
Roland McGrathe1e584b2003-06-02 19:18:58 +00001331
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001332 if (upeek(tcp, 4*15, &pc) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001333 PRINTBADPC;
Roland McGrathef388682003-06-03 23:28:59 +00001334 return;
1335 }
1336 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001337# elif defined(BFIN)
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001338 long pc;
1339
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001340 if (upeek(tcp, PT_PC, &pc) < 0) {
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001341 PRINTBADPC;
1342 return;
1343 }
1344 tprintf("[%08lx] ", pc);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001345# endif /* architecture */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001346#endif /* LINUX */
1347
1348#ifdef SUNOS4
1349 struct regs regs;
1350
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001351 if (do_ptrace(PTRACE_GETREGS, tcp, (char *) &regs, 0) < 0) {
Roland McGrath7a918832005-02-02 20:55:23 +00001352 PRINTBADPC;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001353 return;
1354 }
1355 tprintf("[%08x] ", regs.r_o7);
1356#endif /* SUNOS4 */
1357
1358#ifdef SVR4
1359 /* XXX */
Roland McGrath7a918832005-02-02 20:55:23 +00001360 PRINTBADPC;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001361#endif
1362
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001363#ifdef FREEBSD
1364 struct reg regs;
1365 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
1366 tprintf("[%08x] ", regs.r_eip);
1367#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001368}
1369
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001370
1371/*
1372 * These #if's are huge, please indent them correctly.
1373 * It's easy to get confused otherwise.
1374 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001375#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001376
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001377# if defined LINUX
Roland McGrathd81f1d92003-01-09 06:53:34 +00001378
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001379# include "syscall.h"
Roland McGrath3291ef22008-05-20 00:34:34 +00001380
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001381# include <sys/syscall.h>
1382# ifndef CLONE_PTRACE
1383# define CLONE_PTRACE 0x00002000
1384# endif
1385# ifndef CLONE_VFORK
1386# define CLONE_VFORK 0x00004000
1387# endif
1388# ifndef CLONE_VM
1389# define CLONE_VM 0x00000100
1390# endif
1391# ifndef CLONE_STOPPED
1392# define CLONE_STOPPED 0x02000000
1393# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001394
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001395# ifdef IA64
Roland McGrathd81f1d92003-01-09 06:53:34 +00001396
Roland McGrath08267b82004-02-20 22:56:43 +00001397/* We don't have fork()/vfork() syscalls on ia64 itself, but the ia32
1398 subsystem has them for x86... */
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001399# define SYS_fork 2
1400# define SYS_vfork 190
Roland McGrath08267b82004-02-20 22:56:43 +00001401
Roland McGrathd81f1d92003-01-09 06:53:34 +00001402typedef unsigned long *arg_setup_state;
1403
1404static int
1405arg_setup(struct tcb *tcp, arg_setup_state *state)
1406{
Jan Kratochvil1f942712008-08-06 21:38:52 +00001407 unsigned long cfm, sof, sol;
1408 long bsp;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001409
Jan Kratochvil1f942712008-08-06 21:38:52 +00001410 if (ia32) {
1411 /* Satisfy a false GCC warning. */
1412 *state = NULL;
Roland McGrath08267b82004-02-20 22:56:43 +00001413 return 0;
Jan Kratochvil1f942712008-08-06 21:38:52 +00001414 }
Roland McGrath08267b82004-02-20 22:56:43 +00001415
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001416 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001417 return -1;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001418 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001419 return -1;
1420
1421 sof = (cfm >> 0) & 0x7f;
1422 sol = (cfm >> 7) & 0x7f;
Jan Kratochvil1f942712008-08-06 21:38:52 +00001423 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
Roland McGrathd81f1d92003-01-09 06:53:34 +00001424
Jan Kratochvil1f942712008-08-06 21:38:52 +00001425 *state = (unsigned long *) bsp;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001426 return 0;
1427}
1428
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001429# define arg_finish_change(tcp, state) 0
Roland McGrathd81f1d92003-01-09 06:53:34 +00001430
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001431# ifdef SYS_fork
Roland McGrathd81f1d92003-01-09 06:53:34 +00001432static int
1433get_arg0 (struct tcb *tcp, arg_setup_state *state, long *valp)
1434{
Roland McGrath08267b82004-02-20 22:56:43 +00001435 int ret;
1436
1437 if (ia32)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001438 ret = upeek (tcp, PT_R11, valp);
Roland McGrath08267b82004-02-20 22:56:43 +00001439 else
1440 ret = umoven (tcp,
1441 (unsigned long) ia64_rse_skip_regs(*state, 0),
1442 sizeof(long), (void *) valp);
1443 return ret;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001444}
1445
1446static int
1447get_arg1 (struct tcb *tcp, arg_setup_state *state, long *valp)
1448{
Roland McGrath08267b82004-02-20 22:56:43 +00001449 int ret;
1450
1451 if (ia32)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001452 ret = upeek (tcp, PT_R9, valp);
Roland McGrath08267b82004-02-20 22:56:43 +00001453 else
1454 ret = umoven (tcp,
1455 (unsigned long) ia64_rse_skip_regs(*state, 1),
1456 sizeof(long), (void *) valp);
1457 return ret;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001458}
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001459# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001460
1461static int
1462set_arg0 (struct tcb *tcp, arg_setup_state *state, long val)
1463{
Roland McGrath08267b82004-02-20 22:56:43 +00001464 int req = PTRACE_POKEDATA;
1465 void *ap;
1466
1467 if (ia32) {
1468 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1469 req = PTRACE_POKEUSER;
1470 } else
1471 ap = ia64_rse_skip_regs(*state, 0);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001472 if (do_ptrace(req, tcp, ap, val) < 0)
1473 return -1;
1474 return 0;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001475}
1476
1477static int
1478set_arg1 (struct tcb *tcp, arg_setup_state *state, long val)
1479{
Roland McGrath08267b82004-02-20 22:56:43 +00001480 int req = PTRACE_POKEDATA;
1481 void *ap;
1482
1483 if (ia32) {
1484 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1485 req = PTRACE_POKEUSER;
1486 } else
1487 ap = ia64_rse_skip_regs(*state, 1);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001488 if (do_ptrace(req, tcp, ap, val) < 0)
1489 return -1;
1490 return 0;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001491}
1492
Roland McGrathb659f872008-07-18 01:19:36 +00001493/* ia64 does not return the input arguments from functions (and syscalls)
1494 according to ia64 RSE (Register Stack Engine) behavior. */
1495
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001496# define restore_arg0(tcp, state, val) ((void) (state), 0)
1497# define restore_arg1(tcp, state, val) ((void) (state), 0)
Roland McGrathb659f872008-07-18 01:19:36 +00001498
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001499# elif defined (SPARC) || defined (SPARC64)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001500
1501typedef struct regs arg_setup_state;
1502
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001503# define arg_setup(tcp, state) \
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001504 (do_ptrace(PTRACE_GETREGS, tcp, (char *) (state), 0))
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001505# define arg_finish_change(tcp, state) \
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001506 (do_ptrace(PTRACE_SETREGS, tcp, (char *) (state), 0))
Roland McGrathd81f1d92003-01-09 06:53:34 +00001507
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001508# define get_arg0(tcp, state, valp) (*(valp) = (state)->r_o0, 0)
1509# define get_arg1(tcp, state, valp) (*(valp) = (state)->r_o1, 0)
1510# define set_arg0(tcp, state, val) ((state)->r_o0 = (val), 0)
1511# define set_arg1(tcp, state, val) ((state)->r_o1 = (val), 0)
1512# define restore_arg0(tcp, state, val) 0
Roland McGrathd81f1d92003-01-09 06:53:34 +00001513
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001514# else /* other architectures */
Roland McGrathd81f1d92003-01-09 06:53:34 +00001515
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001516# if defined S390 || defined S390X
Roland McGrath7b308222003-01-20 09:04:36 +00001517/* Note: this is only true for the `clone' system call, which handles
1518 arguments specially. We could as well say that its first two arguments
1519 are swapped relative to other architectures, but that would just be
1520 another #ifdef in the calls. */
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001521# define arg0_offset PT_GPR3
1522# define arg1_offset PT_ORIGGPR2
1523# define restore_arg0(tcp, state, val) ((void) (state), 0)
1524# define restore_arg1(tcp, state, val) ((void) (state), 0)
1525# define arg0_index 1
1526# define arg1_index 0
1527# elif defined (ALPHA) || defined (MIPS)
1528# define arg0_offset REG_A0
1529# define arg1_offset (REG_A0+1)
1530# elif defined (POWERPC)
1531# define arg0_offset (sizeof(unsigned long)*PT_R3)
1532# define arg1_offset (sizeof(unsigned long)*PT_R4)
1533# define restore_arg0(tcp, state, val) ((void) (state), 0)
1534# elif defined (HPPA)
1535# define arg0_offset PT_GR26
1536# define arg1_offset (PT_GR26-4)
1537# elif defined (X86_64)
1538# define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1539# define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1540# elif defined (SH)
1541# define arg0_offset (4*(REG_REG0+4))
1542# define arg1_offset (4*(REG_REG0+5))
1543# elif defined (SH64)
1544 /* ABI defines arg0 & 1 in r2 & r3 */
1545# define arg0_offset (REG_OFFSET+16)
1546# define arg1_offset (REG_OFFSET+24)
1547# define restore_arg0(tcp, state, val) 0
1548# else
1549# define arg0_offset 0
1550# define arg1_offset 4
1551# if defined ARM
1552# define restore_arg0(tcp, state, val) 0
1553# endif
1554# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001555
1556typedef int arg_setup_state;
1557
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001558# define arg_setup(tcp, state) (0)
1559# define arg_finish_change(tcp, state) 0
1560# define get_arg0(tcp, cookie, valp) \
1561 (upeek ((tcp), arg0_offset, (valp)))
1562# define get_arg1(tcp, cookie, valp) \
1563 (upeek ((tcp), arg1_offset, (valp)))
Roland McGrathd81f1d92003-01-09 06:53:34 +00001564
1565static int
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001566set_arg0(struct tcb *tcp, void *cookie, long val)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001567{
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001568 return do_ptrace(PTRACE_POKEUSER, tcp, (char*)arg0_offset, val);
Roland McGrathd81f1d92003-01-09 06:53:34 +00001569}
1570
1571static int
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001572set_arg1(struct tcb *tcp, void *cookie, long val)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001573{
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001574 return do_ptrace(PTRACE_POKEUSER, tcp, (char*)arg1_offset, val);
Roland McGrathd81f1d92003-01-09 06:53:34 +00001575}
1576
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001577# endif /* architectures */
Roland McGrathd81f1d92003-01-09 06:53:34 +00001578
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001579# ifndef restore_arg0
1580# define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1581# endif
1582# ifndef restore_arg1
1583# define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1584# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001585
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001586# ifndef arg0_index
1587# define arg0_index 0
1588# define arg1_index 1
1589# endif
Roland McGrath90d0afd2004-03-01 21:05:16 +00001590
Roland McGrathd81f1d92003-01-09 06:53:34 +00001591int
Denys Vlasenko418d66a2009-01-17 01:52:54 +00001592setbpt(struct tcb *tcp)
Roland McGrathd81f1d92003-01-09 06:53:34 +00001593{
Roland McGrath3291ef22008-05-20 00:34:34 +00001594 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
Roland McGrathd81f1d92003-01-09 06:53:34 +00001595 arg_setup_state state;
1596
1597 if (tcp->flags & TCB_BPTSET) {
1598 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1599 return -1;
1600 }
1601
Roland McGrath3291ef22008-05-20 00:34:34 +00001602 /*
1603 * It's a silly kludge to initialize this with a search at runtime.
1604 * But it's better than maintaining another magic thing in the
1605 * godforsaken tables.
1606 */
1607 if (clone_scno[current_personality] == 0) {
1608 int i;
1609 for (i = 0; i < nsyscalls; ++i)
1610 if (sysent[i].sys_func == sys_clone) {
1611 clone_scno[current_personality] = i;
1612 break;
1613 }
1614 }
1615
Roland McGrath76989d72005-06-07 23:21:31 +00001616 switch (known_scno(tcp)) {
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001617# ifdef SYS_vfork
Roland McGrath9383c6c2003-01-18 00:19:31 +00001618 case SYS_vfork:
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001619# endif
1620# ifdef SYS_fork
Roland McGrathd81f1d92003-01-09 06:53:34 +00001621 case SYS_fork:
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001622# endif
1623# if defined SYS_fork || defined SYS_vfork
Roland McGrathd81f1d92003-01-09 06:53:34 +00001624 if (arg_setup (tcp, &state) < 0
1625 || get_arg0 (tcp, &state, &tcp->inst[0]) < 0
1626 || get_arg1 (tcp, &state, &tcp->inst[1]) < 0
Roland McGrath3291ef22008-05-20 00:34:34 +00001627 || change_syscall(tcp, clone_scno[current_personality]) < 0
Roland McGrathd81f1d92003-01-09 06:53:34 +00001628 || set_arg0 (tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1629 || set_arg1 (tcp, &state, 0) < 0
1630 || arg_finish_change (tcp, &state) < 0)
1631 return -1;
Roland McGrathc9dc3c12004-03-01 20:57:09 +00001632 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1633 tcp->u_arg[arg1_index] = 0;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001634 tcp->flags |= TCB_BPTSET;
1635 return 0;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001636# endif
Roland McGrathd81f1d92003-01-09 06:53:34 +00001637
1638 case SYS_clone:
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001639# ifdef SYS_clone2
Roland McGrathd81f1d92003-01-09 06:53:34 +00001640 case SYS_clone2:
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001641# endif
Jan Kratochvil8fc95752008-08-06 21:43:35 +00001642 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1643 contrary to x86 SYS_vfork above. Even on x86 we turn the
1644 vfork semantics into plain fork - each application must not
1645 depend on the vfork specifics according to POSIX. We would
1646 hang waiting for the parent resume otherwise. We need to
1647 clear also CLONE_VM but only in the CLONE_VFORK case as
1648 otherwise we would break pthread_create. */
1649
Roland McGrathd6ff0d52008-07-18 01:09:44 +00001650 if ((arg_setup (tcp, &state) < 0
1651 || set_arg0 (tcp, &state,
1652 (tcp->u_arg[arg0_index] | CLONE_PTRACE)
Jan Kratochvil8fc95752008-08-06 21:43:35 +00001653 & ~(tcp->u_arg[arg0_index] & CLONE_VFORK
1654 ? CLONE_VFORK | CLONE_VM : 0)) < 0
Roland McGrathd6ff0d52008-07-18 01:09:44 +00001655 || arg_finish_change (tcp, &state) < 0))
1656 return -1;
Roland McGrathd81f1d92003-01-09 06:53:34 +00001657 tcp->flags |= TCB_BPTSET;
Roland McGrathc9dc3c12004-03-01 20:57:09 +00001658 tcp->inst[0] = tcp->u_arg[arg0_index];
1659 tcp->inst[1] = tcp->u_arg[arg1_index];
Roland McGrathd81f1d92003-01-09 06:53:34 +00001660 return 0;
1661
1662 default:
1663 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1664 tcp->scno, tcp->pid);
1665 break;
1666 }
1667
1668 return -1;
1669}
1670
1671int
1672clearbpt(tcp)
1673struct tcb *tcp;
1674{
1675 arg_setup_state state;
1676 if (arg_setup (tcp, &state) < 0
Roland McGrathe1df47f2003-01-14 09:46:15 +00001677 || restore_arg0 (tcp, &state, tcp->inst[0]) < 0
1678 || restore_arg1 (tcp, &state, tcp->inst[1]) < 0
Roland McGrathd81f1d92003-01-09 06:53:34 +00001679 || arg_finish_change (tcp, &state))
1680 return -1;
1681 tcp->flags &= ~TCB_BPTSET;
1682 return 0;
1683}
1684
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001685# else /* !defined LINUX */
Roland McGrathd81f1d92003-01-09 06:53:34 +00001686
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001687int
1688setbpt(tcp)
1689struct tcb *tcp;
1690{
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001691# ifdef LINUX
1692 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1693# if defined (SPARC) || defined (SPARC64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001694 /* We simply use the SunOS breakpoint code. */
1695
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001696 struct regs regs;
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001697 unsigned long inst;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001698# define LOOPA 0x30800000 /* ba,a 0 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001699
1700 if (tcp->flags & TCB_BPTSET) {
1701 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1702 return -1;
1703 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001704 if (do_ptrace(PTRACE_GETREGS, tcp, (char *)&regs, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001705 return -1;
1706 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001707 tcp->baddr = regs.r_o7 + 8;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001708 tcp->inst[0] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *)tcp->baddr, 0);
1709 if (errno)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001710 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001711
1712 /*
1713 * XXX - BRUTAL MODE ON
1714 * We cannot set a real BPT in the child, since it will not be
1715 * traced at the moment it will reach the trap and would probably
1716 * die with a core dump.
1717 * Thus, we are force our way in by taking out two instructions
1718 * and insert an eternal loop instead, in expectance of the SIGSTOP
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001719 * generated by our PTRACE_ATTACH.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001720 * Of cause, if we evaporate ourselves in the middle of all this...
1721 */
1722 errno = 0;
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001723 inst = LOOPA;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001724# if defined (SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001725 inst <<= 32;
1726 inst |= (tcp->inst[0] & 0xffffffffUL);
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001727# endif
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001728 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, inst) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001729 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001730 tcp->flags |= TCB_BPTSET;
1731
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001732# else /* !SPARC && !SPARC64 */
1733# ifdef IA64
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001734 if (ia32) {
1735# define LOOP 0x0000feeb
1736 if (tcp->flags & TCB_BPTSET) {
1737 fprintf(stderr, "PANIC: bpt already set in pid %u\n",
1738 tcp->pid);
1739 return -1;
1740 }
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001741 if (upeek(tcp, PT_CR_IIP, &tcp->baddr) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001742 return -1;
1743 if (debug)
1744 fprintf(stderr, "[%d] setting bpt at %lx\n",
1745 tcp->pid, tcp->baddr);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001746 tcp->inst[0] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *) tcp->baddr, 0);
1747 if (errno)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001748 return -1;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001749 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, LOOP) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001750 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001751 tcp->flags |= TCB_BPTSET;
1752 } else {
1753 /*
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001754 * Our strategy here is to replace the bundle that
1755 * contained the clone() syscall with a bundle of the
1756 * form:
1757 *
1758 * { 1: br 1b; br 1b; br 1b }
1759 *
1760 * This ensures that the newly forked child will loop
1761 * endlessly until we've got a chance to attach to it.
1762 */
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001763# define LOOP0 0x0000100000000017
1764# define LOOP1 0x4000000000200000
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001765 unsigned long addr, ipsr;
1766 pid_t pid;
1767
1768 pid = tcp->pid;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001769 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001770 return -1;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001771 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001772 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001773 /* store "ri" in low two bits */
1774 tcp->baddr = addr | ((ipsr >> 41) & 0x3);
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001775
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001776 tcp->inst[0] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *) addr + 0, 0);
1777 if (!errno)
1778 tcp->inst[1] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *) addr + 8, 0);
1779 if (errno)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001780 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001781
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001782 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) addr + 0, LOOP0) < 0
1783 || do_ptrace(PTRACE_POKETEXT, tcp, (char *) addr + 8, LOOP1) < 0
1784 ) {
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001785 return -1;
1786 }
1787 tcp->flags |= TCB_BPTSET;
1788 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001789# else /* !IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001790
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001791# if defined (I386) || defined(X86_64)
1792# define LOOP 0x0000feeb
1793# elif defined (M68K)
1794# define LOOP 0x60fe0000
1795# elif defined (ALPHA)
1796# define LOOP 0xc3ffffff
1797# elif defined (POWERPC)
1798# define LOOP 0x48000000
1799# elif defined(ARM)
1800# define LOOP 0xEAFFFFFE
1801# elif defined(MIPS)
1802# define LOOP 0x1000ffff
1803# elif defined(S390)
1804# define LOOP 0xa7f40000 /* BRC 15,0 */
1805# elif defined(S390X)
1806# define LOOP 0xa7f4000000000000UL /* BRC 15,0 */
1807# elif defined(HPPA)
1808# define LOOP 0xe81f1ff7 /* b,l,n <loc>,r0 */
1809# elif defined(SH)
1810# ifdef __LITTLE_ENDIAN__
1811# define LOOP 0x0000affe
1812# else
1813# define LOOP 0xfeaf0000
1814# endif
1815# else
1816# error unknown architecture
1817# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001818
1819 if (tcp->flags & TCB_BPTSET) {
1820 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1821 return -1;
1822 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001823# if defined (I386)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001824 if (upeek(tcp, 4*EIP, &tcp->baddr) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001825 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001826# elif defined (X86_64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001827 if (upeek(tcp, 8*RIP, &tcp->baddr) < 0)
Michal Ludvig0e035502002-09-23 15:41:01 +00001828 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001829# elif defined (M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001830 if (upeek(tcp, 4*PT_PC, &tcp->baddr) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001831 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001832# elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001833 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001834# elif defined (ARM)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001835 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001836# elif defined (MIPS)
Wichert Akkermanf90da011999-10-31 21:15:38 +00001837 return -1; /* FIXME: I do not know what i do - Flo */
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001838# elif defined (POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001839 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &tcp->baddr) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001840 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001841# elif defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001842 if (upeek(tcp,PT_PSWADDR, &tcp->baddr) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001843 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001844# elif defined(HPPA)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001845 if (upeek(tcp, PT_IAOQ0, &tcp->baddr) < 0)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001846 return -1;
1847 tcp->baddr &= ~0x03;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001848# elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001849 if (upeek(tcp, 4*REG_PC, &tcp->baddr) < 0)
1850 return -1;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001851# else
1852# error unknown architecture
1853# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001854 if (debug)
1855 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001856 tcp->inst[0] = do_ptrace(PTRACE_PEEKTEXT, tcp, (char *) tcp->baddr, 0);
1857 if (errno)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001858 return -1;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001859 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, LOOP) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001860 return -1;
1861 }
1862 tcp->flags |= TCB_BPTSET;
1863
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001864# endif /* !IA64 */
1865# endif /* !SPARC && !SPARC64 */
1866# endif /* LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001867
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001868# ifdef SUNOS4
1869# ifdef SPARC /* This code is slightly sparc specific */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001870
Wichert Akkermane6f876c1999-06-22 15:28:30 +00001871 struct regs regs;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001872# define BPT 0x91d02001 /* ta 1 */
1873# define LOOP 0x10800000 /* ba 0 */
1874# define LOOPA 0x30800000 /* ba,a 0 */
1875# define NOP 0x01000000
1876# if LOOPA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001877 static int loopdeloop[1] = {LOOPA};
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001878# else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001879 static int loopdeloop[2] = {LOOP, NOP};
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001880# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001881
1882 if (tcp->flags & TCB_BPTSET) {
1883 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1884 return -1;
1885 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001886 if (do_ptrace(PTRACE_GETREGS, tcp, (char *)&regs, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001887 return -1;
1888 }
1889 tcp->baddr = regs.r_o7 + 8;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001890 if (do_ptrace5(PTRACE_READTEXT, tcp, (char *)tcp->baddr,
1891 sizeof tcp->inst, (char *)tcp->inst, "READTEXT") < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001892 return -1;
1893 }
1894
1895 /*
1896 * XXX - BRUTAL MODE ON
1897 * We cannot set a real BPT in the child, since it will not be
1898 * traced at the moment it will reach the trap and would probably
1899 * die with a core dump.
1900 * Thus, we are force our way in by taking out two instructions
1901 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1902 * generated by out PTRACE_ATTACH.
1903 * Of cause, if we evaporate ourselves in the middle of all this...
1904 */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001905 if (do_ptrace5(PTRACE_WRITETEXT, tcp, (char *) tcp->baddr,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001906 sizeof loopdeloop, (char *) loopdeloop) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001907 return -1;
1908 }
1909 tcp->flags |= TCB_BPTSET;
1910
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001911# endif /* SPARC */
1912# endif /* SUNOS4 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001913
1914 return 0;
1915}
1916
1917int
1918clearbpt(tcp)
1919struct tcb *tcp;
1920{
1921
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001922# ifdef LINUX
1923 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1924# if defined(I386) || defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001925 long eip;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001926# elif defined(POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001927 long pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001928# elif defined(M68K)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001929 long pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001930# elif defined(ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001931 long pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001932# elif defined(HPPA)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001933 long iaoq;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001934# elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001935 long pc;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001936# endif /* architecture */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001937
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001938# if defined (SPARC) || defined (SPARC64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001939 /* Again, we borrow the SunOS breakpoint code. */
1940 if (!(tcp->flags & TCB_BPTSET)) {
1941 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1942 return -1;
1943 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001944 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, tcp->inst[0]) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001945 return -1;
1946 }
1947 tcp->flags &= ~TCB_BPTSET;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00001948# elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001949 if (ia32) {
1950 unsigned long addr;
1951
1952 if (debug)
1953 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1954 if (!(tcp->flags & TCB_BPTSET)) {
1955 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1956 return -1;
1957 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001958 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, tcp->inst[0]) < 0) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001959 return -1;
1960 }
1961 tcp->flags &= ~TCB_BPTSET;
1962
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001963 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001964 return -1;
1965 if (addr != tcp->baddr) {
1966 /* The breakpoint has not been reached yet. */
1967 if (debug)
1968 fprintf(stderr,
1969 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1970 addr, tcp->baddr);
1971 return 0;
1972 }
1973 } else {
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001974 unsigned long addr, ipsr;
1975 pid_t pid;
1976
1977 pid = tcp->pid;
1978
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001979 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001980 return -1;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001981 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001982 return -1;
1983
1984 /* restore original bundle: */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001985 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) addr + 0, tcp->inst[0]) < 0
1986 || do_ptrace(PTRACE_POKETEXT, tcp, (char *) addr + 8, tcp->inst[1]) < 0
1987 ) {
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001988 return -1;
1989 }
1990
1991 /* restore original "ri" in ipsr: */
1992 ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41);
Denys Vlasenko4dedd562009-02-24 15:17:53 +00001993 if (do_ptrace(PTRACE_POKEUSER, tcp, (char *) PT_CR_IPSR, ipsr) < 0) {
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001994 return -1;
1995 }
1996
1997 tcp->flags &= ~TCB_BPTSET;
1998
1999 if (addr != (tcp->baddr & ~0x3)) {
2000 /* the breakpoint has not been reached yet. */
2001 if (debug)
2002 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2003 addr, tcp->baddr);
2004 return 0;
2005 }
2006 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002007# else /* !IA64 && !SPARC && !SPARC64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002008
2009 if (debug)
2010 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
2011 if (!(tcp->flags & TCB_BPTSET)) {
2012 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2013 return -1;
2014 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002015 if (do_ptrace(PTRACE_POKETEXT, tcp, (char *) tcp->baddr, tcp->inst[0]) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002016 return -1;
2017 }
2018 tcp->flags &= ~TCB_BPTSET;
2019
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002020# ifdef I386
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002021 if (upeek(tcp, 4*EIP, &eip) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002022 return -1;
2023 if (eip != tcp->baddr) {
2024 /* The breakpoint has not been reached yet. */
2025 if (debug)
2026 fprintf(stderr,
2027 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2028 eip, tcp->baddr);
2029 return 0;
2030 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002031# elif defined(X86_64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002032 if (upeek(tcp, 8*RIP, &eip) < 0)
Michal Ludvig0e035502002-09-23 15:41:01 +00002033 return -1;
2034 if (eip != tcp->baddr) {
2035 /* The breakpoint has not been reached yet. */
2036 if (debug)
2037 fprintf(stderr,
2038 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2039 eip, tcp->baddr);
2040 return 0;
2041 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002042# elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002043 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002044 return -1;
2045 if (pc != tcp->baddr) {
2046 /* The breakpoint has not been reached yet. */
2047 if (debug)
2048 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2049 pc, tcp->baddr);
2050 return 0;
2051 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002052# elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002053 if (upeek(tcp, 4*PT_PC, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002054 return -1;
2055 if (pc != tcp->baddr) {
2056 /* The breakpoint has not been reached yet. */
2057 if (debug)
2058 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2059 pc, tcp->baddr);
2060 return 0;
2061 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002062# elif defined(ALPHA)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002063 if (upeek(tcp, REG_PC, &pc) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002064 return -1;
2065 if (pc != tcp->baddr) {
2066 /* The breakpoint has not been reached yet. */
2067 if (debug)
2068 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2069 pc, tcp->baddr);
2070 return 0;
2071 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002072# elif defined(HPPA)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002073 if (upeek(tcp, PT_IAOQ0, &iaoq) < 0)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00002074 return -1;
2075 iaoq &= ~0x03;
2076 if (iaoq != tcp->baddr && iaoq != tcp->baddr + 4) {
2077 /* The breakpoint has not been reached yet. */
2078 if (debug)
2079 fprintf(stderr, "NOTE: PC not at bpt (iaoq %#lx baddr %#lx)\n",
2080 iaoq, tcp->baddr);
2081 return 0;
2082 }
2083 iaoq = tcp->baddr | 3;
2084 /* We should be pointing at a 'ldi -1000,r1' in glibc, so it is
2085 * safe to set both IAOQ0 and IAOQ1 to that so the PSW N bit
2086 * has no significant effect.
2087 */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002088 if (do_ptrace(PTRACE_POKEUSER, tcp, (void *)PT_IAOQ0, iaoq) < 0
2089 || do_ptrace(PTRACE_POKEUSER, tcp, (void *)PT_IAOQ1, iaoq) < 0
2090 ) {
2091 return -1;
2092 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002093# elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00002094 if (upeek(tcp, 4*REG_PC, &pc) < 0)
2095 return -1;
2096 if (pc != tcp->baddr) {
2097 /* The breakpoint has not been reached yet. */
2098 if (debug)
2099 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2100 pc, tcp->baddr);
2101 return 0;
2102 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00002103
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002104# endif /* arch */
2105# endif /* !SPARC && !SPARC64 && !IA64 */
2106# endif /* LINUX */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002107
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002108# ifdef SUNOS4
2109# ifdef SPARC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002110
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002111# if !LOOPA
Wichert Akkermane6f876c1999-06-22 15:28:30 +00002112 struct regs regs;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002113# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002114
2115 if (!(tcp->flags & TCB_BPTSET)) {
2116 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2117 return -1;
2118 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002119 if (do_ptrace5(PTRACE_WRITETEXT, tcp, (char *) tcp->baddr,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002120 sizeof tcp->inst, (char *) tcp->inst) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002121 return -1;
2122 }
2123 tcp->flags &= ~TCB_BPTSET;
2124
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002125# if !LOOPA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002126 /*
2127 * Since we don't have a single instruction breakpoint, we may have
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002128 * to adjust the program counter after removing our `breakpoint'.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002129 */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002130 if (do_ptrace(PTRACE_GETREGS, tcp, (char *)&regs, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002131 return -1;
2132 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002133 if ((regs.r_pc < tcp->baddr) || (regs.r_pc > tcp->baddr + 4)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002134 /* The breakpoint has not been reached yet */
2135 if (debug)
2136 fprintf(stderr,
2137 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002138 regs.r_pc, tcp->baddr);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002139 return 0;
2140 }
2141 if (regs.r_pc != tcp->baddr)
2142 if (debug)
2143 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
2144 regs.r_pc, tcp->baddr);
2145
2146 regs.r_pc = tcp->baddr;
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002147 if (do_ptrace(PTRACE_SETREGS, tcp, (char *)&regs, 0) < 0) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002148 return -1;
2149 }
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002150# endif /* LOOPA */
2151# endif /* SPARC */
2152# endif /* SUNOS4 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002153
2154 return 0;
2155}
2156
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002157# endif /* !defined LINUX */
Roland McGrathd81f1d92003-01-09 06:53:34 +00002158
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002159#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002160
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002161
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002162#ifdef SUNOS4
2163
2164static int
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002165getex(tcp, hdr)
2166struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002167struct exec *hdr;
2168{
2169 int n;
2170
2171 for (n = 0; n < sizeof *hdr; n += 4) {
2172 long res;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002173 if (upeek(tcp, uoff(u_exdata) + n, &res) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002174 return -1;
2175 memcpy(((char *) hdr) + n, &res, 4);
2176 }
2177 if (debug) {
2178 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
2179 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
2180 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
2181 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
2182 }
2183 return 0;
2184}
2185
2186int
2187fixvfork(tcp)
2188struct tcb *tcp;
2189{
2190 int pid = tcp->pid;
2191 /*
2192 * Change `vfork' in a freshly exec'ed dynamically linked
2193 * executable's (internal) symbol table to plain old `fork'
2194 */
2195
2196 struct exec hdr;
2197 struct link_dynamic dyn;
2198 struct link_dynamic_2 ld;
2199 char *strtab, *cp;
2200
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00002201 if (getex(tcp, &hdr) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002202 return -1;
2203 if (!hdr.a_dynamic)
2204 return -1;
2205
2206 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
2207 fprintf(stderr, "Cannot read DYNAMIC\n");
2208 return -1;
2209 }
2210 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
2211 fprintf(stderr, "Cannot read link_dynamic_2\n");
2212 return -1;
2213 }
2214 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
Roland McGrath46100d02005-06-01 18:55:42 +00002215 fprintf(stderr, "out of memory\n");
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002216 return -1;
2217 }
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002218 if (umoven(tcp, (int)ld.ld_symbols + (int)N_TXTADDR(hdr),
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002219 (int)ld.ld_symb_size, strtab) < 0)
2220 goto err;
2221
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002222# if 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002223 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2224 fprintf(stderr, "[symbol: %s]\n", cp);
2225 cp += strlen(cp)+1;
2226 }
2227 return 0;
Denys Vlasenko3bb7cd62009-02-09 18:55:59 +00002228# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002229 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2230 if (strcmp(cp, "_vfork") == 0) {
2231 if (debug)
2232 fprintf(stderr, "fixvfork: FOUND _vfork\n");
2233 strcpy(cp, "_fork");
2234 break;
2235 }
2236 cp += strlen(cp)+1;
2237 }
2238 if (cp < strtab + ld.ld_symb_size)
2239 /*
2240 * Write entire symbol table back to avoid
2241 * memory alignment bugs in ptrace
2242 */
Denys Vlasenko4dedd562009-02-24 15:17:53 +00002243 if (tload(tcp, (int)ld.ld_symbols + (int)N_TXTADDR(hdr),
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002244 (int)ld.ld_symb_size, strtab) < 0)
2245 goto err;
2246
2247 free(strtab);
2248 return 0;
2249
2250err:
2251 free(strtab);
2252 return -1;
2253}
2254
2255#endif /* SUNOS4 */