Skip to content

Commit

Permalink
PanicKernel prints a backtrace using _Unwind_Backtrace().
Browse files Browse the repository at this point in the history
  • Loading branch information
foxostro committed Mar 20, 2019
1 parent 58ff46e commit 9e00d83
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions Kernel/src/platform/i386/panic.cpp
Expand Up @@ -4,11 +4,13 @@
#include <common/text_terminal.hpp>
#include <halt.h>
#include <interrupt_asm.h>
#include <backtrace.hpp> // for enumerate_stack_frames()
#include <unwind.h> // for _Unwind_* symbols

#include <cstdio>
#include <cstdarg>

static _Unwind_Reason_Code trace(struct _Unwind_Context* context, void* param);

class PanicKernel : private KernelPolicy {
public:
virtual ~PanicKernel() = default;
Expand All @@ -17,7 +19,7 @@ class PanicKernel : private KernelPolicy {
: message_(message),
terminal_(display_)
{
hardware_interrupt_controller_.init(/*panic=*/ true);
// hardware_interrupt_controller_.init(/*panic=*/ true);
}

__attribute__((noreturn)) void run() noexcept
Expand All @@ -39,14 +41,22 @@ class PanicKernel : private KernelPolicy {
void backtrace()
{
puts("Back Trace:\n");
enumerate_stack_frames([&](uintptr_t instruction_pointer){
_Unwind_Backtrace(::trace, reinterpret_cast<void*>(this));
puts("End Back Trace\n");
}

_Unwind_Reason_Code trace(struct _Unwind_Context* context)
{
void* ip = reinterpret_cast<void*>(_Unwind_GetIP(context));
if (ip) {
char buffer[32] = {0};
snprintf(buffer, sizeof(buffer),
"[%p]\n",
reinterpret_cast<void*>(instruction_pointer));
snprintf(buffer, sizeof(buffer), "[%p]\n",
reinterpret_cast<void*>(ip));
puts(buffer);
});
puts("End Back Trace\n");
return _URC_NO_REASON;
} else {
return _URC_END_OF_STACK;
}
}

__attribute__((noreturn)) void interrupt() noexcept
Expand All @@ -65,6 +75,11 @@ class PanicKernel : private KernelPolicy {
LoggerTextOutputStream logger_;
};

static _Unwind_Reason_Code trace(struct _Unwind_Context* context, void* param)
{
return reinterpret_cast<PanicKernel*>(param)->trace(context);
}

static PanicKernel* g_panic_kernel;

// Prints a message to the screen and halts forever.
Expand Down

0 comments on commit 9e00d83

Please sign in to comment.