如何让调试器在D语言抛出异常时自动中断?
I totally get the frustration here—debugging D code that crashes from an uncaught exception can feel like hitting a wall. Here's the core problem: when an uncaught exception fires, D's runtime fully unwinds the stack first before printing a stack trace. So by the time you fire up GDB and run bt, you're stuck with that useless "No stack" message.
The stack trace it does print only shows the line where the exception was thrown, but that's rarely enough—especially if the exception only pops up after thousands of iterations. You have no clue about the chain of calls that led to that point, making it nearly impossible to trace back the root cause.
The solution? Configure your debugger to break the second any exception is thrown, before the stack gets unwound. For GDB (the most common debugger used with D), here's how you do it:
- D's runtime relies on a function called
_d_throwExceptionto handle throwing exceptions (double-check your specific D implementation's documentation if this doesn't match your setup). - In GDB, set a breakpoint on this function with the command:
break _d_throwException - Run your program normally. Now GDB will pause execution the moment an exception is triggered, before any stack unwinding occurs. You can then run
btto get a complete stack trace that shows every step leading up to the exception—exactly the context you need to fix the issue.
This small tweak turns a nearly impossible debugging scenario into something manageable, especially for those tricky heisenbugs that only show up after repeated runs or complex execution paths.
内容的提问来源于stack exchange,提问作者Ray

