Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking on `die` in Perl debugger

Tags:

debugging

perl

Does the Perl debugger support breaking on die?

I find myself often running a program once to find the die line, then running it a second time to set a breakpoint.

It'd be nice if the debugger could be made to automatically break on die, holding on to the dying context.

like image 347
ajwood Avatar asked Dec 08 '25 06:12

ajwood


1 Answers

You could set a __DIE__ handler and set a breakpoint in it.

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

This isn't a great answer, as inside the __DIE__ handler you don't have the context of the function that called die. But the global package variables will be accessible, and if you have PadWalker installed then with the debugger command y 1 or y 1 <pattern>, you can view the lexical variables in scope when die was called.

Proof-of-concept:

# 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> 
like image 124
mob Avatar answered Dec 09 '25 21:12

mob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!