]> granicus.if.org Git - strace/blobdiff - syscall.c
mips n32: fix preadv/pwritev offset decoding
[strace] / syscall.c
index 3566a013f01a85d1bfde8e50886b93e790129be2..228536b479a7f51ac8dd4cb98fd69939c73e8f5a 100644 (file)
--- a/syscall.c
+++ b/syscall.c
  */
 
 #include "defs.h"
-#include <sys/user.h>
 #include <sys/param.h>
 
-#ifdef HAVE_SYS_REG_H
-# include <sys/reg.h>
-# ifndef PTRACE_PEEKUSR
-#  define PTRACE_PEEKUSR PTRACE_PEEKUSER
-# endif
-#elif defined(HAVE_LINUX_PTRACE_H)
-# undef PTRACE_SYSCALL
-# ifdef HAVE_STRUCT_IA64_FPREG
-#  define ia64_fpreg XXX_ia64_fpreg
-# endif
-# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
-#  define pt_all_user_regs XXX_pt_all_user_regs
-# endif
-# include <linux/ptrace.h>
-# undef ia64_fpreg
-# undef pt_all_user_regs
-#endif
+/* for struct iovec */
+#include <sys/uio.h>
+
+#include "regs.h"
+#include "ptrace.h"
 
 #if defined(SPARC64)
 # undef PTRACE_GETREGS
 # define PTRACE_SETREGS PTRACE_SETREGS64
 #endif
 
-#if defined(IA64)
-# include <asm/ptrace_offsets.h>
-# include <asm/rse.h>
-#endif
-
-#if defined(X86_64) || defined(X32)
-# include <linux/ptrace.h>
-# include <sys/uio.h>
-# include <elf.h>
+#if defined SPARC64
+# include <asm/psrcompat.h>
+#elif defined SPARC
+# include <asm/psr.h>
 #endif
 
-#if defined(AARCH64)
-# include <asm/ptrace.h>
-# include <sys/uio.h>
-# include <elf.h>
-#endif
-
-#if defined(OR1K)
-# include <sys/uio.h>
-# include <elf.h>
-#endif
-
-#ifndef ERESTARTSYS
-# define ERESTARTSYS   512
-#endif
-#ifndef ERESTARTNOINTR
-# define ERESTARTNOINTR        513
-#endif
-#ifndef ERESTARTNOHAND
-# define ERESTARTNOHAND        514     /* restart if no handler */
-#endif
-#ifndef ERESTART_RESTARTBLOCK
-# define ERESTART_RESTARTBLOCK 516     /* restart by calling sys_restart_syscall */
+#ifndef NT_PRSTATUS
+# define NT_PRSTATUS 1
 #endif
 
 #ifndef NSIG
 # warning: NSIG is not defined, using 32
 # define NSIG 32
 #endif
-#ifdef ARM
-/* Ugh. Is this really correct? ARM has no RT signals?! */
-# undef NSIG
-# define NSIG 32
-#endif
 
 #include "syscall.h"
 
 #define TM TRACE_MEMORY
 #define NF SYSCALL_NEVER_FAILS
 #define MA MAX_ARGS
+#define SI STACKTRACE_INVALIDATE_CACHE
+#define SE STACKTRACE_CAPTURE_ON_ENTER
 
 const struct_sysent sysent0[] = {
 #include "syscallent.h"
@@ -144,9 +103,11 @@ static const struct_sysent sysent2[] = {
 #undef TM
 #undef NF
 #undef MA
+#undef SI
+#undef SE
 
 /*
- * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
+ * `ioctlent[012].h' files are automatically generated by the auxiliary
  * program `ioctlsort', such that the list is sorted by the `code' field.
  * This has the side-effect of resolving the _IO.. macros into
  * plain integers, eliminating the need to include here everything
@@ -160,7 +121,7 @@ const char *const signalent0[] = {
 #include "signalent.h"
 };
 const struct_ioctlent ioctlent0[] = {
-#include "ioctlent.h"
+#include "ioctlent0.h"
 };
 
 #if SUPPORTED_PERSONALITIES > 1
@@ -338,7 +299,7 @@ set_personality(int personality)
 }
 
 static void
-update_personality(struct tcb *tcp, int personality)
+update_personality(struct tcb *tcp, unsigned int personality)
 {
        if (personality == current_personality)
                return;
@@ -385,7 +346,7 @@ update_personality(struct tcb *tcp, int personality)
 static int qual_syscall(), qual_signal(), qual_desc();
 
 static const struct qual_options {
-       int bitflag;
+       unsigned int bitflag;
        const char *option_name;
        int (*qualify)(const char *, int, int);
        const char *argument_name;
@@ -411,7 +372,7 @@ static const struct qual_options {
 };
 
 static void
-reallocate_qual(int n)
+reallocate_qual(const unsigned int n)
 {
        unsigned p;
        qualbits_t *qp;
@@ -425,9 +386,9 @@ reallocate_qual(int n)
 }
 
 static void
-qualify_one(int n, int bitflag, int not, int pers)
+qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
 {
-       unsigned p;
+       int p;
 
        if (num_quals <= n)
                reallocate_qual(n + 1);
@@ -443,10 +404,10 @@ qualify_one(int n, int bitflag, int not, int pers)
 }
 
 static int
-qual_syscall(const char *s, int bitflag, int not)
+qual_syscall(const char *s, const unsigned int bitflag, const int not)
 {
-       unsigned p;
-       unsigned i;
+       int p;
+       unsigned int i;
        int rc = -1;
 
        if (*s >= '0' && *s <= '9') {
@@ -472,9 +433,9 @@ qual_syscall(const char *s, int bitflag, int not)
 }
 
 static int
-qual_signal(const char *s, int bitflag, int not)
+qual_signal(const char *s, const unsigned int bitflag, const int not)
 {
-       int i;
+       unsigned int i;
 
        if (*s >= '0' && *s <= '9') {
                int signo = string_to_uint(s);
@@ -495,7 +456,7 @@ qual_signal(const char *s, int bitflag, int not)
 }
 
 static int
-qual_desc(const char *s, int bitflag, int not)
+qual_desc(const char *s, const unsigned int bitflag, const int not)
 {
        if (*s >= '0' && *s <= '9') {
                int desc = string_to_uint(s);
@@ -531,20 +492,20 @@ void
 qualify(const char *s)
 {
        const struct qual_options *opt;
-       int not;
        char *copy;
        const char *p;
-       int i, n;
+       int not;
+       unsigned int i;
 
        if (num_quals == 0)
                reallocate_qual(MIN_QUALS);
 
        opt = &qual_options[0];
        for (i = 0; (p = qual_options[i].option_name); i++) {
-               n = strlen(p);
-               if (strncmp(s, p, n) == 0 && s[n] == '=') {
+               unsigned int len = strlen(p);
+               if (strncmp(s, p, len) == 0 && s[len] == '=') {
                        opt = &qual_options[i];
-                       s += n + 1;
+                       s += len + 1;
                        break;
                }
        }
@@ -570,6 +531,7 @@ qualify(const char *s)
        if (!copy)
                die_out_of_memory();
        for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
+               int n;
                if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
                        unsigned pers;
                        for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
@@ -685,10 +647,10 @@ getrval2(struct tcb *tcp)
 # if defined(SPARC) || defined(SPARC64)
        val = sparc_regs.u_regs[U_REG_O1];
 # elif defined(SH)
-       if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
+       if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
                return -1;
 # elif defined(IA64)
-       if (upeek(tcp, PT_R9, &val) < 0)
+       if (upeek(tcp->pid, PT_R9, &val) < 0)
                return -1;
 # endif
 
@@ -696,23 +658,10 @@ getrval2(struct tcb *tcp)
 }
 #endif
 
-int
-is_restart_error(struct tcb *tcp)
-{
-       switch (tcp->u_error) {
-               case ERESTARTSYS:
-               case ERESTARTNOINTR:
-               case ERESTARTNOHAND:
-               case ERESTART_RESTARTBLOCK:
-                       return 1;
-               default:
-                       break;
-       }
-       return 0;
-}
-
 #if defined(I386)
-struct user_regs_struct i386_regs;
+static struct user_regs_struct i386_regs;
+long *const i386_esp_ptr = &i386_regs.esp;
+# define ARCH_REGS_FOR_GETREGS i386_regs
 #elif defined(X86_64) || defined(X32)
 /*
  * On i386, pt_regs and user_regs_struct are the same,
@@ -745,21 +694,47 @@ static union {
 } x86_regs_union;
 # define x86_64_regs x86_regs_union.x86_64_r
 # define i386_regs   x86_regs_union.i386_r
+uint32_t *const i386_esp_ptr = &i386_regs.esp;
 static struct iovec x86_io = {
        .iov_base = &x86_regs_union
 };
+# define ARCH_REGS_FOR_GETREGSET x86_regs_union
+# define ARCH_IOVEC_FOR_GETREGSET x86_io
 #elif defined(IA64)
-long ia32 = 0; /* not static */
+static bool ia64_ia32mode;
 static long ia64_r8, ia64_r10;
 #elif defined(POWERPC)
-static long ppc_result;
+struct pt_regs ppc_regs; /* not static */
+# define ARCH_REGS_FOR_GETREGS ppc_regs
 #elif defined(M68K)
 static long m68k_d0;
 #elif defined(BFIN)
 static long bfin_r0;
 #elif defined(ARM)
 struct pt_regs arm_regs; /* not static */
+# define ARCH_REGS_FOR_GETREGS arm_regs
 #elif defined(AARCH64)
+struct arm_pt_regs {
+        int uregs[18];
+};
+# define ARM_cpsr       uregs[16]
+# define ARM_pc         uregs[15]
+# define ARM_lr         uregs[14]
+# define ARM_sp         uregs[13]
+# define ARM_ip         uregs[12]
+# define ARM_fp         uregs[11]
+# define ARM_r10        uregs[10]
+# define ARM_r9         uregs[9]
+# define ARM_r8         uregs[8]
+# define ARM_r7         uregs[7]
+# define ARM_r6         uregs[6]
+# define ARM_r5         uregs[5]
+# define ARM_r4         uregs[4]
+# define ARM_r3         uregs[3]
+# define ARM_r2         uregs[2]
+# define ARM_r1         uregs[1]
+# define ARM_r0         uregs[0]
+# define ARM_ORIG_r0    uregs[17]
 static union {
        struct user_pt_regs aarch64_r;
        struct arm_pt_regs  arm_r;
@@ -769,22 +744,23 @@ static union {
 static struct iovec aarch64_io = {
        .iov_base = &arm_regs_union
 };
+# define ARCH_REGS_FOR_GETREGSET arm_regs_union
+# define ARCH_IOVEC_FOR_GETREGSET aarch64_io
 #elif defined(ALPHA)
 static long alpha_r0;
 static long alpha_a3;
 #elif defined(AVR32)
 static struct pt_regs avr32_regs;
+# define ARCH_REGS_FOR_GETREGS avr32_regs
 #elif defined(SPARC) || defined(SPARC64)
 struct pt_regs sparc_regs; /* not static */
-#elif defined(LINUX_MIPSN32)
-static long long mips_a3;
-static long long mips_r2;
+# define ARCH_REGS_FOR_GETREGS sparc_regs
 #elif defined(MIPS)
-static long mips_a3;
-static long mips_r2;
+struct mips_regs mips_regs; /* not static */
+/* PTRACE_GETREGS on MIPS is available since linux v2.6.15. */
+# define ARCH_REGS_FOR_GETREGS mips_regs
 #elif defined(S390) || defined(S390X)
-static long gpr2;
-static long syscall_mode;
+static long s390_gpr2;
 #elif defined(HPPA)
 static long hppa_r28;
 #elif defined(SH)
@@ -794,180 +770,197 @@ static long sh64_r9;
 #elif defined(CRISV10) || defined(CRISV32)
 static long cris_r10;
 #elif defined(TILE)
-struct pt_regs tile_regs;
+struct pt_regs tile_regs; /* not static */
+# define ARCH_REGS_FOR_GETREGS tile_regs
 #elif defined(MICROBLAZE)
 static long microblaze_r3;
 #elif defined(OR1K)
 static struct user_regs_struct or1k_regs;
-static struct iovec or1k_io = {
-       .iov_base = &or1k_regs
-};
+# define ARCH_REGS_FOR_GETREGSET or1k_regs
+#elif defined(METAG)
+static struct user_gp_regs metag_regs;
+# define ARCH_REGS_FOR_GETREGSET metag_regs
+#elif defined(XTENSA)
+static long xtensa_a2;
+# elif defined(ARC)
+static struct user_regs_struct arc_regs;
+# define ARCH_REGS_FOR_GETREGSET arc_regs
 #endif
 
+static long get_regs_error;
+
 void
-printcall(struct tcb *tcp)
+print_pc(struct tcb *tcp)
 {
-#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
-                          sizeof(long) == 8 ? "[????????????????] " : \
-                          NULL /* crash */)
+       const char *fmt;
+       const char *bad;
+
+#ifdef current_wordsize
+# define pc_wordsize current_wordsize
+#else
+# define pc_wordsize personality_wordsize[tcp->currpers]
+#endif
+
+       if (pc_wordsize == 4) {
+               fmt = "[%08lx] ";
+               bad = "[????????] ";
+       } else {
+               fmt = "[%016lx] ";
+               bad = "[????????????????] ";
+       }
+
+#undef pc_wordsize
+#define PRINTBADPC tprints(bad)
+
        if (get_regs_error) {
                PRINTBADPC;
                return;
        }
+
 #if defined(I386)
-       tprintf("[%08lx] ", i386_regs.eip);
+       tprintf(fmt, i386_regs.eip);
+#elif defined(X86_64) || defined(X32)
+       if (x86_io.iov_len == sizeof(i386_regs))
+               tprintf(fmt, (unsigned long) i386_regs.eip);
+       else
+               tprintf(fmt, (unsigned long) x86_64_regs.rip);
 #elif defined(S390) || defined(S390X)
        long psw;
-       if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
+       if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
                PRINTBADPC;
                return;
        }
-# ifdef S390
-       tprintf("[%08lx] ", psw);
-# elif S390X
-       tprintf("[%016lx] ", psw);
-# endif
-#elif defined(X86_64) || defined(X32)
-       if (x86_io.iov_len == sizeof(i386_regs)) {
-               tprintf("[%08x] ", (unsigned) i386_regs.eip);
-       } else {
-# if defined(X86_64)
-               tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
-# elif defined(X32)
-               /* Note: this truncates 64-bit rip to 32 bits */
-               tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
-# endif
-       }
+       tprintf(fmt, psw);
 #elif defined(IA64)
        long ip;
-       if (upeek(tcp, PT_B0, &ip) < 0) {
+       if (upeek(tcp->pid, PT_B0, &ip) < 0) {
                PRINTBADPC;
                return;
        }
-       tprintf("[%08lx] ", ip);
+       tprintf(fmt, ip);
 #elif defined(POWERPC)
-       long pc;
-       if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
-               PRINTBADPC;
-               return;
-       }
-# ifdef POWERPC64
-       tprintf("[%016lx] ", pc);
-# else
-       tprintf("[%08lx] ", pc);
-# endif
+       tprintf(fmt, ppc_regs.nip);
 #elif defined(M68K)
        long pc;
-       if (upeek(tcp, 4*PT_PC, &pc) < 0) {
-               tprints("[????????] ");
+       if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
+               PRINTBADPC;
                return;
        }
-       tprintf("[%08lx] ", pc);
+       tprintf(fmt, pc);
 #elif defined(ALPHA)
        long pc;
-       if (upeek(tcp, REG_PC, &pc) < 0) {
-               tprints("[????????????????] ");
+       if (upeek(tcp->pid, REG_PC, &pc) < 0) {
+               PRINTBADPC;
                return;
        }
-       tprintf("[%08lx] ", pc);
+       tprintf(fmt, pc);
 #elif defined(SPARC)
-       tprintf("[%08lx] ", sparc_regs.pc);
+       tprintf(fmt, sparc_regs.pc);
 #elif defined(SPARC64)
-       tprintf("[%08lx] ", sparc_regs.tpc);
+       tprintf(fmt, sparc_regs.tpc);
 #elif defined(HPPA)
        long pc;
-       if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
-               tprints("[????????] ");
-               return;
-       }
-       tprintf("[%08lx] ", pc);
-#elif defined(MIPS)
-       long pc;
-       if (upeek(tcp, REG_EPC, &pc) < 0) {
-               tprints("[????????] ");
+       if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
+               PRINTBADPC;
                return;
        }
-       tprintf("[%08lx] ", pc);
+       tprintf(fmt, pc);
+#elif defined MIPS
+       tprintf(fmt, (unsigned long) mips_REG_EPC);
 #elif defined(SH)
        long pc;
-       if (upeek(tcp, 4*REG_PC, &pc) < 0) {
-               tprints("[????????] ");
+       if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
+               PRINTBADPC;
                return;
        }
-       tprintf("[%08lx] ", pc);
+       tprintf(fmt, pc);
 #elif defined(SH64)
        long pc;
-       if (upeek(tcp, REG_PC, &pc) < 0) {
-               tprints("[????????????????] ");
+       if (upeek(tcp->pid, REG_PC, &pc) < 0) {
+               PRINTBADPC;
                return;
        }
-       tprintf("[%08lx] ", pc);
-#elif defined(ARM)
-       tprintf("[%08lx] ", arm_regs.ARM_pc);
+       tprintf(fmt, pc);
 #elif defined(AARCH64)
-       /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
+       if (aarch64_io.iov_len == sizeof(arm_regs))
+               tprintf(fmt, (unsigned long) arm_regs.ARM_pc);
+       else
+               tprintf(fmt, (unsigned long) aarch64_regs.pc);
+#elif defined(ARM)
+       tprintf(fmt, arm_regs.ARM_pc);
 #elif defined(AVR32)
-       tprintf("[%08lx] ", avr32_regs.pc);
+       tprintf(fmt, avr32_regs.pc);
 #elif defined(BFIN)
        long pc;
-       if (upeek(tcp, PT_PC, &pc) < 0) {
+       if (upeek(tcp->pid, PT_PC, &pc) < 0) {
                PRINTBADPC;
                return;
        }
-       tprintf("[%08lx] ", pc);
+       tprintf(fmt, pc);
 #elif defined(CRISV10)
        long pc;
-       if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
+       if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
                PRINTBADPC;
                return;
        }
-       tprintf("[%08lx] ", pc);
+       tprintf(fmt, pc);
 #elif defined(CRISV32)
        long pc;
-       if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
+       if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
                PRINTBADPC;
                return;
        }
-       tprintf("[%08lx] ", pc);
+       tprintf(fmt, pc);
 #elif defined(TILE)
-# ifdef _LP64
-       tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
-# else
-       tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
-# endif
+       tprintf(fmt, (unsigned long) tile_regs.pc);
 #elif defined(OR1K)
-       tprintf("[%08lx] ", or1k_regs.pc);
+       tprintf(fmt, or1k_regs.pc);
+#elif defined(METAG)
+       tprintf(fmt, metag_regs.pc);
+#elif defined(XTENSA)
+       long pc;
+       if (upeek(tcp->pid, REG_PC, &pc) < 0) {
+               PRINTBADPC;
+               return;
+       }
+       tprintf(fmt, pc);
+#elif defined(ARC)
+       tprintf(fmt, arc_regs.efa);
+#else
+# warning print_pc is not implemented for this architecture
+       PRINTBADPC;
 #endif /* architecture */
 }
 
-/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
- * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
+/*
+ * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
+ * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
  */
-#if defined(ARM) /* So far only ARM needs this */
+#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
 static long
 shuffle_scno(unsigned long scno)
 {
-       if (scno <= ARM_LAST_ORDINARY_SYSCALL)
+       if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
                return scno;
 
        /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
-       if (scno == 0x000ffff0)
-               return ARM_LAST_ORDINARY_SYSCALL+1;
-       if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
+       if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
                return 0x000ffff0;
+       if (scno == 0x000ffff0)
+               return ARM_FIRST_SHUFFLED_SYSCALL;
 
-       /* Is it ARM specific syscall?
-        * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
+#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
+       /*
+        * Is it ARM specific syscall?
+        * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
+        * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
         */
-       if (scno >= 0x000f0000
-        && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
-       ) {
-               return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
+       if (scno >= 0x000f0000 &&
+           scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
+               return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
        }
-       if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
-        && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
-       ) {
-               return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
+       if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
+               return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
        }
 
        return scno;
@@ -985,85 +978,145 @@ undefined_scno_name(struct tcb *tcp)
        return buf;
 }
 
-#ifndef get_regs
-long get_regs_error;
+#ifdef POWERPC
+/*
+ * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
+ * we provide a slow fallback for old kernels.
+ */
+static int powerpc_getregs_old(pid_t pid)
+{
+       int i;
+       long r;
+
+       if (iflag) {
+               r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
+               if (r)
+                       goto out;
+       }
+#ifdef POWERPC64 /* else we never use it */
+       r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
+       if (r)
+               goto out;
+#endif
+       r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
+       if (r)
+               goto out;
+       r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
+       if (r)
+               goto out;
+       for (i = 0; i <= 8; i++) {
+               r = upeek(pid, sizeof(long) * (PT_R0 + i),
+                         (long *)&ppc_regs.gpr[i]);
+               if (r)
+                       goto out;
+       }
+ out:
+       return r;
+}
+#endif
+
+void
+clear_regs(void)
+{
+       get_regs_error = -1;
+}
+
+#if defined ARCH_REGS_FOR_GETREGSET
+static long
+get_regset(pid_t pid)
+{
+# ifdef ARCH_IOVEC_FOR_GETREGSET
+       /* variable iovec */
+       ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
+       return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
+                     &ARCH_IOVEC_FOR_GETREGSET);
+# else
+       /* constant iovec */
+       static struct iovec io = {
+               .iov_base = &ARCH_REGS_FOR_GETREGSET,
+               .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
+       };
+       return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
+
+# endif
+}
+#endif /* ARCH_REGS_FOR_GETREGSET */
+
 void
 get_regs(pid_t pid)
 {
-# if defined(AVR32)
-       get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
-# elif defined(I386)
-       get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs);
-# elif defined(X86_64) || defined(X32)
-       /*
-        * PTRACE_GETREGSET was introduced in 2.6.33.
-        * Let's be paranoid and require a bit later kernel.
-        */
-       if (os_release >= KERNEL_VERSION(2,6,35)) {
-               /*x86_io.iov_base = &x86_regs_union; - already is */
-               x86_io.iov_len = sizeof(x86_regs_union);
-               get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (long) &x86_io);
-       } else {
-               /* Use old method, with heuristical detection of 32-bitness */
-               x86_io.iov_len = sizeof(x86_64_regs);
-               get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
-               if (!get_regs_error && x86_64_regs.cs == 0x23) {
-                       x86_io.iov_len = sizeof(i386_regs);
-                       /*
-                        * The order is important: i386_regs and x86_64_regs
-                        * are overlaid in memory!
-                        */
-                       i386_regs.ebx = x86_64_regs.rbx;
-                       i386_regs.ecx = x86_64_regs.rcx;
-                       i386_regs.edx = x86_64_regs.rdx;
-                       i386_regs.esi = x86_64_regs.rsi;
-                       i386_regs.edi = x86_64_regs.rdi;
-                       i386_regs.ebp = x86_64_regs.rbp;
-                       i386_regs.eax = x86_64_regs.rax;
-                       /*i386_regs.xds = x86_64_regs.ds; unused by strace */
-                       /*i386_regs.xes = x86_64_regs.es; ditto... */
-                       /*i386_regs.xfs = x86_64_regs.fs;*/
-                       /*i386_regs.xgs = x86_64_regs.gs;*/
-                       i386_regs.orig_eax = x86_64_regs.orig_rax;
-                       i386_regs.eip = x86_64_regs.rip;
-                       /*i386_regs.xcs = x86_64_regs.cs;*/
-                       /*i386_regs.eflags = x86_64_regs.eflags;*/
-                       i386_regs.esp = x86_64_regs.rsp;
-                       /*i386_regs.xss = x86_64_regs.ss;*/
+#ifdef ARCH_REGS_FOR_GETREGSET
+# ifdef X86_64
+       /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
+       static int getregset_support;
+
+       if (getregset_support >= 0) {
+               get_regs_error = get_regset(pid);
+               if (getregset_support > 0)
+                       return;
+               if (get_regs_error >= 0) {
+                       getregset_support = 1;
+                       return;
                }
+               if (errno == EPERM || errno == ESRCH)
+                       return;
+               getregset_support = -1;
        }
-# elif defined(ARM)
-       get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
-# elif defined(AARCH64)
-       /*aarch64_io.iov_base = &arm_regs_union; - already is */
-       aarch64_io.iov_len = sizeof(arm_regs_union);
-       get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
-#  if 0
-       /* Paranoia checks */
-       if (get_regs_error)
-               return;
-       switch (aarch64_io.iov_len) {
-               case sizeof(aarch64_regs):
-                       /* We are in 64-bit mode */
-                       break;
-               case sizeof(arm_regs):
-                       /* We are in 32-bit mode */
-                       break;
-               default:
-                       get_regs_error = -1;
-                       break;
+       /* Use old method, with unreliable heuristical detection of 32-bitness. */
+       x86_io.iov_len = sizeof(x86_64_regs);
+       get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
+       if (!get_regs_error && x86_64_regs.cs == 0x23) {
+               x86_io.iov_len = sizeof(i386_regs);
+               /*
+                * The order is important: i386_regs and x86_64_regs
+                * are overlaid in memory!
+                */
+               i386_regs.ebx = x86_64_regs.rbx;
+               i386_regs.ecx = x86_64_regs.rcx;
+               i386_regs.edx = x86_64_regs.rdx;
+               i386_regs.esi = x86_64_regs.rsi;
+               i386_regs.edi = x86_64_regs.rdi;
+               i386_regs.ebp = x86_64_regs.rbp;
+               i386_regs.eax = x86_64_regs.rax;
+               /* i386_regs.xds = x86_64_regs.ds; unused by strace */
+               /* i386_regs.xes = x86_64_regs.es; ditto... */
+               /* i386_regs.xfs = x86_64_regs.fs; */
+               /* i386_regs.xgs = x86_64_regs.gs; */
+               i386_regs.orig_eax = x86_64_regs.orig_rax;
+               i386_regs.eip = x86_64_regs.rip;
+               /* i386_regs.xcs = x86_64_regs.cs; */
+               /* i386_regs.eflags = x86_64_regs.eflags; */
+               i386_regs.esp = x86_64_regs.rsp;
+               /* i386_regs.xss = x86_64_regs.ss; */
+       }
+# else /* !X86_64 */
+       /* Assume that PTRACE_GETREGSET works. */
+       get_regs_error = get_regset(pid);
+# endif
+#elif defined ARCH_REGS_FOR_GETREGS
+# if defined SPARC || defined SPARC64
+       /* SPARC systems have the meaning of data and addr reversed */
+       get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
+# elif defined POWERPC
+       static bool old_kernel = 0;
+       if (old_kernel)
+               goto old;
+       get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
+       if (get_regs_error && errno == EIO) {
+               old_kernel = 1;
+ old:
+               get_regs_error = powerpc_getregs_old(pid);
        }
-#  endif
-# elif defined(SPARC) || defined(SPARC64)
-       get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
-# elif defined(TILE)
-       get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
-# elif defined(OR1K)
-       or1k_io.iov_len = sizeof(or1k_regs);
-       get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io);
+# else
+       /* Assume that PTRACE_GETREGS works. */
+       get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
 # endif
-}
+
+#else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
+#  warning get_regs is not implemented for this architecture yet
+       get_regs_error = 0;
 #endif
+}
 
 /* Returns:
  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
@@ -1077,14 +1130,14 @@ get_scno(struct tcb *tcp)
        long scno = 0;
 
 #if defined(S390) || defined(S390X)
-       if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
+       if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
                return -1;
 
-       if (syscall_mode != -ENOSYS) {
+       if (s390_gpr2 != -ENOSYS) {
                /*
                 * Since kernel version 2.5.44 the scno gets passed in gpr2.
                 */
