From owner-dev-commits-src-branches@freebsd.org Mon Jul 5 09:47:10 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 888FD658992; Mon, 5 Jul 2021 09:47:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4GJLSZ38djz4kRt; Mon, 5 Jul 2021 09:47:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4D8CA23A15; Mon, 5 Jul 2021 09:47:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1659lAhb071611; Mon, 5 Jul 2021 09:47:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1659lAvS071610; Mon, 5 Jul 2021 09:47:10 GMT (envelope-from git) Date: Mon, 5 Jul 2021 09:47:10 GMT Message-Id: <202107050947.1659lAvS071610@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Alex Richardson Subject: git: 6e2b0498aa70 - stable/13 - truss: minor cleanup and pedantic warning fixes MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: arichardson X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 6e2b0498aa702bace3859273c36bd27d7dff85c3 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 05 Jul 2021 09:47:10 -0000 The branch stable/13 has been updated by arichardson: URL: https://cgit.FreeBSD.org/src/commit/?id=6e2b0498aa702bace3859273c36bd27d7dff85c3 commit 6e2b0498aa702bace3859273c36bd27d7dff85c3 Author: Alex Richardson AuthorDate: 2021-06-16 15:35:55 +0000 Commit: Alex Richardson CommitDate: 2021-07-05 09:46:12 +0000 truss: minor cleanup and pedantic warning fixes Noticed while porting the recent truss compat32 changes to CheriBSD. This also fixes i386 tracing by zero-extending user addresses instead of sign-extending them. Reviewed By: jhb MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D30211 (cherry picked from commit 31dddc6a19042bad303cb3d3aa8f52a2cb22dd77) --- usr.bin/truss/main.c | 5 ++-- usr.bin/truss/syscalls.c | 69 ++++++++++++++++++++++++++++-------------------- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/usr.bin/truss/main.c b/usr.bin/truss/main.c index 1a3aae75be88..942f98008071 100644 --- a/usr.bin/truss/main.c +++ b/usr.bin/truss/main.c @@ -55,7 +55,7 @@ __FBSDID("$FreeBSD$"); #include "extern.h" #include "syscall.h" -static void +static __dead2 void usage(void) { fprintf(stderr, "%s\n%s\n", @@ -118,7 +118,8 @@ main(int ac, char **av) fname = optarg; break; case 's': /* Specified string size */ - trussinfo->strsize = strtonum(optarg, 0, INT_MAX, &errstr); + trussinfo->strsize = (int)strtonum(optarg, 0, INT_MAX, + &errstr); if (errstr) errx(1, "maximum string size is %s: %s", errstr, optarg); break; diff --git a/usr.bin/truss/syscalls.c b/usr.bin/truss/syscalls.c index b8022a3a395b..6740ab21ff2c 100644 --- a/usr.bin/truss/syscalls.c +++ b/usr.bin/truss/syscalls.c @@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -863,18 +864,17 @@ lookup(struct xlat *xlat, int val, int base) if (xlat->val == val) return (xlat->str); switch (base) { - case 8: - sprintf(tmp, "0%o", val); - break; - case 16: - sprintf(tmp, "0x%x", val); - break; - case 10: - sprintf(tmp, "%u", val); - break; - default: - errx(1,"Unknown lookup base"); - break; + case 8: + sprintf(tmp, "0%o", val); + break; + case 16: + sprintf(tmp, "0x%x", val); + break; + case 10: + sprintf(tmp, "%u", val); + break; + default: + errx(1, "Unknown lookup base"); } return (tmp); } @@ -1116,12 +1116,12 @@ get_syscall(struct threadinfo *t, u_int number, u_int nargs) * Copy a fixed amount of bytes from the process. */ static int -get_struct(pid_t pid, uintptr_t offset, void *buf, int len) +get_struct(pid_t pid, psaddr_t offset, void *buf, size_t len) { struct ptrace_io_desc iorequest; iorequest.piod_op = PIOD_READ_D; - iorequest.piod_offs = (void *)offset; + iorequest.piod_offs = (void *)(uintptr_t)offset; iorequest.piod_addr = buf; iorequest.piod_len = len; if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) @@ -1137,7 +1137,7 @@ get_struct(pid_t pid, uintptr_t offset, void *buf, int len) * only get that much. */ static char * -get_string(pid_t pid, uintptr_t addr, int max) +get_string(pid_t pid, psaddr_t addr, int max) { struct ptrace_io_desc iorequest; char *buf, *nbuf; @@ -1148,7 +1148,7 @@ get_string(pid_t pid, uintptr_t addr, int max) size = max + 1; else { /* Read up to the end of the current page. */ - size = PAGE_SIZE - ((uintptr_t)addr % PAGE_SIZE); + size = PAGE_SIZE - (addr % PAGE_SIZE); if (size > MAXSIZE) size = MAXSIZE; } @@ -1158,7 +1158,7 @@ get_string(pid_t pid, uintptr_t addr, int max) return (NULL); for (;;) { iorequest.piod_op = PIOD_READ_D; - iorequest.piod_offs = (void *)(addr + offset); + iorequest.piod_offs = (void *)((uintptr_t)addr + offset); iorequest.piod_addr = buf + offset; iorequest.piod_len = size; if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) { @@ -1718,6 +1718,20 @@ print_sysctl(FILE *fp, int *oid, size_t len) fprintf(fp, "%s", name); } +/* + * Convert a 32-bit user-space pointer to psaddr_t. Currently, this + * sign-extends on MIPS and zero-extends on all other architectures. + */ +static psaddr_t +user_ptr32_to_psaddr(int32_t user_pointer) +{ +#if defined(__mips__) + return ((psaddr_t)(intptr_t)user_pointer); +#else + return ((psaddr_t)(uintptr_t)user_pointer); +#endif +} + /* * Converts a syscall argument into a string. Said string is * allocated via malloc(), so needs to be free()'d. sc is @@ -1769,7 +1783,7 @@ print_arg(struct syscall_arg *sc, unsigned long *args, register_t *retval, break; case ShmName: /* Handle special SHM_ANON value. */ - if ((char *)args[sc->offset] == SHM_ANON) { + if ((char *)(uintptr_t)args[sc->offset] == SHM_ANON) { fprintf(fp, "SHM_ANON"); break; } @@ -1829,7 +1843,7 @@ print_arg(struct syscall_arg *sc, unsigned long *args, register_t *retval, case ExecArgs: case ExecEnv: case StringArray: { - uintptr_t addr; + psaddr_t addr; union { int32_t strarray32[PAGE_SIZE / sizeof(int32_t)]; int64_t strarray64[PAGE_SIZE / sizeof(int64_t)]; @@ -1859,7 +1873,7 @@ print_arg(struct syscall_arg *sc, unsigned long *args, register_t *retval, * a partial page. */ addr = args[sc->offset]; - if (addr % pointer_size != 0) { + if (!__is_aligned(addr, pointer_size)) { print_pointer(fp, args[sc->offset]); break; } @@ -1875,20 +1889,19 @@ print_arg(struct syscall_arg *sc, unsigned long *args, register_t *retval, first = 1; i = 0; for (;;) { - uintptr_t straddr; + psaddr_t straddr; if (pointer_size == 4) { - if (u.strarray32[i] == 0) - break; - /* sign-extend 32-bit pointers */ - straddr = (intptr_t)u.strarray32[i]; + straddr = user_ptr32_to_psaddr(u.strarray32[i]); } else if (pointer_size == 8) { - if (u.strarray64[i] == 0) - break; - straddr = (intptr_t)u.strarray64[i]; + straddr = (psaddr_t)u.strarray64[i]; } else { errx(1, "Unsupported pointer size: %zu", pointer_size); } + + /* Stop once we read the first NULL pointer. */ + if (straddr == 0) + break; string = get_string(pid, straddr, 0); fprintf(fp, "%s \"%s\"", first ? "" : ",", string); free(string);