don't click here

MD Debugger and Error Handler

Discussion in 'Engineering & Reverse Engineering' started by vladikcomper, Jul 3, 2023.

  1. vladikcomper

    vladikcomper

    Tech Member
    205
    134
    43
    Sonic Warped
    Wow, I just realized I never posted version 2.x on Sonic Retro...

    Let me introduce you one of the most feature-rich autonomous debuggers and error handlers for the Mega-Drive:

    MD Debugger and Error Handler 2.5

    [​IMG] [​IMG]

    MD Debugger and Error Handler
    (or simply "MD Debugger", also known as "The Advanced Error Handler and Debugger") is a ready-to-use error handler that comes with a powerful integrated debugger. It aims to provide robust and extensible debugging tools built-in directly into the Mega-Drive ROM, that can be used anywhere (from emulators to the real hardware).

    Currently, it targets The AS Macroassembler and ASM68K assemblers. It has installation instructions and full support for the mainline Sonic disassemblies, but it can be integrated into any AS or ASM68K project (both the error handler and debugger) or even any pre-existing ROM (error handler only in a binary form).

    Features

    [​IMG]

    • Debug symbol support.
      • It can extract symbols from AS and ASM68K at build time and bundle them with the ROM;
      • Debug symbols are efficiently compressed to save space and stored in custom database-like format; they are not visible as plain-text.
      • Error dumps will display symbols from your source code instead of raw offsets, making debugging crashes times more easier;

    • Backtrace support, caller guessing and more.
      • Press the B button on the exception screen to display the backtrace and see the full call chain that led to the exception;
      • Press the A button to display which symbols address registers point to;
      • Generic exception screen displays a caller out-of-the box.

    • Detailed and informative exceptions.
      • Generic exception screen is as detailed and informative as possible;
      • See exception location, caller address, all the main CPU registers, stack dump and more;
      • Additional details can be displayed like VInt and HInt addresses (if dynamic) as well as USP and SR registers.

    • Easily write your own debuggers with "high-level" macros.
      • Write your own debug programs to display what you need;
      • Use "high-level" macros that debugger environment provides, like Console.Write "My d0 is %<.w d0 hex>";
      • Formatted string syntax in the debugger is extremely powerful: display any value from any memory location as: hexadecimal, decimal, binary, signed, unsigned, symbol or even a null-terminated ASCII string. Control output by modifying colors or adding line breaks.

    • Throw custom exceptions and customize error handling.
      • You can throw custom exceptions at any time using RaiseError macro;
      • Use your own debug programs in exceptions if needed;
      • Customize generic exception screen showing or hiding some less frequently used exception details;
      • Map your debug programs to buttons on the exception screen.

    • Assertions.
      • Use one of the most powerful debugging techniques and take advantage of self-testing code!
      • Assertions, widely adopted by many high-level languages, are provided by the debugger out-of-the box;
      • Use assert pseudo-instruction that is only compiled in DEBUG builds. This means zero run-time cost for your final (RELEASE) builds to implement self-testing code.

    • KDebug integration for logging, breakpoints and cycle-counting (experimental).
      • Display formatted strings at any point straight in your emulator's debug console!
      • Use a similar "high-level" macro interface as in console programs (KDebug.WriteLine instead of Console.WriteLine), but without interrupting your programs;
      • Currently, the only emulators to support KDebug are Gens KMod and Blastem-nightly;
      • Create manual breakpoints with KDebug.BreakPoint;
      • Measure your code performance using KDebug.StartTimer and KDebug.EndTimer.

    • Easy to install and extremely lightweight.
      • Error handler blob is below 3 KiB, which is quite small for the number of features it provides; optional debugger extensions take a few hundreds of bytes each;
      • It's quite easy to install, with installation instructions and ready configurations provided for the most mainline Sonic disassemblies.

    Installation instructions
    If you'd like to contribute new installation instructions or update the existing ones, feel free to open a Pull Request: https://github.com/vladikcomper/md-modules/pulls

    Version 2.0 to 2.5 migration guide

    If you were already using version 2.0, migration is really simple as the new version is 100% backwards compatible.
    1. Download the correct version for your project (errorhandler-as.7z or errorhandler-asm68k.7z).
    2. Replace the old files; remove ErrorHandler.bin from the old installation because it's now inlined into ErrorHandler.asm.
    3. Download and replace convsym utility for your platform:
    4. Everything should work already, but to support features like assertions and KDebug integration you need to modify your build system to generate DEBUG builds. Just find an installation guide for your disassembly and follow the "Install ConvSym to generate debug symbols" step.

    Documentation and help

    Guides
    References

    Source code

    MD Debugger and Error Handler is fully open-source. The source code is available in this monorepo: https://github.com/vladikcomper/md-modules
     
  2. Sonic Hachelle-Bee

    Sonic Hachelle-Bee

    Taking a Sand Shower Tech Member
    809
    203
    43
    Lyon, France
    Sonic 2 Long Version
    I am installing this right now! This update looks incredible.
    Thank you for your awesome work!

    I have one additional request (the cherry on the cake, the holy grail). Is there a possibility (in the future) to integrate Mega Everdrive USB / Serial feature as input/output for the debug console? I can display variables and debug information through the serial link already, but using your awesome debugger certainly will make more sense.
     
    Last edited: Jul 3, 2023
  3. vladikcomper

    vladikcomper

    Tech Member
    205
    134
    43
    Sonic Warped
    As long as its interface is about pushing characters to some kind of port/memory address, it relatively easy to do! Debugger's core is so high-level, it actually implements buffered IO. This means that to output to different devices, be it VDP or KDebug registers, you just need to implement a "buffer flush" function.

    You can take a look at this commit where I implemented `KDebug.WriteLine` for the reference: https://github.com/vladikcomper/md-modules/commit/c70c22d85b36cb7c99833f388e7a957972b9c689. Extending it can be surprisingly easy: just copy-paste the case and rewrite `@FlushBuffer` function to conform your target interface (and `_FlushLine`/`_BreakLine` optionally, if you need `.BreakLine` call).

    Under the hood, both `KDebug.WriteLine` and `Console.WriteLine` use almost the same boilerplate code for setting things up: they call `FormatString` function that does buffered IO and pass their own `@FlushBuffer` callback to flush the output to the target device (`Console` or `KDebug`).
     
  4. JGMR

    JGMR

    Member
    5
    0
    1
    It’s been five years…

    (Copypaste from SSRG)

    Awesome new version. Already got this new version put in my own hacks replacing the old 2.0 version. This has been a well five years worth of wait! I've been using this error handler way back since version 2.0 came out in 2018, and I'm ecstatic that this new update has gone full-circle! Congrats!
     
  5. XPointZPoint

    XPointZPoint

    That's no good! Member
    56
    13
    8
    this is some outstanding work. never thought i would be hyped over an exception screen, but here we are.
     
  6. Hivebrain

    Hivebrain

    Administrator
    3,049
    162
    43
    53.4N, 1.5W
    Github
    Very nice work, I've added it to Sonic 1-squared. The only issue I had is that "dec" is a Z80 macro in axm68k, so I renamed the constant in Debugger to "deci".