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);
182 * Print 64bit argument at position llarg and return the index of the next
186 printllval(struct tcb *tcp, const char *format, int llarg)
188 #if defined(X86_64) || defined(POWERPC64)
189 if (current_personality == 0) {
190 /* Technically, format expects "long long",
191 * but we supply "long". We expect that
192 * on this arch, they are the same.
194 tprintf(format, tcp->u_arg[llarg]);
198 /* Align 64bit argument to 64bit boundary. */
199 llarg = (llarg + 1) & 0x1e;
201 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
204 #elif defined IA64 || defined ALPHA
205 /* Technically, format expects "long long",
206 * but we supply "long". We expect that
207 * on this arch, they are the same.
209 tprintf(format, tcp->u_arg[llarg]);
211 #elif defined LINUX_MIPSN32 || defined X32
212 tprintf(format, tcp->ext_arg[llarg]);
216 # error BUG: must not combine two args for long long on this arch
218 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
225 * Interpret `xlat' as an array of flags
226 * print the entries whose bits are on in `flags'
227 * return # of flags printed.
230 addflags(const struct xlat *xlat, int flags)
232 for (; xlat->str; xlat++) {
233 if (xlat->val && (flags & xlat->val) == xlat->val) {
234 tprintf("|%s", xlat->str);
239 tprintf("|%#x", flags);
244 * Interpret `xlat' as an array of flags.
245 * Print to static string the entries whose bits are on in `flags'
246 * Return static string.
249 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
251 static char outstr[1024];
255 outptr = stpcpy(outstr, prefix);
257 for (; xlat->str; xlat++) {
258 if ((flags & xlat->val) == xlat->val) {
261 outptr = stpcpy(outptr, xlat->str);
271 outptr += sprintf(outptr, "%#x", flags);
278 printflags(const struct xlat *xlat, int flags, const char *dflt)
283 if (flags == 0 && xlat->val == 0) {
289 for (n = 0; xlat->str; xlat++) {
290 if (xlat->val && (flags & xlat->val) == xlat->val) {
291 tprintf("%s%s", sep, xlat->str);
300 tprintf("%s%#x", sep, flags);
305 tprintf("%#x", flags);
307 tprintf(" /* %s */", dflt);
318 printnum(struct tcb *tcp, long addr, const char *fmt)
326 if (umove(tcp, addr, &num) < 0) {
327 tprintf("%#lx", addr);
336 printnum_int(struct tcb *tcp, long addr, const char *fmt)
344 if (umove(tcp, addr, &num) < 0) {
345 tprintf("%#lx", addr);
354 printfd(struct tcb *tcp, int fd)
358 if (show_fd_path && (p = getfdpath(tcp, fd)))
359 tprintf("%d<%s>", fd, p);
365 printuid(const char *text, unsigned long uid)
367 tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid);
371 * Quote string `instr' of length `size'
372 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
373 * If `len' is -1, treat `instr' as a NUL-terminated string
374 * and quote at most (`size' - 1) bytes.
376 * Returns 0 if len == -1 and NUL was seen, 1 otherwise.
377 * Note that if len >= 0, always returns 1.
380 string_quote(const char *instr, char *outstr, long len, int size)
382 const unsigned char *ustr = (const unsigned char *) instr;
384 int usehex, c, i, eol;
386 eol = 0x100; /* this can never match a char */
396 /* Check for presence of symbol which require
397 to hex-quote the whole string. */
398 for (i = 0; i < size; ++i) {
400 /* Check for NUL-terminated string. */
403 if (!isprint(c) && !isspace(c)) {
413 /* Hex-quote the whole string. */
414 for (i = 0; i < size; ++i) {
416 /* Check for NUL-terminated string. */
421 *s++ = "0123456789abcdef"[c >> 4];
422 *s++ = "0123456789abcdef"[c & 0xf];
425 for (i = 0; i < size; ++i) {
427 /* Check for NUL-terminated string. */
431 case '\"': case '\\':
462 && ustr[i + 1] >= '0'
463 && ustr[i + 1] <= '9'
466 *s++ = '0' + (c >> 6);
467 *s++ = '0' + ((c >> 3) & 0x7);
472 *s++ = '0' + (c >> 6);
473 *s++ = '0' + ((c >> 3) & 0x7);
476 *s++ = '0' + (c & 0x7);
486 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
487 if (len == -1 && ustr[i] == '\0') {
488 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
489 * but next char is NUL.
499 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
504 * Print path string specified by address `addr' and length `n'.
505 * If path length exceeds `n', append `...' to the output.
508 printpathn(struct tcb *tcp, long addr, int n)
510 char path[MAXPATHLEN + 1];
518 /* Cap path length to the path buffer size */
519 if (n > sizeof path - 1)
522 /* Fetch one byte more to find out whether path length > n. */
523 nul_seen = umovestr(tcp, addr, n + 1, path);
525 tprintf("%#lx", addr);
531 outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
532 string_quote(path, outstr, -1, n);
540 printpath(struct tcb *tcp, long addr)
542 /* Size must correspond to char path[] size in printpathn */
543 printpathn(tcp, addr, MAXPATHLEN);
547 * Print string specified by address `addr' and length `len'.
548 * If `len' < 0, treat the string as a NUL-terminated string.
549 * If string length exceeds `max_strlen', append `...' to the output.
552 printstr(struct tcb *tcp, long addr, long len)
554 static char *str = NULL;
563 /* Allocate static buffers if they are not allocated yet. */
565 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
567 if (outstr_size / 4 != max_strlen)
569 str = malloc(max_strlen + 1);
572 outstr = malloc(outstr_size);
579 * Treat as a NUL-terminated string: fetch one byte more
580 * because string_quote() quotes one byte less.
582 size = max_strlen + 1;
583 if (umovestr(tcp, addr, size, str) < 0) {
584 tprintf("%#lx", addr);
590 if (size > (unsigned long)len)
591 size = (unsigned long)len;
592 if (umoven(tcp, addr, size, str) < 0) {
593 tprintf("%#lx", addr);
598 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
599 * or we were requested to print more than -s NUM chars)...
601 ellipsis = (string_quote(str, outstr, len, size) &&
602 (len < 0 || len > max_strlen));
611 dumpiov(struct tcb *tcp, int len, long addr)
613 #if SUPPORTED_PERSONALITIES > 1
615 struct { u_int32_t base; u_int32_t len; } *iov32;
616 struct { u_int64_t base; u_int64_t len; } *iov64;
618 #define iov iovu.iov64
620 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
621 #define iov_iov_base(i) \
622 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
623 #define iov_iov_len(i) \
624 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
627 #define sizeof_iov sizeof(*iov)
628 #define iov_iov_base(i) iov[i].iov_base
629 #define iov_iov_len(i) iov[i].iov_len
634 size = sizeof_iov * len;
635 /* Assuming no sane program has millions of iovs */
636 if ((unsigned)len > 1024*1024 /* insane or negative size? */
637 || (iov = malloc(size)) == NULL) {
638 fprintf(stderr, "Out of memory\n");
641 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
642 for (i = 0; i < len; i++) {
643 /* include the buffer number to make it easy to
644 * match up the trace with the source */
645 tprintf(" * %lu bytes in buffer %d\n",
646 (unsigned long)iov_iov_len(i), i);
647 dumpstr(tcp, (long) iov_iov_base(i),
660 dumpstr(struct tcb *tcp, long addr, int len)
662 static int strsize = -1;
663 static unsigned char *str;
672 fprintf(stderr, "Out of memory\n");
678 if (umoven(tcp, addr, len, (char *) str) < 0)
681 for (i = 0; i < len; i += 16) {
685 sprintf(s, " | %05x ", i);
687 for (j = 0; j < 16; j++) {
691 sprintf(s, " %02x", str[i + j]);
695 *s++ = ' '; *s++ = ' '; *s++ = ' ';
698 *s++ = ' '; *s++ = ' ';
699 for (j = 0; j < 16; j++) {
703 if (isprint(str[i + j]))
711 tprintf("%s |\n", outstr);
715 #ifdef HAVE_PROCESS_VM_READV
716 /* C library supports this, but the kernel might not. */
717 static bool process_vm_readv_not_supported = 0;
720 /* Need to do this since process_vm_readv() is not yet available in libc.
721 * When libc is be updated, only "static bool process_vm_readv_not_supported"
722 * line should remain.
724 #if !defined(__NR_process_vm_readv)
726 # define __NR_process_vm_readv 347
727 # elif defined(X86_64)
728 # define __NR_process_vm_readv 310
729 # elif defined(POWERPC)
730 # define __NR_process_vm_readv 351
734 #if defined(__NR_process_vm_readv)
735 static bool process_vm_readv_not_supported = 0;
736 /* Have to avoid duplicating with the C library headers. */
737 static ssize_t strace_process_vm_readv(pid_t pid,
738 const struct iovec *lvec,
739 unsigned long liovcnt,
740 const struct iovec *rvec,
741 unsigned long riovcnt,
744 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
746 #define process_vm_readv strace_process_vm_readv
748 static bool process_vm_readv_not_supported = 1;
749 # define process_vm_readv(...) (errno = ENOSYS, -1)
752 #endif /* end of hack */
754 #define PAGMASK (~(PAGSIZ - 1))
756 * move `len' bytes of data from process `pid'
757 * at address `addr' to our space at `laddr'
760 umoven(struct tcb *tcp, long addr, int len, char *laddr)
767 char x[sizeof(long)];
770 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
771 if (current_wordsize < sizeof(addr))
772 addr &= (1ul << 8 * current_wordsize) - 1;
775 if (!process_vm_readv_not_supported) {
776 struct iovec local[1], remote[1];
779 local[0].iov_base = laddr;
780 remote[0].iov_base = (void*)addr;
781 local[0].iov_len = remote[0].iov_len = len;
782 r = process_vm_readv(pid,
789 process_vm_readv_not_supported = 1;
790 else if (errno != EINVAL && errno != ESRCH)
791 /* EINVAL or ESRCH could be seen if process is gone,
792 * all the rest is strange and should be reported. */
793 perror_msg("%s", "process_vm_readv");
794 goto vm_readv_didnt_work;
801 if (addr & (sizeof(long) - 1)) {
802 /* addr not a multiple of sizeof(long) */
803 n = addr - (addr & -sizeof(long)); /* residue */
804 addr &= -sizeof(long); /* residue */
806 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
808 /* But if not started, we had a bogus address. */
809 if (addr != 0 && errno != EIO && errno != ESRCH)
810 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
814 m = MIN(sizeof(long) - n, len);
815 memcpy(laddr, &u.x[n], m);
816 addr += sizeof(long), laddr += m, len -= m;
820 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
822 if (started && (errno==EPERM || errno==EIO)) {
823 /* Ran into 'end of memory' - stupid "printpath" */
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), len);
832 memcpy(laddr, u.x, m);
833 addr += sizeof(long), laddr += m, len -= m;
840 * Like `umove' but make the additional effort of looking
841 * for a terminating zero byte.
843 * Returns < 0 on error, > 0 if NUL was seen,
844 * (TODO if useful: return count of bytes including NUL),
845 * else 0 if len bytes were read but no NUL byte seen.
847 * Note: there is no guarantee we won't overwrite some bytes
848 * in laddr[] _after_ terminating NUL (but, of course,
849 * we never write past laddr[len-1]).
852 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
859 char x[sizeof(long)];
862 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
863 if (current_wordsize < sizeof(addr))
864 addr &= (1ul << 8 * current_wordsize) - 1;
867 if (!process_vm_readv_not_supported) {
868 struct iovec local[1], remote[1];
870 local[0].iov_base = laddr;
871 remote[0].iov_base = (void*)addr;
878 /* Don't read kilobytes: most strings are short */
882 /* Don't cross pages. I guess otherwise we can get EFAULT
883 * and fail to notice that terminating NUL lies
884 * in the existing (first) page.
885 * (I hope there aren't arches with pages < 4K)
887 end_in_page = ((addr + chunk_len) & 4095);
888 r = chunk_len - end_in_page;
889 if (r > 0) /* if chunk_len > end_in_page */
890 chunk_len = r; /* chunk_len -= end_in_page */
892 local[0].iov_len = remote[0].iov_len = chunk_len;
893 r = process_vm_readv(pid,
900 process_vm_readv_not_supported = 1;
901 else if (errno != EINVAL && errno != ESRCH)
902 /* EINVAL or ESRCH could be seen
903 * if process is gone, all the rest
904 * is strange and should be reported. */
905 perror_msg("%s", "process_vm_readv");
906 goto vm_readv_didnt_work;
908 if (memchr(local[0].iov_base, '\0', r))
910 local[0].iov_base += r;
911 remote[0].iov_base += r;
919 if (addr & (sizeof(long) - 1)) {
920 /* addr not a multiple of sizeof(long) */
921 n = addr - (addr & -sizeof(long)); /* residue */
922 addr &= -sizeof(long); /* residue */
924 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
926 if (addr != 0 && errno != EIO && errno != ESRCH)
927 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
931 m = MIN(sizeof(long) - n, len);
932 memcpy(laddr, &u.x[n], m);
933 while (n & (sizeof(long) - 1))
934 if (u.x[n++] == '\0')
936 addr += sizeof(long), laddr += m, len -= m;
940 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
942 if (started && (errno==EPERM || errno==EIO)) {
943 /* Ran into 'end of memory' - stupid "printpath" */
946 if (addr != 0 && errno != EIO && errno != ESRCH)
947 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
951 m = MIN(sizeof(long), len);
952 memcpy(laddr, u.x, m);
953 for (i = 0; i < sizeof(long); i++)
956 addr += sizeof(long), laddr += m, len -= m;
962 upeek(struct tcb *tcp, long off, long *res)
967 val = ptrace(PTRACE_PEEKUSER, tcp->pid, (char *) off, 0);
968 if (val == -1 && errno) {
969 if (errno != ESRCH) {
970 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", tcp->pid, off);
978 /* Note! On new kernels (about 2.5.46+), we use PTRACE_O_TRACECLONE
979 * and PTRACE_O_TRACE[V]FORK for tracing children.
980 * If you are adding a new arch which is only supported by newer kernels,
981 * you most likely don't need to add any code below
982 * beside a dummy "return 0" block in change_syscall().
986 * These #if's are huge, please indent them correctly.
987 * It's easy to get confused otherwise.
993 # define CLONE_PTRACE 0x00002000
996 # define CLONE_VFORK 0x00004000
999 # define CLONE_VM 0x00000100
1004 typedef unsigned long *arg_setup_state;
1007 arg_setup(struct tcb *tcp, arg_setup_state *state)
1009 unsigned long cfm, sof, sol;
1013 /* Satisfy a false GCC warning. */
1018 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1020 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1023 sof = (cfm >> 0) & 0x7f;
1024 sol = (cfm >> 7) & 0x7f;
1025 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1027 *state = (unsigned long *) bsp;
1031 # define arg_finish_change(tcp, state) 0
1034 get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp)
1039 ret = upeek(tcp, PT_R11, valp);
1042 (unsigned long) ia64_rse_skip_regs(*state, 0),
1043 sizeof(long), (void *) valp);
1048 get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp)
1053 ret = upeek(tcp, PT_R9, valp);
1056 (unsigned long) ia64_rse_skip_regs(*state, 1),
1057 sizeof(long), (void *) valp);
1062 set_arg0(struct tcb *tcp, arg_setup_state *state, long val)
1064 int req = PTRACE_POKEDATA;
1068 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1069 req = PTRACE_POKEUSER;
1071 ap = ia64_rse_skip_regs(*state, 0);
1073 ptrace(req, tcp->pid, ap, val);
1074 return errno ? -1 : 0;
1078 set_arg1(struct tcb *tcp, arg_setup_state *state, long val)
1080 int req = PTRACE_POKEDATA;
1084 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1085 req = PTRACE_POKEUSER;
1087 ap = ia64_rse_skip_regs(*state, 1);
1089 ptrace(req, tcp->pid, ap, val);
1090 return errno ? -1 : 0;
1093 /* ia64 does not return the input arguments from functions (and syscalls)
1094 according to ia64 RSE (Register Stack Engine) behavior. */
1096 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1097 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1099 #elif defined(SPARC) || defined(SPARC64)
1101 # if defined(SPARC64)
1102 # undef PTRACE_GETREGS
1103 # define PTRACE_GETREGS PTRACE_GETREGS64
1104 # undef PTRACE_SETREGS
1105 # define PTRACE_SETREGS PTRACE_SETREGS64
1108 typedef struct pt_regs arg_setup_state;
1110 # define arg_setup(tcp, state) \
1111 (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0))
1112 # define arg_finish_change(tcp, state) \
1113 (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0))
1115 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1116 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1117 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1118 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1119 # define restore_arg0(tcp, state, val) 0
1121 #else /* other architectures */
1123 # if defined S390 || defined S390X
1124 /* Note: this is only true for the `clone' system call, which handles
1125 arguments specially. We could as well say that its first two arguments
1126 are swapped relative to other architectures, but that would just be
1127 another #ifdef in the calls. */
1128 # define arg0_offset PT_GPR3
1129 # define arg1_offset PT_ORIGGPR2
1130 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1131 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1132 # define arg0_index 1
1133 # define arg1_index 0
1134 # elif defined(ALPHA) || defined(MIPS)
1135 # define arg0_offset REG_A0
1136 # define arg1_offset (REG_A0+1)
1137 # elif defined(POWERPC)
1138 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1139 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1140 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1141 # elif defined(HPPA)
1142 # define arg0_offset PT_GR26
1143 # define arg1_offset (PT_GR26-4)
1144 # elif defined(X86_64) || defined(X32)
1145 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1146 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1148 # define arg0_offset (4*(REG_REG0+4))
1149 # define arg1_offset (4*(REG_REG0+5))
1150 # elif defined(SH64)
1151 /* ABI defines arg0 & 1 in r2 & r3 */
1152 # define arg0_offset (REG_OFFSET+16)
1153 # define arg1_offset (REG_OFFSET+24)
1154 # define restore_arg0(tcp, state, val) 0
1155 # elif defined CRISV10 || defined CRISV32
1156 # define arg0_offset (4*PT_R11)
1157 # define arg1_offset (4*PT_ORIG_R10)
1158 # define restore_arg0(tcp, state, val) 0
1159 # define restore_arg1(tcp, state, val) 0
1160 # define arg0_index 1
1161 # define arg1_index 0
1163 # define arg0_offset 0
1164 # define arg1_offset 4
1166 # define restore_arg0(tcp, state, val) 0
1170 typedef int arg_setup_state;
1172 # define arg_setup(tcp, state) (0)
1173 # define arg_finish_change(tcp, state) 0
1174 # define get_arg0(tcp, cookie, valp) (upeek((tcp), arg0_offset, (valp)))
1175 # define get_arg1(tcp, cookie, valp) (upeek((tcp), arg1_offset, (valp)))
1178 set_arg0(struct tcb *tcp, void *cookie, long val)
1180 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1184 set_arg1(struct tcb *tcp, void *cookie, long val)
1186 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1189 #endif /* architectures */
1191 #ifndef restore_arg0
1192 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1194 #ifndef restore_arg1
1195 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1199 # define arg0_index 0
1200 # define arg1_index 1
1204 change_syscall(struct tcb *tcp, arg_setup_state *state, int new)
1207 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_EAX * 4), new) < 0)
1210 #elif defined(X86_64)
1211 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_RAX * 8), new) < 0)
1215 /* setbpt/clearbpt never used: */
1216 /* X32 is only supported since about linux-3.0.30 */
1217 #elif defined(POWERPC)
1218 if (ptrace(PTRACE_POKEUSER, tcp->pid,
1219 (char*)(sizeof(unsigned long)*PT_R0), new) < 0)
1222 #elif defined(S390) || defined(S390X)
1223 /* s390 linux after 2.4.7 has a hook in entry.S to allow this */
1224 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GPR2), new) < 0)
1228 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_ORIG_D0), new) < 0)
1231 #elif defined(SPARC) || defined(SPARC64)
1232 state->u_regs[U_REG_G1] = new;
1235 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), new) < 0)
1238 #elif defined(ALPHA)
1239 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), new) < 0)
1242 #elif defined(AVR32)
1243 /* setbpt/clearbpt never used: */
1244 /* AVR32 is only supported since about linux-2.6.19 */
1246 /* setbpt/clearbpt never used: */
1247 /* Blackfin is only supported since about linux-2.6.23 */
1252 break; /* x86 SYS_fork */
1257 fprintf(stderr, "%s: unexpected syscall %d\n",
1261 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R1), new) < 0)
1263 } else if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R15), new) < 0)
1267 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR20), new) < 0)
1271 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*(REG_REG0+3)), new) < 0)
1275 /* Top half of reg encodes the no. of args n as 0x1n.
1276 Assume 0 args as kernel never actually checks... */
1277 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_SYSCALL),
1278 0x100000 | new) < 0)
1281 #elif defined(CRISV10) || defined(CRISV32)
1282 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_R9), new) < 0)
1286 /* Some kernels support this, some (pre-2.6.16 or so) don't. */
1287 # ifndef PTRACE_SET_SYSCALL
1288 # define PTRACE_SET_SYSCALL 23
1290 if (ptrace(PTRACE_SET_SYSCALL, tcp->pid, 0, new & 0xffff) != 0)
1293 #elif defined(AARCH64)
1294 /* setbpt/clearbpt never used: */
1295 /* AARCH64 is only supported since about linux-3.0.31 */
1297 /* setbpt/clearbpt never used: */
1298 /* Tilera CPUs are only supported since about linux-2.6.34 */
1299 #elif defined(MICROBLAZE)
1300 /* setbpt/clearbpt never used: */
1301 /* microblaze is only supported since about linux-2.6.30 */
1303 /* never reached; OR1K is only supported by kernels since 3.1.0. */
1305 #warning Do not know how to handle change_syscall for this architecture
1306 #endif /* architecture */
1311 setbpt(struct tcb *tcp)
1313 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1314 arg_setup_state state;
1316 if (tcp->flags & TCB_BPTSET) {
1317 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1322 * It's a silly kludge to initialize this with a search at runtime.
1323 * But it's better than maintaining another magic thing in the
1324 * godforsaken tables.
1326 if (clone_scno[current_personality] == 0) {
1328 for (i = 0; i < nsyscalls; ++i)
1329 if (sysent[i].sys_func == sys_clone) {
1330 clone_scno[current_personality] = i;
1335 if (sysent[tcp->scno].sys_func == sys_fork ||
1336 sysent[tcp->scno].sys_func == sys_vfork) {
1337 if (arg_setup(tcp, &state) < 0
1338 || get_arg0(tcp, &state, &tcp->inst[0]) < 0
1339 || get_arg1(tcp, &state, &tcp->inst[1]) < 0
1340 || change_syscall(tcp, &state,
1341 clone_scno[current_personality]) < 0
1342 || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1343 || set_arg1(tcp, &state, 0) < 0
1344 || arg_finish_change(tcp, &state) < 0)
1346 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1347 tcp->u_arg[arg1_index] = 0;
1348 tcp->flags |= TCB_BPTSET;
1352 if (sysent[tcp->scno].sys_func == sys_clone) {
1353 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1354 contrary to x86 vfork above. Even on x86 we turn the
1355 vfork semantics into plain fork - each application must not
1356 depend on the vfork specifics according to POSIX. We would
1357 hang waiting for the parent resume otherwise. We need to
1358 clear also CLONE_VM but only in the CLONE_VFORK case as
1359 otherwise we would break pthread_create. */
1361 long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE);
1362 if (new_arg0 & CLONE_VFORK)
1363 new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM);
1364 if (arg_setup(tcp, &state) < 0
1365 || set_arg0(tcp, &state, new_arg0) < 0
1366 || arg_finish_change(tcp, &state) < 0)
1368 tcp->inst[0] = tcp->u_arg[arg0_index];
1369 tcp->inst[1] = tcp->u_arg[arg1_index];
1370 tcp->flags |= TCB_BPTSET;
1374 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1375 tcp->scno, tcp->pid);
1380 clearbpt(struct tcb *tcp)
1382 arg_setup_state state;
1383 if (arg_setup(tcp, &state) < 0
1384 || change_syscall(tcp, &state, tcp->scno) < 0
1385 || restore_arg0(tcp, &state, tcp->inst[0]) < 0
1386 || restore_arg1(tcp, &state, tcp->inst[1]) < 0
1387 || arg_finish_change(tcp, &state))
1390 tcp->flags &= ~TCB_BPTSET;