Kernel: Move debug printing to its own file

It didn't make sense that dprint was defined in Serial.h.
We also now dump dprint to tty if there is no serial and tty is initialized
This commit is contained in:
Bananymous 2023-01-25 21:39:03 +02:00
parent b315fdc27f
commit c0bc002ac6
16 changed files with 73 additions and 55 deletions

View File

@ -34,6 +34,7 @@ $(KERNEL_ARCH_OBJS) \
kernel/APIC.o \
kernel/build_libc.o \
kernel/CPUID.o \
kernel/Debug.o \
kernel/font.o \
kernel/Input.o \
kernel/InterruptController.o \

View File

@ -1,7 +1,7 @@
#include <BAN/Errors.h>
#include <kernel/Debug.h>
#include <kernel/MMU.h>
#include <kernel/kmalloc.h>
#include <kernel/Serial.h>
#include <string.h>

View File

@ -0,0 +1,32 @@
#pragma once
#include <BAN/Formatter.h>
#include <kernel/PIT.h>
#define dprintln(...) \
do { \
BAN::Formatter::print(Debug::putchar, "[{5}.{3}] {}:{}: ", PIT::ms_since_boot() / 1000, PIT::ms_since_boot() % 1000, __FILE__, __LINE__); \
BAN::Formatter::print(Debug::putchar, __VA_ARGS__); \
BAN::Formatter::print(Debug::putchar, "\r\n"); \
} while(false)
#define dwarnln(...) \
do { \
BAN::Formatter::print(Debug::putchar, "\e[33m"); \
dprintln(__VA_ARGS__); \
BAN::Formatter::print(Debug::putchar, "\e[m"); \
} while(false)
#define derrorln(...) \
do { \
BAN::Formatter::print(Debug::putchar, "\e[31m"); \
dprintln(__VA_ARGS__); \
BAN::Formatter::print(Debug::putchar, "\e[m"); \
} while(false)
#define BOCHS_BREAK() asm volatile("xchgw %bx, %bx")
namespace Debug
{
void putchar(char);
}

View File

@ -1,8 +1,7 @@
#pragma once
#include <kernel/Debug.h>
#include <kernel/kprint.h>
#include <kernel/Serial.h>
#include <kernel/TTY.h>
#define Panic(...) PanicImpl(__FILE__, __LINE__, __VA_ARGS__)
@ -15,15 +14,9 @@ namespace Kernel
__attribute__((__noreturn__))
static void PanicImpl(const char* file, int line, const char* message, Args... args)
{
derrorln("Kernel panic at {}:{}", file, line);
kprintln("\e[31mKernel panic at {}:{}\e[m", file, line);
derrorln(message, args...);
dump_stacktrace();
if (TTY::IsInitialized())
{
kprint("\e[31mKernel panic at {}:{}\n", file, line);
kprint(message, args...);
kprint("\e[m\n");
}
asm volatile("cli");
for (;;)
asm volatile("hlt");

View File

@ -1,36 +1,11 @@
#pragma once
#include <BAN/Formatter.h>
#include <kernel/PIT.h>
#define dprintln(...) \
do { \
BAN::Formatter::print(Serial::serial_putc, "[{5}.{3}] {}:{}: ", PIT::ms_since_boot() / 1000, PIT::ms_since_boot() % 1000, __FILE__, __LINE__); \
BAN::Formatter::print(Serial::serial_putc, __VA_ARGS__); \
BAN::Formatter::print(Serial::serial_putc, "\r\n"); \
} while(false)
#define dwarnln(...) \
do { \
BAN::Formatter::print(Serial::serial_putc, "\e[33m"); \
dprintln(__VA_ARGS__); \
BAN::Formatter::print(Serial::serial_putc, "\e[m"); \
} while(false)
#define derrorln(...) \
do { \
BAN::Formatter::print(Serial::serial_putc, "\e[31m"); \
dprintln(__VA_ARGS__); \
BAN::Formatter::print(Serial::serial_putc, "\e[m"); \
} while(false)
#define BOCHS_BREAK() asm volatile("xchgw %bx, %bx")
namespace Serial
{
void initialize();
void Initialize();
bool IsInitialized();
void serial_putc(char);
void putchar(char);
}

View File

@ -1,7 +1,6 @@
#pragma once
#include <kernel/TerminalDriver.h>
#include <kernel/Serial.h>
class TTY
{

View File

@ -1,10 +1,9 @@
#include <BAN/ScopeGuard.h>
#include <kernel/Debug.h>
#include <kernel/APIC.h>
#include <kernel/CPUID.h>
#include <kernel/IDT.h>
#include <kernel/IO.h>
#include <kernel/MMU.h>
#include <kernel/Serial.h>
#include <string.h>

16
kernel/kernel/Debug.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <kernel/Debug.h>
#include <kernel/Serial.h>
#include <kernel/TTY.h>
namespace Debug
{
void putchar(char ch)
{
if (Serial::IsInitialized())
return Serial::putchar(ch);
if (TTY::IsInitialized())
return TTY::PutCharCurrent(ch);
}
}

View File

@ -1,11 +1,11 @@
#include <BAN/Queue.h>
#include <kernel/Debug.h>
#include <kernel/IDT.h>
#include <kernel/Input.h>
#include <kernel/InterruptController.h>
#include <kernel/IO.h>
#include <kernel/kprint.h>
#include <kernel/PIT.h>
#include <kernel/Serial.h>
#include <kernel/KeyboardLayout/FI.h>

View File

@ -12,10 +12,10 @@ namespace Kernel
};
stackframe* frame = (stackframe*)__builtin_frame_address(0);
BAN::Formatter::print(Serial::serial_putc, "\e[36mStack trace:\r\n");
BAN::Formatter::print(Debug::putchar, "\e[36mStack trace:\r\n");
while (frame)
{
BAN::Formatter::print(Serial::serial_putc, " {}\r\n", (void*)frame->eip);
BAN::Formatter::print(Debug::putchar, " {}\r\n", (void*)frame->eip);
frame = frame->ebp;
}
}

