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/APIC.o \
kernel/build_libc.o \ kernel/build_libc.o \
kernel/CPUID.o \ kernel/CPUID.o \
kernel/Debug.o \
kernel/font.o \ kernel/font.o \
kernel/Input.o \ kernel/Input.o \
kernel/InterruptController.o \ kernel/InterruptController.o \

View File

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

View File

@ -1,36 +1,11 @@
#pragma once #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 namespace Serial
{ {
void initialize(); void Initialize();
bool IsInitialized();
void serial_putc(char); void putchar(char);
} }

View File

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

View File

@ -1,10 +1,9 @@
#include <BAN/ScopeGuard.h> #include <BAN/ScopeGuard.h>
#include <kernel/Debug.h>
#include <kernel/APIC.h> #include <kernel/APIC.h>
#include <kernel/CPUID.h> #include <kernel/CPUID.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
#include <kernel/IO.h>
#include <kernel/MMU.h> #include <kernel/MMU.h>
#include <kernel/Serial.h>
#include <string.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 <BAN/Queue.h>
#include <kernel/Debug.h>
#include <kernel/IDT.h> #include <kernel/IDT.h>
#include <kernel/Input.h> #include <kernel/Input.h>
#include <kernel/InterruptController.h> #include <kernel/InterruptController.h>
#include <kernel/IO.h> #include <kernel/IO.h>
#include <kernel/kprint.h> #include <kernel/kprint.h>
#include <kernel/PIT.h> #include <kernel/PIT.h>
#include <kernel/Serial.h>
#include <kernel/KeyboardLayout/FI.h> #include <kernel/KeyboardLayout/FI.h>

View File

@ -12,10 +12,10 @@ namespace Kernel
}; };
stackframe* frame = (stackframe*)__builtin_frame_address(0); 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) 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; frame = frame->ebp;
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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