]> granicus.if.org Git - strace/blobdiff - syscall.c
2004-09-03 Roland McGrath <roland@redhat.com>
[strace] / syscall.c
index ff8603258ebd6cc83ee52f066c64b3eb20c88186..50f3abee82cd23255ece5199f5ba0ccc745f1169 100644 (file)
--- a/syscall.c
+++ b/syscall.c
 #include <sys/param.h>
 
 #if HAVE_ASM_REG_H
-#ifdef SPARC
+#if defined (SPARC) || defined (SPARC64)
 #  define fpq kernel_fpq
 #  define fq kernel_fq
 #  define fpu kernel_fpu
 #endif
 #include <asm/reg.h>
-#ifdef SPARC
+#if defined (SPARC) || defined (SPARC64)
 #  undef fpq
 #  undef fq
-#  undef fpu 
+#  undef fpu
 #endif
 #endif
 
 #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
 
+#if defined (LINUX) && defined (SPARC64)
+# define r_pc r_tpc
+# undef PTRACE_GETREGS
+# define PTRACE_GETREGS PTRACE_GETREGS64
+# undef PTRACE_SETREGS
+# define PTRACE_SETREGS PTRACE_SETREGS64
+#endif /* LINUX && SPARC64 */
+
 #if defined(LINUX) && defined(IA64)
 # include <asm/ptrace_offsets.h>
 # include <asm/rse.h>
 #endif
 
-#ifndef SYS_ERRLIST_DECLARED
-extern int sys_nerr;
-extern char *sys_errlist[];
-#endif /* SYS_ERRLIST_DECLARED */
-
 #define NR_SYSCALL_BASE 0
 #ifdef LINUX
 #ifndef ERESTARTSYS
@@ -90,6 +101,9 @@ extern char *sys_errlist[];
 #ifndef ENOIOCTLCMD
 #define ENOIOCTLCMD    515     /* No ioctl command */
 #endif
+#ifndef ERESTART_RESTARTBLOCK
+#define ERESTART_RESTARTBLOCK 516      /* restart by calling sys_restart_syscall */
+#endif
 #ifndef NSIG
 #define NSIG 32
 #endif
@@ -110,26 +124,26 @@ extern char *sys_errlist[];
 #define TP TRACE_PROCESS
 #define TS TRACE_SIGNAL
 
