In our ever more abstracted and open systems,
it's becoming increasingly important to be able to inspect code more effectively.
A very useful technique for this, is to trigger your debugger from code.
I.E. rather than setting arbitrarily complex breakpoints in your debugger,
one can use the following simple triggers in various languages.
Language | Trigger |
C | asm("int $0x3\n"); |
Python | import pdb; pdb.set_trace() |
Javascript | debugger |
These will pass control to your debugger where you can inspect or modify the current context and continue.
- To catch the C trigger, your program must be running in a debugger, which I usually do like gdb --tui --args .... If not running under a debugger your program will generally dump core and exit. Note that this asm() is x86 specific, and if you want a more portable equivalent, see the debugbreak.h wrapper.
- The Javascript trigger is ignored unless something like firebug is enabled. To support debugging a page on a mobile device for example, one can use webkit remote debugging
- The Python trigger will start an interactive console. To support debugging a service not connected to a console, one can easily export the debug session over the network with a helper like rdb. Note one can also trigger pdb after an uncaught exception. Note python since 3.7 has a more concise breakpoint() equivalent, which is less coupled to the use of pdb.
© Feb 24 2011