How does a Perl script know if it's running in the debugger?

Viewed 147

I have a script that redirects STDIN/STDOUT to a file normally. But, debugging is a lot more efficient if it doesn't do that. Is there a $DB:xxx variable or something that lets the script know so it can behave differently?

2 Answers

I didn't find any way of determining if they debugger is running directly, but you could check if well-known debugger variable $DB::single exists using the following:

if ($DB::{single}) {
   say "Debugging";
}

Another approach would be to check if the debugger module is loaded.

if ($INC{"perl5db.pl"}) {
   say "Debugging";
}

Readnig through the perl source code of DB.pm (perldoc -m DB), I noticed that $SIG{INT} is globally set at the end.

This seems to work, at least for a trivial program:

if (\&DB::catch && $SIG{'INT'} && $SIG{'INT'} == \&DB::catch) {
    say "Debugging ?"
}

If it's not applicable, I guess it's possible to subclass DB.pm and create a simple debugger which overrides cont and does extra bookkeeping...

Related