aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/truss
diff options
context:
space:
mode:
authorAlex Richardson <arichardson@FreeBSD.org>2021-06-16 15:35:55 +0000
committerAlex Richardson <arichardson@FreeBSD.org>2021-06-16 15:35:55 +0000
commit31dddc6a19042bad303cb3d3aa8f52a2cb22dd77 (patch)
tree290ba3047f69c74b80cc0abf6585cafac32fe61b /usr.bin/truss
parent8ba2e89e987c7ae171c5f099e59d599294d626b7 (diff)
downloadsrc-31dddc6a19042bad303cb3d3aa8f52a2cb22dd77.tar.gz
src-31dddc6a19042bad303cb3d3aa8f52a2cb22dd77.zip
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
Diffstat (limited to 'usr.bin/truss')
-rw-r--r--usr.bin/truss/main.c5
-rw-r--r--usr.bin/truss/syscalls.c69
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 c19f3c3e598d..8c3e833adaec 100644
--- a/usr.bin/truss/syscalls.c
+++ b/usr.bin/truss/syscalls.c
@@ -48,6 +48,7 @@ __FBSDID("$FreeBSD$");
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/poll.h>
+#include <sys/procfs.h>
#include <sys/ptrace.h>
#include <sys/resource.h>
#include <sys/sched.h>
@@ -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) {
@@ -1719,6 +1719,20 @@ print_sysctl(FILE *fp, int *oid, size_t len)
}
/*
+ * 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
* a pointer to the syscall description (see above); args 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);