-struct sysent sysent0[] = {
+static const struct sysent sysent0[] = {
 #include "syscallent.h"
 };
-int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
+static const int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
 
 #if SUPPORTED_PERSONALITIES >= 2
-struct sysent sysent1[] = {
+static const struct sysent sysent1[] = {
 #include "syscallent1.h"
 };
-int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
+static const int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
 #endif /* SUPPORTED_PERSONALITIES >= 2 */
 
 #if SUPPORTED_PERSONALITIES >= 3
-struct sysent sysent2[] = {
+static const struct sysent sysent2[] = {
 #include "syscallent2.h"
 };
-int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
+static const int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
 #endif /* SUPPORTED_PERSONALITIES >= 3 */
 
-struct sysent *sysent;
+const struct sysent *sysent;
 int nsyscalls;
 
 /* Now undef them since short defines cause wicked namespace pollution. */
@@ -139,26 +153,26 @@ int nsyscalls;
 #undef TP
 #undef TS
 
-char *errnoent0[] = {
+static const char *const errnoent0[] = {
 #include "errnoent.h"
 };
-int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
+static const int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
 
 #if SUPPORTED_PERSONALITIES >= 2
-char *errnoent1[] = {
+static const char *const errnoent1[] = {
 #include "errnoent1.h"
 };
-int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
+static const int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
 #endif /* SUPPORTED_PERSONALITIES >= 2 */
 
 #if SUPPORTED_PERSONALITIES >= 3
-char *errnoent2[] = {
+static const char *const errnoent2[] = {
 #include "errnoent2.h"
 };
-int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
+static const int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
 #endif /* SUPPORTED_PERSONALITIES >= 3 */
 
-char **errnoent;
+const char *const *errnoent;
 int nerrnos;
 
 int current_personality;
@@ -222,91 +236,121 @@ static int sorted_count[MAX_QUALS];
 
 static struct timeval shortest = { 1000000, 0 };
 
-static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
+static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
 
 static struct qual_options {
        int bitflag;
        char *option_name;
-       int (*lookup)();
+       int (*qualify)();
        char *argument_name;
 } qual_options[] = {
-       { QUAL_TRACE,   "trace",        lookup_syscall, "system call"   },
-       { QUAL_TRACE,   "t",            lookup_syscall, "system call"   },
-       { QUAL_ABBREV,  "abbrev",       lookup_syscall, "system call"   },
-       { QUAL_ABBREV,  "a",            lookup_syscall, "system call"   },
-       { QUAL_VERBOSE, "verbose",      lookup_syscall, "system call"   },
-       { QUAL_VERBOSE, "v",            lookup_syscall, "system call"   },
-       { QUAL_RAW,     "raw",          lookup_syscall, "system call"   },
-       { QUAL_RAW,     "x",            lookup_syscall, "system call"   },
-       { QUAL_SIGNAL,  "signal",       lookup_signal,  "signal"        },
-       { QUAL_SIGNAL,  "signals",      lookup_signal,  "signal"        },
-       { QUAL_SIGNAL,  "s",            lookup_signal,  "signal"        },
-       { QUAL_FAULT,   "fault",        lookup_fault,   "fault"         },
-       { QUAL_FAULT,   "faults",       lookup_fault,   "fault"         },
-       { QUAL_FAULT,   "m",            lookup_fault,   "fault"         },
-       { QUAL_READ,    "read",         lookup_desc,    "descriptor"    },
-       { QUAL_READ,    "reads",        lookup_desc,    "descriptor"    },
-       { QUAL_READ,    "r",            lookup_desc,    "descriptor"    },
-       { QUAL_WRITE,   "write",        lookup_desc,    "descriptor"    },
-       { QUAL_WRITE,   "writes",       lookup_desc,    "descriptor"    },
-       { QUAL_WRITE,   "w",            lookup_desc,    "descriptor"    },
+       { QUAL_TRACE,   "trace",        qual_syscall,   "system call"   },
+       { QUAL_TRACE,   "t",            qual_syscall,   "system call"   },
+       { QUAL_ABBREV,  "abbrev",       qual_syscall,   "system call"   },
+       { QUAL_ABBREV,  "a",            qual_syscall,   "system call"   },
+       { QUAL_VERBOSE, "verbose",      qual_syscall,   "system call"   },
+       { QUAL_VERBOSE, "v",            qual_syscall,   "system call"   },
+       { QUAL_RAW,     "raw",          qual_syscall,   "system call"   },
+       { QUAL_RAW,     "x",            qual_syscall,   "system call"   },
+       { QUAL_SIGNAL,  "signal",       qual_signal,    "signal"        },
+       { QUAL_SIGNAL,  "signals",      qual_signal,    "signal"        },
+       { QUAL_SIGNAL,  "s",            qual_signal,    "signal"        },
+       { QUAL_FAULT,   "fault",        qual_fault,     "fault"         },
+       { QUAL_FAULT,   "faults",       qual_fault,     "fault"         },
+       { QUAL_FAULT,   "m",            qual_fault,     "fault"         },
+       { QUAL_READ,    "read",         qual_desc,      "descriptor"    },
+       { QUAL_READ,    "reads",        qual_desc,      "descriptor"    },
+       { QUAL_READ,    "r",            qual_desc,      "descriptor"    },
+       { QUAL_WRITE,   "write",        qual_desc,      "descriptor"    },
+       { QUAL_WRITE,   "writes",       qual_desc,      "descriptor"    },
+       { QUAL_WRITE,   "w",            qual_desc,      "descriptor"    },
        { 0,            NULL,           NULL,           NULL            },
 };
 
+static void
+qualify_one(n, opt, not)
+       int n;
+       struct qual_options *opt;
+       int not;
+{
+       if (not)
+               qual_flags[n] &= ~opt->bitflag;
+       else
+               qual_flags[n] |= opt->bitflag;
+}
+
 static int
-lookup_syscall(s)
-char *s;
+qual_syscall(s, opt, not)
+       char *s;
+       struct qual_options *opt;
+       int not;
 {
        int i;
+       int any = 0;
 
        for (i = 0; i < nsyscalls; i++) {
-               if (strcmp(s, sysent[i].sys_name) == 0)
-                       return i;
+               if (strcmp(s, sysent[i].sys_name) == 0) {
+                       qualify_one(i, opt, not);
+                       any = 1;
+               }
        }
-       return -1;
+       return !any;
 }
 
 static int
-lookup_signal(s)
-char *s;
+qual_signal(s, opt, not)
+       char *s;
+       struct qual_options *opt;
+       int not;
 {
        int i;
        char buf[32];
 
-       if (s && *s && isdigit((unsigned char)*s))
-               return atoi(s);
+       if (s && *s && isdigit((unsigned char)*s)) {
+               qualify_one(atoi(s), opt, not);
+               return 1;
+       }
+       if (strlen(s) >= sizeof buf)
+               return 0;
        strcpy(buf, s);
        s = buf;
        for (i = 0; s[i]; i++)
                s[i] = toupper((unsigned char)(s[i]));
        if (strncmp(s, "SIG", 3) == 0)
                s += 3;
-       for (i = 0; i <= NSIG; i++) {
-               if (strcmp(s, signame(i) + 3) == 0)
-                       return i;
-       }
-       return -1;
+       for (i = 0; i <= NSIG; i++)
+               if (strcmp(s, signame(i) + 3) == 0) {
+                       qualify_one(atoi(s), opt, not);
+                       return 1;
+               }
+       return 0;
 }
 
 static int
-lookup_fault(s)
-char *s;
+qual_fault(s, opt, not)
+       char *s;
+       struct qual_options *opt;
+       int not;
 {
        return -1;
 }
 
 static int
-lookup_desc(s)
-char *s;
+qual_desc(s, opt, not)
+       char *s;
+       struct qual_options *opt;
+       int not;
 {
-       if (s && *s && isdigit((unsigned char)*s))
-               return atoi(s);
+       if (s && *s && isdigit((unsigned char)*s)) {
+               qualify_one(atoi(s), opt, not);
+               return 0;
+       }
        return -1;
 }
 
 static int
 lookup_class(s)
-char *s;
+       char *s;
 {
        if (strcmp(s, "file") == 0)
                return TRACE_FILE;
@@ -375,15 +419,11 @@ char *s;
                        }
                        continue;
                }
-               if ((n = (*opt->lookup)(p)) < 0) {
+               if (opt->qualify(p, opt, not)) {
                        fprintf(stderr, "strace: invalid %s `%s'\n",
                                opt->argument_name, p);
                        exit(1);
                }
-               if (not)
-                       qual_flags[n] &= ~opt->bitflag;
-               else
-                       qual_flags[n] |= opt->bitflag;
        }
        return;
 }
@@ -398,6 +438,12 @@ struct tcb *tcp;
                return;
        switch (tcp->scno + NR_SYSCALL_BASE) {
        case SYS_read:
+#ifdef SYS_pread64
+       case SYS_pread64:
+#endif
+#if defined SYS_pread && SYS_pread64 != SYS_pread
+       case SYS_pread:
+#endif
 #ifdef SYS_recv
        case SYS_recv:
 #endif
@@ -408,6 +454,12 @@ struct tcb *tcp;
                        dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
                break;
        case SYS_write:
+#ifdef SYS_pwrite64
+       case SYS_pwrite64:
+#endif
+#if defined SYS_pwrite && SYS_pwrite64 != SYS_pwrite
+       case SYS_pwrite:
+#endif
 #ifdef SYS_send
        case SYS_send:
 #endif
@@ -506,7 +558,8 @@ int subcall;
 int nsubcalls;
 enum subcall_style style;
 {
-       int i, addr, mask, arg;
+       long addr, mask, arg;
+       int i;
 
        switch (style) {
        case shift_style:
@@ -602,6 +655,9 @@ struct tcb *tcp;
 #endif
 #ifdef SYS_rforkall
        case SYS_rforkall:
+#endif
+#ifdef SYS_rfork
+       case SYS_rfork:
 #endif
                internal_fork(tcp);
                break;
@@ -642,14 +698,25 @@ struct tcb *tcp;
 #ifdef SYS_waitsys
        case SYS_waitsys:
 #endif
-               internal_wait(tcp);
+               internal_wait(tcp, 2);
+               break;
+#ifdef SYS_waitid
+       case SYS_waitid:
+               internal_wait(tcp, 3);
                break;
+#endif
 
 #ifdef SYS_exit
        case SYS_exit:
 #endif
 #ifdef SYS32_exit
        case SYS32_exit:
+#endif
+#ifdef __NR_exit_group
+       case __NR_exit_group:
+#endif
+#ifdef IA64
+       case 252: /* IA-32 __NR_exit_group */
 #endif
                internal_exit(tcp);
                break;
@@ -669,51 +736,147 @@ struct tcb *tcp;
 #elif defined (M68K)
        static int d0;
 #elif defined (ARM)
-       static int r0;
+       static struct pt_regs regs;
 #elif defined (ALPHA)
        static long r0;
        static long a3;
-#elif defined (SPARC)
+#elif defined (SPARC) || defined (SPARC64)
        static struct regs regs;
        static unsigned long trap;
 #elif defined(MIPS)
        static long a3;
        static long r2;
-#elif defined(S390)
+#elif defined(S390) || defined(S390X)
        static long gpr2;
        static long pc;
+       static long syscall_mode;
 #elif defined(HPPA)
        static long r28;
 #elif defined(SH)
        static long r0;
+#elif defined(SH64)
+       static long r9;
 #elif defined(X86_64)
        static long rax;
-#endif 
+#endif
 #endif /* LINUX */
 #ifdef FREEBSD
        struct reg regs;
-#endif /* FREEBSD */   
+#endif /* FREEBSD */
 
 int
 get_scno(tcp)
 struct tcb *tcp;
 {
        long scno = 0;
-       static int currpers=-1;
 #ifndef USE_PROCFS
        int pid = tcp->pid;
-#endif /* !PROCFS */   
+#endif /* !PROCFS */
 
 #ifdef LINUX
-#if defined(S390)
-       if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
-               return -1;
-       scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
-       if (errno)
-               return -1;
-       scno&=0xFF;
+#if defined(S390) || defined(S390X)
+       if (tcp->flags & TCB_WAITEXECVE) {
+               /*
+                * When the execve system call completes successfully, the
+                * new process still has -ENOSYS (old style) or __NR_execve
+                * (new style) in gpr2.  We cannot recover the scno again
+                * by disassembly, because the image that executed the
+                * syscall is gone now.  Fortunately, we don't want it.  We
+                * leave the flag set so that syscall_fixup can fake the
+                * result.
+                */
+               if (tcp->flags & TCB_INSYSCALL)
+                       return 1;
+               /*
+                * This is the SIGTRAP after execve.  We cannot try to read
+                * the system call here either.
+                */
+               tcp->flags &= ~TCB_WAITEXECVE;
+               return 0;
+       }
+
+       if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
+                       return -1;
+
+       if (syscall_mode != -ENOSYS) {
+               /*
+                * Since kernel version 2.5.44 the scno gets passed in gpr2.
+                */
+               scno = syscall_mode;
+       } else {
+               /*
+                * Old style of "passing" the scno via the SVC instruction.
+                */
+
+               long opcode, offset_reg, tmp;
+               void * svc_addr;
+               int gpr_offset[16] = {PT_GPR0,  PT_GPR1,  PT_ORIGGPR2, PT_GPR3,
+                                     PT_GPR4,  PT_GPR5,  PT_GPR6,     PT_GPR7,
+                                     PT_GPR8,  PT_GPR9,  PT_GPR10,    PT_GPR11,
+                                     PT_GPR12, PT_GPR13, PT_GPR14,    PT_GPR15};
+
+               if (upeek(pid, PT_PSWADDR, &pc) < 0)
+                       return -1;
+               errno = 0;
+               opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
+               if (errno) {
+                       perror("peektext(pc-oneword)");
+                       return -1;
+               }
+
+               /*
+                *  We have to check if the SVC got executed directly or via an
+                *  EXECUTE instruction. In case of EXECUTE it is necessary to do
+                *  instruction decoding to derive the system call number.
+                *  Unfortunately the opcode sizes of EXECUTE and SVC are differently,
+                *  so that this doesn't work if a SVC opcode is part of an EXECUTE
+                *  opcode. Since there is no way to find out the opcode size this
+                *  is the best we can do...
+                */
+
+               if ((opcode & 0xff00) == 0x0a00) {
+                       /* SVC opcode */
+                       scno = opcode & 0xff;
+               }
+               else {
+                       /* SVC got executed by EXECUTE instruction */
+
+                       /*
+                        *  Do instruction decoding of EXECUTE. If you really want to
+                        *  understand this, read the Principles of Operations.
+                        */
+                       svc_addr = (void *) (opcode & 0xfff);
+
+                       tmp = 0;
+                       offset_reg = (opcode & 0x000f0000) >> 16;
+                       if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
+                               return -1;
+                       svc_addr += tmp;
+
+                       tmp = 0;
+                       offset_reg = (opcode & 0x0000f000) >> 12;
+                       if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
+                               return -1;
+                       svc_addr += tmp;
+
+                       scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
+                       if (errno)
+                               return -1;
+#if defined(S390X)
+                       scno >>= 48;
+#else
+                       scno >>= 16;
+#endif
+                       tmp = 0;
+                       offset_reg = (opcode & 0x00f00000) >> 20;
+                       if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
+                               return -1;
+
+                       scno = (scno | tmp) & 0xff;
+               }
+       }
 #elif defined (POWERPC)
-       if (upeek(pid, 4*PT_R0, &scno) < 0)
+       if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
                return -1;
        if (!(tcp->flags & TCB_INSYSCALL)) {
                /* Check if we return from execve. */
@@ -729,13 +892,14 @@ struct tcb *tcp;
        if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
                return -1;
 
-       if (!(tcp->flags & TCB_INSYSCALL)) { 
+       if (!(tcp->flags & TCB_INSYSCALL)) {
+               static int currpers=-1;
                long val;
 
                /* Check CS register value. On x86-64 linux it is:
                 *      0x33    for long mode (64 bit)
                 *      0x23    for compatibility mode (32 bit)
-                * It takes only one ptrace and thus doesn't need 
+                * It takes only one ptrace and thus doesn't need
                 * to be cached.
                 */
                if (upeek(pid, 8*CS, &val) < 0)
@@ -760,14 +924,14 @@ struct tcb *tcp;
 
                if(upeek(pid, 8*RIP, &rip)<0)
                        perror("upeek(RIP)");
-               
+
                /* sizeof(syscall) == sizeof(int 0x80) == 2 */
                rip-=2;
                errno = 0;
 
-               call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0); 
-               if (errno) 
-                       printf("ptrace_peektext failed: %s\n", 
+               call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
+               if (errno)
+                       printf("ptrace_peektext failed: %s\n",
                                        strerror(errno));
                switch (call & 0xffff)
                {
@@ -777,7 +941,7 @@ struct tcb *tcp;
                        case 0x80cd: currpers = 1; break;
                        default:
                                currpers = current_personality;
-                               fprintf(stderr, 
+                               fprintf(stderr,
                                        "Unknown syscall opcode (0x%04X) while "
                                        "detecting personality of process "
                                        "PID=%d\n", (int)call, pid);
@@ -788,10 +952,10 @@ struct tcb *tcp;
                {
                        char *names[]={"64 bit", "32 bit"};
                        set_personality(currpers);
-                       printf("[ Process PID=%d runs in %s mode. ]\n", 
+                       printf("[ Process PID=%d runs in %s mode. ]\n",
                                        pid, names[current_personality]);
                }
-       } 
+       }
 #elif defined(IA64)
 #      define IA64_PSR_IS      ((long)1 << 34)
        if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
@@ -800,11 +964,15 @@ struct tcb *tcp;
                if (ia32) {
                        if (upeek(pid, PT_R1, &scno) < 0)       /* orig eax */
                                return -1;
-                       /* Check if we return from execve. */
                } else {
                        if (upeek (pid, PT_R15, &scno) < 0)
                                return -1;
                }
+               /* Check if we return from execve. */
+               if (tcp->flags & TCB_WAITEXECVE) {
+                       tcp->flags &= ~TCB_WAITEXECVE;
+                       return 0;
+               }
        } else {
                /* syscall in progress */
                if (upeek (pid, PT_R8, &r8) < 0)
@@ -812,17 +980,60 @@ struct tcb *tcp;
                if (upeek (pid, PT_R10, &r10) < 0)
                        return -1;
        }
-       if (tcp->flags & TCB_WAITEXECVE) {
-               tcp->flags &= ~TCB_WAITEXECVE;
-               return 0;
-       }
-
 #elif defined (ARM)
-       { 
-           long pc;
-           upeek(pid, 4*15, &pc);
-           umoven(tcp, pc-4, 4, (char *)&scno);
-           scno &= 0x000fffff;
+       /*
+        * Read complete register set in one go.
+        */
+       if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)&regs) == -1)
+               return -1;
+
+       /*
+        * We only need to grab the syscall number on syscall entry.
+        */
+       if (regs.ARM_ip == 0) {
+               /*
+                * Note: we only deal with only 32-bit CPUs here.
+                */
+               if (regs.ARM_cpsr & 0x20) {
+                       /*
+                        * Get the Thumb-mode system call number
+                        */
+                       scno = regs.ARM_r7;
+               } else {
+                       /*
+                        * Get the ARM-mode system call number
+                        */
+                       errno = 0;
+                       scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
+                       if (errno)
+                               return -1;
+
+                       if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
+                               tcp->flags &= ~TCB_WAITEXECVE;
+                               return 0;
+                       }
+
+                       if ((scno & 0x0ff00000) != 0x0f900000) {
+                               fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
+                                       scno);
+                               return -1;
+                       }
+
+                       /*
+                        * Fixup the syscall number
+                        */
+                       scno &= 0x000fffff;
+               }
+
+               if (tcp->flags & TCB_INSYSCALL) {
+                       fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
+                       tcp->flags &= ~TCB_INSYSCALL;
+               }
+       } else {
+               if (!(tcp->flags & TCB_INSYSCALL)) {
+                       fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
+                       tcp->flags |= TCB_INSYSCALL;
+               }
        }
 #elif defined (M68K)
        if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
@@ -876,7 +1087,7 @@ struct tcb *tcp;
                if (upeek(pid, REG_R0, &r0) < 0)
                        return -1;
        }
-#elif defined (SPARC)
+#elif defined (SPARC) || defined (SPARC64)
        /* Everything we need is in the current register set. */
        if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
                return -1;
@@ -886,6 +1097,9 @@ struct tcb *tcp;
                /* Retrieve the syscall trap instruction. */
                errno = 0;
                trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
+#if defined(SPARC64)
+               trap >>= 32;
+#endif
                if (errno)
                        return -1;
 
@@ -897,8 +1111,8 @@ struct tcb *tcp;
                        break;
                case 0x91d0206d:
                        /* Linux/SPARC64 syscall trap. */
-                       fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
-                       return -1;
+                       set_personality(2);
+                       break;
                case 0x91d02000:
                        /* SunOS syscall trap. (pers 1) */
                        fprintf(stderr,"syscall: SunOS no support\n");
@@ -906,7 +1120,7 @@ struct tcb *tcp;
                case 0x91d02008:
                        /* Solaris 2.x syscall trap. (per 2) */
                        set_personality(1);
-                       break; 
+                       break;
                case 0x91d02009:
                        /* NetBSD/FreeBSD syscall trap. */
                        fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
@@ -921,7 +1135,11 @@ struct tcb *tcp;
                                tcp->flags &= ~TCB_WAITEXECVE;
                                return 0;
                        }
+#if defined (SPARC64)
+                       fprintf(stderr,"syscall: unknown syscall trap %08lx %016lx\n", trap, regs.r_tpc);
+#else
                        fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
+#endif
                        return -1;
                }
 
@@ -974,15 +1192,32 @@ struct tcb *tcp;
                        return 0;
                }
        }
-#endif /* SH */
+#elif defined(SH64)
+       if (upeek(pid, REG_SYSCALL, &scno) < 0)
+               return -1;
+        scno &= 0xFFFF;
+
+       if (!(tcp->flags & TCB_INSYSCALL)) {
+               /* Check if we return from execve. */
+               if (tcp->flags & TCB_WAITEXECVE) {
+                       tcp->flags &= ~TCB_WAITEXECVE;
+                       return 0;
+               }
+       }
+#endif /* SH64 */
 #endif /* LINUX */
 #ifdef SUNOS4
        if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
                return -1;
 #elif defined(SH)
-       /* new syscall ABI returns result in R0 */
-       if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
-               return -1;
+        /* new syscall ABI returns result in R0 */
+        if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
+                return -1;
+#elif defined(SH64)
+        /* ABI defines result returned in r9 */
+        if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
+                return -1;
+
 #endif
 #ifdef USE_PROCFS
 #ifdef HAVE_PR_SYSCALL
@@ -1019,7 +1254,7 @@ struct tcb *tcp;
 {
 #ifndef USE_PROCFS
        int pid = tcp->pid;
-#else /* USE_PROCFS */ 
+#else /* USE_PROCFS */
        int scno = tcp->scno;
 
        if (!(tcp->flags & TCB_INSYSCALL)) {
@@ -1093,24 +1328,37 @@ struct tcb *tcp;
 #elif defined (X86_64)
        if (upeek(pid, 8*RAX, &rax) < 0)
                return -1;
+       if (current_personality == 1)
+               rax = (long int)(int)rax; /* sign extend from 32 bits */
        if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
                if (debug)
                        fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
                return 0;
        }
-#elif defined (S390)
+#elif defined (S390) || defined (S390X)
        if (upeek(pid, PT_GPR2, &gpr2) < 0)
                return -1;
-       if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
+       if (syscall_mode != -ENOSYS)
+               syscall_mode = tcp->scno;
+       if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
                if (debug)
                        fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
                return 0;
        }
+       else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
+                 == (TCB_INSYSCALL|TCB_WAITEXECVE))
+                && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
+               /*
+                * Fake a return value of zero.  We leave the TCB_WAITEXECVE
+                * flag set for the post-execve SIGTRAP to see and reset.
+                */
+               gpr2 = 0;
+       }
 #elif defined (POWERPC)
 # define SO_MASK 0x10000000
