logo

mstrace

Small Linux strace(1) implementation
commit: 76b8c037209b30660ccb2cc4f7de533de648ab5c
parent c3e7239fd9a12f02c1fbccf760e863f1546cf9f3
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 24 Nov 2024 11:23:56 +0100

print_syscall: Use custom Pri_ macros to increase readability

Diffstat:

Mprint_syscall.c94++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 67 insertions(+), 27 deletions(-)

diff --git a/print_syscall.c b/print_syscall.c @@ -43,28 +43,37 @@ read_str(intptr_t addr, size_t len) return str_buf; } +#define Pri_str "\"%s\"" +#define Pri_i "%" PRIi64 +#define Pri_u "%" PRIu64 +#define Pri_o "0o%" PRIo64 +#define Pri_x "0x%" PRIx64 + void print_syscall(__u64 nr, __u64 args[6]) { - // Handle bog-standard syscalls, limited to ones in POSIX but exhaustivity isn't warranted + // * Non-exhaustive + // * Doesn't handles special cases like structures or arrays + // Ordered by number-of-arguments/complexity switch(nr) { - // path, buf, size + // int, path, buf, size case SYS_readlinkat: fprintf(stderr, - "%s(%" PRIi64 ",\"%s\", 0x%" PRIx64 ", %zd)", + "%s(" Pri_i ", " Pri_str ", " Pri_x ", %zd)", str_syscalls[nr], (int64_t)args[0], - read_str(args[0], PATH_MAX), - (int64_t)args[1], - (size_t)args[2]); + read_str(args[1], PATH_MAX), + (int64_t)args[2], + (size_t)args[3]); return; - // int, path, hex, hex + // int, path, oct, oct case SYS_openat: case SYS_faccessat: fprintf(stderr, - "openat(%" PRIi64 ", \"%s\", %" PRIx64 ", %" PRIx64 ")", + "%s(" Pri_i ", " Pri_str ", " Pri_o ", " Pri_o ")", + str_syscalls[nr], (int64_t)args[0], read_str(args[1], PATH_MAX), (int64_t)args[2], @@ -73,7 +82,8 @@ print_syscall(__u64 nr, __u64 args[6]) // path, hex, hex case SYS_execve: fprintf(stderr, - "execve(\"%s\", %" PRIx64 ", %" PRIx64 ")", + "%s(" Pri_str ", " Pri_x ", " Pri_x ")", + str_syscalls[nr], read_str(args[0], PATH_MAX), (uint64_t)args[1], (uint64_t)args[2]); @@ -83,7 +93,7 @@ print_syscall(__u64 nr, __u64 args[6]) case SYS_read: // Need handling of non-printable characters to print buf fprintf(stderr, - "%s(%" PRIi64 ", 0x%" PRIx64 ", %zd)", + "%s(" Pri_i ", " Pri_x ", %zd)", str_syscalls[nr], (int64_t)args[0], (int64_t)args[1], @@ -93,7 +103,7 @@ print_syscall(__u64 nr, __u64 args[6]) case SYS_linkat: case SYS_symlinkat: fprintf(stderr, - "%s(%" PRIi64 ", \"%s\", \"%s\")", + "%s(" Pri_i ", " Pri_str ", " Pri_str ")", str_syscalls[nr], (int64_t)args[0], read_str(args[1], PATH_MAX), @@ -102,18 +112,26 @@ print_syscall(__u64 nr, __u64 args[6]) // path, buf, size case SYS_readlink: fprintf(stderr, - "%s(\"%s\", 0x%" PRIx64 ", %zd)", + "%s(" Pri_str ", " Pri_x ", %zd)", str_syscalls[nr], read_str(args[0], PATH_MAX), (int64_t)args[1], (size_t)args[2]); return; - // path, int, int + // path, oct, oct case SYS_open: + fprintf(stderr, + "%s(" Pri_str ", " Pri_o ", " Pri_o ")", + str_syscalls[nr], + read_str(args[0], PATH_MAX), + (int64_t)args[1], + (int64_t)args[2]); + return; + // path, int, int case SYS_chown: case SYS_lchown: fprintf(stderr, - "%s(\"%s\", %" PRIi64 ", %" PRIi64 ")", + "%s(" Pri_str ", " Pri_i ", " Pri_o ")", str_syscalls[nr], read_str(args[0], PATH_MAX), (int64_t)args[1], @@ -122,8 +140,9 @@ print_syscall(__u64 nr, __u64 args[6]) // path, path case SYS_link: case SYS_symlink: + case SYS_rename: fprintf(stderr, - "%s(\"%s\", \"%s\")", + "%s(" Pri_str ", " Pri_str ")", str_syscalls[nr], read_str(args[0], PATH_MAX), read_str(args[1], PATH_MAX)); @@ -131,59 +150,79 @@ print_syscall(__u64 nr, __u64 args[6]) // path, off_t case SYS_truncate: fprintf(stderr, - "%s(\"%s\", %" PRIu64 ")", + "%s(" Pri_str ", " Pri_u ")", str_syscalls[nr], read_str(args[0], PATH_MAX), (off_t)args[1]); return; // path, size case SYS_getcwd: - fprintf( - stderr, "%s(\"%s\", %zd)", str_syscalls[nr], read_str(args[0], PATH_MAX), (size_t)args[1]); + fprintf(stderr, + "%s(" Pri_str ", %zd)", + str_syscalls[nr], + read_str(args[0], PATH_MAX), + (size_t)args[1]); return; - // path, int + // path, oct case SYS_access: case SYS_stat: case SYS_lstat: + case SYS_mkdir: + case SYS_creat: + case SYS_chmod: fprintf(stderr, - "%s(\"%s\", 0x%" PRIx64 ")", + "%s(" Pri_str ", " Pri_o ")", str_syscalls[nr], read_str(args[0], PATH_MAX), (int64_t)args[1]); return; // int, path case SYS_unlinkat: - fprintf(stderr, "unlinkat(%" PRIu64 "\"%s\")", (int64_t)args[0], read_str(args[1], PATH_MAX)); + fprintf(stderr, + "%s(" Pri_i ", " Pri_str ")", + str_syscalls[nr], + (int64_t)args[0], + read_str(args[1], PATH_MAX)); return; // int, off_t case SYS_ftruncate: + fprintf(stderr, "%s(" Pri_i ", " Pri_u ")", str_syscalls[nr], (int64_t)args[0], (off_t)args[1]); + return; + // int, oct + case SYS_fchmod: fprintf( - stderr, "%s(%" PRIu64 ", %" PRIu64 ")", str_syscalls[nr], (int64_t)args[0], (off_t)args[1]); + stderr, "%s(" Pri_i ", " Pri_o ")", str_syscalls[nr], (int64_t)args[0], (int64_t)args[1]); return; // int, int case SYS_kill: case SYS_dup2: fprintf( - stderr, "%s(%" PRIi64 ", %" PRIi64 ")", str_syscalls[nr], (int64_t)args[0], (int64_t)args[1]); + stderr, "%s(" Pri_i ", " Pri_i ")", str_syscalls[nr], (int64_t)args[0], (int64_t)args[1]); return; // path case SYS_unlink: case SYS_rmdir: case SYS_chdir: - fprintf(stderr, "%s(\"%s\")", str_syscalls[nr], read_str(args[0], PATH_MAX)); + fprintf(stderr, "%s(" Pri_str ")", str_syscalls[nr], read_str(args[0], PATH_MAX)); return; // hex case SYS_brk: - fprintf(stderr, "%s(%" PRIx64 ")", str_syscalls[nr], (int64_t)args[0]); + fprintf(stderr, "%s(" Pri_x ")", str_syscalls[nr], (int64_t)args[0]); + return; + // oct + case SYS_umask: + fprintf(stderr, "%s(" Pri_o ")", str_syscalls[nr], (int64_t)args[0]); return; // int case SYS_dup: case SYS_close: - fprintf(stderr, "%s(%" PRIi64 ")", str_syscalls[nr], (int64_t)args[0]); + case SYS_fsync: + case SYS_fdatasync: + fprintf(stderr, "%s(" Pri_i ")", str_syscalls[nr], (int64_t)args[0]); return; // uint case SYS_alarm: - fprintf(stderr, "%s(%" PRIu64 ")", str_syscalls[nr], (uint64_t)args[0]); + fprintf(stderr, "%s(" Pri_u ")", str_syscalls[nr], (uint64_t)args[0]); return; // syscall0 case SYS_getpid: @@ -192,6 +231,7 @@ print_syscall(__u64 nr, __u64 args[6]) case SYS_setsid: case SYS_sync: case SYS_fork: + case SYS_vfork: case SYS_pause: fprintf(stderr, "%s()", str_syscalls[nr]); return;