From b553da4d102089da41d1d8303474999d51b4881f Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Sun, 13 Jul 2014 14:44:05 -0700 Subject: [PATCH] Implement Dump and Registers commands --- sys/amd64/debug.c | 28 ++++++++++++++++++++++++ sys/kern/debug.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/sys/amd64/debug.c b/sys/amd64/debug.c index b560df3..92e0fb6 100644 --- a/sys/amd64/debug.c +++ b/sys/amd64/debug.c @@ -6,6 +6,8 @@ #include #include +#include "amd64.h" +#include "amd64op.h" #include "trap.h" TrapFrame *frames[MAX_CPUS]; @@ -25,5 +27,31 @@ Debug_Breakpoint(TrapFrame *tf) void Debug_Registers(int argc, const char *argv[]) { + TrapFrame *tf = frames[CPU()]; + + kprintf("Interrupt %d Error Code: %016llx\n", + tf->vector, tf->errcode); + kprintf("cr0: %016llx cr2: %016llx\n", + read_cr0(), read_cr2()); + kprintf("cr3: %016llx cr4: %016llx\n", + read_cr3(), read_cr4()); + kprintf("dr0: %016llx dr1: %016llx dr2: %016llx\n", + read_dr0(), read_dr1(), read_dr2()); + kprintf("dr3: %016llx dr6: %016llx dr7: %016llx\n", + read_dr3(), read_dr6(), read_dr7()); + kprintf("rip: %04x:%016x rsp: %04x:%016x\n", + tf->cs, tf->rip, tf->ss, tf->rsp); + kprintf("rflags: %016x ds: %04x es: %04x fs: %04x gs: %04x\n", + tf->rflags, read_ds(), read_es(), read_fs(), read_gs()); + kprintf("rax: %016llx rbx: %016llx rcx: %016llx\n", + tf->rax, tf->rbx, tf->rcx); + kprintf("rdx: %016llx rsi: %016llx rdi: %016llx\n", + tf->rdx, tf->rsi, tf->rdi); + kprintf("rbp: %016llx r8: %016llx r9: %016llx\n", + tf->rbp, tf->r8, tf->r9); + kprintf("r10: %016llx r11: %016llx r12: %016llx\n", + tf->r10, tf->r11, tf->r12); + kprintf("r13: %016llx r14: %016llx r15: %016llx\n", + tf->r13, tf->r14, tf->r15); } diff --git a/sys/kern/debug.c b/sys/kern/debug.c index 460a9be..5b76e73 100644 --- a/sys/kern/debug.c +++ b/sys/kern/debug.c @@ -77,12 +77,56 @@ Debug_PrintHex(const char *data, size_t length, off_t off, size_t limit) } } +uint64_t +Debug_StrToInt(const char *s) +{ + int i = 0; + int base = 10; + uint64_t val = 0; + + if (s[0] == '0' && s[1] == 'x') + { + base = 16; + i = 2; + } + + while (s[i] != '\0') { + if (s[i] >= '0' && s[i] <= '9') { + val = val * base + (uint64_t)(s[i] - '0'); + } else if (s[i] >= 'a' && s[i] <= 'f') { + if (base != 16) + kprintf("Not base 16!\n"); + val = val * base + (uint64_t)(s[i] - 'a' + 10); + } else if (s[i] >= 'A' && s[i] <= 'F') { + if (base != 16) + kprintf("Not base 16!\n"); + val = val * base + (uint64_t)(s[i] - 'A' + 10); + } else { + kprintf("Not a number!\n"); + } + i++; + } + + return val; +} + +uint64_t +Debug_SymbolToInt(const char *s) +{ + if (*s >= '0' || *s <= '9') + return Debug_StrToInt(s); + + kprintf("Unknown symbol '%s'\n"); + return 0; +} + #define PHELP(_cmd, _msg) kprintf("%-16s %s\n", _cmd, _msg) void Debug_Help(int argc, const char *argv[]) { PHELP("dump", "Dump a region of memory"); + PHELP("registers", "Show CPU registers"); PHELP("help", "Display the list of commands"); } @@ -101,6 +145,14 @@ Debug_Echo(int argc, const char *argv[]) void Debug_Dump(int argc, const char *argv[]) { + uint64_t off, len; + if (argc == 3) + { + off = Debug_SymbolToInt(argv[1]); + len = Debug_SymbolToInt(argv[2]); + } + kprintf("Dump 0x%llx 0x%llx\n", off, len); + Debug_PrintHex((const char *)off, len, 0, len); } void @@ -136,6 +188,8 @@ Debug_Prompt() Debug_Help(argc, argv); } else if (strcmp(argv[0], "dump") == 0) { Debug_Dump(argc, argv); + } else if (strcmp(argv[0], "registers") == 0) { + Debug_Registers(argc, argv); } else if (strcmp(argv[0], "echo") == 0) { Debug_Echo(argc, argv); } else if (strcmp(argv[0], "") != 0) {