-       if (upeek(pid, 4*PT_CCR, &flags) < 0)
+       if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
                return -1;
-       if (upeek(pid, 4*PT_R3, &result) < 0)
+       if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
                return -1;
        if (flags & SO_MASK)
                result = -result;
@@ -1123,13 +1371,9 @@ struct tcb *tcp;
                return 0;
        }
 #elif defined (ARM)
-       if (upeek(pid, 4*0, (long *)&r0) < 0)
-               return -1;
-       if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
-               if (debug)
-                       fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
-               return 0;
-       }
+       /*
+        * Nothing required
+        */
 #elif defined (HPPA)
        if (upeek(pid, PT_GR28, &r28) < 0)
                return -1;
@@ -1154,7 +1398,7 @@ struct tcb *tcp;
 {
        int u_error = 0;
 #ifdef LINUX
-#ifdef S390
+#if defined(S390) || defined(S390X)
                if (gpr2 && (unsigned) -gpr2 < nerrnos) {
                        tcp->u_rval = -1;
                        u_error = -gpr2;
@@ -1163,7 +1407,7 @@ struct tcb *tcp;
                        tcp->u_rval = gpr2;
                        u_error = 0;
                }
-#else /* !S390 */
+#else /* !S390 && !S390X */
 #ifdef I386
                if (eax < 0 && -eax < nerrnos) {
                        tcp->u_rval = -1;
@@ -1217,7 +1461,7 @@ struct tcb *tcp;
                }
 #else
 #ifdef POWERPC
-               if (result && (unsigned) -result < nerrnos) {
+               if (result && (unsigned long) -result < nerrnos) {
                        tcp->u_rval = -1;
                        u_error = -result;
                }
@@ -1237,12 +1481,12 @@ struct tcb *tcp;
                }
 #else /* !M68K */
 #ifdef ARM
-               if (r0 && (unsigned) -r0 < nerrnos) {
+               if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
                        tcp->u_rval = -1;
-                       u_error = -r0;
+                       u_error = -regs.ARM_r0;
                }
                else {
-                       tcp->u_rval = r0;
+                       tcp->u_rval = regs.ARM_r0;
                        u_error = 0;
                }
 #else /* !ARM */
@@ -1266,6 +1510,16 @@ struct tcb *tcp;
                        u_error = 0;
                }
 #else /* !SPARC */
