Unverified Commit 9c4f6eac authored by PhoeniX's avatar PhoeniX

Stacktrace support

parent fd21fe66
......@@ -210,6 +210,7 @@ _efi_start: # EFI
x64_entry:
call reloc_vtables
call static_init
xor %rbp, %rbp
call _ZN9Pagetable4initEv
call _ZN7Display5setupEv
call _ZN10Interrupts4initEv
......
......@@ -158,6 +158,7 @@ struct intcb_regs {
typedef bool intcb(uint32_t intr, uint32_t code, intcb_regs *regs);
class Process;
class Interrupts {
private:
static List<intcb*> *callbacks;
......@@ -171,7 +172,7 @@ class Interrupts {
static uint64_t handle(uint8_t intr, uint64_t stack, uint64_t *cr3);
public:
static void print(uint8_t num, intcb_regs *regs, uint32_t code);
static void print(uint8_t num, intcb_regs *regs, uint32_t code, const Process *process = nullptr);
static void maskIRQ(uint16_t mask);
static uint16_t getIRQmask();
static void addCallback(uint8_t intr, intcb* cb);
......
......@@ -4,6 +4,7 @@
#include "interrupts.hpp"
#include "acpi.hpp"
#include "pagetable.hpp"
#include "process.hpp"
INTERRUPT64 *Interrupts::idt = 0;
GDT *Interrupts::gdt = 0;
......@@ -158,7 +159,7 @@ struct int_info {
uint64_t rip, cs, rflags, rsp, ss;
} PACKED;
void Interrupts::print(uint8_t num, intcb_regs *regs, uint32_t code) {
void Interrupts::print(uint8_t num, intcb_regs *regs, uint32_t code, const Process *process) {
uint64_t cr2;
asm volatile("mov %%cr2, %0":"=a"(cr2));
uint32_t cpuid = ACPI::getController()->getCPUID();
......@@ -197,6 +198,8 @@ void Interrupts::print(uint8_t num, intcb_regs *regs, uint32_t code) {
regs->rax, regs->rcx, regs->rdx, regs->rbx,
regs->r8, regs->r9, regs->r10, regs->r11,
regs->r12, regs->r13, regs->r14, regs->r15);
Process::print_stacktrace(regs->rbp, process);
}
uint64_t __attribute__((sysv_abi)) Interrupts::handle(
......
......@@ -48,8 +48,10 @@ class Process {
uintptr_t linkLibrary(const char* funcname);
void writeData(uintptr_t address, void* src, size_t size);
void readData(void* dst, uintptr_t address, size_t size);
char* readString(uintptr_t address);
void readData(void* dst, uintptr_t address, size_t size) const;
char* readString(uintptr_t address) const;
void* getPhysicalAddress(uintptr_t ptr) PURE;
void* getPhysicalAddress(uintptr_t ptr) const PURE;
static void print_stacktrace(uintptr_t base = 0, const Process *process = nullptr);
};
......@@ -174,7 +174,7 @@ void Process::writeData(uintptr_t address, void* src, size_t size) {
address += count;
}
}
void Process::readData(void* dst, uintptr_t address, size_t size) {
void Process::readData(void* dst, uintptr_t address, size_t size) const {
char *ptr = static_cast<char*>(dst);
while (size > 0) {
void *src = getPhysicalAddress(address);
......@@ -186,7 +186,7 @@ void Process::readData(void* dst, uintptr_t address, size_t size) {
address += count;
}
}
char *Process::readString(uintptr_t address) {
char *Process::readString(uintptr_t address) const {
size_t length = 0;
const char *src = static_cast<const char*>(getPhysicalAddress(address));
size_t limit = 0x1000 - (uintptr_t(src) & 0xFFF);
......@@ -203,7 +203,7 @@ char *Process::readString(uintptr_t address) {
readData(buf, address, length + 1);
return buf;
}
void *Process::getPhysicalAddress(uintptr_t ptr) {
void *Process::getPhysicalAddress(uintptr_t ptr) const {
if (pagetable == 0)
return 0;
uintptr_t off = ptr & 0xFFF;
......@@ -292,3 +292,34 @@ void Process::exit(int code) {
}
delete this;
}
void Process::print_stacktrace(uintptr_t base, const Process *process) {
struct stackframe {
struct stackframe* rbp;
uintptr_t rip;
} __attribute__((packed));
printf("STACK TRACE:");
struct stackframe tmpframe;
const struct stackframe *frame;
if (base) {
frame = reinterpret_cast<struct stackframe*>(base);
} else {
asm volatile("mov %%rbp, %q0":"=r"(frame)::);
}
size_t lim = 10;
while (lim-- && frame != NULL) {
if (process) {
process->readData(&tmpframe.rbp, uintptr_t(frame), sizeof(uintptr_t));
if (tmpframe.rbp) {
process->readData(&tmpframe.rip, uintptr_t(frame)+sizeof(uintptr_t), sizeof(uintptr_t));
} else {
break;
}
frame = &tmpframe;
}
printf(" [%p]:%p", frame->rbp, reinterpret_cast<void*>(frame->rip));
frame = frame->rbp;
}
printf("\n");
}
......@@ -103,7 +103,7 @@ bool ProcessManager::HandleFault(
processSwitchMutex.release();
LeaveCritical(t);
if (!thread) return false;
Interrupts::print(intr, regs, code);
Interrupts::print(intr, regs, code, thread->process);
delete thread->process;
delete thread;
if (SwitchProcess(regs))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment