从Perl脚本创建/引发SIGINT(Ctrl-C) - 并从处理程序中以步进模式进入调试器?

时间:2013-02-07 16:09:49

标签: perl debugging error-handling event-handling sigint

我在Perl(在Linux下)中遇到了一个相当复杂的程序的问题,我正在尝试调试。我可以在这里用简单的代码片段模拟问题(test.pl):

use warnings;
use strict;
use feature qw/say/;

my @testa = ();
my $numelems = 10000;

# populate array/list of arrays
for (my $ix = 0; $ix < $numelems; $ix++) {
  my @miniarr = ($ix, 1);
  push(@testa, \@miniarr);
}

say "Array is now " . scalar(@testa) . " elements long";

my $BADnumelems = $numelems + 2;
my $sum = 0;

# loop through array/list of arrays
for (my $ix = 0; $ix < $BADnumelems; $ix++) {
  my @minientry = @{$testa[$ix]};
  $sum += $minientry[0];
}

say "Sum of elements is $sum";

运行此程序将退出:

$ perl test.pl 
Array is now 10000 elements long
Can't use an undefined value as an ARRAY reference at test.pl line 22.

所以,现在我想调试它 - 但错误会导致程序死掉,并退出调试器:

$ perl -d test.pl 

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

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

main::(test.pl:6):  my @testa = ();
  DB<1> c
Array is now 10000 elements long
Can't use an undefined value as an ARRAY reference at test.pl line 22.
 at test.pl line 22
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> p $sum

  DB<2> exit
$ 

然后,我找到break on warning in debugger (PerlMonks);所以我尝试添加这个:

...
use feature qw/say/;

$SIG{__DIE__} = sub { my($signal) = @_; say "DIEhandler: $signal"; $DB::single = 1; };
$SIG{__WARN__} = sub { my($signal) = @_; say "WARNhandler: $signal"; $DB::single = 1; };

my @testa = ();
...

...但这也会杀死调试器:

$ perl -d test.pl 

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

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

main::(test.pl:6):  $SIG{__DIE__} = sub { my($signal) = @_; say "DIEhandler: $signal"; $DB::single = 1; };
  DB<1> c
Array is now 10000 elements long
DIEhandler: Can't use an undefined value as an ARRAY reference at test.pl line 25.

Can't use an undefined value as an ARRAY reference at test.pl line 25.
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.  

现在,问题是,我知道如果我用Ctrl-C中断程序 - 这通常会导致调试器进入步进模式;例如,你可以设置my $numelems = 100000000;,当它循环时,按Ctrl-C - 你可以调试:

$ perl -d test.pl 

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

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

main::(test.pl:6):  $SIG{__DIE__} = sub { my($signal) = @_; say "DIEhandler: $signal"; $DB::single = 1; };
  DB<1> c
^Cmain::(test.pl:14):     my @miniarr = ($ix, 1);
  DB<1> p $ix
148607
  DB<2> q

现在,我可以insert break point into source perl program

...
for (my $ix = 0; $ix < $BADnumelems; $ix++) {
  $DB::single = 1;               ### <=== BREAK HERE
  my @minientry = @{$testa[$ix]};
...

但它在$ix = 0

时进入循环
$ perl -d test.pl 

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

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

main::(test.pl:6):  $SIG{__DIE__} = sub { my($signal) = @_; say "DIEhandler: $signal"; $DB::single = 1; };
  DB<1> c
Array is now 10000 elements long
main::(test.pl:26):   my @minientry = @{$testa[$ix]};
  DB<1> p $ix
0
  DB<2> q

...而且我不想逐步通过10000个元素来找到有问题的部分:)

所以,我想到了以下内容 - 如果有办法从Perl脚本本身提升/生成/创建SIGINT(Ctrl-C) ,那么我就可以从die处理程序,并希望在进程终止之前在调试器中引发一个步骤。所以,我的问题是:

  • 是否有可能从Perl脚本中引发SIGINT,这会破坏自己 - 如果是这样,怎么样?
  • 如果可以使用前一个,是否可以在进程终止之前使用来自骰子处理程序的Ctrl-C进入调试步骤模式?
  • 如果前一个可能 - 我有什么可能导致进入调试器步骤模式,就在程序die的行?

2 个答案:

答案 0 :(得分:0)

得到它(虽然不确定我是否理解了所有事情:)),多亏了这些:

只需使用eval换行和warn修改代码:

...
# loop through array/list of arrays
for (my $ix = 0; $ix < $BADnumelems; $ix++) {
  eval {
    my @minientry;
    @minientry = @{$testa[$ix]};
    $sum += $minientry[0];
  }; # just this causes step into debugger at $ix = 10001
  warn $@ if $@; # this causes step into debugger at $ix=10000 (OK)
}
...

...然后调试器的工作原理如下:

$ perl -d test.pl 

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

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

main::(test.pl:6):  $SIG{__DIE__} = sub { my($signal) = @_; say "DIEhandler: $signal"; @_ = (); $DB::single = 1; };
  DB<1> c
Array is now 10000 elements long
DIEhandler: Can't use an undefined value as an ARRAY reference at test.pl line 27.

main::(test.pl:30):   warn $@ if $@; # this causes step into debugger at $ix=10000 (OK)
  DB<1> p $ix
10000
  DB<2> p @{$testa[$ix]}

  DB<3> p @{$testa[$ix-1]}
99991
  DB<4> p join("--", @{$testa[$ix]})

  DB<5> p join("--", @{$testa[$ix-1]})
9999--1
  DB<6> q

...也就是说,直接进入问题线,这就是我想要的。

嗯,希望这有助于某人,
干杯!

编辑:也可能相关:Why do I need to localize $@ before using eval?

答案 1 :(得分:0)

$DB::single可以设置为表达式,这样它只会在指定的条件下中断。

...
for (my $ix = 0; $ix < $BADnumelems; $ix++) {

  $DB::single ||= ref($testa[$ix]) ne 'ARRAY';

  my @minientry = @{$testa[$ix]};
  $sum += $minientry[0];
}
...

...
for (my $ix = 0; $ix < $BADnumelems; $ix++) {
  eval {
      my @minientry = @{$testa[$ix]};
      $sum += $minientry[0];
  };
  $DB::single ||= $@; # break if eval block fails
  # give debugger somewhere to break before proceeding to next iteration
  1;
}
...

(我更喜欢$DB::single ||= ...$DB::single = ...,因为如果您真的正在逐步执行该块中的每个迭代(或该块之后的任何代码),Perl将希望将$DB::single设置为true对你而言,你不希望你的代码无意中取消设置该值并阻止你单步执行代码的其他行。)