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.
36 #include <sys/param.h>
42 #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1)
43 # include <linux/ptrace.h>
47 # include <asm/ptrace_offsets.h>
53 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
54 #elif defined(HAVE_LINUX_PTRACE_H)
55 # undef PTRACE_SYSCALL
56 # ifdef HAVE_STRUCT_IA64_FPREG
57 # define ia64_fpreg XXX_ia64_fpreg
59 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
60 # define pt_all_user_regs XXX_pt_all_user_regs
62 # include <linux/ptrace.h>
64 # undef pt_all_user_regs
68 # undef PTRACE_GETREGS
69 # define PTRACE_GETREGS PTRACE_GETREGS64
70 # undef PTRACE_SETREGS
71 # define PTRACE_SETREGS PTRACE_SETREGS64
76 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
79 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
83 tv_nz(struct timeval *a)
85 return a->tv_sec || a->tv_usec;
89 tv_cmp(struct timeval *a, struct timeval *b)
91 if (a->tv_sec < b->tv_sec
92 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
94 if (a->tv_sec > b->tv_sec
95 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
101 tv_float(struct timeval *tv)
103 return tv->tv_sec + tv->tv_usec/1000000.0;
107 tv_add(struct timeval *tv, struct timeval *a, struct timeval *b)
109 tv->tv_sec = a->tv_sec + b->tv_sec;
110 tv->tv_usec = a->tv_usec + b->tv_usec;
111 if (tv->tv_usec >= 1000000) {
113 tv->tv_usec -= 1000000;
118 tv_sub(struct timeval *tv, struct timeval *a, struct timeval *b)
120 tv->tv_sec = a->tv_sec - b->tv_sec;
121 tv->tv_usec = a->tv_usec - b->tv_usec;
122 if (((long) tv->tv_usec) < 0) {
124 tv->tv_usec += 1000000;
129 tv_div(struct timeval *tv, struct timeval *a, int n)
131 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
132 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
133 tv->tv_usec %= 1000000;
137 tv_mul(struct timeval *tv, struct timeval *a, int n)
139 tv->tv_usec = a->tv_usec * n;
140 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
141 tv->tv_usec %= 1000000;
145 xlookup(const struct xlat *xlat, int val)
147 for (; xlat->str != NULL; xlat++)
148 if (xlat->val == val)
153 #if !defined HAVE_STPCPY
155 stpcpy(char *dst, const char *src)
157 while ((*dst = *src++) != '\0')
164 * Used when we want to unblock stopped traced process.
165 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
166 * Returns 0 on success or if error was ESRCH
167 * (presumably process was killed while we talk to it).
168 * Otherwise prints error message and returns -1.
171 ptrace_restart(int op, struct tcb *tcp, int sig)
177 ptrace(op, tcp->pid, (void *) 0, (long) sig);
179 if (!err || err == ESRCH)
182 tcp->ptrace_errno = err;
184 if (op == PTRACE_CONT)
186 if (op == PTRACE_DETACH)
189 if (op == PTRACE_LISTEN)
192 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
197 * Print entry in struct xlat table, if there.
200 printxval(const struct xlat *xlat, int val, const char *dflt)
202 const char *str = xlookup(xlat, val);
207 tprintf("%#x /* %s */", val, dflt);
212 * Print 64bit argument at position llarg and return the index of the next
216 printllval(struct tcb *tcp, const char *format, int llarg)
218 # if defined(X86_64) || defined(POWERPC64)
219 if (current_personality == 0) {
220 tprintf(format, tcp->u_arg[llarg]);
224 /* Align 64bit argument to 64bit boundary. */
225 llarg = (llarg + 1) & 0x1e;
227 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
230 # elif defined IA64 || defined ALPHA
231 tprintf(format, tcp->u_arg[llarg]);
233 # elif defined LINUX_MIPSN32
234 tprintf(format, tcp->ext_arg[llarg]);
237 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
245 * Interpret `xlat' as an array of flags
246 * print the entries whose bits are on in `flags'
247 * return # of flags printed.
250 addflags(const struct xlat *xlat, int flags)
252 for (; xlat->str; xlat++) {
253 if (xlat->val && (flags & xlat->val) == xlat->val) {
254 tprintf("|%s", xlat->str);
259 tprintf("|%#x", flags);
264 * Interpret `xlat' as an array of flags.
265 * Print to static string the entries whose bits are on in `flags'
266 * Return static string.
269 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
271 static char outstr[1024];
275 outptr = stpcpy(outstr, prefix);
277 for (; xlat->str; xlat++) {
278 if ((flags & xlat->val) == xlat->val) {
281 outptr = stpcpy(outptr, xlat->str);
291 outptr += sprintf(outptr, "%#x", flags);
298 printflags(const struct xlat *xlat, int flags, const char *dflt)
303 if (flags == 0 && xlat->val == 0) {
309 for (n = 0; xlat->str; xlat++) {
310 if (xlat->val && (flags & xlat->val) == xlat->val) {
311 tprintf("%s%s", sep, xlat->str);
320 tprintf("%s%#x", sep, flags);
325 tprintf("%#x", flags);
327 tprintf(" /* %s */", dflt);
338 printnum(struct tcb *tcp, long addr, const char *fmt)
346 if (umove(tcp, addr, &num) < 0) {
347 tprintf("%#lx", addr);
356 printnum_int(struct tcb *tcp, long addr, const char *fmt)
364 if (umove(tcp, addr, &num) < 0) {
365 tprintf("%#lx", addr);
374 printfd(struct tcb *tcp, int fd)
378 if (show_fd_path && (p = getfdpath(tcp, fd)))
379 tprintf("%d<%s>", fd, p);
385 printuid(const char *text, unsigned long uid)
387 tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid);
391 * Quote string `instr' of length `size'
392 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
393 * If `len' < 0, treat `instr' as a NUL-terminated string
394 * and quote at most (`size' - 1) bytes.
396 * Returns 0 if len < 0 and NUL was seen, 1 otherwise.
397 * Note that if len >= 0, always returns 1.
400 string_quote(const char *instr, char *outstr, int len, int size)
402 const unsigned char *ustr = (const unsigned char *) instr;
404 int usehex, c, i, eol;
406 eol = 0x100; /* this can never match a char */
416 /* Check for presence of symbol which require
417 to hex-quote the whole string. */
418 for (i = 0; i < size; ++i) {
420 /* Check for NUL-terminated string. */
423 if (!isprint(c) && !isspace(c)) {
433 /* Hex-quote the whole string. */
434 for (i = 0; i < size; ++i) {
436 /* Check for NUL-terminated string. */
441 *s++ = "0123456789abcdef"[c >> 4];
442 *s++ = "0123456789abcdef"[c & 0xf];
445 for (i = 0; i < size; ++i) {
447 /* Check for NUL-terminated string. */
451 case '\"': case '\\':
482 && ustr[i + 1] >= '0'
483 && ustr[i + 1] <= '9'
486 *s++ = '0' + (c >> 6);
487 *s++ = '0' + ((c >> 3) & 0x7);
492 *s++ = '0' + (c >> 6);
493 *s++ = '0' + ((c >> 3) & 0x7);
496 *s++ = '0' + (c & 0x7);
506 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
507 if (len < 0 && ustr[i] == '\0') {
508 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
509 * but next char is NUL.
519 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
524 * Print path string specified by address `addr' and length `n'.
525 * If path length exceeds `n', append `...' to the output.
528 printpathn(struct tcb *tcp, long addr, int n)
530 char path[MAXPATHLEN + 1];
538 /* Cap path length to the path buffer size */
539 if (n > sizeof path - 1)
542 /* Fetch one byte more to find out whether path length > n. */
543 nul_seen = umovestr(tcp, addr, n + 1, path);
545 tprintf("%#lx", addr);
551 outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
552 string_quote(path, outstr, -1, n);
560 printpath(struct tcb *tcp, long addr)
562 /* Size must correspond to char path[] size in printpathn */
563 printpathn(tcp, addr, MAXPATHLEN);
567 * Print string specified by address `addr' and length `len'.
568 * If `len' < 0, treat the string as a NUL-terminated string.
569 * If string length exceeds `max_strlen', append `...' to the output.
572 printstr(struct tcb *tcp, long addr, int len)
574 static char *str = NULL;
583 /* Allocate static buffers if they are not allocated yet. */
585 str = malloc(max_strlen + 1);
588 outstr = malloc(4 * max_strlen + /*for quotes and NUL:*/ 3);
595 * Treat as a NUL-terminated string: fetch one byte more
596 * because string_quote() quotes one byte less.
598 size = max_strlen + 1;
599 if (umovestr(tcp, addr, size, str) < 0) {
600 tprintf("%#lx", addr);
605 size = MIN(len, max_strlen);
606 if (umoven(tcp, addr, size, str) < 0) {
607 tprintf("%#lx", addr);
612 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
613 * or we were requested to print more than -s NUM chars)...
615 ellipsis = (string_quote(str, outstr, len, size) &&
616 (len < 0 || len > max_strlen));
625 dumpiov(struct tcb *tcp, int len, long addr)
627 #if SUPPORTED_PERSONALITIES > 1
629 struct { u_int32_t base; u_int32_t len; } *iov32;
630 struct { u_int64_t base; u_int64_t len; } *iov64;
632 #define iov iovu.iov64
634 (personality_wordsize[current_personality] == 4 \
635 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
636 #define iov_iov_base(i) \
637 (personality_wordsize[current_personality] == 4 \
638 ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base)
639 #define iov_iov_len(i) \
640 (personality_wordsize[current_personality] == 4 \
641 ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len)
644 #define sizeof_iov sizeof(*iov)
645 #define iov_iov_base(i) iov[i].iov_base
646 #define iov_iov_len(i) iov[i].iov_len
651 size = sizeof_iov * len;
652 /* Assuming no sane program has millions of iovs */
653 if ((unsigned)len > 1024*1024 /* insane or negative size? */
654 || (iov = malloc(size)) == NULL) {
655 fprintf(stderr, "Out of memory\n");
658 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
659 for (i = 0; i < len; i++) {
660 /* include the buffer number to make it easy to
661 * match up the trace with the source */
662 tprintf(" * %lu bytes in buffer %d\n",
663 (unsigned long)iov_iov_len(i), i);
664 dumpstr(tcp, (long) iov_iov_base(i),
677 dumpstr(struct tcb *tcp, long addr, int len)
679 static int strsize = -1;
680 static unsigned char *str;
689 fprintf(stderr, "Out of memory\n");
695 if (umoven(tcp, addr, len, (char *) str) < 0)
698 for (i = 0; i < len; i += 16) {
702 sprintf(s, " | %05x ", i);
704 for (j = 0; j < 16; j++) {
708 sprintf(s, " %02x", str[i + j]);
712 *s++ = ' '; *s++ = ' '; *s++ = ' ';
715 *s++ = ' '; *s++ = ' ';
716 for (j = 0; j < 16; j++) {
720 if (isprint(str[i + j]))
728 tprintf("%s |\n", outstr);
732 #ifdef HAVE_PROCESS_VM_READV
733 /* C library supports this, but the kernel might not. */
734 static bool process_vm_readv_not_supported = 0;
737 /* Need to do this since process_vm_readv() is not yet available in libc.
738 * When libc is be updated, only "static bool process_vm_readv_not_supported"
739 * line should remain.
741 #if !defined(__NR_process_vm_readv)
743 # define __NR_process_vm_readv 347
744 # elif defined(X86_64)
745 # define __NR_process_vm_readv 310
746 # elif defined(POWERPC)
747 # define __NR_process_vm_readv 351
751 #if defined(__NR_process_vm_readv)
752 static bool process_vm_readv_not_supported = 0;
753 static ssize_t process_vm_readv(pid_t pid,
754 const struct iovec *lvec,
755 unsigned long liovcnt,
756 const struct iovec *rvec,
757 unsigned long riovcnt,
760 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
763 static bool process_vm_readv_not_supported = 1;
764 # define process_vm_readv(...) (errno = ENOSYS, -1)
767 #endif /* end of hack */
769 #define PAGMASK (~(PAGSIZ - 1))
771 * move `len' bytes of data from process `pid'
772 * at address `addr' to our space at `laddr'
775 umoven(struct tcb *tcp, long addr, int len, char *laddr)
782 char x[sizeof(long)];
785 #if SUPPORTED_PERSONALITIES > 1
786 if (personality_wordsize[current_personality] < sizeof(addr))
787 addr &= (1ul << 8 * personality_wordsize[current_personality]) - 1;
790 if (!process_vm_readv_not_supported) {
791 struct iovec local[1], remote[1];
794 local[0].iov_base = laddr;
795 remote[0].iov_base = (void*)addr;
796 local[0].iov_len = remote[0].iov_len = len;
797 r = process_vm_readv(pid,
804 process_vm_readv_not_supported = 1;
805 else if (errno != EINVAL) /* EINVAL is seen if process is gone */
807 perror("process_vm_readv");
808 goto vm_readv_didnt_work;
815 if (addr & (sizeof(long) - 1)) {
816 /* addr not a multiple of sizeof(long) */
817 n = addr - (addr & -sizeof(long)); /* residue */
818 addr &= -sizeof(long); /* residue */
820 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
822 /* But if not started, we had a bogus address. */
823 if (addr != 0 && errno != EIO && errno != ESRCH)
824 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
828 m = MIN(sizeof(long) - n, len);
829 memcpy(laddr, &u.x[n], m);
830 addr += sizeof(long), laddr += m, len -= m;
834 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
836 if (started && (errno==EPERM || errno==EIO)) {
837 /* Ran into 'end of memory' - stupid "printpath" */
840 if (addr != 0 && errno != EIO && errno != ESRCH)
841 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
845 m = MIN(sizeof(long), len);
846 memcpy(laddr, u.x, m);
847 addr += sizeof(long), laddr += m, len -= m;
854 * Like `umove' but make the additional effort of looking
855 * for a terminating zero byte.
857 * Returns < 0 on error, > 0 if NUL was seen,
858 * (TODO if useful: return count of bytes including NUL),
859 * else 0 if len bytes were read but no NUL byte seen.
861 * Note: there is no guarantee we won't overwrite some bytes
862 * in laddr[] _after_ terminating NUL (but, of course,
863 * we never write past laddr[len-1]).
866 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
873 char x[sizeof(long)];
876 #if SUPPORTED_PERSONALITIES > 1
877 if (personality_wordsize[current_personality] < sizeof(addr))
878 addr &= (1ul << 8 * personality_wordsize[current_personality]) - 1;
881 if (!process_vm_readv_not_supported) {
882 struct iovec local[1], remote[1];
884 local[0].iov_base = laddr;
885 remote[0].iov_base = (void*)addr;
892 /* Don't read kilobytes: most strings are short */
896 /* Don't cross pages. I guess otherwise we can get EFAULT
897 * and fail to notice that terminating NUL lies
898 * in the existing (first) page.
899 * (I hope there aren't arches with pages < 4K)
901 end_in_page = ((addr + chunk_len) & 4095);
902 r = chunk_len - end_in_page;
903 if (r > 0) /* if chunk_len > end_in_page */
904 chunk_len = r; /* chunk_len -= end_in_page */
906 local[0].iov_len = remote[0].iov_len = chunk_len;
907 r = process_vm_readv(pid,
914 process_vm_readv_not_supported = 1;
915 else if (errno != EINVAL) /* EINVAL is seen if process is gone */
917 perror("process_vm_readv");
918 goto vm_readv_didnt_work;
920 if (memchr(local[0].iov_base, '\0', r))
922 local[0].iov_base += r;
923 remote[0].iov_base += r;
931 if (addr & (sizeof(long) - 1)) {
932 /* addr not a multiple of sizeof(long) */
933 n = addr - (addr & -sizeof(long)); /* residue */
934 addr &= -sizeof(long); /* residue */
936 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
938 if (addr != 0 && errno != EIO && errno != ESRCH)
939 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
943 m = MIN(sizeof(long) - n, len);
944 memcpy(laddr, &u.x[n], m);
945 while (n & (sizeof(long) - 1))
946 if (u.x[n++] == '\0')
948 addr += sizeof(long), laddr += m, len -= m;
952 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
954 if (started && (errno==EPERM || errno==EIO)) {
955 /* Ran into 'end of memory' - stupid "printpath" */
958 if (addr != 0 && errno != EIO && errno != ESRCH)
959 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
963 m = MIN(sizeof(long), len);
964 memcpy(laddr, u.x, m);
965 for (i = 0; i < sizeof(long); i++)
968 addr += sizeof(long), laddr += m, len -= m;
973 #if !defined(SPARC) && !defined(SPARC64)
974 # define PTRACE_WRITETEXT 101
975 # define PTRACE_WRITEDATA 102
976 #endif /* !SPARC && !SPARC64 */
979 upeek(struct tcb *tcp, long off, long *res)
984 val = ptrace(PTRACE_PEEKUSER, tcp->pid, (char *) off, 0);
985 if (val == -1 && errno) {
986 if (errno != ESRCH) {
987 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", tcp->pid, off);
996 printcall(struct tcb *tcp)
998 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
999 sizeof(long) == 8 ? "[????????????????] " : \
1005 if (upeek(tcp, 4*EIP, &eip) < 0) {
1009 tprintf("[%08lx] ", eip);
1011 #elif defined(S390) || defined(S390X)
1013 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
1018 tprintf("[%08lx] ", psw);
1020 tprintf("[%16lx] ", psw);
1023 #elif defined(X86_64)
1026 if (upeek(tcp, 8*RIP, &rip) < 0) {
1030 tprintf("[%16lx] ", rip);
1034 if (upeek(tcp, PT_B0, &ip) < 0) {
1038 tprintf("[%08lx] ", ip);
1039 #elif defined(POWERPC)
1042 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
1047 tprintf("[%016lx] ", pc);
1049 tprintf("[%08lx] ", pc);
1054 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
1055 tprints("[????????] ");
1058 tprintf("[%08lx] ", pc);
1059 #elif defined(ALPHA)
1062 if (upeek(tcp, REG_PC, &pc) < 0) {
1063 tprints("[????????????????] ");
1066 tprintf("[%08lx] ", pc);
1067 #elif defined(SPARC) || defined(SPARC64)
1068 struct pt_regs regs;
1069 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1073 # if defined(SPARC64)
1074 tprintf("[%08lx] ", regs.tpc);
1076 tprintf("[%08lx] ", regs.pc);
1081 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
1082 tprints("[????????] ");
1085 tprintf("[%08lx] ", pc);
1089 if (upeek(tcp, REG_EPC, &pc) < 0) {
1090 tprints("[????????] ");
1093 tprintf("[%08lx] ", pc);
1097 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
1098 tprints("[????????] ");
1101 tprintf("[%08lx] ", pc);
1105 if (upeek(tcp, REG_PC, &pc) < 0) {
1106 tprints("[????????????????] ");
1109 tprintf("[%08lx] ", pc);
1113 if (upeek(tcp, 4*15, &pc) < 0) {
1117 tprintf("[%08lx] ", pc);
1118 #elif defined(AVR32)
1121 if (upeek(tcp, REG_PC, &pc) < 0) {
1122 tprints("[????????] ");
1125 tprintf("[%08lx] ", pc);
1129 if (upeek(tcp, PT_PC, &pc) < 0) {
1133 tprintf("[%08lx] ", pc);
1134 #elif defined(CRISV10)
1137 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
1141 tprintf("[%08lx] ", pc);
1142 #elif defined(CRISV32)
1145 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
1149 tprintf("[%08lx] ", pc);
1150 #endif /* architecture */
1154 * These #if's are huge, please indent them correctly.
1155 * It's easy to get confused otherwise.
1158 #include "syscall.h"
1160 #ifndef CLONE_PTRACE
1161 # define CLONE_PTRACE 0x00002000
1164 # define CLONE_VFORK 0x00004000
1167 # define CLONE_VM 0x00000100
1169 #ifndef CLONE_STOPPED
1170 # define CLONE_STOPPED 0x02000000
1175 typedef unsigned long *arg_setup_state;
1178 arg_setup(struct tcb *tcp, arg_setup_state *state)
1180 unsigned long cfm, sof, sol;
1184 /* Satisfy a false GCC warning. */
1189 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1191 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1194 sof = (cfm >> 0) & 0x7f;
1195 sol = (cfm >> 7) & 0x7f;
1196 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1198 *state = (unsigned long *) bsp;
1202 # define arg_finish_change(tcp, state) 0
1205 get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp)
1210 ret = upeek(tcp, PT_R11, valp);
1213 (unsigned long) ia64_rse_skip_regs(*state, 0),
1214 sizeof(long), (void *) valp);
1219 get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp)
1224 ret = upeek(tcp, PT_R9, valp);
1227 (unsigned long) ia64_rse_skip_regs(*state, 1),
1228 sizeof(long), (void *) valp);
1233 set_arg0(struct tcb *tcp, arg_setup_state *state, long val)
1235 int req = PTRACE_POKEDATA;
1239 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1240 req = PTRACE_POKEUSER;
1242 ap = ia64_rse_skip_regs(*state, 0);
1244 ptrace(req, tcp->pid, ap, val);
1245 return errno ? -1 : 0;
1249 set_arg1(struct tcb *tcp, arg_setup_state *state, long val)
1251 int req = PTRACE_POKEDATA;
1255 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1256 req = PTRACE_POKEUSER;
1258 ap = ia64_rse_skip_regs(*state, 1);
1260 ptrace(req, tcp->pid, ap, val);
1261 return errno ? -1 : 0;
1264 /* ia64 does not return the input arguments from functions (and syscalls)
1265 according to ia64 RSE (Register Stack Engine) behavior. */
1267 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1268 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1270 #elif defined(SPARC) || defined(SPARC64)
1272 typedef struct pt_regs arg_setup_state;
1274 # define arg_setup(tcp, state) \
1275 (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0))
1276 # define arg_finish_change(tcp, state) \
1277 (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0))
1279 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1280 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1281 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1282 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1283 # define restore_arg0(tcp, state, val) 0
1285 #else /* other architectures */
1287 # if defined S390 || defined S390X
1288 /* Note: this is only true for the `clone' system call, which handles
1289 arguments specially. We could as well say that its first two arguments
1290 are swapped relative to other architectures, but that would just be
1291 another #ifdef in the calls. */
1292 # define arg0_offset PT_GPR3
1293 # define arg1_offset PT_ORIGGPR2
1294 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1295 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1296 # define arg0_index 1
1297 # define arg1_index 0
1298 # elif defined(ALPHA) || defined(MIPS)
1299 # define arg0_offset REG_A0
1300 # define arg1_offset (REG_A0+1)
1301 # elif defined(AVR32)
1302 # define arg0_offset (REG_R12)
1303 # define arg1_offset (REG_R11)
1304 # elif defined(POWERPC)
1305 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1306 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1307 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1308 # elif defined(HPPA)
1309 # define arg0_offset PT_GR26
1310 # define arg1_offset (PT_GR26-4)
1311 # elif defined(X86_64)
1312 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1313 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1315 # define arg0_offset (4*(REG_REG0+4))
1316 # define arg1_offset (4*(REG_REG0+5))
1317 # elif defined(SH64)
1318 /* ABI defines arg0 & 1 in r2 & r3 */
1319 # define arg0_offset (REG_OFFSET+16)
1320 # define arg1_offset (REG_OFFSET+24)
1321 # define restore_arg0(tcp, state, val) 0
1322 # elif defined CRISV10 || defined CRISV32
1323 # define arg0_offset (4*PT_R11)
1324 # define arg1_offset (4*PT_ORIG_R10)
1325 # define restore_arg0(tcp, state, val) 0
1326 # define restore_arg1(tcp, state, val) 0
1327 # define arg0_index 1
1328 # define arg1_index 0
1330 # define arg0_offset 0
1331 # define arg1_offset 4
1333 # define restore_arg0(tcp, state, val) 0
1337 typedef int arg_setup_state;
1339 # define arg_setup(tcp, state) (0)
1340 # define arg_finish_change(tcp, state) 0
1341 # define get_arg0(tcp, cookie, valp) (upeek((tcp), arg0_offset, (valp)))
1342 # define get_arg1(tcp, cookie, valp) (upeek((tcp), arg1_offset, (valp)))
1345 set_arg0(struct tcb *tcp, void *cookie, long val)
1347 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1351 set_arg1(struct tcb *tcp, void *cookie, long val)
1353 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1356 #endif /* architectures */
1358 #ifndef restore_arg0
1359 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1361 #ifndef restore_arg1
1362 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1366 # define arg0_index 0
1367 # define arg1_index 1
1371 setbpt(struct tcb *tcp)
1373 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1374 arg_setup_state state;
1376 if (tcp->flags & TCB_BPTSET) {
1377 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1382 * It's a silly kludge to initialize this with a search at runtime.
1383 * But it's better than maintaining another magic thing in the
1384 * godforsaken tables.
1386 if (clone_scno[current_personality] == 0) {
1388 for (i = 0; i < nsyscalls; ++i)
1389 if (sysent[i].sys_func == sys_clone) {
1390 clone_scno[current_personality] = i;
1395 if (sysent[tcp->scno].sys_func == sys_fork ||
1396 sysent[tcp->scno].sys_func == sys_vfork) {
1397 if (arg_setup(tcp, &state) < 0
1398 || get_arg0(tcp, &state, &tcp->inst[0]) < 0
1399 || get_arg1(tcp, &state, &tcp->inst[1]) < 0
1400 || change_syscall(tcp, clone_scno[current_personality]) < 0
1401 || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1402 || set_arg1(tcp, &state, 0) < 0
1403 || arg_finish_change(tcp, &state) < 0)
1405 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1406 tcp->u_arg[arg1_index] = 0;
1407 tcp->flags |= TCB_BPTSET;
1411 if (sysent[tcp->scno].sys_func == sys_clone) {
1412 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1413 contrary to x86 vfork above. Even on x86 we turn the
1414 vfork semantics into plain fork - each application must not
1415 depend on the vfork specifics according to POSIX. We would
1416 hang waiting for the parent resume otherwise. We need to
1417 clear also CLONE_VM but only in the CLONE_VFORK case as
1418 otherwise we would break pthread_create. */
1420 long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE);
1421 if (new_arg0 & CLONE_VFORK)
1422 new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM);
1423 if (arg_setup(tcp, &state) < 0
1424 || set_arg0(tcp, &state, new_arg0) < 0
1425 || arg_finish_change(tcp, &state) < 0)
1427 tcp->flags |= TCB_BPTSET;
1428 tcp->inst[0] = tcp->u_arg[arg0_index];
1429 tcp->inst[1] = tcp->u_arg[arg1_index];
1433 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1434 tcp->scno, tcp->pid);
1439 clearbpt(struct tcb *tcp)
1441 arg_setup_state state;
1442 if (arg_setup(tcp, &state) < 0
1443 || restore_arg0(tcp, &state, tcp->inst[0]) < 0
1444 || restore_arg1(tcp, &state, tcp->inst[1]) < 0
1445 || arg_finish_change(tcp, &state))
1448 tcp->flags &= ~TCB_BPTSET;