2023-01-25 21:39:03 +02:00
|
|
|
#include <kernel/Debug.h>
|
2023-04-22 17:58:51 +03:00
|
|
|
#include <kernel/InterruptController.h>
|
2024-02-28 22:39:02 +02:00
|
|
|
#include <kernel/Lock/SpinLock.h>
|
2023-08-31 21:36:23 +03:00
|
|
|
#include <kernel/Memory/PageTable.h>
|
2023-09-04 19:34:18 +03:00
|
|
|
#include <kernel/Terminal/Serial.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
|
|
|
|
2023-10-13 03:31:36 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2024-05-31 10:47:05 +03:00
|
|
|
extern Kernel::TerminalDriver* g_terminal_driver;
|
2023-10-13 03:31:36 +03:00
|
|
|
|
2023-01-25 21:39:03 +02:00
|
|
|
namespace Debug
|
|
|
|
{
|
|
|
|
|
2024-02-28 22:39:02 +02:00
|
|
|
Kernel::RecursiveSpinLock s_debug_lock;
|
|
|
|
|
2023-02-01 21:05:44 +02:00
|
|
|
void dump_stack_trace()
|
2023-01-25 22:28:18 +02:00
|
|
|
{
|
2023-08-31 21:36:23 +03:00
|
|
|
using namespace Kernel;
|
|
|
|
|
2023-01-25 22:28:18 +02:00
|
|
|
struct stackframe
|
|
|
|
{
|
2024-03-22 14:48:33 +02:00
|
|
|
stackframe* bp;
|
|
|
|
uintptr_t ip;
|
2023-01-25 22:28:18 +02:00
|
|
|
};
|
|
|
|
|
2024-03-09 23:52:06 +02:00
|
|
|
SpinLockGuard _(s_debug_lock);
|
|
|
|
|
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;
|
|
|
|
}
|
2024-03-22 14:48:33 +02:00
|
|
|
uintptr_t first_ip = frame->ip;
|
|
|
|
uintptr_t last_ip = 0;
|
2023-08-31 21:36:23 +03:00
|
|
|
bool first = true;
|
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-08-31 21:36:23 +03:00
|
|
|
if (PageTable::current().is_page_free((vaddr_t)frame & PAGE_ADDR_MASK))
|
|
|
|
{
|
|
|
|
derrorln(" {} not mapped", frame);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-03-22 14:48:33 +02:00
|
|
|
BAN::Formatter::print(Debug::putchar, " {}\r\n", (void*)frame->ip);
|
2023-01-30 18:56:57 +02:00
|
|
|
|
2024-03-22 14:48:33 +02:00
|
|
|
if (!first && frame->ip == first_ip)
|
2023-01-30 18:56:57 +02:00
|
|
|
{
|
|
|
|
derrorln("looping kernel panic :(");
|
2023-04-12 01:32:41 +03:00
|
|
|
break;
|
2023-01-30 18:56:57 +02:00
|
|
|
}
|
2024-03-22 14:48:33 +02:00
|
|
|
else if (!first && frame->ip == last_ip)
|
2023-04-12 01:32:41 +03:00
|
|
|
{
|
2023-08-31 21:36:23 +03:00
|
|
|
derrorln("repeating stack trace");
|
2023-04-12 01:32:41 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-03-22 14:48:33 +02:00
|
|
|
last_ip = frame->ip;
|
|
|
|
frame = frame->bp;
|
2023-08-31 21:36:23 +03:00
|
|
|
first = false;
|
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)
|
|
|
|
{
|
2024-05-31 10:47:05 +03:00
|
|
|
using namespace Kernel;
|
|
|
|
|
2023-09-04 14:57:05 +03:00
|
|
|
if (Kernel::Serial::has_devices())
|
|
|
|
return Kernel::Serial::putchar_any(ch);
|
2023-04-05 00:56:09 +03:00
|
|
|
if (Kernel::TTY::is_initialized())
|
|
|
|
return Kernel::TTY::putchar_current(ch);
|
2023-10-13 03:31:36 +03:00
|
|
|
|
|
|
|
if (g_terminal_driver)
|
|
|
|
{
|
|
|
|
static uint32_t col = 0;
|
|
|
|
static uint32_t row = 0;
|
|
|
|
|
2023-10-13 12:43:52 +03:00
|
|
|
uint32_t row_copy = row;
|
|
|
|
|
2023-10-13 03:31:36 +03:00
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
row++;
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
else if (ch == '\r')
|
|
|
|
{
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!isprint(ch))
|
|
|
|
ch = '?';
|
|
|
|
g_terminal_driver->putchar_at(ch, col, row, TerminalColor::BRIGHT_WHITE, TerminalColor::BLACK);
|
|
|
|
|
|
|
|
col++;
|
|
|
|
if (col >= g_terminal_driver->width())
|
|
|
|
{
|
|
|
|
row++;
|
|
|
|
col = 0;
|
|
|
|
}
|
|
|
|
}
|
2023-10-13 12:43:52 +03:00
|
|
|
|
|
|
|
if (row >= g_terminal_driver->height())
|
|
|
|
row = 0;
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2023-10-13 12:43:52 +03:00
|
|
|
if (row != row_copy)
|
|
|
|
{
|
|
|
|
for (uint32_t i = col; i < g_terminal_driver->width(); i++)
|
|
|
|
{
|
|
|
|
g_terminal_driver->putchar_at(' ', i, row, TerminalColor::BRIGHT_WHITE, TerminalColor::BLACK);
|
|
|
|
if (row + 1 < g_terminal_driver->height())
|
|
|
|
g_terminal_driver->putchar_at(' ', i, row + 1, TerminalColor::BRIGHT_WHITE, TerminalColor::BLACK);
|
|
|
|
}
|
|
|
|
}
|
2023-10-13 03:31:36 +03:00
|
|
|
}
|
2023-01-25 21:39:03 +02:00
|
|
|
}
|
|
|
|
|
2023-08-04 10:22:20 +03:00
|
|
|
void print_prefix(const char* file, int line)
|
|
|
|
{
|
2023-08-04 15:50:11 +03:00
|
|
|
auto ms_since_boot = Kernel::SystemTimer::is_initialized() ? Kernel::SystemTimer::get().ms_since_boot() : 0;
|
2023-08-04 10:22:20 +03:00
|
|
|
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
|
|
|
|
2024-01-24 14:43:46 +02:00
|
|
|
}
|