Author: jhb
Date: Wed Apr 29 16:04:59 2020
New Revision: 360468
URL: https://svnweb.freebsd.org/changeset/base/360468

Log:
  MFC 350069: Use PT_GET_SC_ARGS and PT_GET_SC_RET in truss.
  
  This removes all of the architecture-specific functions from truss.
  
  A per-ABI structure is still needed to map syscall numbers to names
  and FreeBSD errno values to ABI error values as well as hold syscall
  counters.  However, the linker set of ABI structures is now replaced
  with a simple table mapping ABI names to structures.  This approach
  permits sharing the same ABI structure among separate names such as
  i386 a.out and ELF binaries as well as ELF v1 vs ELF v2 for powerpc64.
  
  A few differences are visible due to using PT_GET_SC_RET to fetch the
  error value of a system call.  Note that ktrace/kdump have had the
  "new" behaviors for a long time already:
  - System calls that return with EJUSTRETURN or ERESTART will now be
    noticed and logged as such.  Previously sigreturn (which uses
    EJUSTRETURN) would report whatever random value was in the register
    holding errno from the previous system call for example.  Now it
    reports EJUSTRETURN.
  - System calls that return errno as their error value such as
    posix_fallocate() and posix_fadvise() now report non-zero return
    values as errors instead of success with a non-zero return value.

Deleted:
  stable/12/usr.bin/truss/aarch64-cloudabi32.c
  stable/12/usr.bin/truss/aarch64-cloudabi64.c
  stable/12/usr.bin/truss/aarch64-freebsd.c
  stable/12/usr.bin/truss/amd64-cloudabi32.c
  stable/12/usr.bin/truss/amd64-cloudabi64.c
  stable/12/usr.bin/truss/amd64-freebsd.c
  stable/12/usr.bin/truss/amd64-freebsd32.c
  stable/12/usr.bin/truss/amd64-linux.c
  stable/12/usr.bin/truss/amd64-linux32.c
  stable/12/usr.bin/truss/arm-freebsd.c
  stable/12/usr.bin/truss/i386-cloudabi32.c
  stable/12/usr.bin/truss/i386-freebsd.c
  stable/12/usr.bin/truss/i386-linux.c
  stable/12/usr.bin/truss/mips-freebsd.c
  stable/12/usr.bin/truss/powerpc-freebsd.c
  stable/12/usr.bin/truss/powerpc64-freebsd.c
  stable/12/usr.bin/truss/powerpc64-freebsd32.c
  stable/12/usr.bin/truss/riscv64-freebsd.c
  stable/12/usr.bin/truss/sparc64-freebsd.c
Modified:
  stable/12/usr.bin/truss/Makefile
  stable/12/usr.bin/truss/setup.c
  stable/12/usr.bin/truss/syscall.h
  stable/12/usr.bin/truss/syscalls.c
  stable/12/usr.bin/truss/truss.h
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/usr.bin/truss/Makefile
==============================================================================
--- stable/12/usr.bin/truss/Makefile    Wed Apr 29 14:36:50 2020        
(r360467)
+++ stable/12/usr.bin/truss/Makefile    Wed Apr 29 16:04:59 2020        
(r360468)
@@ -9,38 +9,4 @@ LIBADD=        sysdecode
 #CFLAGS+= -I${.CURDIR} -I. -I${SRCTOP}/sys
 CFLAGS+= -I${SRCTOP}/sys
 
-ABIS+=         freebsd
-# Each ABI is expected to have an ABI.c, MACHINE_ARCH-ABI.c or
-# MACHINE_CPUARCH-ABI.c file that will be used to map the syscall arguments.
-.if ${MACHINE_ARCH} == "aarch64"
-ABIS+=         cloudabi32
-ABIS+=         cloudabi64
-.endif
-.if ${MACHINE_CPUARCH} == "i386"
-ABIS+=         i386-linux
-ABIS+=         cloudabi32
-.endif
-.if ${MACHINE_CPUARCH} == "amd64"
-ABIS+=         amd64-linux
-ABIS+=         amd64-linux32
-ABIS+=         freebsd32
-ABIS+=         cloudabi32
-ABIS+=         cloudabi64
-.endif
-.if ${MACHINE_ARCH} == "powerpc64"
-ABIS+=         freebsd32
-.endif
-
-.for abi in ${ABIS}
-# Find the right file to handle this ABI.
-abi_src=
-ABI_SRCS=      ${abi}.c ${MACHINE_ARCH}-${abi}.c ${MACHINE_CPUARCH}-${abi}.c
-.for f in ${ABI_SRCS}
-.if exists(${.CURDIR}/${f}) && empty(abi_src)
-abi_src=       ${f}
-.endif
-.endfor
-SRCS:=         ${SRCS} ${abi_src}
-.endfor
-
 .include <bsd.prog.mk>