-               scno = syscall_mode;
+               scno = s390_gpr2;
        } else {
                /*
                 * Old style of "passing" the scno via the SVC instruction.
@@ -1099,7 +1152,7 @@ get_scno(struct tcb *tcp)
                                PT_GPR12, PT_GPR13, PT_GPR14,    PT_GPR15
                };
 
-               if (upeek(tcp, PT_PSWADDR, &psw) < 0)
+               if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
                        return -1;
                errno = 0;
                opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
@@ -1132,13 +1185,13 @@ get_scno(struct tcb *tcp)
 
                        tmp = 0;
                        offset_reg = (opcode & 0x000f0000) >> 16;
-                       if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
+                       if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
                                return -1;
                        svc_addr += tmp;
 
                        tmp = 0;
                        offset_reg = (opcode & 0x0000f000) >> 12;
-                       if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
+                       if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
                                return -1;
                        svc_addr += tmp;
 
@@ -1152,36 +1205,30 @@ get_scno(struct tcb *tcp)
 # endif
                        tmp = 0;
                        offset_reg = (opcode & 0x00f00000) >> 20;
-                       if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
+                       if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
                                return -1;
 
                        scno = (scno | tmp) & 0xff;
                }
        }
 #elif defined(POWERPC)
-       if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
-               return -1;
+       scno = ppc_regs.gpr[0];
 # ifdef POWERPC64
-       /* TODO: speed up strace by not doing this at every syscall.
-        * We only need to do it after execve.
-        */
-       int currpers;
-       long val;
+       unsigned int currpers;
 
-       /* Check for 64/32 bit mode. */
-       if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
-               return -1;
-       /* SF is bit 0 of MSR */
-       if (val < 0)
-               currpers = 0;
-       else
-               currpers = 1;
+       /*
+        * Check for 64/32 bit mode.
+        * Embedded implementations covered by Book E extension of PPC use
+        * bit 0 (CM) of 32-bit Machine state register (MSR).
+        * Other implementations use bit 0 (SF) of 64-bit MSR.
+        */
+       currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
        update_personality(tcp, currpers);
 # endif
 #elif defined(AVR32)
        scno = avr32_regs.r8;
 #elif defined(BFIN)
-       if (upeek(tcp, PT_ORIG_P0, &scno))
+       if (upeek(tcp->pid, PT_ORIG_P0, &scno))
                return -1;
 #elif defined(I386)
        scno = i386_regs.orig_eax;
@@ -1189,7 +1236,7 @@ get_scno(struct tcb *tcp)
 # ifndef __X32_SYSCALL_BIT
 #  define __X32_SYSCALL_BIT    0x40000000
 # endif
-       int currpers;
+       unsigned int currpers;
 # if 1
        /* GETREGSET of NT_PRSTATUS tells us regset size,
         * which unambiguously detects i386.
@@ -1207,8 +1254,22 @@ get_scno(struct tcb *tcp)
                scno = x86_64_regs.orig_rax;
                currpers = 0;
                if (scno & __X32_SYSCALL_BIT) {
-                       scno -= __X32_SYSCALL_BIT;
-                       currpers = 2;
+                       /*
+                        * Syscall number -1 requires special treatment:
+                        * it might be a side effect of SECCOMP_RET_ERRNO
+                        * filtering that sets orig_rax to -1
+                        * in some versions of linux kernel.
+                        * If that is the case, then
+                        * __X32_SYSCALL_BIT logic does not apply.
+                        */
+                       if ((long long) x86_64_regs.orig_rax != -1) {
+                               scno -= __X32_SYSCALL_BIT;
+                               currpers = 2;
+                       } else {
+#  ifdef X32
+                               currpers = 2;
+#  endif
+                       }
                }
        }
 # elif 0
@@ -1278,13 +1339,13 @@ get_scno(struct tcb *tcp)
 #elif defined(IA64)
 #      define IA64_PSR_IS      ((long)1 << 34)
        long psr;
-       if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
-               ia32 = (psr & IA64_PSR_IS) != 0;
-       if (ia32) {
-               if (upeek(tcp, PT_R1, &scno) < 0)
+       if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
+               ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
+       if (ia64_ia32mode) {
+               if (upeek(tcp->pid, PT_R1, &scno) < 0)
                        return -1;
        } else {
-               if (upeek(tcp, PT_R15, &scno) < 0)
+               if (upeek(tcp->pid, PT_R15, &scno) < 0)
                        return -1;
        }
 #elif defined(AARCH64)
@@ -1296,7 +1357,9 @@ get_scno(struct tcb *tcp)
                        break;
                case sizeof(arm_regs):
                        /* We are in 32-bit mode */
+                       /* Note: we don't support OABI, unlike 32-bit ARM build */
                        scno = arm_regs.ARM_r7;
+                       scno = shuffle_scno(scno);
                        update_personality(tcp, 0);
                        break;
        }
@@ -1309,67 +1372,51 @@ get_scno(struct tcb *tcp)
        }
        /* Note: we support only 32-bit CPUs, not 26-bit */
 
