Hack #59. Customize the Debugger
Write your own debugger commands.
Adding a command to the debugger (or modifying an existing one) by editing the debugger is a difficult job; to do this, you have to patch the debugger source in perl5db.pl and replace it. Sometimes you don't have the necessary privileges to do this, and given the complexity of the debugger, it's a difficult job—especially because you can't debug the debugger.
Yet modifying your tools the way you want them is important. Fortunately, Devel::Command module makes this much simpler. With Devel::Command, you write simple modules to define your commands, and the debugger finds them and loads them for you automatically.
The Hack
Writing a command is simple. There are only a few things to remember:
- Input and output
The debugger reads input from
DB::INand writes toDB::OUT. If you want your command to work just like a native debugger command, you need to use these filehandles for input and output. Generally, you'll only need to print toDB::OUT.- Debugger context versus program context
To evaluate an expression in the context of the program that's being debugged (for example, you want to pass the value of a variable in the program to your command), call the subroutine
&evalon it. To evaluate something in the debugger's context, use plain oldeval.
A "hello, world" command looks like:
package Devel::Command::HelloWorld;
use base 'Devel::Command';
sub command
{
print DB::OUT "Hello world!\\n";
1;
}
1;
Devel::Command defaults to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access