Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96264

Invoking Perl debugger so that it runs until first breakpoint

When I invoke my Perl debugger with perl -d myscript.pl, the debugger starts, but it does not execute any code until I press n (next) or c (continue).

Is there anyway to invoke the debugger and have it run through the code by default until it hits a breakpoint?

If so, is there any statement that I can use in my code as a breakpoint to have the debugger stop when it hits it?

Update:

Here is what I have in my .perldb file:

print "Reading ~/.perldb options.\n";
push @DB::typeahead, "c";
parse_options("NonStop=1");

Here is my hello_world.pl file:

use strict;
use warnings;

print "Hello world.\n"; 
$DB::single=1;
print "How are you?";

Here is the debugging session from running: perl -d hello_world.pl:

Reading ~/.perldb options.
Hello world
main::(hello_world.pl:6):       print "How are you?";
auto(-1)  DB<1> c
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> v
9563    9564    
9565    sub at_exit {
9566==>     "Debugged program terminated.  Use `q' to quit or `R' to restart.";
9567    }
9568    
9569    package DB;    # Do not trace this 1; below!
  DB<1> 

In other words, my debugger skips print "How are you?", and instead stops once the program finishes, which is not what I want.

What I want is have the debugger run my code without stopping anywhere (nor at the beginning, nor at the end of my script), unless I explicitly have a $DB::single=1; statement, in which case I would like it to stop before running the next line. Any ways to do this?

For reference, I am using:

$perl --version 

This is perl 5, version 14, subversion 1 (v5.14.1) built for x86_64-linux

Upvotes: 6

Views: 1977

Answers (2)

rocky
rocky

Reputation: 7098

Also check out Enbugger. And while on the topic of debuggers, see Devel::Trepan which also works with Enbugger.

Upvotes: 2

mob
mob

Reputation: 118605

Put

$DB::single = 1;

before any statement to set a permanent breakpoint in your code. This works with compile-time code, too, and may be the only good way to set a breakpoint during the compile phase.


To have the debugger automatically start your code, you can manipulate the @DB::typeahead array in either a .perldb file or in a compile-time (BEGIN) block in your code. For example:

# .perldb file
push @DB::typeahead, "c";

or

BEGIN { push @DB::typeahead, "p 'Hello!'", "c" }
...
$DB::single = 1;
$x = want_to_stop_here();

There is also a "NonStop" option that you could set in .perldb or in the PERLDB_OPTS environment variable:

PERLDB_OPTS=NonStop perl -d myprogram.pl

All of this (and much more) is discussed deep in the bowels of perldebug and perl5db.pl

Update:

To address the issues raised in the most recent update. Use the following in ./perldb:

print "Reading ~/.perldb options.\n";
push @DB::typeahead, "c";
parse_options("inhibit_exit=0");

Upvotes: 12

Related Questions