Search code examples
perlperldb

How to set breakpoint in a required module from a .perldb init file?


A follow-up question to Set breakpoint from .perldb init file. I am trying to set a breakpoint in a module that is loaded at run time. For testing purposes I have created the following files:

/home/hakon/test/perl/perldb/p.pl:

use feature qw(say);
use strict;
use warnings;
use lib "./lib";

say "Line 6";
say "Line 7";
require My::Module;
say "Line 9";

/home/hakon/test/perl/perldb/lib/My/Module.pm

package My::Module;
use feature qw(say);
use strict;
use warnings;

say "MM Line 6";
say "MM Line 7";
say "MM Line 8";

1;

/home/hakon/test/perl/perldb/.perldb

sub afterinit {
    push @DB::typeahead,
      "b p.pl:8",
      "c",
      "b /home/hakon/test/perl/perldb/lib/My/Module.pm:7",
      "c"
      ;
}

When I run the script with the debugger, I get:

$ perl -d p.pl

Loading DB routines from perl5db.pl version 1.55
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(p.pl:6): say "Line 6";
auto(-4)  DB<1> b p.pl:8
auto(-3)  DB<2> c
Line 6
Line 7
main::(p.pl:8): require My::Module;
auto(-2)  DB<2> b /home/hakon/test/perl/perldb/lib/My/Module.pm:7
auto(-1)  DB<3> c
MM Line 6
MM Line 7
MM Line 8
Line 9
Debugged program terminated.  Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.

So the the breakpoint at line 7 of Module.pm is ignored. How can I make the debugger stop at the breakpoint?


Solution

  • It seems that one cannot set a breakpoint in the required module before it has been compiled, but it is possible to work around the issue by using the b load filename command first, and then setting the breakpoint since then the required module will already have been compiled:

    sub afterinit {
        push @DB::typeahead,
          "b p.pl:8",
          "c",
          "b load lib/My/Module.pm",
          "c",
          "b lib/My/Module.pm:7",
          "c"
          ;
    }
    

    This works for me.

    Note:

    If I had used

    use FindBin qw/$Bin/;
    use lib "$Bin/lib";
    

    instead of

    use lib './lib';
    

    in the script p.pl, I would have to use absolute paths with the b commmand instead of relative paths:

    sub afterinit {
        push @DB::typeahead,
          "b p.pl:8",
          "c",
          "b load /home/hakon/test/perl/perldb/lib/My/Module.pm",
          "c",
          "b /home/hakon/test/perl/perldb/lib/My/Module.pm:7",
          "c"
          ;
    }