【问题标题】:How can I set a working breakpoint to a constant expression?如何将工作断点设置为常量表达式?
【发布时间】:2018-12-19 11:16:55
【问题描述】:

我有一个 Perl 代码,它使用一个带有如下初始化块的常量:

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

当我尝试在...;设置断点时,它不起作用,这意味着:我可以设置断点,但调试器不止于此

我试过了:

  1. 使用调试器启动程序 (perl -d program.pl)
  2. 在调试器中设置断点 (b 2)
  3. 使用 R 重新加载,然后运行 ​​(r) 程序

但调试器仍然没有停在该行,就像我没有设置断点一样。

我的 Perl 不是最新的;现在是 5.18.2,以防万一……

【问题讨论】:

    标签: perl debugging breakpoints constant-expression


    【解决方案1】:

    您试图在use 块中设置断点。 使用块实际上是一个 BEGIN 块,其中包含一个 require。 Perl 调试器默认不会在编译阶段停止。 但是,您可以通过将变量 $DB::single 设置为 1 来强制 Perl 调试器进入 BEGIN 块内的单步模式

    Debugging Compile-Time Statementsperldoc perldebug

    如果您将代码更改为

    use constant C => map {
        $DB::single = 1;
        ...;
    } (0..255);
    

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

    【讨论】:

    • 有没有办法避免修改源(比如一些命令行开关)?
    • @U.Windl 我不知道
    • 我在回答中说明了一种避免代码修改的方法。
    【解决方案2】:

    如果您创建一个像这样 (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命令前进,否则会跳过test.pl中的BEGIN

    【讨论】:

    • 我也有类似的想法,使用环境变量有条件地启用$DB::single = 1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 2021-07-20
    相关资源
    最近更新 更多