+#ifdef SPARC64
+               if (regs.r_tstate & 0x1100000000UL) {
+                       tcp->u_rval = -1;
+                       u_error = regs.r_o0;
+               }
+               else {
+                       tcp->u_rval = regs.r_o0;
+                       u_error = 0;
+               }
+#else /* !SPARC64 */
 #ifdef HPPA
                if (r28 && (unsigned) -r28 < nerrnos) {
                        tcp->u_rval = -1;
@@ -1286,9 +1540,22 @@ struct tcb *tcp;
                        tcp->u_rval = r0;
                        u_error = 0;
                }
+#else
+#ifdef SH64
+                /* interpret result as return value or error number */
+                if (r9 && (unsigned) -r9 < nerrnos) {
+                       tcp->u_rval = -1;
+                       u_error = -r9;
+                }
+                else {
+                        tcp->u_rval = r9;
+                       u_error = 0;
+                }
+#endif /* SH64 */
 #endif /* SH */
 #endif /* HPPA */
 #endif /* SPARC */
+#endif /* SPARC64 */
 #endif /* ALPHA */
 #endif /* ARM */
 #endif /* M68K */
@@ -1297,7 +1564,7 @@ struct tcb *tcp;
 #endif /* IA64 */
 #endif /* X86_64 */
 #endif /* I386 */
