Sprad has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to break into the debugger (preferably ptkdb, but the default one will do) while the program is already running? I'd like to be able to check for certain conditions and drop to debug mode if they happen.

---
A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: Breaking into debugger
by perrin (Chancellor) on Nov 12, 2003 at 00:17 UTC
    $DB::single=1;

    IIRC, you have to start your script with -d and hit "c" (continue) to run until it calls the above code.

Re: Breaking into debugger
by ptkdb (Monk) on Nov 12, 2003 at 13:19 UTC
    Internal to ptkdb the variable $DB::no_stop_at_start, will prevent the debugger from stopping immediately when the program starts up. You can create a .ptkdbrc file which supports several api calls to the debugger. One of them is: condbrkpt($fname, $line, $expr) Which will set a conditional breakpoint in $fname at $line and will eval $expr and only stop if it's true.

    update:

    Breakpoints can even be included into the code if necessary.

    sub BEGIN { $DB::no_stop_at_start = 1 ; } Devel::ptkdb::condbrkpt(__FILE__,__LINE__+1, 'int($ARGV[0]) > 2') if d +efined $DB::single ; print "hello world\n" ;
    Update: Snippet of code for inline breakpoint function: snip
Re: Breaking into debugger
by scrottie (Scribe) on Dec 22, 2003 at 23:05 UTC
    Use B::Generate to change the nextstate ops into dbstate ops. Here are a few links to get you started: an article by Simon Cozen, author of B::Generate as published on perl.com that shows how to rewrite programs using B::Generate. Perl Assembly - structure of the bytecode, common ops and idioms. If you want to enable debugging completely after the fact from a module, it is possible, but it is tricky, and because of that you might want to consider changing your requirements - but again, it can be done.

    -scott