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?
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.
$^P
. –
Practise $^P
(in perlvar) and -d
(in perlrun). –
Practise $DB::single
is false when we step over the function. Did you mean if( exists $DB::{ single })
? You mention that in the description, but forgot in the code. –
Kimberlite $DB::{ single }
isn't the same as $DB::single
. It's the same as *DB::single
, except that it doesn't vivify the symbol (which would render the test useless). –
Practise 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.
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
}
$^P
indicates whether Perl is running in debug support mode or not, not whether the debugger is running or not. This is not quite the same. For example, $^P
will be true when using Devel::Trace (perl -d:Trace ...
) even though the debugger isn't running. And $DB::single
won't do anything then either, since the debugger is not running. But, it could be what the OP actually wants, so +1. –
Practise running under the debugger
. It seems this sections requires more description. Also perlrun
said that -d:MOD runs the program under the control of a debugging
. I believe, it is not possible that $DB::single
won't do anything, when -d
is used ( debugger Perl_init_debugger(aTHX)
is initialized). perldebguts. Please correct me, If I am wrong. –
Kimberlite Devel::Trace
just do not bring interactive debugging, but that does not meant it does not make use of $DB::single
and not under debugger. –
Kimberlite -d:Trace
puts Devel::Trace "in control". Devel::Trace is not the debugger. -d
without a module puts the debugger "in control". –
Practise -d
turns on Perl's debugging functionality (eg. Perl_init_debugger( aTHX ) is called ). After that Perl inserts value of PERL5DB
environment variable as first line (The line with number zero) of a script. By default it is BEGIN{ require "perl5db.pl" }
. Which brings the interactive debugging. In case of -d:MOD
the use Devel::MOD
will be used as the first line and, I assume, PERL5DB
will be ignored. Still -d:MOD
runs script under debugger, but depending on MOD implementation it could be without the interactive debugging. –
Kimberlite perl5db.pl
or Devel::MOD
when -d:MOD
is used. When you used the debugger
you mean the interactive debugging, but I mean Perl's debugging functionality. –
Kimberlite perl5db.pl
), and there is debugging support (enabled by -d
). The OP asked how to detect if the debugger is being used, but your answer shows how to detect debugging support. My original comment stands. –
Practise 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";
}
© 2022 - 2024 — McMap. All rights reserved.