print registers on fatal exception

This commit is contained in:
trimill 2024-02-03 21:40:17 -05:00
parent 361561c5f4
commit 22df3f473c
No known key found for this signature in database
GPG key ID: 4F77A16E17E10BCB
4 changed files with 119 additions and 108 deletions

View file

@ -4,69 +4,8 @@
#include <serial.h>
#include "debugger.h"
struct dr6 {
uint64_t b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
: 7,
bld : 1,
: 1,
bd : 1,
bs : 1,
bt : 1,
rtm : 1,
: 47;
};
struct dr7 {
uint64_t l0 : 1,
g0 : 1,
l1 : 1,
g1 : 1,
l2 : 1,
g2 : 1,
l3 : 1,
g3 : 1,
: 8,
rw0 : 2,
len0 : 2,
rw1 : 2,
len1 : 2,
rw2 : 2,
len2 : 2,
rw3 : 2,
len3 : 2,
: 32;
};
struct rflags {
uint64_t cf : 1,
: 1,
pf : 1,
: 1,
af : 1,
: 1,
zf : 1,
sf : 1,
tf : 1,
if_ : 1,
df : 1,
of : 1,
iopl : 2,
nt : 1,
md : 1,
rf : 1,
vm : 1,
ac : 1,
vif : 1,
vip : 1,
id : 1,
: 42;
};
#include "registers.h"
#include "idt.h"
struct breakpoint {
uint64_t addr;
@ -138,50 +77,6 @@ static void debugger_msg(int cause, struct dr6 dr6) {
kputs("\n");
}
static void debugger_print_regs(struct isr_regs *state) {
kprintf("rax: %#016lx (%lu)\n", state->rax, state->rax);
kprintf("rbx: %#016lx (%lu)\n", state->rbx, state->rbx);
kprintf("rcx: %#016lx (%lu)\n", state->rcx, state->rcx);
kprintf("rdx: %#016lx (%lu)\n", state->rdx, state->rdx);
kprintf("rsi: %#016lx (%lu)\n", state->rsi, state->rsi);
kprintf("rdi: %#016lx (%lu)\n", state->rdi, state->rdi);
kprintf("rsp: %#016lx (%lu)\n", state->rsp, state->rsp);
kprintf("rbp: %#016lx (%lu)\n", state->rbp, state->rbp);
kprintf("r8 : %#016lx (%lu)\n", state->r8 , state->r8 );
kprintf("r9 : %#016lx (%lu)\n", state->r9 , state->r9 );
kprintf("r10: %#016lx (%lu)\n", state->r10, state->r10);
kprintf("r11: %#016lx (%lu)\n", state->r11, state->r11);
kprintf("r12: %#016lx (%lu)\n", state->r12, state->r12);
kprintf("r13: %#016lx (%lu)\n", state->r13, state->r13);
kprintf("r14: %#016lx (%lu)\n", state->r14, state->r14);
kprintf("r15: %#016lx (%lu)\n", state->r15, state->r15);
kputs("---\n");
kprintf("rip: %#016lx (%lu)\n", state->rip, state->rip);
kputs("---\n");
kprintf("rflags: %#016lx (%lu)\n", state->rflags, state->rflags);
struct rflags *rflags = (struct rflags *)state->rflags;
kputs("rflags: ");
if (rflags->cf) kputs("CF ");
if (rflags->pf) kputs("PF ");
if (rflags->af) kputs("AF ");
if (rflags->zf) kputs("ZF ");
if (rflags->sf) kputs("SF ");
if (rflags->tf) kputs("TF ");
if (rflags->if_) kputs("IF ");
if (rflags->df) kputs("DF ");
if (rflags->of) kputs("OF ");
if (rflags->iopl) kputs("IOPL ");
if (rflags->nt) kputs("NT ");
if (rflags->md) kputs("MD ");
if (rflags->rf) kputs("RF ");
if (rflags->vm) kputs("VM ");
if (rflags->ac) kputs("AC ");
if (rflags->vif) kputs("VIF ");
if (rflags->vip) kputs("VIP ");
if (rflags->id) kputs("ID ");
kputs("\n");
}
#define PROMPT_LEN 60
static int debugger_handle_bkp_cmd(char *msg) {
@ -324,7 +219,7 @@ static int debugger_prompt(struct isr_regs *state) {
dbg_continue = 1;
return 0;
case 'r': // print registers
debugger_print_regs(state);
isr_print_regs(state);
return 1;
case 'k': // backtrace
log_backtrace_ex((void *)state->rip, (void *)state->rbp);

View file

@ -5,6 +5,7 @@
#include "idt.h"
#include "backtrace.h"
#include "debugger.h"
#include "registers.h"
#include "../paging.h"
#include "../bindings.h"
#include "../drivers/pic.h"
@ -73,6 +74,48 @@ void idt_init(void) {
__asm__ volatile ("lidt %0" : : "m"(idtr));
}
void isr_print_regs(struct isr_regs *regs) {
kprintf("rax: %#016lx (%lu)\n", regs->rax, regs->rax);
kprintf("rbx: %#016lx (%lu)\n", regs->rbx, regs->rbx);
kprintf("rcx: %#016lx (%lu)\n", regs->rcx, regs->rcx);
kprintf("rdx: %#016lx (%lu)\n", regs->rdx, regs->rdx);
kprintf("rsi: %#016lx (%lu)\n", regs->rsi, regs->rsi);
kprintf("rdi: %#016lx (%lu)\n", regs->rdi, regs->rdi);
kprintf("rsp: %#016lx (%lu)\n", regs->rsp, regs->rsp);
kprintf("rbp: %#016lx (%lu)\n", regs->rbp, regs->rbp);
kprintf("r8 : %#016lx (%lu)\n", regs->r8 , regs->r8 );
kprintf("r9 : %#016lx (%lu)\n", regs->r9 , regs->r9 );
kprintf("r10: %#016lx (%lu)\n", regs->r10, regs->r10);
kprintf("r11: %#016lx (%lu)\n", regs->r11, regs->r11);
kprintf("r12: %#016lx (%lu)\n", regs->r12, regs->r12);
kprintf("r13: %#016lx (%lu)\n", regs->r13, regs->r13);
kprintf("r14: %#016lx (%lu)\n", regs->r14, regs->r14);
kprintf("r15: %#016lx (%lu)\n", regs->r15, regs->r15);
kprintf("rip: %#016lx (%lu)\n", regs->rip, regs->rip);
kprintf("rflags: %#016lx (%lu)\n", regs->rflags, regs->rflags);
struct rflags *rflags = (struct rflags *)regs->rflags;
kputs("rflags: ");
if (rflags->cf) kputs("CF ");
if (rflags->pf) kputs("PF ");
if (rflags->af) kputs("AF ");
if (rflags->zf) kputs("ZF ");
if (rflags->sf) kputs("SF ");
if (rflags->tf) kputs("TF ");
if (rflags->if_) kputs("IF ");
if (rflags->df) kputs("DF ");
if (rflags->of) kputs("OF ");
if (rflags->iopl) kputs("IOPL ");
if (rflags->nt) kputs("NT ");
if (rflags->md) kputs("MD ");
if (rflags->rf) kputs("RF ");
if (rflags->vm) kputs("VM ");
if (rflags->ac) kputs("AC ");
if (rflags->vif) kputs("VIF ");
if (rflags->vip) kputs("VIP ");
if (rflags->id) kputs("ID ");
kputs("\n");
}
#define EX_DEBUG 0x01
#define EX_BREAKPOINT 0x03
#define EX_PAGE_FAULT 0x0e
@ -138,6 +181,10 @@ void idt_exception_handler(uint64_t exception, uint64_t code, struct isr_regs *s
kprintf("Page fault address: %#016lx\n", cr2);
}
kputs("\n");
isr_print_regs(state);
kputs("\n");
log_backtrace_ex((void *)state->rip, (void *)state->rbp);

View file

@ -27,3 +27,5 @@ struct isr_regs {
};
void idt_init(void);
void isr_print_regs(struct isr_regs *regs);

View file

@ -0,0 +1,67 @@
#pragma once
#include <stdint.h>
struct dr6 {
uint64_t b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
: 7,
bld : 1,
: 1,
bd : 1,
bs : 1,
bt : 1,
rtm : 1,
: 47;
};
struct dr7 {
uint64_t l0 : 1,
g0 : 1,
l1 : 1,
g1 : 1,
l2 : 1,
g2 : 1,
l3 : 1,
g3 : 1,
: 8,
rw0 : 2,
len0 : 2,
rw1 : 2,
len1 : 2,
rw2 : 2,
len2 : 2,
rw3 : 2,
len3 : 2,
: 32;
};
struct rflags {
uint64_t cf : 1,
: 1,
pf : 1,
: 1,
af : 1,
: 1,
zf : 1,
sf : 1,
tf : 1,
if_ : 1,
df : 1,
of : 1,
iopl : 2,
nt : 1,
md : 1,
rf : 1,
vm : 1,
ac : 1,
vif : 1,
vip : 1,
id : 1,
: 42;
};