繁体   English   中英

在Perl调试器中打破`die`

[英]Breaking on `die` in Perl debugger

是否Perl调试器支持断点上die

我发现自己经常运行程序一次找到die线,然后第二次运行它来设置断点。

这会是很好,如果调试器可以作出自动打破die ,抱着一种垂死的上下文。

您可以设置__DIE__处理程序并在其中设置断点。

$SIG{__DIE__} = sub {
    $DB::single = 1;
    die @_;
};

这不是一个很好的答案,因为在__DIE__处理程序中你没有调用die的函数的上下文。 但是可以访问全局包变量,如果安装了PadWalker然后使用调试器命令y 1y 1 <pattern> ,则可以在调用die时查看范围内的词法变量。

证明的概念:

# dying.pl
$SIG{__DIE__} = sub { $DB::single = 1; die @_; };

for my $i (1 .. 10000) {
    my $j = sqrt($i * ($i+1)) - 0.49;
    die "$i $j" if $j - int($j) < 0.1;
}

$ perl -d dying.pl

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

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

main::(dying.pl:1):     $SIG{__DIE__} = sub { $DB::single = 1; die @_; };
  DB<1> c
main::CODE(0x1000c918)(dying.pl:1):
1:      $SIG{__DIE__} = sub { $DB::single = 1; die @_; };
  DB<1> y 1 j
$j = 13.000737563232
  DB<2> y 1
$i = 13
$j = 13.000737563232
  DB<3> c
13 13.000737563232 at dying.pl line 5.
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<3> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM