]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/syscall_shim: Move binary syscall strace to `syscall_exittab`
authorSergiu Moga <sergiu@unikraft.io>
Tue, 28 Jan 2025 19:00:58 +0000 (21:00 +0200)
committerUnikraft Bot <monkey@unikraft.io>
Fri, 14 Feb 2025 17:47:25 +0000 (17:47 +0000)
Move `strace` printing to a `syscall_exittab` handler.
Since we want this to be printed on binary system calls exit only, make
sure to check for it.

Lastly, add an assertion for a nested depth of 1. It should be
impossible that this would be different from 1 as it would either mean
a kernel internal system call invoked the binary handler somehow or the
TLS counter nesting variable is corrupted.

Signed-off-by: Sergiu Moga <sergiu@unikraft.io>
Approved-by: Michalis Pappas <michalis@unikraft.io>
Reviewed-by: Michalis Pappas <michalis@unikraft.io>
GitHub-Closes: #1277

lib/syscall_shim/uk_syscall_binary.c

index de7d3b81d884f9861955a876b4094a1b70e3024f..30baecde7879840ded01870978a594835e9b2ee4 100644 (file)
@@ -40,6 +40,7 @@
 #include <uk/arch/ctx.h>
 #include <uk/assert.h>
 #include <uk/essentials.h>
+#include <uk/prio.h>
 #include <uk/thread.h>
 #if CONFIG_LIBSYSCALL_SHIM_STRACE
 #if CONFIG_LIBUKCONSOLE
@@ -70,14 +71,6 @@ struct uk_syscall_ctx {
 
 void ukplat_syscall_handler(struct uk_syscall_ctx *usc)
 {
-#if CONFIG_LIBSYSCALL_SHIM_STRACE
-#if CONFIG_LIBSYSCALL_SHIM_STRACE_ANSI_COLOR
-       char prsyscallbuf[512]; /* ANSI color is pretty hungry */
-#else /* !CONFIG_LIBSYSCALL_SHIM_STRACE_ANSI_COLOR */
-       char prsyscallbuf[256];
-#endif /* !CONFIG_LIBSYSCALL_SHIM_STRACE_ANSI_COLOR */
-       int prsyscalllen __maybe_unused;
-#endif /* CONFIG_LIBSYSCALL_SHIM_STRACE */
        struct uk_syscall_enter_ctx enter_ctx;
        struct uk_syscall_exit_ctx exit_ctx;
        struct ukarch_auxspcb *auxspcb;
@@ -132,7 +125,29 @@ void ukplat_syscall_handler(struct uk_syscall_ctx *usc)
        uk_syscall_exittab_run(&exit_ctx);
        uk_syscall_nested_depth--;
 
+#if CONFIG_LIBSYSCALL_SHIM_HANDLER_ULTLS
+       t->tlsp = ukarch_sysctx_get_tlsp(&execenv->sysctx);
+#endif /* CONFIG_LIBSYSCALL_SHIM_HANDLER_ULTLS */
+}
+
 #if CONFIG_LIBSYSCALL_SHIM_STRACE
+static void binary_syscall_strace(struct uk_syscall_exit_ctx *exit_ctx)
+{
+#if CONFIG_LIBSYSCALL_SHIM_STRACE_ANSI_COLOR
+       char prsyscallbuf[512]; /* ANSI color is pretty hungry */
+#else /* !CONFIG_LIBSYSCALL_SHIM_STRACE_ANSI_COLOR */
+       char prsyscallbuf[256];
+#endif /* !CONFIG_LIBSYSCALL_SHIM_STRACE_ANSI_COLOR */
+       int prsyscalllen __maybe_unused;
+       struct ukarch_execenv *execenv;
+
+       UK_ASSERT(exit_ctx);
+
+       if (!(exit_ctx->flags & UK_SYSCALL_EXIT_CTX_BINARY_SYSCALL))
+               return;
+
+       execenv = exit_ctx->execenv;
+
        prsyscalllen = uk_snprsyscall(prsyscallbuf, ARRAY_SIZE(prsyscallbuf),
 #if CONFIG_LIBSYSCALL_SHIM_STRACE_ANSI_COLOR
                     UK_PRSYSCALL_FMTF_ANSICOLOR | UK_PRSYSCALL_FMTF_NEWLINE,
@@ -155,13 +170,11 @@ void ukplat_syscall_handler(struct uk_syscall_ctx *usc)
         * print calls also turn into a no-op if `ukconsole` is not available.
         */
 #if CONFIG_LIBUKCONSOLE
-       uk_console_out(prsyscallbuf, (__sz) prsyscalllen);
+       uk_console_out(prsyscallbuf, (__sz)prsyscalllen);
 #else /* !CONFIG_LIBUKCONSOLE */
        uk_pr_info(prsyscallbuf);
 #endif /* !CONFIG_LIBUKCONSOLE */
-#endif /* CONFIG_LIBSYSCALL_SHIM_STRACE */
-
-#if CONFIG_LIBSYSCALL_SHIM_HANDLER_ULTLS
-       t->tlsp = ukarch_sysctx_get_tlsp(&execenv->sysctx);
-#endif /* CONFIG_LIBSYSCALL_SHIM_HANDLER_ULTLS */
 }
+
+uk_syscall_exittab_prio(binary_syscall_strace, UK_PRIO_LATEST);
+#endif /* CONFIG_LIBSYSCALL_SHIM_STRACE */