From cbe66b679885b43e07ed3e6babdef9d796573beb Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Wed, 31 Dec 2014 15:45:22 -0500 Subject: [PATCH] sc / stackcrawl debugger command --- bin/commands.cpp | 108 +++++++++++++++++++++++++++++++++++++---------- bin/debugger.cpp | 2 +- bin/debugger.h | 2 + bin/lexer.rl | 7 +++ bin/parser.lemon | 7 ++- 5 files changed, 102 insertions(+), 24 deletions(-) diff --git a/bin/commands.cpp b/bin/commands.cpp index 2dd3685..df61ec3 100644 --- a/bin/commands.cpp +++ b/bin/commands.cpp @@ -6,6 +6,9 @@ #include #include +#include +#include + #include "debugger.h" #include "debugger_internal.h" @@ -66,6 +69,7 @@ namespace Debug { struct tm *tm; time_t t = OS::MacToUnix(value); + // localtime vs gmtime? tm = ::localtime(&t); strftime(buffer, sizeof(buffer), "%Y-%m-%d %I:%M:%S %p", tm); puts(buffer); @@ -76,30 +80,90 @@ namespace Debug { { puts(""); - puts(" ## "); - puts(" ## ## #### "); - puts(" ## #### ## "); - puts(" ## ## "); - puts(" ## ## ## ## "); - puts(" ## ## #### "); - puts("## ## ## ## "); - puts(" ######## #### ## ## "); - puts(" ## #################### ## "); - puts(" ## ############## ## "); - puts(" #### ############ ## "); - puts(" ###### ###### ## "); - puts(" ###### ## "); - puts(" #### ## "); - puts(" ## ## "); - puts(" ## ################ ## "); - puts(" ## ## ## ## "); - puts(" ## ## ## ## "); - puts(" ## ## ## ## "); - puts(" ## ## ## ## "); - puts(" ## ## ## ## "); - puts(" ###### ###### "); + puts(" ## "); + puts(" ## ## #### "); + puts(" ## #### ## "); + puts(" ## ## "); + puts(" ## ## ## ## "); + puts(" ## ## #### "); + puts(" ## ## ## ## "); + puts(" ######## #### ## ## "); + puts(" ## #################### ## "); + puts(" ## ############## ## "); + puts(" #### ############ ## "); + puts(" ###### ###### ## "); + puts(" ###### ## "); + puts(" #### ## "); + puts(" ## ## "); + puts(" ## ################ ## "); + puts(" ## ## ## ## "); + puts(" ## ## ## ## "); + puts(" ## ## ## ## "); + puts(" ## ## ## ## "); + puts(" ## ## ## ## "); + puts(" ###### ###### "); puts(""); } + + void StackCrawl(void) + { + // stack crawl based on a6 frame pointers. (link/unlink) + // start with current a6. + + uint32_t a6 = cpuGetAReg(6); + + /* + * a6: previous a6 + * a6+4 return pc + * .... parameters, locals, stack data + * previous a6 + * return pc + * .... + */ + // todo -- need a function to verify address is w/in stack range. + + + if (!a6) return; + printf("a6: %08x\n", a6); + + while(a6) + { + + uint32_t prevA6 = ReadLong(a6); + uint32_t pc = ReadLong(a6+4); // + + puts("------------"); + printf("a6: %08x\n", prevA6); + printf("pc: %08x", pc); + + // find the routine name... + for (const auto &kv : SymbolTable) + { + const auto &name = kv.first; + auto range = kv.second; + + // range end may be 0 + if ((pc == range.first) || (pc >= range.first && pc < range.second)) + { + uint32_t offset = pc - range.first; + if (offset) + printf(" %s+$%x", name.c_str(), offset); + else + printf(" %s", name.c_str()); + break; + } + } + puts(""); + + // hexdump contents between pc and previous.... + + + a6 = prevA6; + } + + + } + } \ No newline at end of file diff --git a/bin/debugger.cpp b/bin/debugger.cpp index 594faf9..e267bb8 100644 --- a/bin/debugger.cpp +++ b/bin/debugger.cpp @@ -759,7 +759,7 @@ void PrintRegisters(const BackTraceInfo &i) i.a[4], i.a[5], i.a[6], i.a[7] ); - printf("PC: %08X CSR: %04x %s\n", i.pc, i.csr, srbits); + printf("PC: %08x CSR: %04x %s\n", i.pc, i.csr, srbits); } diff --git a/bin/debugger.h b/bin/debugger.h index 03784ee..46b7bbf 100644 --- a/bin/debugger.h +++ b/bin/debugger.h @@ -147,6 +147,8 @@ void ReadWriteBreak(int32_t address); void PrintError(uint32_t value); void PrintDate(uint32_t value); +void StackCrawl(void); + void ApplyTemplate(int32_t address, const std::string &name); } diff --git a/bin/lexer.rl b/bin/lexer.rl index 80a06da..49e1f9a 100644 --- a/bin/lexer.rl +++ b/bin/lexer.rl @@ -150,6 +150,7 @@ namespace { fgoto ident; }; + *|; main := |* @@ -268,6 +269,12 @@ namespace { Parse(parser, tkDUMP, 0, command); }; + + 'sc'i | 'stackcrawl'i { + Parse(parser, tkSTACKCRAWL, 0, command); + }; + + 'h'i | 'help'i { Parse(parser, tkHELP, 0, command); }; diff --git a/bin/parser.lemon b/bin/parser.lemon index 3797f8b..762f0cb 100644 --- a/bin/parser.lemon +++ b/bin/parser.lemon @@ -162,6 +162,10 @@ stmt ::= LIST expr(a) EOL. Debug::List(a.intValue); } +stmt ::= STACKCRAWL EOL. +{ + Debug::StackCrawl(); +} stmt ::= expr(a) SEMI SEMIH EOL. { @@ -205,12 +209,13 @@ stmt ::= expr(a) SEMI SEMIDATE EOL. Debug::PrintDate(a.intValue); } - stmt ::= expr(a) SEMI SEMIERROR EOL. { Debug::PrintError(a.intValue); } + + stmt ::= expr(a) SEMI SEMIT IDENTIFIER(b) EOL. { Debug::ApplyTemplate(a.intValue, *b.stringValue);