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 jobespecially 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::IN and writes to DB::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 to DB::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 &eval on it. To evaluate something in the debugger's context, use plain old eval.

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 using the command( ) as the actual command code. Run this by putting it somewhere in your @INC and then start the debugger:

flatbox ~ $ perl -de0 Default die handler restored. Patching with Devel::Command::DBSub::DB_5_8 Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or \Qh h' for help, or \Qman perldebug' for more help. main::(-e:1):    0   DB<1> cmds cmds helloworld   DB<2> helloworld Hello world!   DB<3> q flatbox ~ $

The message that begins Patching with... lets you know that Devel::Command has successfully activated. cmds lists the commands and typing helloworld runs your command.

Overriding a debugger command

Overriding a command is simple: just return true if your command routine wants to handle the command or false if you don't.

package Devel::Command::X; use base 'Devel::Command'; sub command {     my ($cmd) = @_;     if ($cmd  =~ /x marks/)     {         print DB::OUT "Arrrrr....\\n";         return 1;     }     else     {         return 0;     } } 1;

Now the x command knows to be piratical when it sees a command beginning with x marks.

flatbox ~ $ perl -de0 Default die handler restored. Patching with Devel::Command::DBSub::DB_5_8 Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or \Qh h' for help, or \Qman perldebug' for more help. main::(-e:1):    0   DB<1> $x = [1,2,3]   DB<2> x $x 0  ARRAY(0x804e2f4)    0  1    1  2    2  3   DB<3> x marks the spot Arrrrr....   DB<4> q flatbox ~ $

Running the Hack

Create a module in the Devel::Command:: namespace. Install Devel::Command from CPAN, and then tell the debugger to load it by adding one line to your debugger initialization file, .perldb (or perldb.ini, for non-Unix systems):

use Devel::Command;

That's it. This makes the debugger automatically search @INC for modules in the Devel::Command:: namespace, load them, and install them as commands. By default, it picks a name for the command by downcasing the last namespace qualifier (so, for example, Devel::Command::My::DoStuff ends up as the dostuff command).

Devel::Command also installs its own cmds command, which lists all commands that it found and loaded, and dynamically patches the debugger's command processing subroutine with a modified version which knows how to find the commands installed by Devel::Command.

Hacking the Hack

To develop tests while using the debugger, try the Devel::Command::Tdump module on CPAN. This module loads Test::More for you and lets you actually write tests and save them from the debugger.

If you want to see drawings of your data structure in the debugger, Devel::Command::Viz and the graphviz package will let you do it. Install those, then use the viz command on a variable:

flatbox ~ $ perl5.8.5 -de0 Patching with Devel::Command::DBSub::DB_5_8_5 Loading DB routines from perl5db.pl version 1.27 Editor support available. Enter h or \Qh h' for help, or \Qman perldebug' for more help. main::(-e:1):    0   DB<1> use WWW::Mechanize   DB<2> $m = WWW::Mechanize->new( )   DB<3> viz $m             

You'll see a graphical depiction of the WWW::Mechanize object in a pop-up window.



Perl Hacks
Perl Hacks: Tips & Tools for Programming, Debugging, and Surviving
ISBN: 0596526741
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net