The Perl debugger has two parts - one in the Perl internals, which hooks into the optree walk that happens during the execution of your program, and the other in perl5db.pl, found in your Perl library.
perl5db.pl does all the work of tracking breakpoints, handling commands, and executing code in the context of the programming being debugged.
Here's how a breakpoint works:
- You run your program under the debugger by adding the -d flag.
- The debugger is loaded and initialized. Perl switches the nextops to dbnexts.
- The debugger initializes, and prints its prompt, starting a loop in DB::DB that reads, executes, and prompts.
- You ask to add a breakpoint via its b command.
- The debugger stores this in the hash %<_yourscript.pl (yes, that's the real name of the hash; inside the debugger it's aliased to %dbline). The keys are line numbers, and the values are 1 if the line is considered breakable by the Perl core, and 0 if not. Any true value stored in %dbline sets a breakpoint for that line (assuming the line is breakable).
- You enter 'c' to continue to the next breakpoint. The debugger sets the variable $DB::single to 0, causing the Perl core to simply move on to the next op without calling the debugger as long as %dbline{$current_line} doesn't exist.
- When the core reaches a breakpoint, it sets $DB::singleback to 1, causing the core to call DB::DB (the debugger's read/execute/print core) just before every line again. You're back in single-step mode.
So the debugger's really just a special Perl program that sets up specific subroutines and watches specific variables shared with the Perl core that contain the source code and breakpoints. This allows the same core hooks to be used for all kinds of things: debuggers, tracers, test coverage, profiling.
I've skipped over subroutine calls and other complications like eval; if you're interested in how it works, you may find reading perl5db.pl more profitable. (I wrote the internal docs and comments for the debugger, which is why I'm holding forth at such length about it here.)