Modified: stable/12/usr.bin/truss/setup.c
==============================================================================
--- stable/12/usr.bin/truss/setup.c     Wed Apr 29 14:36:50 2020        
(r360467)
+++ stable/12/usr.bin/truss/setup.c     Wed Apr 29 16:04:59 2020        
(r360468)
@@ -61,7 +61,10 @@ __FBSDID("$FreeBSD$");
 #include "syscall.h"
 #include "extern.h"
 
-SET_DECLARE(procabi, struct procabi);
+struct procabi_table {
+       const char *name;
+       struct procabi *abi;
+};
 
 static sig_atomic_t detaching;
 
@@ -69,6 +72,79 @@ static void  enter_syscall(struct trussinfo *, struct t
                    struct ptrace_lwpinfo *);
 static void    new_proc(struct trussinfo *, pid_t, lwpid_t);
 
+
+static struct procabi cloudabi32 = {
+       "CloudABI32",
+       SYSDECODE_ABI_CLOUDABI32,
+       STAILQ_HEAD_INITIALIZER(cloudabi32.extra_syscalls),
+       { NULL }
+};
+
+static struct procabi cloudabi64 = {
+       "CloudABI64",
+       SYSDECODE_ABI_CLOUDABI64,
+       STAILQ_HEAD_INITIALIZER(cloudabi64.extra_syscalls),
+       { NULL }
+};
+
+static struct procabi freebsd = {
+       "FreeBSD",
+       SYSDECODE_ABI_FREEBSD,
+       STAILQ_HEAD_INITIALIZER(freebsd.extra_syscalls),
+       { NULL }
+};
+
+#ifdef __LP64__
+static struct procabi freebsd32 = {
+       "FreeBSD32",
+       SYSDECODE_ABI_FREEBSD32,
+       STAILQ_HEAD_INITIALIZER(freebsd32.extra_syscalls),
+       { NULL }
+};
+#endif
+
+static struct procabi linux = {
+       "Linux",
+       SYSDECODE_ABI_LINUX,
+       STAILQ_HEAD_INITIALIZER(linux.extra_syscalls),
+       { NULL }
+};
+
+#ifdef __LP64__
+static struct procabi linux32 = {
+       "Linux32",
+       SYSDECODE_ABI_LINUX32,
+       STAILQ_HEAD_INITIALIZER(linux32.extra_syscalls),
+       { NULL }
+};
+#endif
+
+static struct procabi_table abis[] = {
+       { "CloudABI ELF32", &cloudabi32 },
+       { "CloudABI ELF64", &cloudabi64 },
+#ifdef __LP64__
+       { "FreeBSD ELF64", &freebsd },
+       { "FreeBSD ELF32", &freebsd32 },
+#else
+       { "FreeBSD ELF32", &freebsd },
+#endif
+#if defined(__powerpc64__)
+       { "FreeBSD ELF64 V2", &freebsd },
+#endif
+#if defined(__amd64__)
+       { "FreeBSD a.out", &freebsd32 },
+#endif
+#if defined(__i386__)
+       { "FreeBSD a.out", &freebsd },
+#endif
+#ifdef __LP64__
+       { "Linux ELF64", &linux },
+       { "Linux ELF32", &linux32 },
+#else
+       { "Linux ELF", &linux },
+#endif
+};
+
 /*
  * setup_and_wait() is called to start a process.  All it really does
  * is fork(), enable tracing in the child, and then exec the given
@@ -153,8 +229,8 @@ detach_proc(pid_t pid)
 static struct procabi *
 find_abi(pid_t pid)
 {
-       struct procabi **pabi;
        size_t len;
+       unsigned int i;
        int error;
        int mib[4];
        char progt[32];
@@ -168,9 +244,9 @@ find_abi(pid_t pid)
        if (error != 0)
                err(2, "can not get sysvec name");
 
-       SET_FOREACH(pabi, procabi) {
-               if (strcmp((*pabi)->type, progt) == 0)
-                       return (*pabi);
+       for (i = 0; i < nitems(abis); i++) {
+               if (strcmp(abis[i].name, progt) == 0)
+                       return (abis[i].abi);
        }
        warnx("ABI %s for pid %ld is not supported", progt, (long)pid);
        return (NULL);
@@ -376,7 +452,8 @@ enter_syscall(struct trussinfo *info, struct threadinf
 
        alloc_syscall(t, pl);
        narg = MIN(pl->pl_syscall_narg, nitems(t->cs.args));
-       if (narg != 0 && t->proc->abi->fetch_args(info, narg) != 0) {
+       if (narg != 0 && ptrace(PT_GET_SC_ARGS, t->tid, (caddr_t)t->cs.args,
+           sizeof(t->cs.args)) != 0) {
                free_syscall(t);
                return;
        }
@@ -408,7 +485,7 @@ enter_syscall(struct trussinfo *info, struct threadinf
 #endif
                if (!(sc->args[i].type & OUT)) {
                        t->cs.s_args[i] = print_arg(&sc->args[i],
-                           t->cs.args, 0, info);
+                           t->cs.args, NULL, info);
                }
        }
 #if DEBUG
@@ -446,9 +523,8 @@ exit_syscall(struct trussinfo *info, struct ptrace_lwp
        struct threadinfo *t;
        struct procinfo *p;
        struct syscall *sc;
-       long retval[2];
+       struct ptrace_sc_ret psr;
        u_int i;
-       int errorp;
 
        t = info->curthread;
        if (!t->in_syscall)
@@ -456,7 +532,7 @@ exit_syscall(struct trussinfo *info, struct ptrace_lwp
 
        clock_gettime(CLOCK_REALTIME, &t->after);
        p = t->proc;
-       if (p->abi->fetch_retval(info, retval, &errorp) < 0) {
+       if (ptrace(PT_GET_SC_RET, t->tid, (caddr_t)&psr, sizeof(psr)) != 0) {
                free_syscall(t);
                return;
        }
@@ -474,18 +550,18 @@ exit_syscall(struct trussinfo *info, struct ptrace_lwp
                         * If an error occurred, then don't bother
                         * getting the data; it may not be valid.
                         */