-       if (arm_regs.ARM_cpsr & 0x20) {
+# if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
+       if (arm_regs.ARM_cpsr & 0x20)
                /* Thumb mode */
-               scno = arm_regs.ARM_r7;
-       } else {
-               /* ARM mode */
-               errno = 0;
-               scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
-               if (errno)
+               goto scno_in_r7;
+       /* ARM mode */
+       /* Check EABI/OABI by examining SVC insn's low 24 bits */
+       errno = 0;
+       scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
+       if (errno)
+               return -1;
+       /* EABI syscall convention? */
+       if ((unsigned long) scno != 0xef000000) {
+               /* No, it's OABI */
+               if ((scno & 0x0ff00000) != 0x0f900000) {
+                       fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
+                               tcp->pid, scno);
                        return -1;
-
-               /* EABI syscall convention? */
-               if (scno == 0xef000000) {
-                       scno = arm_regs.ARM_r7; /* yes */
-               } else {
-                       if ((scno & 0x0ff00000) != 0x0f900000) {
-                               fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
-                                       tcp->pid, scno);
-                               return -1;
-                       }
-                       /* Fixup the syscall number */
-                       scno &= 0x000fffff;
                }
+               /* Fixup the syscall number */
+               scno &= 0x000fffff;
+       } else {
+ scno_in_r7:
+               scno = arm_regs.ARM_r7;
        }
-
+# else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
+       scno = arm_regs.ARM_r7;
+# endif
        scno = shuffle_scno(scno);
 #elif defined(M68K)
-       if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
-               return -1;
-#elif defined(LINUX_MIPSN32)
-       unsigned long long regs[38];
-
-       if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
+       if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
                return -1;
-       mips_a3 = regs[REG_A3];
-       mips_r2 = regs[REG_V0];
-
-       scno = mips_r2;
-       if (!SCNO_IN_RANGE(scno)) {
-               if (mips_a3 == 0 || mips_a3 == -1) {
-                       if (debug_flag)
-                               fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
-                       return 0;
-               }
-       }
 #elif defined(MIPS)
-       if (upeek(tcp, REG_A3, &mips_a3) < 0)
-               return -1;
-       if (upeek(tcp, REG_V0, &scno) < 0)
-               return -1;
+       scno = mips_REG_V0;
 
        if (!SCNO_IN_RANGE(scno)) {
-               if (mips_a3 == 0 || mips_a3 == -1) {
+               if (mips_REG_A3 == 0 || mips_REG_A3 == (uint64_t) -1) {
                        if (debug_flag)
                                fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
                        return 0;
                }
        }
 #elif defined(ALPHA)
-       if (upeek(tcp, REG_A3, &alpha_a3) < 0)
+       if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
                return -1;
-       if (upeek(tcp, REG_R0, &scno) < 0)
+       if (upeek(tcp->pid, REG_R0, &scno) < 0)
                return -1;
 
        /*
@@ -1442,13 +1489,13 @@ get_scno(struct tcb *tcp)
                memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
        }
 #elif defined(HPPA)
-       if (upeek(tcp, PT_GR20, &scno) < 0)
+       if (upeek(tcp->pid, PT_GR20, &scno) < 0)
                return -1;
 #elif defined(SH)
        /*
         * In the new syscall ABI, the system call number is in R3.
         */
-       if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
+       if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
                return -1;
 
        if (scno < 0) {
@@ -1465,14 +1512,14 @@ get_scno(struct tcb *tcp)
                scno = correct_scno;
        }
 #elif defined(SH64)
-       if (upeek(tcp, REG_SYSCALL, &scno) < 0)
+       if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
                return -1;
        scno &= 0xFFFF;
 #elif defined(CRISV10) || defined(CRISV32)
-       if (upeek(tcp, 4*PT_R9, &scno) < 0)
+       if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
                return -1;
 #elif defined(TILE)
-       int currpers;
+       unsigned int currpers;
        scno = tile_regs.regs[10];
 # ifdef __tilepro__
        currpers = 1;
@@ -1487,10 +1534,17 @@ get_scno(struct tcb *tcp)
 # endif
        update_personality(tcp, currpers);
 #elif defined(MICROBLAZE)
-       if (upeek(tcp, 0, &scno) < 0)
+       if (upeek(tcp->pid, 0, &scno) < 0)
                return -1;
 #elif defined(OR1K)
        scno = or1k_regs.gpr[11];
+#elif defined(METAG)
+       scno = metag_regs.dx[0][1];     /* syscall number in D1Re0 (D1.0) */
+#elif defined(XTENSA)
+       if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
+               return -1;
+# elif defined(ARC)
+       scno = arc_regs.scratch.r8;
 #endif
 
        tcp->scno = scno;
@@ -1510,174 +1564,51 @@ get_scno(struct tcb *tcp)
        return 1;
 }
 
-/* Called at each syscall entry.
- * Returns:
- * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
- * 1: ok, continue in trace_syscall_entering().
- * other: error, trace_syscall_entering() should print error indicator
- *    ("????" etc) and bail out.
+/*
+ * Cannot rely on __kernel_[u]long_t being defined,
+ * it is quite a recent feature of <asm/posix_types.h>.
  */