View File

@ -1,5 +1,5 @@
#include <BAN/Errors.h>
#include <kernel/IO.h>
#include <kernel/Serial.h>
#define COM1_PORT 0x3f8
@ -8,7 +8,7 @@ namespace Serial
static bool s_initialized = false;
void initialize()
void Initialize()
{
IO::outb(COM1_PORT + 1, 0x00); // Disable all interrupts
IO::outb(COM1_PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
@ -30,14 +30,18 @@ namespace Serial
s_initialized = true;
}
int is_transmit_empty() {
bool IsInitialized()
{
return s_initialized;
}
static int is_transmit_empty() {
return IO::inb(COM1_PORT + 5) & 0x20;
}
void serial_putc(char c)
void putchar(char c)
{
if (!s_initialized)
return;
ASSERT(s_initialized);
while (is_transmit_empty() == 0);
IO::outb(COM1_PORT, c);
}

View File

@ -2,14 +2,11 @@
#include <BAN/StringView.h>
#include <BAN/Vector.h>
#include <kernel/CPUID.h>
#include <kernel/font.h>
#include <kernel/Input.h>
#include <kernel/IO.h>
#include <kernel/PIT.h>
#include <kernel/RTC.h>
#include <kernel/Serial.h>
#include <kernel/Shell.h>
#include <kernel/TTY.h>
#include <ctype.h>

View File

@ -1,5 +1,5 @@
#include <BAN/Errors.h>
#include <kernel/Serial.h>
#include <kernel/Debug.h>
#include <kernel/TTY.h>
#include <string.h>

View File

@ -1,7 +1,7 @@
#include <BAN/Errors.h>
#include <kernel/Debug.h>
#include <kernel/MMU.h>
#include <kernel/multiboot.h>
#include <kernel/Serial.h>
#include <kernel/VesaTerminalDriver.h>
extern const struct bitmap_font font;

View File

@ -1,3 +1,4 @@
#include <kernel/Debug.h>
#include <kernel/IDT.h>
#include <kernel/Input.h>
#include <kernel/InterruptController.h>

View File

@ -1,6 +1,7 @@
#include <BAN/Errors.h>
#include <BAN/Math.h>
#include <kernel/kmalloc.h>
#include <kernel/kprint.h>
#include <kernel/multiboot.h>
#include <stdint.h>