-                       if (errorp) {
+                       if (psr.sr_error != 0) {
                                asprintf(&temp, "0x%lx",
                                    t->cs.args[sc->args[i].offset]);
                        } else {
                                temp = print_arg(&sc->args[i],
-                                   t->cs.args, retval, info);
+                                   t->cs.args, psr.sr_retval, info);
                        }
                        t->cs.s_args[i] = temp;
                }
        }
 
-       print_syscall_ret(info, errorp, retval);
+       print_syscall_ret(info, psr.sr_error, psr.sr_retval);
        free_syscall(t);
 
        /*

Modified: stable/12/usr.bin/truss/syscall.h
==============================================================================
--- stable/12/usr.bin/truss/syscall.h   Wed Apr 29 14:36:50 2020        
(r360467)
+++ stable/12/usr.bin/truss/syscall.h   Wed Apr 29 16:04:59 2020        
(r360468)
@@ -227,7 +227,8 @@ struct syscall {
 };
 
 struct syscall *get_syscall(struct threadinfo *, u_int, u_int);
-char *print_arg(struct syscall_args *, unsigned long*, long *, struct 
trussinfo *);
+char *print_arg(struct syscall_args *, unsigned long*, register_t *,
+    struct trussinfo *);
 
 /*
  * Linux Socket defines
@@ -271,5 +272,5 @@ struct linux_socketcall_args {
 
 void init_syscalls(void);
 void print_syscall(struct trussinfo *);
-void print_syscall_ret(struct trussinfo *, int, long *);
+void print_syscall_ret(struct trussinfo *, int, register_t *);
 void print_summary(struct trussinfo *trussinfo);

Modified: stable/12/usr.bin/truss/syscalls.c
==============================================================================
--- stable/12/usr.bin/truss/syscalls.c  Wed Apr 29 14:36:50 2020        
(r360467)
+++ stable/12/usr.bin/truss/syscalls.c  Wed Apr 29 16:04:59 2020        
(r360468)
@@ -61,6 +61,8 @@ __FBSDID("$FreeBSD$");
 #include <assert.h>
 #include <ctype.h>
 #include <err.h>
+#define _WANT_KERNEL_ERRNO
+#include <errno.h>
 #include <fcntl.h>
 #include <poll.h>
 #include <sched.h>
@@ -1567,7 +1569,7 @@ print_sysctl_oid(FILE *fp, int *oid, int len)
  * an array of all of the system call arguments.
  */
 char *
-print_arg(struct syscall_args *sc, unsigned long *args, long *retval,
+print_arg(struct syscall_args *sc, unsigned long *args, register_t *retval,
     struct trussinfo *trussinfo)
 {
        FILE *fp;
@@ -2337,7 +2339,7 @@ print_arg(struct syscall_args *sc, unsigned long *args
                 * Overwrite the first retval to signal a successful
                 * return as well.
                 */
-               fprintf(fp, "{ %ld, %ld }", retval[0], retval[1]);
+               fprintf(fp, "{ %d, %d }", (int)retval[0], (int)retval[1]);
                retval[0] = 0;
                break;
        case Utrace: {
@@ -2706,12 +2708,11 @@ print_syscall(struct trussinfo *trussinfo)
 }
 
 void
-print_syscall_ret(struct trussinfo *trussinfo, int errorp, long *retval)
+print_syscall_ret(struct trussinfo *trussinfo, int error, register_t *retval)
 {
        struct timespec timediff;
        struct threadinfo *t;
        struct syscall *sc;
-       int error;
 
        t = trussinfo->curthread;
        sc = t->cs.sc;
@@ -2719,7 +2720,7 @@ print_syscall_ret(struct trussinfo *trussinfo, int err
                timespecsub(&t->after, &t->before, &timediff);
                timespecadd(&sc->time, &timediff, &sc->time);
                sc->ncalls++;
-               if (errorp)
+               if (error != 0)
                        sc->nerror++;
                return;
        }
@@ -2736,11 +2737,14 @@ print_syscall_ret(struct trussinfo *trussinfo, int err
                return;
        }
 
-       if (errorp) {
-               error = sysdecode_abi_to_freebsd_errno(t->proc->abi->abi,
-                   retval[0]);
-               fprintf(trussinfo->outfile, " ERR#%ld '%s'\n", retval[0],
-                   error == INT_MAX ? "Unknown error" : strerror(error));
+       if (error == ERESTART)
+               fprintf(trussinfo->outfile, " ERESTART\n");
+       else if (error == EJUSTRETURN)
+               fprintf(trussinfo->outfile, " EJUSTRETURN\n");
+       else if (error != 0) {
+               fprintf(trussinfo->outfile, " ERR#%d '%s'\n",
+                   sysdecode_freebsd_to_abi_errno(t->proc->abi->abi, error),
+                   strerror(error));
        }
 #ifndef __LP64__
        else if (sc->ret_type == 2) {
@@ -2756,8 +2760,8 @@ print_syscall_ret(struct trussinfo *trussinfo, int err
        }
 #endif
        else
-               fprintf(trussinfo->outfile, " = %ld (0x%lx)\n", retval[0],
-                   retval[0]);
+               fprintf(trussinfo->outfile, " = %jd (0x%jx)\n",
+                   (intmax_t)retval[0], (intmax_t)retval[0]);
 }
 
 void

Modified: stable/12/usr.bin/truss/truss.h
==============================================================================
--- stable/12/usr.bin/truss/truss.h     Wed Apr 29 14:36:50 2020        
(r360467)
+++ stable/12/usr.bin/truss/truss.h     Wed Apr 29 16:04:59 2020        
(r360468)
@@ -27,7 +27,6 @@
  * $FreeBSD$
  */
 
-#include <sys/linker_set.h>
 #include <sys/queue.h>
 
 #define        FOLLOWFORKS             0x00000001
@@ -59,13 +58,9 @@ struct extra_syscall {
 struct procabi {
        const char *type;
        enum sysdecode_abi abi;
-       int (*fetch_args)(struct trussinfo *, u_int);
-       int (*fetch_retval)(struct trussinfo *, long *, int *);
        STAILQ_HEAD(, extra_syscall) extra_syscalls;
        struct syscall *syscalls[SYSCALL_NORMAL_COUNT];
 };
-
-#define        PROCABI(abi)    DATA_SET(procabi, abi)
 
 /*
  * This is confusingly named.  It holds per-thread state about the
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to