2023-01-25 21:39:03 +02:00
|
|
|
#include <kernel/Debug.h>
|
2023-04-22 17:58:51 +03:00
|
|
|
#include <kernel/InterruptController.h>
|
2023-01-25 21:39:03 +02:00
|
|
|
#include <kernel/Serial.h>
|
2023-04-22 17:58:51 +03:00
|
|
|
#include <kernel/SpinLock.h>
|
2023-04-05 00:56:09 +03:00
|
|
|
#include <kernel/Terminal/TTY.h>
|
2023-08-04 10:22:20 +03:00
|
|
|
#include <kernel/Timer/Timer.h>
|
2023-01-25 21:39:03 +02:00
|
|
|
|
|
|
|
namespace Debug
|
|
|
|
{
|
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
void dump_stack_trace()
|
2023-01-25 22:28:18 +02:00
|
|
|
{
|
|
|
|
struct stackframe
|
|
|
|
{
|
2023-01-30 18:56:57 +02:00
|
|
|
stackframe* rbp;
|
|
|
|
uintptr_t rip;
|
2023-01-25 22:28:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
stackframe* frame = (stackframe*)__builtin_frame_address(0);
|
2023-01-30 18:56:57 +02:00
|
|
|
if (!frame)
|
|
|
|
{
|
|
|
|
dprintln("Could not get frame address");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uintptr_t first_rip = frame->rip;
|
2023-04-12 01:32:41 +03:00
|
|
|
uintptr_t last_rip = 0;
|
2023-01-30 18:56:57 +02:00
|
|
|
|
2023-01-25 22:28:18 +02:00
|
|
|
BAN::Formatter::print(Debug::putchar, "\e[36mStack trace:\r\n");
|
|
|
|
while (frame)
|
|
|
|
{
|
2023-01-30 18:56:57 +02:00
|
|
|
BAN::Formatter::print(Debug::putchar, " {}\r\n", (void*)frame->rip);
|
|
|
|
frame = frame->rbp;
|
|
|
|
|
|
|
|
if (frame && frame->rip == first_rip)
|
|
|
|
{
|
|
|
|
derrorln("looping kernel panic :(");
|
2023-04-12 01:32:41 +03:00
|
|
|
break;
|
2023-01-30 18:56:57 +02:00
|
|
|
}
|
2023-04-12 01:32:41 +03:00
|
|
|
|
|
|
|
if (frame && frame->rip == last_rip)
|
|
|
|
{
|
|
|
|
derrorln("repeating stack strace");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_rip = frame->rip;
|
2023-01-25 22:28:18 +02:00
|
|
|
}
|
2023-03-08 01:21:17 +02:00
|
|
|
BAN::Formatter::print(Debug::putchar, "\e[m");
|
2023-01-25 22:28:18 +02:00
|
|
|
}
|
|
|
|
|
2023-01-25 21:39:03 +02:00
|
|
|
void putchar(char ch)
|
|
|
|
{
|
2023-02-01 21:05:44 +02:00
|
|
|
if (Serial::is_initialized())
|
2023-01-25 21:39:03 +02:00
|
|
|
return Serial::putchar(ch);
|
2023-04-05 00:56:09 +03:00
|
|
|
if (Kernel::TTY::is_initialized())
|
|
|
|
return Kernel::TTY::putchar_current(ch);
|
2023-01-25 21:39:03 +02:00
|
|
|
}
|
|
|
|
|
2023-08-04 10:22:20 +03:00
|
|
|
void print_prefix(const char* file, int line)
|
|
|
|
{
|
|
|
|
auto ms_since_boot = Kernel::TimerHandler::is_initialized() ? Kernel::TimerHandler::get().ms_since_boot() : 0;
|
|
|
|
BAN::Formatter::print(Debug::putchar, "[{5}.{3}] {}:{}: ", ms_since_boot / 1000, ms_since_boot % 1000, file, line);
|
|
|
|
}
|
2023-04-22 17:58:51 +03:00
|
|
|
|
|
|
|
static Kernel::RecursiveSpinLock s_debug_lock;
|
|
|
|
|
|
|
|
void DebugLock::lock()
|
|
|
|
{
|
|
|
|
if (interrupts_enabled())
|
|
|
|
s_debug_lock.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DebugLock::unlock()
|
|
|
|
{
|
|
|
|
if (interrupts_enabled())
|
|
|
|
s_debug_lock.unlock();
|
|
|
|
}
|
|
|
|
|
2023-01-25 21:39:03 +02:00
|
|
|
}
|