Skip to content

Commit

Permalink
feat: stacktraces
Browse files Browse the repository at this point in the history
Signed-off-by: Ry <ry.diffusion@proton.me>
  • Loading branch information
ry-diffusion committed Nov 5, 2023
1 parent e1f8757 commit 7b32228
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Kernel/Arch/x86_64-pc/Asm/Asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void sti(void) { asm("sti"); }
void cli(void) { asm("cli"); }
void halt(void) { asm("hlt"); }

void hcf(void) {
__attribute__((noreturn)) void hcf(void) {
asm("cli");
for (;;) {
asm("hlt");
Expand Down
9 changes: 3 additions & 6 deletions Kernel/Arch/x86_64-pc/Entry/Entry.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "Kernel/Panic.h"
#include <Asm/Asm.h>
#include <Framebuffer/Framebuffer.h>
#include <LibK/stdio.h>
Expand All @@ -6,10 +7,7 @@
#include <System/Interrupts.h>
#include <System/PIC.h>
#include <limine.h>

#ifndef GIT_VERSION
#define GIT_VERSION "Undefined"
#endif
#include <meta.h>

extern void far_jump(void);

Expand Down Expand Up @@ -81,8 +79,7 @@ void Arch_entry(void) {
asm("int $0");
asm("int $0");

#ifdef GIT_VERSION
kprintf("Welcome to BlobOS!\nVersion: %s\n", GIT_VERSION);
#endif

halt();
}
5 changes: 5 additions & 0 deletions Kernel/Arch/x86_64-pc/Include/meta.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#ifndef GIT_VERSION
#define GIT_VERSION "UNKNOWN"
#endif
2 changes: 2 additions & 0 deletions Kernel/Arch/x86_64-pc/System/GDTFarJump.asm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[bits 64]
[global reloadSegments]
[global set_up_stack]

reloadSegments:
push 0x08 ; Push code segment to stack
Expand All @@ -15,3 +16,4 @@ reloadSegments:
mov gs, ax
mov ss, ax
ret

4 changes: 3 additions & 1 deletion Kernel/Include/Asm/Asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
bool interrupts_enabled();
void loadIDT(idtr_t *idtr);
void halt(void);
void hcf(void);
__attribute__((noreturn)) void hcf(void);
void sti(void);
void cli(void);

extern void reloadSegments(void);
6 changes: 5 additions & 1 deletion Kernel/Include/Kernel/Panic.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#pragma once

void panic(char *message);
/**
@brief Panics the kernel
@param message The message to put in kernel panic, must be non null.
*/
__attribute__((noreturn)) void panic(char message[static 1]);
7 changes: 7 additions & 0 deletions Kernel/Include/Kernel/Stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#define DUMP_STACK_ENTRIES_MAX 20

/**
@brief Dumps the stack into console
*/
void dumpStack(void);
15 changes: 7 additions & 8 deletions Kernel/Kernel/Panic.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
#include <Framebuffer/Framebuffer.h>
#include <Kernel/Panic.h>
#include <LibK/stdio.h>
#include <stdint.h>
#include <meta.h>

struct stackframe {
struct stackframe *rbp;
uint64_t rip;
};
#include <Kernel/Stack.h>

// TODO: Unwind the stack and get a cool stack trace :)
void panic(char *message) {
void panic(char message[static 1]) {
framebuffer_clear(0xcccccc, 0xc40404);
kprintf(
"BlobOS encountered an error which it couldn't recover from.\nPlease "
"file an issue to https://github.com/RedsonBr140/BlobOS/issues "
"with detailed information.\nYou will need to restart your "
"computer.\n");

kprintf("\nError message: %s\n", message);
kprintf("\nVersion: %s\n", GIT_VERSION);
kprintf("Error message: %s\n", message);

dumpStack();
hcf();
}
39 changes: 39 additions & 0 deletions Kernel/Kernel/Stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <Kernel/Stack.h>
#include <LibK/stdio.h>
#include <stdint.h>

#if defined(__x86_64__)
void dumpStack(void) {
kprintf("Stacktrace:\n");
uint64_t rbp = 0;

__asm__ volatile("mov %%rbp, %0" : "=r"(rbp));

for (uint64_t frame = 0; rbp && frame < DUMP_STACK_ENTRIES_MAX; ++frame) {
kprintf(" [%p]\n", rbp);
rbp += sizeof(void *);
}
}

#elif defined(__i386__)
/* NOTE: NOT TESTED. */

struct stackframe {
struct stackframe *ebp;
uint64_t rip;
};

void dumpStack(void) {
kprintf("Stacktrace:\n");
struct stackframe *stackFrame;
asm("movl %%ebp,%0" : "=r"(stackFrame)::);
for (uint64_t frame = 0; stackFrame && frame < PANIC_STACK_ENTRIES_MAX;
++frame) {

kprintf(" [%p]\n", stackFrame->rip);
stackFrame = stackFrame->ebp;
}
}
#else
void dumpStack(void) { kprintf("Stacktrace unavaliable.\n"); }
#endif

0 comments on commit 7b32228

Please sign in to comment.