diff --git a/userspace/libraries/LibC/unistd.cpp b/userspace/libraries/LibC/unistd.cpp index d28233a3..f14dbaab 100644 --- a/userspace/libraries/LibC/unistd.cpp +++ b/userspace/libraries/LibC/unistd.cpp @@ -209,6 +209,18 @@ static void __dump_backtrace(int sig, siginfo_t*, void* context) return "unknown signal"; }; + // NOTE: we cannot use stddbg as that is not async-signal-safe. + // POSIX says dprintf isn't either but our implementation is! + + int fd = open("/dev/debug", O_WRONLY); + if (fd == -1) + { + perror("failed to open debug device for backtrace"); + return; + } + + dprintf(fd, "received %s, backtrace:\n", signal_name(sig)); + const auto* ucontext = static_cast(context); #if defined(__x86_64__) const uintptr_t stack_base = ucontext->uc_mcontext.gregs[REG_RBP]; @@ -226,18 +238,6 @@ static void __dump_backtrace(int sig, siginfo_t*, void* context) const auto* stackframe = reinterpret_cast(stack_base); - // NOTE: we cannot use stddbf as that is not async-signal-safe. - // POSIX says dprintf isn't either but our implementation is! - - int fd = open("/dev/debug", O_WRONLY); - if (fd == -1) - { - perror("failed to open debug device for backtrace"); - return; - } - - dprintf(fd, "received %s, backtrace:\n", signal_name(sig)); - __dump_symbol(fd, reinterpret_cast(instruction)); for (size_t i = 0; i < 128 && stackframe; i++) {