Saving Breakpoints in the Perl Debugger

I'll state right off the bat that I have re-invented the wheel here. However, I did not know that until I asked on Perlmonks and was pointed to the resources that I couldn't find on Google. It was suggested on Perlmonks that a blog post, even for a re-invented wheel might be good, just for the purpose of getting the information out there. So, here it goes.

If you use the Devel::ptkdb debugger, then you know that one of its features is the ability to save breakpoints (and other info) in a *.ptkdb file that is reloaded the next time you run that debugger.

It's a feature I missed on those occasions where I needed to use the default debugger, not Devel::ptkdb. However, after scrounging around what docs there are on the default debugger, I was able to cobble together the following process:

1 - Create a file .perldb in your home directory and add this code to it.

use Cwd;
use FindBin qw($Bin);
use File::Spec::Functions;
sub afterinit {
    for my $cmdfile ( $ENV{PERLDB_MYCOMMANDFILE},
                      catfile(getcwd(), '.perldbinit'),
                      catfile($Bin, '.perldbinit')) {
        if ( -e $cmdfile ) {
            print STDERR "Running commands from $cmdfile...\n";
            open my $cmdh, "<", $cmdfile or
                die "Cannot open $cmdfile:$!";
            push @DB::typeahead, <$cmdh>;
            close $cmdh;
            last;
        }
    }
}

2 - Set the file .perldb to be read/write by you only or else you'll get a warning and it will not be executed.

3 - In the current working directory where you are when you start the debugger, write a .perldbinit file. Here's a sample:


    f Some.pm
    b 75
    f bin/mymainscript
    b 180
    b 192
    L

The f command sets the current scope to the file specified. If the file is not a complete match for the full file in %INC, then it is considered a regex that is matched against the keys of %INC. It is not clear to me which key would be returned if more than one match is found. All commands after an f pertain to that file (until the next f comamnd).

The last f command should be your main file so that you end up at the normal start point for your program. I use the L command to list all the breakpoints I just set.

Note that I only use simple breakpoints, but I believe that any command should be available. If you experiment with other commands to see what can be done, please comment with what works, and, more importantly, what doesn't.

Notice, from the loop in .perldb, I look for the first of three files:

  • Whatever you put in PERLDB_MYCOMMANDFILE, as in PERLDB_MYCOMMANDFILE=/some/other/list/of/dbcommands perl -d ./myScript
  • .perldbinit in the current working directory
  • .perldbinit in the same directory as the script you are debugging. This could be a default set of commands.

You can change that as you see fit.

As for the "reinvented" part, I asked my original question here, and was pointed to this reference, in addition to getting (and taking) this advice. So, thank you all in these threads for your help.

I hope everyone finds this to be another useful tool in your Perl toolbox. Hopefully the Google bots will find it!

3 Comments

I don't use Markdown, I write my blog posts in HTML, and PRE works OK for code. As a cargo cult, I usually insert CODE with class="prettyprint" into PRE, but I'm not sure it makes any difference. Unfortunately, Preview is broken.

Leave a comment

About Matthew Persico

user-pic I was never in love with C or SQL like this...