-#endif /* S390 */
+#endif /* S390 || S390X */
 #endif /* LINUX */
 #ifdef SUNOS4
                /* get error code from user struct */
@@ -1369,27 +1636,212 @@ struct tcb *tcp;
                          ((unsigned long long) regs.r_edx << 32) +  regs.r_eax;
                        u_error = 0;
                }
-#endif /* FREEBSD */   
+#endif /* FREEBSD */
        tcp->u_error = u_error;
        return 1;
 }
 
+int
+force_result(tcp, error, rval)
+       struct tcb *tcp;
+       int error;
+       long rval;
+{
+#ifdef LINUX
+#if defined(S390) || defined(S390X)
+       gpr2 = error ? -error : rval;
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
+               return -1;
+#else /* !S390 && !S390X */
+#ifdef I386
+       eax = error ? -error : rval;
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
+               return -1;
+#else /* !I386 */
+#ifdef X86_64
+       rax = error ? -error : rval;
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
+               return -1;
+#else
+#ifdef IA64
+       if (ia32) {
+               r8 = error ? -error : rval;
+               if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
+                       return -1;
+       }
+       else {
+               if (error) {
+                       r8 = error;
+                       r10 = -1;
+               }
+               else {
+                       r8 = rval;
+                       r10 = 0;
+               }
+               if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
+                   ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
+                       return -1;
+       }
+#else /* !IA64 */
+#ifdef MIPS
+       if (error) {
+               r2 = error;
+               a3 = -1;
+       }
+       else {
+               r2 = rval;
+               a3 = 0;
+       }
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
+           ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
+               return -1;
+#else
+#ifdef POWERPC
+       if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
+               return -1;
+       if (error) {
+               flags |= SO_MASK;
+               result = error;
+       }
+       else {
+               flags &= ~SO_MASK;
+               result = rval;
+       }
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
+           ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
+               return -1;
+#else /* !POWERPC */
+#ifdef M68K
+       d0 = error ? -error : rval;
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
+               return -1;
+#else /* !M68K */
+#ifdef ARM
+       regs.ARM_r0 = error ? -error : rval;
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
+               return -1;
+#else /* !ARM */
+#ifdef ALPHA
+       if (error) {
+               a3 = -1;
+               r0 = error;
+       }
+       else {
+               a3 = 0;
+               r0 = rval;
+       }
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
+           ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
+               return -1;
+#else /* !ALPHA */
+#ifdef SPARC
+       if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
+               return -1;
+       if (error) {
+               regs.r_psr |= PSR_C;
+               regs.r_o0 = error;
+       }
+       else {
+               regs.r_psr &= ~PSR_C;
+               regs.r_o0 = rval;
+       }
+       if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
+               return -1;
+#else /* !SPARC */
+#ifdef SPARC64
+       if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
+               return -1;
+       if (error) {
+               regs.r_tstate |= 0x1100000000UL;
+               regs.r_o0 = error;
+       }
+       else {
+               regs.r_tstate &= ~0x1100000000UL;
+               regs.r_o0 = rval;
+       }
+       if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
+               return -1;
+#else /* !SPARC64 */
+#ifdef HPPA
+       r28 = error ? -error : rval;
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
+               return -1;
+#else
+#ifdef SH
+       r0 = error ? -error : rval;
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
+               return -1;
+#else
+#ifdef SH64
+        r9 = error ? -error : rval;
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
+               return -1;
+#endif /* SH64 */
+#endif /* SH */
+#endif /* HPPA */
+#endif /* SPARC */
+#endif /* SPARC64 */
+#endif /* ALPHA */
+#endif /* ARM */
+#endif /* M68K */
+#endif /* POWERPC */
+#endif /* MIPS */
+#endif /* IA64 */
+#endif /* X86_64 */
+#endif /* I386 */
+#endif /* S390 || S390X */
+#endif /* LINUX */
+#ifdef SUNOS4
+       if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
+                  error << 24) < 0 ||
+           ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
+               return -1;
+#endif /* SUNOS4 */
+#ifdef SVR4
+       /* XXX no clue */
+       return -1;
+#endif /* SVR4 */
+#ifdef FREEBSD
+       if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
+               perror("pread");
+                return -1;
+        }
+       if (error) {
+               regs.r_eflags |= PSL_C;
+               regs.r_eax = error;
+       }
+       else {
+               regs.r_eflags &= ~PSL_C;
+               regs.r_eax = rval;
+       }
+       if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
+               perror("pwrite");
+                return -1;
+        }
+#endif /* FREEBSD */
+
+       /* All branches reach here on success (only).  */
+       tcp->u_error = error;
+       tcp->u_rval = rval;
+       return 0;
+}
+
 int syscall_enter(tcp)
 struct tcb *tcp;
 {
 #ifndef USE_PROCFS
        int pid = tcp->pid;
-#endif /* !USE_PROCFS */       
+#endif /* !USE_PROCFS */
 #ifdef LINUX
-#if defined(S390)
+#if defined(S390) || defined(S390X)
        {
                int i;
                if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
                        tcp->u_nargs = sysent[tcp->scno].nargs;
-               else 
+               else
                        tcp->u_nargs = MAX_ARGS;
                for (i = 0; i < tcp->u_nargs; i++) {
-                       if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
+                       if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
                                return -1;
                }
        }
@@ -1398,7 +1850,7 @@ struct tcb *tcp;
                int i;
                if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
                        tcp->u_nargs = sysent[tcp->scno].nargs;
-               else 
+               else
                        tcp->u_nargs = MAX_ARGS;
                for (i = 0; i < tcp->u_nargs; i++) {
                        /* WTA: if scno is out-of-bounds this will bomb. Add range-check
@@ -1471,7 +1923,7 @@ struct tcb *tcp;
 
                if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
                        nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
-               else 
+               else
                        nargs = tcp->u_nargs = MAX_ARGS;
                if(nargs > 4) {
                        if(upeek(pid, REG_SP, &sp) < 0)
@@ -1490,24 +1942,30 @@ struct tcb *tcp;
                }
        }
 #elif defined (POWERPC)
+#ifndef PT_ORIG_R3
+#define PT_ORIG_R3 34
+#endif
        {
                int i;
                if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
                        tcp->u_nargs = sysent[tcp->scno].nargs;
-               else 
+               else
                        tcp->u_nargs = MAX_ARGS;
                for (i = 0; i < tcp->u_nargs; i++) {
-                       if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
+                       if (upeek(pid, (i==0) ?
+                               (sizeof(unsigned long)*PT_ORIG_R3) :
+                               ((i+PT_R3)*sizeof(unsigned long)),
+                                       &tcp->u_arg[i]) < 0)
                                return -1;
                }
        }
-#elif defined (SPARC)
+#elif defined (SPARC) || defined (SPARC64)
        {
                int i;
 
                if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
                        tcp->u_nargs = sysent[tcp->scno].nargs;
-               else 
+               else
                        tcp->u_nargs = MAX_ARGS;
                for (i = 0; i < tcp->u_nargs; i++)
                        tcp->u_arg[i] = *((&regs.r_o0) + i);
@@ -1518,16 +1976,27 @@ struct tcb *tcp;
 
                if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
                        tcp->u_nargs = sysent[tcp->scno].nargs;
-               else 
+               else
                        tcp->u_nargs = MAX_ARGS;
                for (i = 0; i < tcp->u_nargs; i++) {
                        if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
                                return -1;
                }
        }
+#elif defined(ARM)
+       {
+               int i;
+
+               if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
+                       tcp->u_nargs = sysent[tcp->scno].nargs;
+               else
+                       tcp->u_nargs = MAX_ARGS;
+               for (i = 0; i < tcp->u_nargs; i++)
+                       tcp->u_arg[i] = regs.uregs[i];
+       }
 #elif defined(SH)
        {
-               int i; 
+               int i;
                static int syscall_regs[] = {
                    REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
                    REG_REG0, REG_REG0+1, REG_REG0+2
@@ -1539,6 +2008,28 @@ struct tcb *tcp;
                                return -1;
                }
         }
+#elif defined(SH64)
+       {
+               int i;
+                /* Registers used by SH5 Linux system calls for parameters */
+               static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
+
+               /*
+                * TODO: should also check that the number of arguments encoded
+                *       in the trap number matches the number strace expects.
+                */
+               /*
+                   assert(sysent[tcp->scno].nargs <
+                       sizeof(syscall_regs)/sizeof(syscall_regs[0]));
+                */
+
+               tcp->u_nargs = sysent[tcp->scno].nargs;
+               for (i = 0; i < tcp->u_nargs; i++) {
+                       if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
+                               return -1;
+               }
+       }
+
 #elif defined(X86_64)
        {
                int i;
@@ -1546,10 +2037,10 @@ struct tcb *tcp;
                        {RDI,RSI,RDX,R10,R8,R9},        /* x86-64 ABI */
                        {RBX,RCX,RDX,RDX,RSI,RDI,RBP}   /* i386 ABI */
                };
-               
+
                if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
                        tcp->u_nargs = sysent[tcp->scno].nargs;
-               else 
+               else
                        tcp->u_nargs = MAX_ARGS;
                for (i = 0; i < tcp->u_nargs; i++) {
                        if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
@@ -1561,21 +2052,21 @@ struct tcb *tcp;
                int i;
                if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
                        tcp->u_nargs = sysent[tcp->scno].nargs;
-               else 
+               else
                        tcp->u_nargs = MAX_ARGS;
                for (i = 0; i < tcp->u_nargs; i++) {
                        if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
                                return -1;
                }
        }
-#endif 
+#endif
 #endif /* LINUX */
 #ifdef SUNOS4
        {
                int i;
                if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
                        tcp->u_nargs = sysent[tcp->scno].nargs;
-               else 
+               else
                        tcp->u_nargs = MAX_ARGS;
                for (i = 0; i < tcp->u_nargs; i++) {
                        struct user *u;
@@ -1641,7 +2132,7 @@ struct tcb *tcp;
        if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
            sysent[tcp->scno].nargs > tcp->status.val)
                tcp->u_nargs = sysent[tcp->scno].nargs;
-       else 
+       else
                tcp->u_nargs = tcp->status.val;
        if (tcp->u_nargs < 0)
                tcp->u_nargs = 0;
@@ -1690,8 +2181,6 @@ struct tcb *tcp;
                res = get_error(tcp);
                if (res != 1)
                        return res;
-               u_error = tcp->u_error;
-
 
                internal_syscall(tcp);
                if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
@@ -1717,8 +2206,16 @@ struct tcb *tcp;
                        tv_sub(&tv, &tv, &tcp->etime);
 #ifdef LINUX
                        if (tv_cmp(&tv, &tcp->dtime) > 0) {
-                               static struct timeval one_tick =
-                                       { 0, 1000000 / HZ };
+                               static struct timeval one_tick;
+                               if (one_tick.tv_usec == 0) {
+                                       /* Initialize it.  */
+                                       struct itimerval it;
+                                       memset(&it, 0, sizeof it);
+                                       it.it_interval.tv_usec = 1;
+                                       setitimer(ITIMER_REAL, &it, NULL);
+                                       getitimer(ITIMER_REAL, &it);
+                                       one_tick = it.it_interval;
+                               }
 
                                if (tv_nz(&tcp->dtime))
                                        tv = tcp->dtime;
@@ -1741,8 +2238,11 @@ struct tcb *tcp;
                if (tcp->scno >= nsyscalls || tcp->scno < 0
                    || (qual_flags[tcp->scno] & QUAL_RAW))
                        sys_res = printargs(tcp);
-               else
+               else {
+                       if (not_failing_only && tcp->u_error)
+                               return 0;       /* ignore failed syscalls */
                        sys_res = (*sysent[tcp->scno].sys_func)(tcp);
+               }
                u_error = tcp->u_error;
                tprintf(") ");
                tabto(acolumn);
@@ -1765,22 +2265,20 @@ struct tcb *tcp;
                        case ERESTARTNOHAND:
                                tprintf("= ? ERESTARTNOHAND (To be restarted)");
                                break;
+                       case ERESTART_RESTARTBLOCK:
+                               tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
+                               break;
 #endif /* LINUX */
                        default:
                                tprintf("= -1 ");
                                if (u_error < 0)
                                        tprintf("E??? (errno %ld)", u_error);
-                               else if (u_error < nerrnos && u_error < sys_nerr)
-                                       tprintf("%s (%s)", errnoent[u_error],
-                                               sys_errlist[u_error]);
                                else if (u_error < nerrnos)
-                                       tprintf("%s (errno %ld)",
-                                               errnoent[u_error], u_error);
-                               else if (u_error < sys_nerr)
-                                       tprintf("ERRNO_%ld (%s)", u_error,
-                                               sys_errlist[u_error]);
+                                       tprintf("%s (%s)", errnoent[u_error],
+                                               strerror(u_error));
                                else
-                                       tprintf("E??? (errno %ld)", u_error);
+                                       tprintf("ERRNO_%ld (%s)", u_error,
+                                               strerror(u_error));
                                break;
                        }
                }
@@ -1845,7 +2343,7 @@ struct tcb *tcp;
 
        switch (tcp->scno + NR_SYSCALL_BASE) {
 #ifdef LINUX
-#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
+#if !defined (ALPHA) && !defined(SPARC) && !defined(SPARC64) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
        case SYS_socketcall:
                decode_subcall(tcp, SYS_socket_subcall,
                        SYS_socket_nsubcalls, deref_style);
@@ -1854,8 +2352,8 @@ struct tcb *tcp;
                decode_subcall(tcp, SYS_ipc_subcall,
                        SYS_ipc_nsubcalls, shift_style);
                break;
-#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
-#ifdef SPARC
+#endif /* !ALPHA && !MIPS && !SPARC && !SPARC64 && !HPPA && !X86_64 */
+#if defined (SPARC) || defined (SPARC64)
        case SYS_socketcall:
                sparc_socket_decode (tcp);
                break;
@@ -1961,7 +2459,7 @@ struct tcb *tcp;
                tprintf("syscall_%lu(", tcp->scno);
        else
                tprintf("%s(", sysent[tcp->scno].sys_name);
-       if (tcp->scno >= nsyscalls || tcp->scno < 0 || 
+       if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
            ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
                sys_res = printargs(tcp);
        else
@@ -1995,12 +2493,18 @@ struct tcb *tcp;
        long val = -1;
 
 #ifdef LINUX
-#ifdef SPARC
+#if defined (SPARC) || defined (SPARC64)
        struct regs regs;
        if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
                return -1;
        val = regs.r_o1;
-#endif /* SPARC */
+#elif defined(SH)
+       if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
+               return -1;
+#endif /* SPARC || SPARC64 */
+#elif defined(IA64)
+       if (upeek(tcp->pid, PT_R9, &val) < 0)
+               return -1;
 #endif /* LINUX */
 
 #ifdef SUNOS4
@@ -2026,7 +2530,7 @@ struct tcb *tcp;
        struct reg regs;
        pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
        val = regs.r_edx;
-#endif 
+#endif
        return val;
 }