How can I set a working breakpoint to a constant expression?

时间:2018-12-19 11:16:55

标签: perl debugging breakpoints constant-expression

I have Perl code that uses a constant with an initializing block like this:

use constant C => map {
    ...;
} (0..255);

When I try to set a breakpoint at the ...; line, it does not work, meaning: I can set the breakpoint, but the debugger does not stop there.

I tried:

  1. Start the program with the debugger (perl -d program.pl)
  2. Set the breakpoint in the debugger (b 2)
  3. Reload using R, then run (r) the program

But still the debugger did not stop at the line, just as if I had no breakpoint set.

My Perl is not the latest; it's 5.18.2, just in case it matters...

2 个答案:

答案 0 :(得分:4)

您正在尝试在use块中放置一个断点。 use块实际上是其中包含BEGIN的{​​{1}}块。 默认情况下,Perl调试器不会在编译阶段停止。 但是,您可以通过将变量require设置为BEGIN

,在$DB::single块中强制Perl调试器进入单步模式。

请参见1中的Debugging Compile-Time Statements

如果您将代码更改为

perldoc perldebug

Perl调试器将在use语句中停止。

答案 1 :(得分:3)

如果创建像这样的简单模块(concept originated here),则可以避免更改代码:

package StopBegin;

BEGIN {
    $DB::single=1;
}
1;

然后,以

运行代码
perl -I./  -MStopBegin -d test.pl

相关答案(先前的,不太相关的答案在此答案下方)

如果test.pl看起来像这样:

use constant C => {
    map {;
        "C$_" => $_;
    } 0 .. 255
};

这是调试交互的样子:

% perl -I./  -MStopBegin -d test.pl

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

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

StopBegin::CODE(0x55db6287dac0)(StopBegin.pm:8):
8:  1;
  DB<1> s
main::CODE(0x55db6287db38)(test.pl:5):
5:  };
  DB<1> -
1   use constant C => {
2:      map {;
3:          "C$_" => $_;
4       } 0 .. 255
5==>    };
  DB<2> b 3
  DB<3> c
main::CODE(0x55db6287db38)(test.pl:3):
3:          "C$_" => $_;
  DB<3> 

请注意使用断点在map内停止。

先前的,无关紧要的答案

如果test.pl看起来像这样:

my $foo;

BEGIN {
    $foo = 1;
};

这是调试交互的样子:

% perl -I./  -MStopBegin -d test.pl

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

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

StopBegin::CODE(0x5567e3d79a80)(StopBegin.pm:8):
8:  1;
  DB<1> s
main::CODE(0x5567e40f0db0)(test.pl:4):
4:      $foo = 1;
  DB<1> s
main::(test.pl:1):  my $foo;
  DB<1> s
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.
  DB<1> 

请注意使用s命令前进,否则将跳过BEGIN中的test.pl