Reputation: 135
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?
Upvotes: 5
Views: 384
Reputation: 351
Both of these are useful for what I was doing. I'll put it on my Perl cheat sheet.
if ( $INC{ "perl5db.pl" } ) {
say "Debugger is running";
}
if ( $^P ) {
say "Perl is in debug support mode";
}
Upvotes: 0
Reputation: 25133
To find that your script is under debugger you need to check: $^P
variable:
if ( $^P ) {
# running under a debugger
}
https://perldoc.perl.org/perldebug#Calling-the-Debugger
If you script was run without -d
option then $DB::single
does noting.
According to the documentation the minimal debugger is sub DB::DB {}
.
So another method to check that your script is under debugger is:
if( defined &DB::DB ){
# running under a debugger
}
Upvotes: 1
Reputation: 830
Reading 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.
Upvotes: 1
Reputation: 385657
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 "Debugger is running";
}
Another approach would be to check if the debugger module is loaded.
if ( $INC{ "perl5db.pl" } ) {
say "Debugger is running";
}
Finally, the debugger requires that Perl is running in debug support mode, and $^P
indicates whether Perl is in this mode or not.
if ( $^P ) {
say "Perl is in debug support mode";
}
The debugger isn't the only tool that requires putting Perl in debug support mode. Others include Devel::NYTProf, Devel::Cover, Devel::Trace, and more. So this approach can't be used to specifically check if the debugger is running. But it might be what you actually want.
Upvotes: 4