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>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
39 #include <sys/param.h>
45 #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1)
46 #include <linux/ptrace.h>
50 # include <asm/ptrace_offsets.h>
56 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
57 #elif defined(HAVE_LINUX_PTRACE_H)
58 # undef PTRACE_SYSCALL
59 # ifdef HAVE_STRUCT_IA64_FPREG
60 # define ia64_fpreg XXX_ia64_fpreg
62 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
63 # define pt_all_user_regs XXX_pt_all_user_regs
65 # include <linux/ptrace.h>
67 # undef pt_all_user_regs
71 # undef PTRACE_GETREGS
72 # define PTRACE_GETREGS PTRACE_GETREGS64
73 # undef PTRACE_SETREGS
74 # define PTRACE_SETREGS PTRACE_SETREGS64
79 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
82 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
86 tv_nz(struct timeval *a)
88 return a->tv_sec || a->tv_usec;
92 tv_cmp(struct timeval *a, struct timeval *b)
94 if (a->tv_sec < b->tv_sec
95 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
97 if (a->tv_sec > b->tv_sec
98 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
104 tv_float(struct timeval *tv)
106 return tv->tv_sec + tv->tv_usec/1000000.0;
110 tv_add(struct timeval *tv, struct timeval *a, struct timeval *b)
112 tv->tv_sec = a->tv_sec + b->tv_sec;
113 tv->tv_usec = a->tv_usec + b->tv_usec;
114 if (tv->tv_usec >= 1000000) {
116 tv->tv_usec -= 1000000;
121 tv_sub(struct timeval *tv, struct timeval *a, struct timeval *b)
123 tv->tv_sec = a->tv_sec - b->tv_sec;
124 tv->tv_usec = a->tv_usec - b->tv_usec;
125 if (((long) tv->tv_usec) < 0) {
127 tv->tv_usec += 1000000;
132 tv_div(struct timeval *tv, struct timeval *a, int n)
134 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
135 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
136 tv->tv_usec %= 1000000;
140 tv_mul(struct timeval *tv, struct timeval *a, int n)
142 tv->tv_usec = a->tv_usec * n;
143 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
144 tv->tv_usec %= 1000000;
148 xlookup(const struct xlat *xlat, int val)
150 for (; xlat->str != NULL; xlat++)
151 if (xlat->val == val)
156 #if !defined HAVE_STPCPY
158 stpcpy(char *dst, const char *src)
160 while ((*dst = *src++) != '\0')
167 * Used when we want to unblock stopped traced process.
168 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
169 * Returns 0 on success or if error was ESRCH
170 * (presumably process was killed while we talk to it).
171 * Otherwise prints error message and returns -1.
174 ptrace_restart(int op, struct tcb *tcp, int sig)
180 ptrace(op, tcp->pid, (void *) 0, (long) sig);
182 if (!err || err == ESRCH)
185 tcp->ptrace_errno = err;
187 if (op == PTRACE_CONT)
189 if (op == PTRACE_DETACH)
192 if (op == PTRACE_LISTEN)
195 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
200 * Print entry in struct xlat table, if there.
203 printxval(const struct xlat *xlat, int val, const char *dflt)
205 const char *str = xlookup(xlat, val);
210 tprintf("%#x /* %s */", val, dflt);
215 * Print 64bit argument at position llarg and return the index of the next
219 printllval(struct tcb *tcp, const char *format, int llarg)
221 # if defined(X86_64) || defined(POWERPC64)
222 if (current_personality == 0) {
223 tprintf(format, tcp->u_arg[llarg]);
227 /* Align 64bit argument to 64bit boundary. */
228 llarg = (llarg + 1) & 0x1e;
230 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
233 # elif defined IA64 || defined ALPHA
234 tprintf(format, tcp->u_arg[llarg]);
236 # elif defined LINUX_MIPSN32
237 tprintf(format, tcp->ext_arg[llarg]);
240 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
248 * Interpret `xlat' as an array of flags
249 * print the entries whose bits are on in `flags'
250 * return # of flags printed.
253 addflags(const struct xlat *xlat, int flags)
255 for (; xlat->str; xlat++) {
256 if (xlat->val && (flags & xlat->val) == xlat->val) {
257 tprintf("|%s", xlat->str);
262 tprintf("|%#x", flags);
267 * Interpret `xlat' as an array of flags.
268 * Print to static string the entries whose bits are on in `flags'
269 * Return static string.
272 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
274 static char outstr[1024];
278 outptr = stpcpy(outstr, prefix);
280 for (; xlat->str; xlat++) {
281 if ((flags & xlat->val) == xlat->val) {
284 outptr = stpcpy(outptr, xlat->str);
294 outptr += sprintf(outptr, "%#x", flags);
301 printflags(const struct xlat *xlat, int flags, const char *dflt)
306 if (flags == 0 && xlat->val == 0) {
312 for (n = 0; xlat->str; xlat++) {
313 if (xlat->val && (flags & xlat->val) == xlat->val) {
314 tprintf("%s%s", sep, xlat->str);
323 tprintf("%s%#x", sep, flags);
328 tprintf("%#x", flags);
330 tprintf(" /* %s */", dflt);
341 printnum(struct tcb *tcp, long addr, const char *fmt)
349 if (umove(tcp, addr, &num) < 0) {
350 tprintf("%#lx", addr);
359 printnum_int(struct tcb *tcp, long addr, const char *fmt)
367 if (umove(tcp, addr, &num) < 0) {
368 tprintf("%#lx", addr);
377 printfd(struct tcb *tcp, int fd)
381 if (show_fd_path && (p = getfdpath(tcp, fd)))
382 tprintf("%d<%s>", fd, p);
388 printuid(const char *text, unsigned long uid)
390 tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid);
394 * Quote string `instr' of length `size'
395 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
396 * If `len' < 0, treat `instr' as a NUL-terminated string
397 * and quote at most (`size' - 1) bytes.
399 * Returns 0 if len < 0 and NUL was seen, 1 otherwise.
400 * Note that if len >= 0, always returns 1.
403 string_quote(const char *instr, char *outstr, int len, int size)
405 const unsigned char *ustr = (const unsigned char *) instr;
407 int usehex, c, i, eol;
409 eol = 0x100; /* this can never match a char */
419 /* Check for presence of symbol which require
420 to hex-quote the whole string. */
421 for (i = 0; i < size; ++i) {
423 /* Check for NUL-terminated string. */
426 if (!isprint(c) && !isspace(c)) {
436 /* Hex-quote the whole string. */
437 for (i = 0; i < size; ++i) {
439 /* Check for NUL-terminated string. */
444 *s++ = "0123456789abcdef"[c >> 4];
445 *s++ = "0123456789abcdef"[c & 0xf];
448 for (i = 0; i < size; ++i) {
450 /* Check for NUL-terminated string. */
454 case '\"': case '\\':
485 && ustr[i + 1] >= '0'
486 && ustr[i + 1] <= '9'
489 *s++ = '0' + (c >> 6);
490 *s++ = '0' + ((c >> 3) & 0x7);
495 *s++ = '0' + (c >> 6);
496 *s++ = '0' + ((c >> 3) & 0x7);
499 *s++ = '0' + (c & 0x7);
509 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
510 if (len < 0 && ustr[i] == '\0') {
511 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
512 * but next char is NUL.
522 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
527 * Print path string specified by address `addr' and length `n'.
528 * If path length exceeds `n', append `...' to the output.
531 printpathn(struct tcb *tcp, long addr, int n)
533 char path[MAXPATHLEN + 1];
541 /* Cap path length to the path buffer size */
542 if (n > sizeof path - 1)
545 /* Fetch one byte more to find out whether path length > n. */
546 nul_seen = umovestr(tcp, addr, n + 1, path);
548 tprintf("%#lx", addr);
554 outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
555 string_quote(path, outstr, -1, n);
563 printpath(struct tcb *tcp, long addr)
565 /* Size must correspond to char path[] size in printpathn */
566 printpathn(tcp, addr, MAXPATHLEN);
570 * Print string specified by address `addr' and length `len'.
571 * If `len' < 0, treat the string as a NUL-terminated string.
572 * If string length exceeds `max_strlen', append `...' to the output.
575 printstr(struct tcb *tcp, long addr, int len)
577 static char *str = NULL;
586 /* Allocate static buffers if they are not allocated yet. */
588 str = malloc(max_strlen + 1);
591 outstr = malloc(4 * max_strlen + /*for quotes and NUL:*/ 3);
598 * Treat as a NUL-terminated string: fetch one byte more
599 * because string_quote() quotes one byte less.
601 size = max_strlen + 1;
602 if (umovestr(tcp, addr, size, str) < 0) {
603 tprintf("%#lx", addr);
608 size = MIN(len, max_strlen);
609 if (umoven(tcp, addr, size, str) < 0) {
610 tprintf("%#lx", addr);
615 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
616 * or we were requested to print more than -s NUM chars)...
618 ellipsis = (string_quote(str, outstr, len, size) &&
619 (len < 0 || len > max_strlen));
628 dumpiov(struct tcb *tcp, int len, long addr)
630 #if SUPPORTED_PERSONALITIES > 1
632 struct { u_int32_t base; u_int32_t len; } *iov32;
633 struct { u_int64_t base; u_int64_t len; } *iov64;
635 #define iov iovu.iov64
637 (personality_wordsize[current_personality] == 4 \
638 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
639 #define iov_iov_base(i) \
640 (personality_wordsize[current_personality] == 4 \
641 ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base)
642 #define iov_iov_len(i) \
643 (personality_wordsize[current_personality] == 4 \
644 ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len)
647 #define sizeof_iov sizeof(*iov)
648 #define iov_iov_base(i) iov[i].iov_base
649 #define iov_iov_len(i) iov[i].iov_len
654 size = sizeof_iov * len;
655 /* Assuming no sane program has millions of iovs */
656 if ((unsigned)len > 1024*1024 /* insane or negative size? */
657 || (iov = malloc(size)) == NULL) {
658 fprintf(stderr, "Out of memory\n");
661 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
662 for (i = 0; i < len; i++) {
663 /* include the buffer number to make it easy to
664 * match up the trace with the source */
665 tprintf(" * %lu bytes in buffer %d\n",
666 (unsigned long)iov_iov_len(i), i);
667 dumpstr(tcp, (long) iov_iov_base(i),
680 dumpstr(struct tcb *tcp, long addr, int len)
682 static int strsize = -1;
683 static unsigned char *str;
692 fprintf(stderr, "Out of memory\n");
698 if (umoven(tcp, addr, len, (char *) str) < 0)
701 for (i = 0; i < len; i += 16) {
705 sprintf(s, " | %05x ", i);
707 for (j = 0; j < 16; j++) {
711 sprintf(s, " %02x", str[i + j]);
715 *s++ = ' '; *s++ = ' '; *s++ = ' ';
718 *s++ = ' '; *s++ = ' ';
719 for (j = 0; j < 16; j++) {
723 if (isprint(str[i + j]))
731 tprintf("%s |\n", outstr);
735 #ifdef HAVE_PROCESS_VM_READV
736 /* C library supports this, but the kernel might not. */
737 static bool process_vm_readv_not_supported = 0;
740 /* Need to do this since process_vm_readv() is not yet available in libc.
741 * When libc is be updated, only "static bool process_vm_readv_not_supported"
742 * line should remain.
744 #if !defined(__NR_process_vm_readv)
746 # define __NR_process_vm_readv 347
747 # elif defined(X86_64)
748 # define __NR_process_vm_readv 310
749 # elif defined(POWERPC)
750 # define __NR_process_vm_readv 351
754 #if defined(__NR_process_vm_readv)
755 static bool process_vm_readv_not_supported = 0;
756 static ssize_t process_vm_readv(pid_t pid,
757 const struct iovec *lvec,
758 unsigned long liovcnt,
759 const struct iovec *rvec,
760 unsigned long riovcnt,
763 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
766 static bool process_vm_readv_not_supported = 1;
767 # define process_vm_readv(...) (errno = ENOSYS, -1)
770 #endif /* end of hack */
772 #define PAGMASK (~(PAGSIZ - 1))
774 * move `len' bytes of data from process `pid'
775 * at address `addr' to our space at `laddr'
778 umoven(struct tcb *tcp, long addr, int len, char *laddr)
785 char x[sizeof(long)];
788 #if SUPPORTED_PERSONALITIES > 1
789 if (personality_wordsize[current_personality] < sizeof(addr))
790 addr &= (1ul << 8 * personality_wordsize[current_personality]) - 1;
793 if (!process_vm_readv_not_supported) {
794 struct iovec local[1], remote[1];
797 local[0].iov_base = laddr;
798 remote[0].iov_base = (void*)addr;
799 local[0].iov_len = remote[0].iov_len = len;
800 r = process_vm_readv(pid,
807 process_vm_readv_not_supported = 1;
808 else if (errno != EINVAL) /* EINVAL is seen if process is gone */
810 perror("process_vm_readv");
811 goto vm_readv_didnt_work;
818 if (addr & (sizeof(long) - 1)) {
819 /* addr not a multiple of sizeof(long) */
820 n = addr - (addr & -sizeof(long)); /* residue */
821 addr &= -sizeof(long); /* residue */
823 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
825 /* But if not started, we had a bogus address. */
826 if (addr != 0 && errno != EIO && errno != ESRCH)
827 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
831 m = MIN(sizeof(long) - n, len);
832 memcpy(laddr, &u.x[n], m);
833 addr += sizeof(long), laddr += m, len -= m;
837 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
839 if (started && (errno==EPERM || errno==EIO)) {
840 /* Ran into 'end of memory' - stupid "printpath" */
843 if (addr != 0 && errno != EIO && errno != ESRCH)
844 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
848 m = MIN(sizeof(long), len);
849 memcpy(laddr, u.x, m);
850 addr += sizeof(long), laddr += m, len -= m;
857 * Like `umove' but make the additional effort of looking
858 * for a terminating zero byte.
860 * Returns < 0 on error, > 0 if NUL was seen,
861 * (TODO if useful: return count of bytes including NUL),
862 * else 0 if len bytes were read but no NUL byte seen.
864 * Note: there is no guarantee we won't overwrite some bytes
865 * in laddr[] _after_ terminating NUL (but, of course,
866 * we never write past laddr[len-1]).
869 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
876 char x[sizeof(long)];
879 #if SUPPORTED_PERSONALITIES > 1
880 if (personality_wordsize[current_personality] < sizeof(addr))
881 addr &= (1ul << 8 * personality_wordsize[current_personality]) - 1;
884 if (!process_vm_readv_not_supported) {
885 struct iovec local[1], remote[1];
887 local[0].iov_base = laddr;
888 remote[0].iov_base = (void*)addr;
895 /* Don't read kilobytes: most strings are short */
899 /* Don't cross pages. I guess otherwise we can get EFAULT
900 * and fail to notice that terminating NUL lies
901 * in the existing (first) page.
902 * (I hope there aren't arches with pages < 4K)
904 end_in_page = ((addr + chunk_len) & 4095);
905 r = chunk_len - end_in_page;
906 if (r > 0) /* if chunk_len > end_in_page */
907 chunk_len = r; /* chunk_len -= end_in_page */
909 local[0].iov_len = remote[0].iov_len = chunk_len;
910 r = process_vm_readv(pid,
917 process_vm_readv_not_supported = 1;
918 else if (errno != EINVAL) /* EINVAL is seen if process is gone */
920 perror("process_vm_readv");
921 goto vm_readv_didnt_work;
923 if (memchr(local[0].iov_base, '\0', r))
925 local[0].iov_base += r;
926 remote[0].iov_base += r;
934 if (addr & (sizeof(long) - 1)) {
935 /* addr not a multiple of sizeof(long) */
936 n = addr - (addr & -sizeof(long)); /* residue */
937 addr &= -sizeof(long); /* residue */
939 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
941 if (addr != 0 && errno != EIO && errno != ESRCH)
942 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
946 m = MIN(sizeof(long) - n, len);
947 memcpy(laddr, &u.x[n], m);
948 while (n & (sizeof(long) - 1))
949 if (u.x[n++] == '\0')
951 addr += sizeof(long), laddr += m, len -= m;
955 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
957 if (started && (errno==EPERM || errno==EIO)) {
958 /* Ran into 'end of memory' - stupid "printpath" */
961 if (addr != 0 && errno != EIO && errno != ESRCH)
962 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
966 m = MIN(sizeof(long), len);
967 memcpy(laddr, u.x, m);
968 for (i = 0; i < sizeof(long); i++)
971 addr += sizeof(long), laddr += m, len -= m;
976 #if !defined(SPARC) && !defined(SPARC64)
977 # define PTRACE_WRITETEXT 101
978 # define PTRACE_WRITEDATA 102
979 #endif /* !SPARC && !SPARC64 */
982 upeek(struct tcb *tcp, long off, long *res)
987 val = ptrace(PTRACE_PEEKUSER, tcp->pid, (char *) off, 0);
988 if (val == -1 && errno) {
989 if (errno != ESRCH) {
990 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", tcp->pid, off);
999 printcall(struct tcb *tcp)
1001 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
1002 sizeof(long) == 8 ? "[????????????????] " : \
1008 if (upeek(tcp, 4*EIP, &eip) < 0) {
1012 tprintf("[%08lx] ", eip);
1014 #elif defined(S390) || defined(S390X)
1016 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
1021 tprintf("[%08lx] ", psw);
1023 tprintf("[%16lx] ", psw);
1026 #elif defined(X86_64)
1029 if (upeek(tcp, 8*RIP, &rip) < 0) {
1033 tprintf("[%16lx] ", rip);
1037 if (upeek(tcp, PT_B0, &ip) < 0) {
1041 tprintf("[%08lx] ", ip);
1042 #elif defined(POWERPC)
1045 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
1050 tprintf("[%016lx] ", pc);
1052 tprintf("[%08lx] ", pc);
1057 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
1058 tprints("[????????] ");
1061 tprintf("[%08lx] ", pc);
1062 #elif defined(ALPHA)
1065 if (upeek(tcp, REG_PC, &pc) < 0) {
1066 tprints("[????????????????] ");
1069 tprintf("[%08lx] ", pc);
1070 #elif defined(SPARC) || defined(SPARC64)
1071 struct pt_regs regs;
1072 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1076 # if defined(SPARC64)
1077 tprintf("[%08lx] ", regs.tpc);
1079 tprintf("[%08lx] ", regs.pc);
1084 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
1085 tprints("[????????] ");
1088 tprintf("[%08lx] ", pc);
1092 if (upeek(tcp, REG_EPC, &pc) < 0) {
1093 tprints("[????????] ");
1096 tprintf("[%08lx] ", pc);
1100 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
1101 tprints("[????????] ");
1104 tprintf("[%08lx] ", pc);
1108 if (upeek(tcp, REG_PC, &pc) < 0) {
1109 tprints("[????????????????] ");
1112 tprintf("[%08lx] ", pc);
1116 if (upeek(tcp, 4*15, &pc) < 0) {
1120 tprintf("[%08lx] ", pc);
1121 #elif defined(AVR32)
1124 if (upeek(tcp, REG_PC, &pc) < 0) {
1125 tprints("[????????] ");
1128 tprintf("[%08lx] ", pc);
1132 if (upeek(tcp, PT_PC, &pc) < 0) {
1136 tprintf("[%08lx] ", pc);
1137 #elif defined(CRISV10)
1140 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
1144 tprintf("[%08lx] ", pc);
1145 #elif defined(CRISV32)
1148 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
1152 tprintf("[%08lx] ", pc);
1153 #endif /* architecture */
1157 * These #if's are huge, please indent them correctly.
1158 * It's easy to get confused otherwise.
1161 #include "syscall.h"
1163 #ifndef CLONE_PTRACE
1164 # define CLONE_PTRACE 0x00002000
1167 # define CLONE_VFORK 0x00004000
1170 # define CLONE_VM 0x00000100
1172 #ifndef CLONE_STOPPED
1173 # define CLONE_STOPPED 0x02000000
1178 typedef unsigned long *arg_setup_state;
1181 arg_setup(struct tcb *tcp, arg_setup_state *state)
1183 unsigned long cfm, sof, sol;
1187 /* Satisfy a false GCC warning. */
1192 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1194 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1197 sof = (cfm >> 0) & 0x7f;
1198 sol = (cfm >> 7) & 0x7f;
1199 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1201 *state = (unsigned long *) bsp;
1205 # define arg_finish_change(tcp, state) 0
1208 get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp)
1213 ret = upeek(tcp, PT_R11, valp);
1216 (unsigned long) ia64_rse_skip_regs(*state, 0),
1217 sizeof(long), (void *) valp);
1222 get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp)
1227 ret = upeek(tcp, PT_R9, valp);
1230 (unsigned long) ia64_rse_skip_regs(*state, 1),
1231 sizeof(long), (void *) valp);
1236 set_arg0(struct tcb *tcp, arg_setup_state *state, long val)
1238 int req = PTRACE_POKEDATA;
1242 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1243 req = PTRACE_POKEUSER;
1245 ap = ia64_rse_skip_regs(*state, 0);
1247 ptrace(req, tcp->pid, ap, val);
1248 return errno ? -1 : 0;
1252 set_arg1(struct tcb *tcp, arg_setup_state *state, long val)
1254 int req = PTRACE_POKEDATA;
1258 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1259 req = PTRACE_POKEUSER;
1261 ap = ia64_rse_skip_regs(*state, 1);
1263 ptrace(req, tcp->pid, ap, val);
1264 return errno ? -1 : 0;
1267 /* ia64 does not return the input arguments from functions (and syscalls)
1268 according to ia64 RSE (Register Stack Engine) behavior. */
1270 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1271 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1273 #elif defined(SPARC) || defined(SPARC64)
1275 typedef struct pt_regs arg_setup_state;
1277 # define arg_setup(tcp, state) \
1278 (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0))
1279 # define arg_finish_change(tcp, state) \
1280 (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0))
1282 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1283 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1284 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1285 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1286 # define restore_arg0(tcp, state, val) 0
1288 #else /* other architectures */
1290 # if defined S390 || defined S390X
1291 /* Note: this is only true for the `clone' system call, which handles
1292 arguments specially. We could as well say that its first two arguments
1293 are swapped relative to other architectures, but that would just be
1294 another #ifdef in the calls. */
1295 # define arg0_offset PT_GPR3
1296 # define arg1_offset PT_ORIGGPR2
1297 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1298 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1299 # define arg0_index 1
1300 # define arg1_index 0
1301 # elif defined(ALPHA) || defined(MIPS)
1302 # define arg0_offset REG_A0
1303 # define arg1_offset (REG_A0+1)
1304 # elif defined(AVR32)
1305 # define arg0_offset (REG_R12)
1306 # define arg1_offset (REG_R11)
1307 # elif defined(POWERPC)
1308 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1309 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1310 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1311 # elif defined(HPPA)
1312 # define arg0_offset PT_GR26
1313 # define arg1_offset (PT_GR26-4)
1314 # elif defined(X86_64)
1315 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1316 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1318 # define arg0_offset (4*(REG_REG0+4))
1319 # define arg1_offset (4*(REG_REG0+5))
1320 # elif defined(SH64)
1321 /* ABI defines arg0 & 1 in r2 & r3 */
1322 # define arg0_offset (REG_OFFSET+16)
1323 # define arg1_offset (REG_OFFSET+24)
1324 # define restore_arg0(tcp, state, val) 0
1325 # elif defined CRISV10 || defined CRISV32
1326 # define arg0_offset (4*PT_R11)
1327 # define arg1_offset (4*PT_ORIG_R10)
1328 # define restore_arg0(tcp, state, val) 0
1329 # define restore_arg1(tcp, state, val) 0
1330 # define arg0_index 1
1331 # define arg1_index 0
1333 # define arg0_offset 0
1334 # define arg1_offset 4
1336 # define restore_arg0(tcp, state, val) 0
1340 typedef int arg_setup_state;
1342 # define arg_setup(tcp, state) (0)
1343 # define arg_finish_change(tcp, state) 0
1344 # define get_arg0(tcp, cookie, valp) (upeek((tcp), arg0_offset, (valp)))
1345 # define get_arg1(tcp, cookie, valp) (upeek((tcp), arg1_offset, (valp)))
1348 set_arg0(struct tcb *tcp, void *cookie, long val)
1350 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1354 set_arg1(struct tcb *tcp, void *cookie, long val)
1356 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1359 #endif /* architectures */
1361 #ifndef restore_arg0
1362 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1364 #ifndef restore_arg1
1365 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1369 # define arg0_index 0
1370 # define arg1_index 1
1374 setbpt(struct tcb *tcp)
1376 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1377 arg_setup_state state;
1379 if (tcp->flags & TCB_BPTSET) {
1380 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1385 * It's a silly kludge to initialize this with a search at runtime.
1386 * But it's better than maintaining another magic thing in the
1387 * godforsaken tables.
1389 if (clone_scno[current_personality] == 0) {
1391 for (i = 0; i < nsyscalls; ++i)
1392 if (sysent[i].sys_func == sys_clone) {
1393 clone_scno[current_personality] = i;
1398 if (sysent[tcp->scno].sys_func == sys_fork ||
1399 sysent[tcp->scno].sys_func == sys_vfork) {
1400 if (arg_setup(tcp, &state) < 0
1401 || get_arg0(tcp, &state, &tcp->inst[0]) < 0
1402 || get_arg1(tcp, &state, &tcp->inst[1]) < 0
1403 || change_syscall(tcp, clone_scno[current_personality]) < 0
1404 || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1405 || set_arg1(tcp, &state, 0) < 0
1406 || arg_finish_change(tcp, &state) < 0)
1408 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1409 tcp->u_arg[arg1_index] = 0;
1410 tcp->flags |= TCB_BPTSET;
1414 if (sysent[tcp->scno].sys_func == sys_clone) {
1415 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1416 contrary to x86 vfork above. Even on x86 we turn the
1417 vfork semantics into plain fork - each application must not
1418 depend on the vfork specifics according to POSIX. We would
1419 hang waiting for the parent resume otherwise. We need to
1420 clear also CLONE_VM but only in the CLONE_VFORK case as
1421 otherwise we would break pthread_create. */
1423 long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE);
1424 if (new_arg0 & CLONE_VFORK)
1425 new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM);
1426 if (arg_setup(tcp, &state) < 0
1427 || set_arg0(tcp, &state, new_arg0) < 0
1428 || arg_finish_change(tcp, &state) < 0)
1430 tcp->flags |= TCB_BPTSET;
1431 tcp->inst[0] = tcp->u_arg[arg0_index];
1432 tcp->inst[1] = tcp->u_arg[arg1_index];
1436 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1437 tcp->scno, tcp->pid);
1442 clearbpt(struct tcb *tcp)
1444 arg_setup_state state;
1445 if (arg_setup(tcp, &state) < 0
1446 || restore_arg0(tcp, &state, tcp->inst[0]) < 0
1447 || restore_arg1(tcp, &state, tcp->inst[1]) < 0
1448 || arg_finish_change(tcp, &state))
1451 tcp->flags &= ~TCB_BPTSET;