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>
43 # include <asm/ptrace_offsets.h>
49 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
50 #elif defined(HAVE_LINUX_PTRACE_H)
51 # undef PTRACE_SYSCALL
52 # ifdef HAVE_STRUCT_IA64_FPREG
53 # define ia64_fpreg XXX_ia64_fpreg
55 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
56 # define pt_all_user_regs XXX_pt_all_user_regs
58 # include <linux/ptrace.h>
60 # undef pt_all_user_regs
65 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
68 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
72 string_to_uint(const char *str)
80 value = strtol(str, &error, 10);
81 if (errno || *error || value < 0 || (long)(int)value != value)
87 tv_nz(struct timeval *a)
89 return a->tv_sec || a->tv_usec;
93 tv_cmp(struct timeval *a, struct timeval *b)
95 if (a->tv_sec < b->tv_sec
96 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
98 if (a->tv_sec > b->tv_sec
99 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
105 tv_float(struct timeval *tv)
107 return tv->tv_sec + tv->tv_usec/1000000.0;
111 tv_add(struct timeval *tv, struct timeval *a, struct timeval *b)
113 tv->tv_sec = a->tv_sec + b->tv_sec;
114 tv->tv_usec = a->tv_usec + b->tv_usec;
115 if (tv->tv_usec >= 1000000) {
117 tv->tv_usec -= 1000000;
122 tv_sub(struct timeval *tv, struct timeval *a, struct timeval *b)
124 tv->tv_sec = a->tv_sec - b->tv_sec;
125 tv->tv_usec = a->tv_usec - b->tv_usec;
126 if (((long) tv->tv_usec) < 0) {
128 tv->tv_usec += 1000000;
133 tv_div(struct timeval *tv, struct timeval *a, int n)
135 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
136 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
137 tv->tv_usec %= 1000000;
141 tv_mul(struct timeval *tv, struct timeval *a, int n)
143 tv->tv_usec = a->tv_usec * n;
144 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
145 tv->tv_usec %= 1000000;
149 xlookup(const struct xlat *xlat, int val)
151 for (; xlat->str != NULL; xlat++)
152 if (xlat->val == val)
157 #if !defined HAVE_STPCPY
159 stpcpy(char *dst, const char *src)
161 while ((*dst = *src++) != '\0')
168 * Print entry in struct xlat table, if there.
171 printxval(const struct xlat *xlat, int val, const char *dflt)
173 const char *str = xlookup(xlat, val);
178 tprintf("%#x /* %s */", val, dflt);
183 * Print 64bit argument at position llarg and return the index of the next
187 printllval(struct tcb *tcp, const char *format, int llarg)
189 # if defined(X86_64) || defined(POWERPC64)
190 if (current_personality == 0) {
191 tprintf(format, tcp->u_arg[llarg]);
195 /* Align 64bit argument to 64bit boundary. */
196 llarg = (llarg + 1) & 0x1e;
198 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
201 # elif defined IA64 || defined ALPHA
202 tprintf(format, tcp->u_arg[llarg]);
204 # elif defined LINUX_MIPSN32 || defined X32
205 tprintf(format, tcp->ext_arg[llarg]);
208 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
216 * Interpret `xlat' as an array of flags
217 * print the entries whose bits are on in `flags'
218 * return # of flags printed.
221 addflags(const struct xlat *xlat, int flags)
223 for (; xlat->str; xlat++) {
224 if (xlat->val && (flags & xlat->val) == xlat->val) {
225 tprintf("|%s", xlat->str);
230 tprintf("|%#x", flags);
235 * Interpret `xlat' as an array of flags.
236 * Print to static string the entries whose bits are on in `flags'
237 * Return static string.
240 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
242 static char outstr[1024];
246 outptr = stpcpy(outstr, prefix);
248 for (; xlat->str; xlat++) {
249 if ((flags & xlat->val) == xlat->val) {
252 outptr = stpcpy(outptr, xlat->str);
262 outptr += sprintf(outptr, "%#x", flags);
269 printflags(const struct xlat *xlat, int flags, const char *dflt)
274 if (flags == 0 && xlat->val == 0) {
280 for (n = 0; xlat->str; xlat++) {
281 if (xlat->val && (flags & xlat->val) == xlat->val) {
282 tprintf("%s%s", sep, xlat->str);
291 tprintf("%s%#x", sep, flags);
296 tprintf("%#x", flags);
298 tprintf(" /* %s */", dflt);
309 printnum(struct tcb *tcp, long addr, const char *fmt)
317 if (umove(tcp, addr, &num) < 0) {
318 tprintf("%#lx", addr);
327 printnum_int(struct tcb *tcp, long addr, const char *fmt)
335 if (umove(tcp, addr, &num) < 0) {
336 tprintf("%#lx", addr);
345 printfd(struct tcb *tcp, int fd)
349 if (show_fd_path && (p = getfdpath(tcp, fd)))
350 tprintf("%d<%s>", fd, p);
356 printuid(const char *text, unsigned long uid)
358 tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid);
362 * Quote string `instr' of length `size'
363 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
364 * If `len' is -1, treat `instr' as a NUL-terminated string
365 * and quote at most (`size' - 1) bytes.
367 * Returns 0 if len == -1 and NUL was seen, 1 otherwise.
368 * Note that if len >= 0, always returns 1.
371 string_quote(const char *instr, char *outstr, long len, int size)
373 const unsigned char *ustr = (const unsigned char *) instr;
375 int usehex, c, i, eol;
377 eol = 0x100; /* this can never match a char */
387 /* Check for presence of symbol which require
388 to hex-quote the whole string. */
389 for (i = 0; i < size; ++i) {
391 /* Check for NUL-terminated string. */
394 if (!isprint(c) && !isspace(c)) {
404 /* Hex-quote the whole string. */
405 for (i = 0; i < size; ++i) {
407 /* Check for NUL-terminated string. */
412 *s++ = "0123456789abcdef"[c >> 4];
413 *s++ = "0123456789abcdef"[c & 0xf];
416 for (i = 0; i < size; ++i) {
418 /* Check for NUL-terminated string. */
422 case '\"': case '\\':
453 && ustr[i + 1] >= '0'
454 && ustr[i + 1] <= '9'
457 *s++ = '0' + (c >> 6);
458 *s++ = '0' + ((c >> 3) & 0x7);
463 *s++ = '0' + (c >> 6);
464 *s++ = '0' + ((c >> 3) & 0x7);
467 *s++ = '0' + (c & 0x7);
477 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
478 if (len == -1 && ustr[i] == '\0') {
479 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
480 * but next char is NUL.
490 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
495 * Print path string specified by address `addr' and length `n'.
496 * If path length exceeds `n', append `...' to the output.
499 printpathn(struct tcb *tcp, long addr, int n)
501 char path[MAXPATHLEN + 1];
509 /* Cap path length to the path buffer size */
510 if (n > sizeof path - 1)
513 /* Fetch one byte more to find out whether path length > n. */
514 nul_seen = umovestr(tcp, addr, n + 1, path);
516 tprintf("%#lx", addr);
522 outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
523 string_quote(path, outstr, -1, n);
531 printpath(struct tcb *tcp, long addr)
533 /* Size must correspond to char path[] size in printpathn */
534 printpathn(tcp, addr, MAXPATHLEN);
538 * Print string specified by address `addr' and length `len'.
539 * If `len' < 0, treat the string as a NUL-terminated string.
540 * If string length exceeds `max_strlen', append `...' to the output.
543 printstr(struct tcb *tcp, long addr, long len)
545 static char *str = NULL;
554 /* Allocate static buffers if they are not allocated yet. */
556 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
558 if (outstr_size / 4 != max_strlen)
560 str = malloc(max_strlen + 1);
563 outstr = malloc(outstr_size);
570 * Treat as a NUL-terminated string: fetch one byte more
571 * because string_quote() quotes one byte less.
573 size = max_strlen + 1;
574 if (umovestr(tcp, addr, size, str) < 0) {
575 tprintf("%#lx", addr);
581 if (size > (unsigned long)len)
582 size = (unsigned long)len;
583 if (umoven(tcp, addr, size, str) < 0) {
584 tprintf("%#lx", addr);
589 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
590 * or we were requested to print more than -s NUM chars)...
592 ellipsis = (string_quote(str, outstr, len, size) &&
593 (len < 0 || len > max_strlen));
602 dumpiov(struct tcb *tcp, int len, long addr)
604 #if SUPPORTED_PERSONALITIES > 1
606 struct { u_int32_t base; u_int32_t len; } *iov32;
607 struct { u_int64_t base; u_int64_t len; } *iov64;
609 #define iov iovu.iov64
611 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
612 #define iov_iov_base(i) \
613 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
614 #define iov_iov_len(i) \
615 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
618 #define sizeof_iov sizeof(*iov)
619 #define iov_iov_base(i) iov[i].iov_base
620 #define iov_iov_len(i) iov[i].iov_len
625 size = sizeof_iov * len;
626 /* Assuming no sane program has millions of iovs */
627 if ((unsigned)len > 1024*1024 /* insane or negative size? */
628 || (iov = malloc(size)) == NULL) {
629 fprintf(stderr, "Out of memory\n");
632 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
633 for (i = 0; i < len; i++) {
634 /* include the buffer number to make it easy to
635 * match up the trace with the source */
636 tprintf(" * %lu bytes in buffer %d\n",
637 (unsigned long)iov_iov_len(i), i);
638 dumpstr(tcp, (long) iov_iov_base(i),
651 dumpstr(struct tcb *tcp, long addr, int len)
653 static int strsize = -1;
654 static unsigned char *str;
663 fprintf(stderr, "Out of memory\n");
669 if (umoven(tcp, addr, len, (char *) str) < 0)
672 for (i = 0; i < len; i += 16) {
676 sprintf(s, " | %05x ", i);
678 for (j = 0; j < 16; j++) {
682 sprintf(s, " %02x", str[i + j]);
686 *s++ = ' '; *s++ = ' '; *s++ = ' ';
689 *s++ = ' '; *s++ = ' ';
690 for (j = 0; j < 16; j++) {
694 if (isprint(str[i + j]))
702 tprintf("%s |\n", outstr);
706 #ifdef HAVE_PROCESS_VM_READV
707 /* C library supports this, but the kernel might not. */
708 static bool process_vm_readv_not_supported = 0;
711 /* Need to do this since process_vm_readv() is not yet available in libc.
712 * When libc is be updated, only "static bool process_vm_readv_not_supported"
713 * line should remain.
715 #if !defined(__NR_process_vm_readv)
717 # define __NR_process_vm_readv 347
718 # elif defined(X86_64)
719 # define __NR_process_vm_readv 310
720 # elif defined(POWERPC)
721 # define __NR_process_vm_readv 351
725 #if defined(__NR_process_vm_readv)
726 static bool process_vm_readv_not_supported = 0;
727 /* Have to avoid duplicating with the C library headers. */
728 static ssize_t strace_process_vm_readv(pid_t pid,
729 const struct iovec *lvec,
730 unsigned long liovcnt,
731 const struct iovec *rvec,
732 unsigned long riovcnt,
735 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
737 #define process_vm_readv strace_process_vm_readv
739 static bool process_vm_readv_not_supported = 1;
740 # define process_vm_readv(...) (errno = ENOSYS, -1)
743 #endif /* end of hack */
745 #define PAGMASK (~(PAGSIZ - 1))
747 * move `len' bytes of data from process `pid'
748 * at address `addr' to our space at `laddr'
751 umoven(struct tcb *tcp, long addr, int len, char *laddr)
758 char x[sizeof(long)];
761 #if SUPPORTED_PERSONALITIES > 1
762 if (current_wordsize < sizeof(addr))
763 addr &= (1ul << 8 * current_wordsize) - 1;
766 if (!process_vm_readv_not_supported) {
767 struct iovec local[1], remote[1];
770 local[0].iov_base = laddr;
771 remote[0].iov_base = (void*)addr;
772 local[0].iov_len = remote[0].iov_len = len;
773 r = process_vm_readv(pid,
780 process_vm_readv_not_supported = 1;
781 else if (errno != EINVAL && errno != ESRCH)
782 /* EINVAL or ESRCH could be seen if process is gone,
783 * all the rest is strange and should be reported. */
784 perror_msg("%s", "process_vm_readv");
785 goto vm_readv_didnt_work;
792 if (addr & (sizeof(long) - 1)) {
793 /* addr not a multiple of sizeof(long) */
794 n = addr - (addr & -sizeof(long)); /* residue */
795 addr &= -sizeof(long); /* residue */
797 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
799 /* But if not started, we had a bogus address. */
800 if (addr != 0 && errno != EIO && errno != ESRCH)
801 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
805 m = MIN(sizeof(long) - n, len);
806 memcpy(laddr, &u.x[n], m);
807 addr += sizeof(long), laddr += m, len -= m;
811 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
813 if (started && (errno==EPERM || errno==EIO)) {
814 /* Ran into 'end of memory' - stupid "printpath" */
817 if (addr != 0 && errno != EIO && errno != ESRCH)
818 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
822 m = MIN(sizeof(long), len);
823 memcpy(laddr, u.x, m);
824 addr += sizeof(long), laddr += m, len -= m;
831 * Like `umove' but make the additional effort of looking
832 * for a terminating zero byte.
834 * Returns < 0 on error, > 0 if NUL was seen,
835 * (TODO if useful: return count of bytes including NUL),
836 * else 0 if len bytes were read but no NUL byte seen.
838 * Note: there is no guarantee we won't overwrite some bytes
839 * in laddr[] _after_ terminating NUL (but, of course,
840 * we never write past laddr[len-1]).
843 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
850 char x[sizeof(long)];
853 #if SUPPORTED_PERSONALITIES > 1
854 if (current_wordsize < sizeof(addr))
855 addr &= (1ul << 8 * current_wordsize) - 1;
858 if (!process_vm_readv_not_supported) {
859 struct iovec local[1], remote[1];
861 local[0].iov_base = laddr;
862 remote[0].iov_base = (void*)addr;
869 /* Don't read kilobytes: most strings are short */
873 /* Don't cross pages. I guess otherwise we can get EFAULT
874 * and fail to notice that terminating NUL lies
875 * in the existing (first) page.
876 * (I hope there aren't arches with pages < 4K)
878 end_in_page = ((addr + chunk_len) & 4095);
879 r = chunk_len - end_in_page;
880 if (r > 0) /* if chunk_len > end_in_page */
881 chunk_len = r; /* chunk_len -= end_in_page */
883 local[0].iov_len = remote[0].iov_len = chunk_len;
884 r = process_vm_readv(pid,
891 process_vm_readv_not_supported = 1;
892 else if (errno != EINVAL && errno != ESRCH)
893 /* EINVAL or ESRCH could be seen
894 * if process is gone, all the rest
895 * is strange and should be reported. */
896 perror_msg("%s", "process_vm_readv");
897 goto vm_readv_didnt_work;
899 if (memchr(local[0].iov_base, '\0', r))
901 local[0].iov_base += r;
902 remote[0].iov_base += r;
910 if (addr & (sizeof(long) - 1)) {
911 /* addr not a multiple of sizeof(long) */
912 n = addr - (addr & -sizeof(long)); /* residue */
913 addr &= -sizeof(long); /* residue */
915 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
917 if (addr != 0 && errno != EIO && errno != ESRCH)
918 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
922 m = MIN(sizeof(long) - n, len);
923 memcpy(laddr, &u.x[n], m);
924 while (n & (sizeof(long) - 1))
925 if (u.x[n++] == '\0')
927 addr += sizeof(long), laddr += m, len -= m;
931 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
933 if (started && (errno==EPERM || errno==EIO)) {
934 /* Ran into 'end of memory' - stupid "printpath" */
937 if (addr != 0 && errno != EIO && errno != ESRCH)
938 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
942 m = MIN(sizeof(long), len);
943 memcpy(laddr, u.x, m);
944 for (i = 0; i < sizeof(long); i++)
947 addr += sizeof(long), laddr += m, len -= m;
953 upeek(struct tcb *tcp, long off, long *res)
958 val = ptrace(PTRACE_PEEKUSER, tcp->pid, (char *) off, 0);
959 if (val == -1 && errno) {
960 if (errno != ESRCH) {
961 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", tcp->pid, off);
969 /* Note! On new kernels (about 2.5.46+), we use PTRACE_O_TRACECLONE
970 * and PTRACE_O_TRACE[V]FORK for tracing children.
971 * If you are adding a new arch which is only supported by newer kernels,
972 * you most likely don't need to add any code below
973 * beside a dummy "return 0" block in change_syscall().
977 * These #if's are huge, please indent them correctly.
978 * It's easy to get confused otherwise.
984 # define CLONE_PTRACE 0x00002000
987 # define CLONE_VFORK 0x00004000
990 # define CLONE_VM 0x00000100
995 typedef unsigned long *arg_setup_state;
998 arg_setup(struct tcb *tcp, arg_setup_state *state)
1000 unsigned long cfm, sof, sol;
1004 /* Satisfy a false GCC warning. */
1009 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1011 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1014 sof = (cfm >> 0) & 0x7f;
1015 sol = (cfm >> 7) & 0x7f;
1016 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1018 *state = (unsigned long *) bsp;
1022 # define arg_finish_change(tcp, state) 0
1025 get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp)
1030 ret = upeek(tcp, PT_R11, valp);
1033 (unsigned long) ia64_rse_skip_regs(*state, 0),
1034 sizeof(long), (void *) valp);
1039 get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp)
1044 ret = upeek(tcp, PT_R9, valp);
1047 (unsigned long) ia64_rse_skip_regs(*state, 1),
1048 sizeof(long), (void *) valp);
1053 set_arg0(struct tcb *tcp, arg_setup_state *state, long val)
1055 int req = PTRACE_POKEDATA;
1059 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1060 req = PTRACE_POKEUSER;
1062 ap = ia64_rse_skip_regs(*state, 0);
1064 ptrace(req, tcp->pid, ap, val);
1065 return errno ? -1 : 0;
1069 set_arg1(struct tcb *tcp, arg_setup_state *state, long val)
1071 int req = PTRACE_POKEDATA;
1075 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1076 req = PTRACE_POKEUSER;
1078 ap = ia64_rse_skip_regs(*state, 1);
1080 ptrace(req, tcp->pid, ap, val);
1081 return errno ? -1 : 0;
1084 /* ia64 does not return the input arguments from functions (and syscalls)
1085 according to ia64 RSE (Register Stack Engine) behavior. */
1087 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1088 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1090 #elif defined(SPARC) || defined(SPARC64)
1092 # if defined(SPARC64)
1093 # undef PTRACE_GETREGS
1094 # define PTRACE_GETREGS PTRACE_GETREGS64
1095 # undef PTRACE_SETREGS
1096 # define PTRACE_SETREGS PTRACE_SETREGS64
1099 typedef struct pt_regs arg_setup_state;
1101 # define arg_setup(tcp, state) \
1102 (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0))
1103 # define arg_finish_change(tcp, state) \
1104 (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0))
1106 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1107 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1108 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1109 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1110 # define restore_arg0(tcp, state, val) 0
1112 #else /* other architectures */
1114 # if defined S390 || defined S390X
1115 /* Note: this is only true for the `clone' system call, which handles
1116 arguments specially. We could as well say that its first two arguments
1117 are swapped relative to other architectures, but that would just be
1118 another #ifdef in the calls. */
1119 # define arg0_offset PT_GPR3
1120 # define arg1_offset PT_ORIGGPR2
1121 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1122 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1123 # define arg0_index 1
1124 # define arg1_index 0
1125 # elif defined(ALPHA) || defined(MIPS)
1126 # define arg0_offset REG_A0
1127 # define arg1_offset (REG_A0+1)
1128 # elif defined(POWERPC)
1129 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1130 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1131 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1132 # elif defined(HPPA)
1133 # define arg0_offset PT_GR26
1134 # define arg1_offset (PT_GR26-4)
1135 # elif defined(X86_64) || defined(X32)
1136 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1137 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1139 # define arg0_offset (4*(REG_REG0+4))
1140 # define arg1_offset (4*(REG_REG0+5))
1141 # elif defined(SH64)
1142 /* ABI defines arg0 & 1 in r2 & r3 */
1143 # define arg0_offset (REG_OFFSET+16)
1144 # define arg1_offset (REG_OFFSET+24)
1145 # define restore_arg0(tcp, state, val) 0
1146 # elif defined CRISV10 || defined CRISV32
1147 # define arg0_offset (4*PT_R11)
1148 # define arg1_offset (4*PT_ORIG_R10)
1149 # define restore_arg0(tcp, state, val) 0
1150 # define restore_arg1(tcp, state, val) 0
1151 # define arg0_index 1
1152 # define arg1_index 0
1154 # define arg0_offset 0
1155 # define arg1_offset 4
1157 # define restore_arg0(tcp, state, val) 0
1161 typedef int arg_setup_state;
1163 # define arg_setup(tcp, state) (0)
1164 # define arg_finish_change(tcp, state) 0
1165 # define get_arg0(tcp, cookie, valp) (upeek((tcp), arg0_offset, (valp)))
1166 # define get_arg1(tcp, cookie, valp) (upeek((tcp), arg1_offset, (valp)))
1169 set_arg0(struct tcb *tcp, void *cookie, long val)
1171 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1175 set_arg1(struct tcb *tcp, void *cookie, long val)
1177 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1180 #endif /* architectures */
1182 #ifndef restore_arg0
1183 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1185 #ifndef restore_arg1
1186 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1190 # define arg0_index 0
1191 # define arg1_index 1
1195 change_syscall(struct tcb *tcp, arg_setup_state *state, int new)
1198 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_EAX * 4), new) < 0)
1201 #elif defined(X86_64)
1202 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_RAX * 8), new) < 0)
1206 /* setbpt/clearbpt never used: */
1207 /* X32 is only supported since about linux-3.0.30 */
1209 #elif defined(POWERPC)
1210 if (ptrace(PTRACE_POKEUSER, tcp->pid,
1211 (char*)(sizeof(unsigned long)*PT_R0), new) < 0)
1214 #elif defined(S390) || defined(S390X)
1215 /* s390 linux after 2.4.7 has a hook in entry.S to allow this */
1216 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GPR2), new) < 0)
1220 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_ORIG_D0), new) < 0)
1223 #elif defined(SPARC) || defined(SPARC64)
1224 state->u_regs[U_REG_G1] = new;
1227 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), new) < 0)
1230 #elif defined(ALPHA)
1231 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), new) < 0)
1234 #elif defined(AVR32)
1235 /* setbpt/clearbpt never used: */
1236 /* AVR32 is only supported since about linux-2.6.19 */
1239 /* setbpt/clearbpt never used: */
1240 /* Blackfin is only supported since about linux-2.6.23 */
1246 break; /* x86 SYS_fork */
1251 fprintf(stderr, "%s: unexpected syscall %d\n",
1255 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R1), new) < 0)
1257 } else if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R15), new) < 0)
1261 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR20), new) < 0)
1265 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*(REG_REG0+3)), new) < 0)
1269 /* Top half of reg encodes the no. of args n as 0x1n.
1270 Assume 0 args as kernel never actually checks... */
1271 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_SYSCALL),
1272 0x100000 | new) < 0)
1275 #elif defined(CRISV10) || defined(CRISV32)
1276 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_R9), new) < 0)
1280 /* Some kernels support this, some (pre-2.6.16 or so) don't. */
1281 # ifndef PTRACE_SET_SYSCALL
1282 # define PTRACE_SET_SYSCALL 23
1284 if (ptrace(PTRACE_SET_SYSCALL, tcp->pid, 0, new & 0xffff) != 0)
1287 #elif defined(AARCH64)
1288 /* setbpt/clearbpt never used: */
1289 /* AARCH64 is only supported since about linux-3.0.31 */
1292 /* setbpt/clearbpt never used: */
1293 /* Tilera CPUs are only supported since about linux-2.6.34 */
1295 #elif defined(MICROBLAZE)
1296 /* setbpt/clearbpt never used: */
1297 /* microblaze is only supported since about linux-2.6.30 */
1300 #warning Do not know how to handle change_syscall for this architecture
1301 #endif /* architecture */
1306 setbpt(struct tcb *tcp)
1308 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1309 arg_setup_state state;
1311 if (tcp->flags & TCB_BPTSET) {
1312 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1317 * It's a silly kludge to initialize this with a search at runtime.
1318 * But it's better than maintaining another magic thing in the
1319 * godforsaken tables.
1321 if (clone_scno[current_personality] == 0) {
1323 for (i = 0; i < nsyscalls; ++i)
1324 if (sysent[i].sys_func == sys_clone) {
1325 clone_scno[current_personality] = i;
1330 if (sysent[tcp->scno].sys_func == sys_fork ||
1331 sysent[tcp->scno].sys_func == sys_vfork) {
1332 if (arg_setup(tcp, &state) < 0
1333 || get_arg0(tcp, &state, &tcp->inst[0]) < 0
1334 || get_arg1(tcp, &state, &tcp->inst[1]) < 0
1335 || change_syscall(tcp, &state,
1336 clone_scno[current_personality]) < 0
1337 || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1338 || set_arg1(tcp, &state, 0) < 0
1339 || arg_finish_change(tcp, &state) < 0)
1341 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1342 tcp->u_arg[arg1_index] = 0;
1343 tcp->flags |= TCB_BPTSET;
1347 if (sysent[tcp->scno].sys_func == sys_clone) {
1348 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1349 contrary to x86 vfork above. Even on x86 we turn the
1350 vfork semantics into plain fork - each application must not
1351 depend on the vfork specifics according to POSIX. We would
1352 hang waiting for the parent resume otherwise. We need to
1353 clear also CLONE_VM but only in the CLONE_VFORK case as
1354 otherwise we would break pthread_create. */
1356 long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE);
1357 if (new_arg0 & CLONE_VFORK)
1358 new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM);
1359 if (arg_setup(tcp, &state) < 0
1360 || set_arg0(tcp, &state, new_arg0) < 0
1361 || arg_finish_change(tcp, &state) < 0)
1363 tcp->inst[0] = tcp->u_arg[arg0_index];
1364 tcp->inst[1] = tcp->u_arg[arg1_index];
1365 tcp->flags |= TCB_BPTSET;
1369 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1370 tcp->scno, tcp->pid);
1375 clearbpt(struct tcb *tcp)
1377 arg_setup_state state;
1378 if (arg_setup(tcp, &state) < 0
1379 || change_syscall(tcp, &state, tcp->scno) < 0
1380 || restore_arg0(tcp, &state, tcp->inst[0]) < 0
1381 || restore_arg1(tcp, &state, tcp->inst[1]) < 0
1382 || arg_finish_change(tcp, &state))
1385 tcp->flags &= ~TCB_BPTSET;