Skip to content

Commit

Permalink
adding basic stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Jul 20, 2024
1 parent e55dfbc commit 94334f7
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 12 deletions.
37 changes: 37 additions & 0 deletions .gdb_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
target remote :1234
clear
c
q
target remote :1234
b stack_trace.stackTrace
b stk.stackTrace
b stk.stk.stackTrace
b stack_trace.stackTrace
b stack_trace.stack_trace.stackTrace
b panic.kpanic
q
q
target remote :1234
b stk.stackTrace
b stack_trace.stackTrace
q
target remote :1234
b stk.stackTrace
b stack_trace.stackTrace
c
n
n
n
n
n
n
q
target remote :1234
b stack_trace.stackTrace
c
n
n
n
n
n
q
7 changes: 3 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ pub fn build(b: *std.Build) void
const kernel = b.addExecutable(.{
.name = "kernel.elf",
.root_source_file = .{ .src_path = .{ .owner = b, .sub_path = "sources/kernel/kmain.zig" } },
.target = b.resolveTargetQuery(.{
.target = b.resolveTargetQuery(.{
.cpu_arch = .x86,
.abi = .none,
.abi = .gnu,
.os_tag = .freestanding,
}),
.optimize = .Debug,
.strip = true,
//.strip = true,
.code_model = .kernel,
.pic = false,
.error_tracing = false,
});
kernel.setLinkerScriptPath(.{ .src_path = .{ .owner = b, .sub_path = "linker.ld" } });

Expand Down
15 changes: 15 additions & 0 deletions linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ SECTIONS
.rodata : ALIGN(4K)
{
*(.rodata)
__debug_info_start = .;
KEEP(*(.debug_info))
__debug_info_end = .;
__debug_abbrev_start = .;
KEEP(*(.debug_abbrev))
__debug_abbrev_end = .;
__debug_str_start = .;
KEEP(*(.debug_str))
__debug_str_end = .;
__debug_line_start = .;
KEEP(*(.debug_line))
__debug_line_end = .;
__debug_ranges_start = .;
KEEP(*(.debug_ranges))
__debug_ranges_end = .;
}

.data : ALIGN(4K)
Expand Down
4 changes: 2 additions & 2 deletions sources/drivers/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ pub fn initDrivers() void
pub fn shutdownDrivers() void
{
@setCold(true);
kernel.logs.klog("[Drivers Manager] unloading drivers...");
kernel.logs.klog("[Drivers Manager] unloaded all drivers");
kernel.logs.klogln("[Drivers Manager] unloading drivers...");
kernel.logs.klogln("[Drivers Manager] unloaded all drivers");
}
1 change: 1 addition & 0 deletions sources/drivers/vga/vga.zig
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub fn init(title : []const u8, title_color : u8, navbar_color : u8, triggered_c
updateCursor();
updateNavbar();
kernel.logs.klogln("[VGA Driver] loaded");
kernel.stk.stackTrace(42);
}

fn putEntry(c: u8, color: u8, x: usize, y: usize) void
Expand Down
1 change: 1 addition & 0 deletions sources/kernel/arch/x86/boot.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export fn _start() align(16) linksection(".text.boot") callconv(.Naked) noreturn
asm volatile
(
\\ movl %[stk], %esp
\\ xor %ebp, %ebp
\\ call kmain
:
: [stk] "{ecx}" (@intFromPtr(&kernel_stack) + @sizeOf(@TypeOf(kernel_stack))),
Expand Down
1 change: 1 addition & 0 deletions sources/kernel/kmain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const libk = @import("libk");

pub const logs = @import("log.zig");
pub const kpanic = @import("panic.zig").kpanic;
pub const stk = @import("stack_trace.zig");

pub const arch = if(!is_test) switch(builtin.cpu.arch)
{
Expand Down
2 changes: 2 additions & 0 deletions sources/kernel/panic.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const vga = @import("drivers").vga;
const arch = @import("kmain.zig").arch;
const logs = @import("log.zig");
const stk = @import("stack_trace.zig");

pub fn kpanic(message: []const u8) noreturn
{
@setCold(true);
vga.setColor(vga.Color.WHITE, vga.Color.RED);
vga.clear(vga.Color.RED);
vga.putString(logs.getLogBuffer());
stk.stackTrace(42);
vga.putString("\nkernel panic : ");
vga.putString(message);
vga.putString("\n[cannot recover, freezing the system]");
Expand Down
27 changes: 27 additions & 0 deletions sources/kernel/stack_trace.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const libk = @import("libk");

const StackFrame = packed struct
{
esp: ?*StackFrame,
eip: u32
};

pub fn stackTrace(max_frame_stack: usize) void
{
var stk: ?*StackFrame = null;
asm volatile
(
\\ movl %ebp, %eax
: [stk] "={eax}" (stk)
:
: "memory"
);
libk.io.kputs("================= Stack Trace ================= \n");
var frame: usize = 0;
while(stk.?.esp != null and frame < max_frame_stack) : (frame += 1)
{
libk.io.kprintf("fn 0x{} {}()\n", .{ stk.?.eip, "??" });
stk = stk.?.esp;
}
libk.io.kputs("=============== End Stack Trace =============== \n");
}
28 changes: 22 additions & 6 deletions sources/libk/io/out.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub fn kputs(message: []const u8) void
const ArgTypes = enum
{
Int,
Bool,
Float,
Char,
String,
Expand Down Expand Up @@ -41,7 +42,7 @@ pub fn kprintf(comptime fmt: []const u8, args: anytype) void
if(args[arg_idx] > 0 and args[arg_idx] < 256)
vga.putChar(args[arg_idx])
else
putNb(args[arg_idx]);
kputNb(args[arg_idx]);
}
else if(@typeInfo(@TypeOf(args[arg_idx])) == .Array and @typeInfo(@TypeOf(args[arg_idx])).Array.child == u8)
kputs(args[arg_idx])
Expand All @@ -59,15 +60,29 @@ pub fn kprintf(comptime fmt: []const u8, args: anytype) void
else switch(@TypeOf(args[arg_idx]))
{
i8, u8, => vga.putChar(args[arg_idx]),
i16, u16, i32, u32, i64, u64, isize, usize => putNb(args[arg_idx]),
i16, u16, i32, u32, i64, u64, isize, usize => kputNb(args[arg_idx]),
f16, f32, f64, comptime_float => {},
bool =>
{
if(args[arg_idx])
kputs("true")
else
kputs("false");
},
else => @compileError("could not manage auto detected type : " ++ @typeName(@TypeOf(args[arg_idx])) ++ "; please add type identifier between brackets"),
}
}
switch(arg_type)
{
.Bool =>
{
if(args[arg_idx])
kputs("true")
else
kputs("false");
},
.Char => vga.putChar(args[arg_idx]),
.Int => putNb(args[arg_idx]),
.Int => kputNb(args[arg_idx]),
.Float => {},
.String => kputs(args[arg_idx]),
.Pointer => { kputs("0x"); kputs(string.toStringBase(@intFromPtr(args[arg_idx]), 16)); },
Expand All @@ -84,6 +99,7 @@ pub fn kprintf(comptime fmt: []const u8, args: anytype) void
{
switch(c)
{
'b' => arg_type = .Bool,
'c' => arg_type = .Char,
'i' => arg_type = .Int,
'f' => arg_type = .Float,
Expand All @@ -105,7 +121,7 @@ pub fn kprintf(comptime fmt: []const u8, args: anytype) void
}
}

pub fn putNb(nbr: i64) void
pub fn kputNb(nbr: i64) void
{
if(nbr <= -2147483648)
vga.putString("-2147483648")
Expand All @@ -114,11 +130,11 @@ pub fn putNb(nbr: i64) void
else if(nbr < 0)
{
vga.putChar('-');
putNb(-nbr);
kputNb(-nbr);
}
else if(nbr >= 10)
{
putNb(@divFloor(nbr, 10));
kputNb(@divFloor(nbr, 10));
vga.putChar(@intCast(@mod(nbr, 10) + @as(u8, 48)));
}
else
Expand Down

0 comments on commit 94334f7

Please sign in to comment.