-static int
-syscall_fixup_on_sysenter(struct tcb *tcp)
-{
-       /* A common case of "not a syscall entry" is post-execve SIGTRAP */
-#if defined(I386)
-       if (i386_regs.eax != -ENOSYS) {
-               if (debug_flag)
-                       fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
-               return 0;
-       }
-#elif defined(X86_64) || defined(X32)
-       {
-               long rax;
-               if (x86_io.iov_len == sizeof(i386_regs)) {
-                       /* Sign extend from 32 bits */
-                       rax = (int32_t)i386_regs.eax;
-               } else {
-                       /* Note: in X32 build, this truncates 64 to 32 bits */
-                       rax = x86_64_regs.rax;
-               }
-               if (rax != -ENOSYS) {
-                       if (debug_flag)
-                               fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
-                       return 0;
-               }
-       }
-#elif defined(S390) || defined(S390X)
-       /* TODO: we already fetched PT_GPR2 in get_scno
-        * and stored it in syscall_mode, reuse it here
-        * instead of re-fetching?
-        */
-       if (upeek(tcp, PT_GPR2, &gpr2) < 0)
-               return -1;
-       if (syscall_mode != -ENOSYS)
-               syscall_mode = tcp->scno;
-       if (gpr2 != syscall_mode) {
-               if (debug_flag)
-                       fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
-               return 0;
-       }
-#elif defined(M68K)
-       /* TODO? Eliminate upeek's in arches below like we did in x86 */
-       if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
-               return -1;
-       if (m68k_d0 != -ENOSYS) {
-               if (debug_flag)
-                       fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
-               return 0;
-       }
-#elif defined(IA64)
-       if (upeek(tcp, PT_R10, &ia64_r10) < 0)
-               return -1;
-       if (upeek(tcp, PT_R8, &ia64_r8) < 0)
-               return -1;
-       if (ia32 && ia64_r8 != -ENOSYS) {
-               if (debug_flag)
-                       fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
-               return 0;
-       }
-#elif defined(CRISV10) || defined(CRISV32)
-       if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
-               return -1;
-       if (cris_r10 != -ENOSYS) {
-               if (debug_flag)
-                       fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
-               return 0;
-       }
-#elif defined(MICROBLAZE)
-       if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
-               return -1;
-       if (microblaze_r3 != -ENOSYS) {
-               if (debug_flag)
-                       fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
-               return 0;
-       }
-#endif
-       return 1;
-}
-
-static void
-internal_fork(struct tcb *tcp)
-{
-#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
-# define ARG_FLAGS     1
+#ifdef __kernel_long_t
+typedef __kernel_long_t kernel_long_t;
+typedef __kernel_ulong_t kernel_ulong_t;
 #else
-# define ARG_FLAGS     0
-#endif
-#ifndef CLONE_UNTRACED
-# define CLONE_UNTRACED        0x00800000
+# ifdef X32
+typedef long long kernel_long_t;
+typedef unsigned long long kernel_ulong_t;
+# else
+typedef long kernel_long_t;
+typedef unsigned long kernel_ulong_t;
+# endif
 #endif
-       if ((ptrace_setoptions
-           & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
-          == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
-               return;
-
-       if (!followfork)
-               return;
-
-       if (entering(tcp)) {
-               /*
-                * We won't see the new child if clone is called with
-                * CLONE_UNTRACED, so we keep the same logic with that option
-                * and don't trace it.
-                */
-               if ((tcp->s_ent->sys_func == sys_clone)
-                && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
-               )
-                       return;
-               setbpt(tcp);
-       } else {
-               if (tcp->flags & TCB_BPTSET)
-                       clearbpt(tcp);
-       }
-}
 
-#if defined(TCB_WAITEXECVE)
-static void
-internal_exec(struct tcb *tcp)
+/*
+ * Check the syscall return value register value for whether it is
+ * a negated errno code indicating an error, or a success return value.
+ */
+static inline bool
+is_negated_errno(kernel_ulong_t val)
 {
-       /* Maybe we have post-execve SIGTRAP suppressed? */
-       if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
-               return; /* yes, no need to do anything */
-
-       if (exiting(tcp) && syserror(tcp))
-               /* Error in execve, no post-execve SIGTRAP expected */
-               tcp->flags &= ~TCB_WAITEXECVE;
-       else
-               tcp->flags |= TCB_WAITEXECVE;
-}
-#endif
+       /* Linux kernel defines MAX_ERRNO to 4095. */
+       kernel_ulong_t max = -(kernel_long_t) 4095;
 
-static void
-syscall_fixup_for_fork_exec(struct tcb *tcp)
-{
+#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
+       if (current_wordsize < sizeof(val)) {
+               val = (uint32_t) val;
+               max = (uint32_t) max;
+       }
+#elif defined X32
        /*
-        * We must always trace a few critical system calls in order to
-        * correctly support following forks in the presence of tracing
-        * qualifiers.
+        * current_wordsize is 4 even in personality 0 (native X32)
+        * but truncation _must not_ be done in it.
+        * can't check current_wordsize here!
         */
-       int (*func)();
-
-       func = tcp->s_ent->sys_func;
-
-       if (   sys_fork == func
-           || sys_vfork == func
-           || sys_clone == func
-          ) {
-               internal_fork(tcp);
-               return;
-       }
-
-#if defined(TCB_WAITEXECVE)
-       if (   sys_execve == func
-# if defined(SPARC) || defined(SPARC64)
-           || sys_execv == func
-# endif
-          ) {
-               internal_exec(tcp);
-               return;
+       if (current_personality != 0) {
+               val = (uint32_t) val;
+               max = (uint32_t) max;
        }
 #endif
+
+       return val >= max;
 }
 
 /* Return -1 on error or 1 on success (never 0!) */
@@ -1690,14 +1621,14 @@ get_syscall_args(struct tcb *tcp)
 
 #if defined(S390) || defined(S390X)
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
                        return -1;
 #elif defined(ALPHA)
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
                        return -1;
 #elif defined(IA64)
-       if (!ia32) {
+       if (!ia64_ia32mode) {
                unsigned long *out0, cfm, sof, sol;
                long rbs_end;
                /* be backwards compatible with kernel < 2.4.4... */
@@ -1705,9 +1636,9 @@ get_syscall_args(struct tcb *tcp)
 #                define PT_RBS_END     PT_AR_BSP
 #              endif
 
-               if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
+               if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
                        return -1;
-               if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
+               if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
                        return -1;
 
                sof = (cfm >> 0) & 0x7f;
@@ -1728,58 +1659,57 @@ get_syscall_args(struct tcb *tcp)
                                                      PT_R13 /* EBP = out5 */};
 
                for (i = 0; i < nargs; ++i) {
-                       if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
+                       if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
                                return -1;
                        /* truncate away IVE sign-extension */
                        tcp->u_arg[i] &= 0xffffffff;
                }
        }
-#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
-       /* N32 and N64 both use up to six registers.  */
-       unsigned long long regs[38];
-
-       if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
-               return -1;
-
-       for (i = 0; i < nargs; ++i) {
-               tcp->u_arg[i] = regs[REG_A0 + i];
-# if defined(LINUX_MIPSN32)
-               tcp->ext_arg[i] = regs[REG_A0 + i];
-# endif
-       }
-#elif defined(MIPS)
+#elif defined LINUX_MIPSN64
+       (void)i;
+       (void)nargs;
+       tcp->u_arg[0] = mips_REG_A0;
+       tcp->u_arg[1] = mips_REG_A1;
+       tcp->u_arg[2] = mips_REG_A2;
+       tcp->u_arg[3] = mips_REG_A3;
+       tcp->u_arg[4] = mips_REG_A4;
+       tcp->u_arg[5] = mips_REG_A5;
+#elif defined LINUX_MIPSN32
+       (void)i;
+       (void)nargs;
+       tcp->u_arg[0] = tcp->ext_arg[0] = mips_REG_A0;
+       tcp->u_arg[1] = tcp->ext_arg[1] = mips_REG_A1;
+       tcp->u_arg[2] = tcp->ext_arg[2] = mips_REG_A2;
+       tcp->u_arg[3] = tcp->ext_arg[3] = mips_REG_A3;
+       tcp->u_arg[4] = tcp->ext_arg[4] = mips_REG_A4;
+       tcp->u_arg[5] = tcp->ext_arg[5] = mips_REG_A5;
+#elif defined LINUX_MIPSO32
+       (void)i;
+       (void)nargs;
+       tcp->u_arg[0] = mips_REG_A0;
+       tcp->u_arg[1] = mips_REG_A1;
+       tcp->u_arg[2] = mips_REG_A2;
+       tcp->u_arg[3] = mips_REG_A3;
        if (nargs > 4) {
-               long sp;
-
-               if (upeek(tcp, REG_SP, &sp) < 0)
-                       return -1;
-               for (i = 0; i < 4; ++i)
-                       if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
-                               return -1;
-               umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
+               umoven(tcp, mips_REG_SP + 4 * 4,
+                      (nargs - 4) * sizeof(tcp->u_arg[0]),
                       (char *)(tcp->u_arg + 4));
-       } else {
-               for (i = 0; i < nargs; ++i)
-                       if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
-                               return -1;
        }
 #elif defined(POWERPC)
-# ifndef PT_ORIG_R3
-#  define PT_ORIG_R3 34
-# endif
-       for (i = 0; i < nargs; ++i) {
-               if (upeek(tcp, (i==0) ?
-                       (sizeof(unsigned long) * PT_ORIG_R3) :
-                       ((i+PT_R3) * sizeof(unsigned long)),
-                               &tcp->u_arg[i]) < 0)
-                       return -1;
-       }
+       (void)i;
+       (void)nargs;
+       tcp->u_arg[0] = ppc_regs.orig_gpr3;
+       tcp->u_arg[1] = ppc_regs.gpr[4];
+       tcp->u_arg[2] = ppc_regs.gpr[5];
+       tcp->u_arg[3] = ppc_regs.gpr[6];
+       tcp->u_arg[4] = ppc_regs.gpr[7];
+       tcp->u_arg[5] = ppc_regs.gpr[8];
 #elif defined(SPARC) || defined(SPARC64)
        for (i = 0; i < nargs; ++i)
                tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
 #elif defined(HPPA)
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
                        return -1;
 #elif defined(ARM) || defined(AARCH64)
 # if defined(AARCH64)
@@ -1803,7 +1733,7 @@ get_syscall_args(struct tcb *tcp)
        static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
 
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
                        return -1;
 #elif defined(SH)
        static const int syscall_regs[MAX_ARGS] = {
@@ -1812,7 +1742,7 @@ get_syscall_args(struct tcb *tcp)
        };
 
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
                        return -1;
 #elif defined(SH64)
        int i;
@@ -1820,7 +1750,7 @@ get_syscall_args(struct tcb *tcp)
        static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
 
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
                        return -1;
 #elif defined(I386)
        (void)i;
@@ -1865,7 +1795,7 @@ get_syscall_args(struct tcb *tcp)
        }
 #elif defined(MICROBLAZE)
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
                        return -1;
 #elif defined(CRISV10) || defined(CRISV32)
        static const int crisregs[MAX_ARGS] = {
@@ -1874,22 +1804,37 @@ get_syscall_args(struct tcb *tcp)
        };
 
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
                        return -1;
 #elif defined(TILE)
        for (i = 0; i < nargs; ++i)
                tcp->u_arg[i] = tile_regs.regs[i];
 #elif defined(M68K)
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
                        return -1;
 #elif defined(OR1K)
        (void)nargs;
        for (i = 0; i < 6; ++i)
                tcp->u_arg[i] = or1k_regs.gpr[3 + i];
+#elif defined(METAG)
+       for (i = 0; i < nargs; i++)
+               /* arguments go backwards from D1Ar1 (D1.3) */
+               tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
+#elif defined(XTENSA)
+       /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
+       static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
+       for (i = 0; i < nargs; ++i)
+               if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
+                       return -1;
+# elif defined(ARC)
+       long *arc_args = &arc_regs.scratch.r0;
+       for (i = 0; i < nargs; ++i)
+               tcp->u_arg[i] = *arc_args--;
+
 #else /* Other architecture (32bits specific) */
        for (i = 0; i < nargs; ++i)
-               if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
+               if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
                        return -1;
 #endif
        return 1;
@@ -1900,24 +1845,11 @@ trace_syscall_entering(struct tcb *tcp)
 {
        int res, scno_good;
 
-#if defined TCB_WAITEXECVE
-       if (tcp->flags & TCB_WAITEXECVE) {
-               /* This is the post-execve SIGTRAP. */
-               tcp->flags &= ~TCB_WAITEXECVE;
-               return 0;
-       }
-#endif
-
        scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
        if (res == 0)
                return res;
-       if (res == 1) {
-               res = syscall_fixup_on_sysenter(tcp);
-               if (res == 0)
-                       return res;
-               if (res == 1)
-                       res = get_syscall_args(tcp);
-       }
+       if (res == 1)
+               res = get_syscall_args(tcp);
 
        if (res != 1) {
                printleader(tcp);
@@ -1934,6 +1866,14 @@ trace_syscall_entering(struct tcb *tcp)
                goto ret;
        }
 
+       if (   sys_execve == tcp->s_ent->sys_func
+# if defined(SPARC) || defined(SPARC64)
+           || sys_execv == tcp->s_ent->sys_func
+# endif
+          ) {
+               hide_log_until_execve = 0;
+       }
+
 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
        while (1) {
 # ifdef SYS_socket_subcall
@@ -1952,9 +1892,6 @@ trace_syscall_entering(struct tcb *tcp)
        }
 #endif
 
-       if (need_fork_exec_workarounds)
-               syscall_fixup_for_fork_exec(tcp);
-
        if (!(tcp->qual_flg & QUAL_TRACE)
         || (tracing_paths && !pathtrace_match(tcp))
        ) {
@@ -1964,11 +1901,18 @@ trace_syscall_entering(struct tcb *tcp)
 
        tcp->flags &= ~TCB_FILTERED;
 
-       if (cflag == CFLAG_ONLY_STATS) {
+       if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
                res = 0;
                goto ret;
        }
 
+#ifdef USE_LIBUNWIND
+       if (stack_trace_enabled) {
+               if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
+                       unwind_capture_stacktrace(tcp);
+       }
+#endif
+
        printleader(tcp);
        if (tcp->qual_flg & UNDEFINED_SCNO)
                tprintf("%s(", undefined_scno_name(tcp));
@@ -1996,149 +1940,57 @@ trace_syscall_entering(struct tcb *tcp)
 static int
 get_syscall_result(struct tcb *tcp)
 {
-#if defined(S390) || defined(S390X)
-       if (upeek(tcp, PT_GPR2, &gpr2) < 0)
-               return -1;
-#elif defined(POWERPC)
-# define SO_MASK 0x10000000
-       {
-               long flags;
-               if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
-                       return -1;
-               if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
-                       return -1;
-               if (flags & SO_MASK)
-                       ppc_result = -ppc_result;
-       }
-#elif defined(AVR32)
+#if defined ARCH_REGS_FOR_GETREGSET || defined ARCH_REGS_FOR_GETREGS
        /* already done by get_regs */
+#elif defined(S390) || defined(S390X)
+       if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
+               return -1;
 #elif defined(BFIN)
-       if (upeek(tcp, PT_R0, &bfin_r0) < 0)
+       if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
                return -1;
-#elif defined(I386)
-       /* already done by get_regs */
-#elif defined(X86_64) || defined(X32)
-       /* already done by get_regs */
 #elif defined(IA64)
 #      define IA64_PSR_IS      ((long)1 << 34)
        long psr;
-       if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
-               ia32 = (psr & IA64_PSR_IS) != 0;
-       if (upeek(tcp, PT_R8, &ia64_r8) < 0)
+       if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
+               ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
+       if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
                return -1;
-       if (upeek(tcp, PT_R10, &ia64_r10) < 0)
+       if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
                return -1;
-#elif defined(ARM)
-       /* already done by get_regs */
-#elif defined(AARCH64)
-       /* register reading already done by get_regs */
-
-       /* Used to do this, but we did it on syscall entry already: */
-       /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
-        * else it's personality 0.
-        */
-       /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
 #elif defined(M68K)
-       if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
-               return -1;
-#elif defined(LINUX_MIPSN32)
-       unsigned long long regs[38];
-
-       if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
-               return -1;
-       mips_a3 = regs[REG_A3];
-       mips_r2 = regs[REG_V0];
-#elif defined(MIPS)
-       if (upeek(tcp, REG_A3, &mips_a3) < 0)
-               return -1;
-       if (upeek(tcp, REG_V0, &mips_r2) < 0)
+       if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
                return -1;
 #elif defined(ALPHA)
-       if (upeek(tcp, REG_A3, &alpha_a3) < 0)
+       if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
                return -1;
-       if (upeek(tcp, REG_R0, &alpha_r0) < 0)
+       if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
                return -1;
-#elif defined(SPARC) || defined(SPARC64)
-       /* already done by get_regs */
 #elif defined(HPPA)
-       if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
+       if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
                return -1;
 #elif defined(SH)
        /* new syscall ABI returns result in R0 */
-       if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
+       if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
                return -1;
 #elif defined(SH64)
        /* ABI defines result returned in r9 */
-       if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
+       if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
                return -1;
 #elif defined(CRISV10) || defined(CRISV32)
-       if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
+       if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
                return -1;
-#elif defined(TILE)
-       /* already done by get_regs */
 #elif defined(MICROBLAZE)
-       if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
+       if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
                return -1;
-#elif defined(OR1K)
-       /* already done by get_regs */
+#elif defined(XTENSA)
+       if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
+               return -1;
+#else
+# error get_syscall_result is not implemented for this architecture
 #endif
        return 1;
 }
 
-/* Called at each syscall exit */
-static void
-syscall_fixup_on_sysexit(struct tcb *tcp)
-{
-#if defined(S390) || defined(S390X)
-       if (syscall_mode != -ENOSYS)
-               syscall_mode = tcp->scno;
-       if ((tcp->flags & TCB_WAITEXECVE)
-                && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
-               /*
-                * Return from execve.
-                * Fake a return value of zero.  We leave the TCB_WAITEXECVE
-                * flag set for the post-execve SIGTRAP to see and reset.
-                */
-               gpr2 = 0;
-       }
-#endif
-}
-
-/*
- * Check the syscall return value register value for whether it is
- * a negated errno code indicating an error, or a success return value.
- */
-static inline int
-is_negated_errno(unsigned long int val)
-{
-       unsigned long int max = -(long int) nerrnos;
-#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
-       if (current_wordsize < sizeof(val)) {
-               val = (unsigned int) val;
-               max = (unsigned int) max;
-       }
-#endif
-       return val > max;
-}
-
-#if defined(X32)
-static inline int
-is_negated_errno_x32(unsigned long long val)
-{
-       unsigned long long max = -(long long) nerrnos;
-       /*
-        * current_wordsize is 4 even in personality 0 (native X32)
-        * but truncation _must not_ be done in it.
-        * can't check current_wordsize here!
-        */
-       if (current_personality != 0) {
-               val = (uint32_t) val;
-               max = (uint32_t) max;
-       }
-       return val > max;
-}
-#endif
-
 /* Returns:
  * 1: ok, continue in trace_syscall_exiting().
  * -1: error, trace_syscall_exiting() should print error indicator
@@ -2153,12 +2005,12 @@ get_error(struct tcb *tcp)
                check_errno = 0;
        }
 #if defined(S390) || defined(S390X)
-       if (check_errno && is_negated_errno(gpr2)) {
+       if (check_errno && is_negated_errno(s390_gpr2)) {
                tcp->u_rval = -1;
-               u_error = -gpr2;
+               u_error = -s390_gpr2;
        }
        else {
-               tcp->u_rval = gpr2;
+               tcp->u_rval = s390_gpr2;
        }
 #elif defined(I386)
        if (check_errno && is_negated_errno(i386_regs.eax)) {
@@ -2168,43 +2020,32 @@ get_error(struct tcb *tcp)
        else {
                tcp->u_rval = i386_regs.eax;
        }
-#elif defined(X86_64)
-       long rax;
-       if (x86_io.iov_len == sizeof(i386_regs)) {
-               /* Sign extend from 32 bits */
-               rax = (int32_t)i386_regs.eax;
-       } else {
-               rax = x86_64_regs.rax;
-       }
-       if (check_errno && is_negated_errno(rax)) {
-               tcp->u_rval = -1;
-               u_error = -rax;
-       }
-       else {
-               tcp->u_rval = rax;
-       }
-#elif defined(X32)
-       /* In X32, return value is 64-bit (llseek uses one).
+#elif defined(X86_64) || defined(X32)
+       /*
+        * In X32, return value is 64-bit (llseek uses one).
         * Using merely "long rax" would not work.
         */
-       long long rax;
+       kernel_long_t rax;
+
        if (x86_io.iov_len == sizeof(i386_regs)) {
                /* Sign extend from 32 bits */
-               rax = (int32_t)i386_regs.eax;
+               rax = (int32_t) i386_regs.eax;
        } else {
                rax = x86_64_regs.rax;
        }
-       /* Careful: is_negated_errno() works only on longs */
-       if (check_errno && is_negated_errno_x32(rax)) {
+       if (check_errno && is_negated_errno(rax)) {
                tcp->u_rval = -1;
                u_error = -rax;
        }
        else {
-               tcp->u_rval = rax; /* truncating */
+               tcp->u_rval = rax;
+# ifdef X32
+               /* tcp->u_rval contains a truncated value */
                tcp->u_lrval = rax;
+# endif
        }
 #elif defined(IA64)
-       if (ia32) {
+       if (ia64_ia32mode) {
                int err;
 
                err = (int)ia64_r8;
@@ -2224,22 +2065,22 @@ get_error(struct tcb *tcp)
                }
        }
 #elif defined(MIPS)
-       if (check_errno && mips_a3) {
+       if (check_errno && mips_REG_A3) {
                tcp->u_rval = -1;
-               u_error = mips_r2;
+               u_error = mips_REG_V0;
        } else {
-               tcp->u_rval = mips_r2;
-# if defined(LINUX_MIPSN32)
-               tcp->u_lrval = mips_r2;
+# if defined LINUX_MIPSN32
+               tcp->u_lrval = mips_REG_V0;
 # endif
+               tcp->u_rval = mips_REG_V0;
        }
 #elif defined(POWERPC)
-       if (check_errno && is_negated_errno(ppc_result)) {
+       if (check_errno && (ppc_regs.ccr & 0x10000000)) {
                tcp->u_rval = -1;
-               u_error = -ppc_result;
+               u_error = ppc_regs.gpr[3];
        }
        else {
-               tcp->u_rval = ppc_result;
+               tcp->u_rval = ppc_regs.gpr[3];
        }
 #elif defined(M68K)
        if (check_errno && is_negated_errno(m68k_d0)) {
@@ -2334,6 +2175,15 @@ get_error(struct tcb *tcp)
        else {
                tcp->u_rval = sh64_r9;
        }
+#elif defined(METAG)
+       /* result pointer in D0Re0 (D0.0) */
+       if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
+               tcp->u_rval = -1;
+               u_error = -metag_regs.dx[0][0];
+       }
+       else {
+               tcp->u_rval = metag_regs.dx[0][0];
+       }
 #elif defined(CRISV10) || defined(CRISV32)
        if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
                tcp->u_rval = -1;
@@ -2371,6 +2221,22 @@ get_error(struct tcb *tcp)
        else {
                tcp->u_rval = or1k_regs.gpr[11];
        }
+#elif defined(XTENSA)
+       if (check_errno && is_negated_errno(xtensa_a2)) {
+               tcp->u_rval = -1;
+               u_error = -xtensa_a2;
+       }
+       else {
+               tcp->u_rval = xtensa_a2;
+       }
+#elif defined(ARC)
+       if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
+               tcp->u_rval = -1;
+               u_error = -arc_regs.scratch.r0;
+       }
+       else {
+               tcp->u_rval = arc_regs.scratch.r0;
+       }
 #endif
        tcp->u_error = u_error;
 }
@@ -2391,11 +2257,21 @@ dumpio(struct tcb *tcp)
                if (func == sys_read ||
                    func == sys_pread ||
                    func == sys_recv ||
-                   func == sys_recvfrom)
+                   func == sys_recvfrom) {
                        dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
-               else if (func == sys_readv)
+                       return;
+               } else if (func == sys_readv) {
                        dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
-               return;
+                       return;
+#if HAVE_SENDMSG
+               } else if (func == sys_recvmsg) {
+                       dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
+                       return;
+               } else if (func == sys_recvmmsg) {
+                       dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
+                       return;
+#endif
+               }
        }
        if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
                if (func == sys_write ||
@@ -2405,7 +2281,12 @@ dumpio(struct tcb *tcp)
                        dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
                else if (func == sys_writev)
                        dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
-               return;
+#if HAVE_SENDMSG
+               else if (func == sys_sendmsg)
+                       dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
+               else if (func == sys_sendmmsg)
+                       dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
+#endif
        }
 }
 
@@ -2421,22 +2302,25 @@ trace_syscall_exiting(struct tcb *tcp)
        if (Tflag || cflag)
                gettimeofday(&tv, NULL);
 
+#ifdef USE_LIBUNWIND
+       if (stack_trace_enabled) {
+               if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
+                       unwind_cache_invalidate(tcp);
+       }
+#endif
+
 #if SUPPORTED_PERSONALITIES > 1
        update_personality(tcp, tcp->currpers);
 #endif
        res = (get_regs_error ? -1 : get_syscall_result(tcp));
        if (res == 1) {
-               syscall_fixup_on_sysexit(tcp); /* never fails */
                get_error(tcp); /* never fails */
-               if (need_fork_exec_workarounds)
-                       syscall_fixup_for_fork_exec(tcp);
-               if (filtered(tcp))
+               if (filtered(tcp) || hide_log_until_execve)
                        goto ret;
        }
 
        if (cflag) {
-               struct timeval t = tv;
-               count_syscall(tcp, &t);
+               count_syscall(tcp, &tv);
                if (cflag == CFLAG_ONLY_STATS) {
                        goto ret;
                }
@@ -2536,7 +2420,7 @@ trace_syscall_exiting(struct tcb *tcp)
                         * after SIG_IGN or SIG_DFL signal it will restart
                         * (thus the name "restart only if has no handler").
                         */
-                       tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
+                       tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
                        break;
                case ERESTART_RESTARTBLOCK:
                        /* Syscalls like nanosleep(), poll() which can't be
@@ -2553,13 +2437,12 @@ trace_syscall_exiting(struct tcb *tcp)
                        tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
                        break;
                default:
-                       if (u_error < 0)
-                               tprintf("= -1 E??? (errno %ld)", u_error);
-                       else if (u_error < nerrnos)
+                       if ((unsigned long) u_error < nerrnos
+                           && errnoent[u_error])
                                tprintf("= -1 %s (%s)", errnoent[u_error],
                                        strerror(u_error));
                        else
-                               tprintf("= -1 ERRNO_%ld (%s)", u_error,
+                               tprintf("= -1 ERRNO_%lu (%s)", u_error,
                                        strerror(u_error));
                        break;
                }
@@ -2572,7 +2455,13 @@ trace_syscall_exiting(struct tcb *tcp)
                else {
                        switch (sys_res & RVAL_MASK) {
                        case RVAL_HEX:
-                               tprintf("= %#lx", tcp->u_rval);
+#if SUPPORTED_PERSONALITIES > 1
+                               if (current_wordsize < sizeof(long))
+                                       tprintf("= %#x",
+                                               (unsigned int) tcp->u_rval);
+                               else
+#endif
+                                       tprintf("= %#lx", tcp->u_rval);
                                break;
                        case RVAL_OCTAL:
                                tprintf("= %#lo", tcp->u_rval);
@@ -2583,6 +2472,14 @@ trace_syscall_exiting(struct tcb *tcp)
                        case RVAL_DECIMAL:
                                tprintf("= %ld", tcp->u_rval);
                                break;
+                       case RVAL_FD:
+                               if (show_fd_path) {
+                                       tprints("= ");
+                                       printfd(tcp, tcp->u_rval);
+                               }
+                               else
+                                       tprintf("= %ld", tcp->u_rval);
+                               break;
 #if defined(LINUX_MIPSN32) || defined(X32)
                        /*
                        case RVAL_LHEX:
@@ -2619,6 +2516,11 @@ trace_syscall_exiting(struct tcb *tcp)
        dumpio(tcp);
        line_ended();
 
+#ifdef USE_LIBUNWIND
+       if (stack_trace_enabled)
+               unwind_print_stacktrace(tcp);
+#endif
+
  ret:
        tcp->flags &= ~TCB_INSYSCALL;
        return 0;