I would like to be able to (reasonably) arbitrarily set my execution point in the Perl debugger. E.g., moving immediately prior to an if from the body of the if and setting a variable.
Rummaging around the perldebug(and perldebguts and the perl debugger POD) page suggests that this kind of functionality is either not supported or not documented.
A cumbersome workaround would be to add labels and conditional goto statements throughout your code. But depending on how badly you want to emulate this feature, it might be worth it.
POINT1: $GOTO=""; # $GOTO is our fake variable that we only set from the debugger
($a,$b,$c)=(1,2,3);
POINT2: $GOTO="";
if ($a < $b) {
goto $GOTO if $GOTO;
if ($a > $c) {
goto $GOTO if $GOTO;
print "foo\n";
} else {
goto $GOTO if $GOTO;
print "bar\n";
}
goto $GOTO if $GOTO;
} else {
goto $GOTO if $GOTO;
print "nothing\n";
goto $GOTO if $GOTO;
}
Sample debugging session:
$ perl -d debuggoto.pl
Loading DB routines from perl5db.pl version 1.28
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(debuggoto.pl:1): POINT1: $GOTO=""; # $GOTO is our fake variable that we only set from the debugger
DB<1> n
main::(debuggoto.pl:2): ($a,$b,$c)=(1,2,3);
DB<1>
main::(debuggoto.pl:3): POINT2: $GOTO="";
DB<1>
main::(debuggoto.pl:4): if ($a < $b) {
DB<1>
main::(debuggoto.pl:5): goto $GOTO if $GOTO;
DB<1>
main::(debuggoto.pl:6): if ($a > $c) {
DB<1>
main::(debuggoto.pl:10): goto $GOTO if $GOTO;
DB<1>
main::(debuggoto.pl:11): print "bar\n";
DB<1>
bar
main::(debuggoto.pl:13): goto $GOTO if $GOTO;
DB<1> $GOTO="POINT2"
DB<2> n
main::(debuggoto.pl:3): POINT2: $GOTO="";
DB<2> $c=0
DB<3> n
main::(debuggoto.pl:4): if ($a < $b) {
DB<3>
main::(debuggoto.pl:5): goto $GOTO if $GOTO;
DB<3>
main::(debuggoto.pl:6): if ($a > $c) {
DB<3>
main::(debuggoto.pl:7): goto $GOTO if $GOTO;
DB<3>
main::(debuggoto.pl:8): print "foo\n";
DB<3>
foo
main::(debuggoto.pl:13): goto $GOTO if $GOTO;
DB<3>
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<3>
Use `q' to quit or `R' to restart. `h q' for details.
DB<3>
I wonder if it would be possible to build a debugger that uses this idea.
I am still not sure what exactly this would achieve, but I bet a custom runloop that skips ops you don't care about until you start caring again might solve your problem.
A good runloop to cargo-cult is Runops::Switch. Remove the switch statement and write a function that skips ops until you are on the one you want to run; then just call into the normal runloop to actually run that op.
Relevant code: http://cpansearch.perl.org/src/RGARCIA/Runops-Switch-0.04/Switch.xs
This is all handwaving, I have never written a runloop before. The goto idea in another post is also good, but this involves writing less code.
Breakpoints can only be set on the first line of a statement.
This can't be done with the existing debugger.
Related
I have Perl code that uses a constant with an initializing block like this:
use constant C => map {
...;
} (0..255);
When I try to set a breakpoint at the ...; line, it does not work, meaning: I can set the breakpoint, but the debugger does not stop there.
I tried:
Start the program with the debugger (perl -d program.pl)
Set the breakpoint in the debugger (b 2)
Reload using R, then run (r) the program
But still the debugger did not stop at the line, just as if I had no breakpoint set.
My Perl is not the latest; it's 5.18.2, just in case it matters...
You are trying to put a break point in a use block.
A use block is in effect a BEGIN block with a require in it.
The Perl debugger by default does not stop in compile phase.
However you can force the Perl debugger into single step mode inside a BEGIN block by setting the variable $DB::single to 1
See Debugging Compile-Time Statements in perldoc perldebug
If you change your code to
use constant C => map {
$DB::single = 1;
...;
} (0..255);
The Perl debugger will stop in the use statement.
You can avoid altering your code if you create a simple module like this (concept originated here):
package StopBegin;
BEGIN {
$DB::single=1;
}
1;
Then, run your code as
perl -I./ -MStopBegin -d test.pl
Pertinent Answer (previous, not-so-pertinent answer is below this one)
If test.pl looks like this:
use constant C => {
map {;
"C$_" => $_;
} 0 .. 255
};
here's what the debug interaction looks like:
% perl -I./ -MStopBegin -d test.pl
Loading DB routines from perl5db.pl version 1.53
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
StopBegin::CODE(0x55db6287dac0)(StopBegin.pm:8):
8: 1;
DB<1> s
main::CODE(0x55db6287db38)(test.pl:5):
5: };
DB<1> -
1 use constant C => {
2: map {;
3: "C$_" => $_;
4 } 0 .. 255
5==> };
DB<2> b 3
DB<3> c
main::CODE(0x55db6287db38)(test.pl:3):
3: "C$_" => $_;
DB<3>
Note the use of the breakpoint to stop inside the map.
Previous, Not-So-Pertinent Answer
If test.pl looks like this:
my $foo;
BEGIN {
$foo = 1;
};
here's what the debug interaction looks like:
% perl -I./ -MStopBegin -d test.pl
Loading DB routines from perl5db.pl version 1.53
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
StopBegin::CODE(0x5567e3d79a80)(StopBegin.pm:8):
8: 1;
DB<1> s
main::CODE(0x5567e40f0db0)(test.pl:4):
4: $foo = 1;
DB<1> s
main::(test.pl:1): my $foo;
DB<1> s
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>
Note the use of the s command to advance, otherwise it'll skip over the BEGIN block in test.pl
I am having one problem in this perl code.
It is showing some error "Bizarre copy of ARRAY in leave at " . Although code is correct, I feel. Can anybody help.
#!/usr/bin/perl -w
use strict;
sub getStatus() {
#my $self = shift;
my $status;
my #details;
my $Up = 2;
my $Down = 3;
$status = "Failed";
push #details, $Up, $Down;
my $detailMsg = join(",", #details);
return [$status, $detailMsg];
}
my $info = &getStatus();
my $status = ${#$info}[0];
my $detailMsg = ${#$info}[1];
print $status;
print $detailMsg;
exit 0;
-----------------------
Now debugging using perl -d option.
-----------------------
Loading DB routines from perl5db.pl version 1.28
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(test.pl:19): my $info = &getStatus();
DB<1> n
main::(test.pl:20): my $status = ${#$info}[0];
DB<1> n
main::(test.pl:20): my $status = ${#$info}[0];
DB<1> n
Bizarre copy of ARRAY in leave at test.pl line 20.
at test.pl line 20
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>
Please suggest any solution. If this is related to problem in perl module, then how can we overcome. Please suggest.
${#$info}[0] is an abomination. $info is the return value of your getStatus sub. It is an array reference. Then, #$info is the dereferenced array. But, you are evaluating it in scalar context, so it evaluates to 2. Then you try to evaluate that as an array reference, and take its first element.
Bizarre indeed. The error message is very appropriate.
PS: Don't use &getStatus(). getStatus() is the right way to invoke your sub.
PPPS: You probably do want $info->[0], but then it is hard to be certain, because what you wrote is so bizarre.
While writing script I noticed some very strange Perl behavior. The task is to trim last character of string stored in $temp1 variable, this can be done using either chop($temp1) or more complicated substr($temp1, 0, length($temp1) - 1). These both work as one-liners but in my script the substr solution does not work. Below is example from debugger:
1st PROBLEM:
1st SOLUTION - NOT WORKING:
main::(delmid_n.pl:24): if (($state == 0) && ($_ !~ $musr) && ($_ !~ $lusr)) {
>> n
main::(delmid_n.pl:54): $strprn=substr($temp1, 0, length($temp1) - 1);
>> p $temp1
(-,user2,t-mobile.co.uk)\
>> p substr($temp1, 0, length($temp1) - 1) . "\n";
(-,user2,t-mobile.co.uk)
DB<4>
main::(delmid_n.pl:55): print $strprn . "\n";
DB<4>
main::(delmid_n.pl:56): $temp1 = "";
DB<4>
As you can see in the $strprn variable, nothing is stored. If the same piece of code (which is stored into the $strprn variable) is printed via the 'p' command, the output is OK. This "bug" can be overcome using the mentioned chop() function (see code below):
2nd SOLUTION - WORKING:
main::(delmid_w.pl:24): if (($state == 0) && ($_ !~ $musr) && ($_ !~ $lusr)) {
>> p $temp1
(-,user2,t-mobile.co.uk)\
DB<4> n
main::(delmid_w.pl:56): chop ($temp1);
DB<4> n
main::(delmid_w.pl:57): print $temp1;
DB<4> n
(-,user2,t-mobile.co.uk)
main::(delmid_w.pl:58): $temp1 = "";
DB<4>
The above code is exactly the same as the first example, but the following two lines are from 1st example:
$strprn=substr($temp1, 0, length($temp1) - 1);
print $strprn . "\n";
are replaced with following two lines in the 2nd example:
chop ($temp1);
print $temp1;
What is wrong with the 2nd solution?
2nd PROBLEM:
This is a problem which I do not have a workaround for, so far.
DB<1>
main::(delbeg_n.pl:15): $state = 0;
DB<1>
main::(delbeg_n.pl:16): $muser = qr/\(-,user1,[^,]+\.co\.uk\)\\$/;
DB<1>
main::(delbeg_n.pl:19): line: while (<>) {
DB<1>
main::(delbeg_n.pl:20): chomp; # strip record separator
DB<1>
main::(delbeg_n.pl:21): #Fld = split(/\s+/, $_,);
DB<1>
main::(delbeg_n.pl:23): if (($state == 0) && ($Fld[1] =~ $muser)) {
DB<1> p $Fld[1]
(-,user1,one2one.co.uk)\
DB<2> n
main::(delbeg_n.pl:43): print $_;
DB<2> p $_
netgroup1 (-,user1,one2one.co.uk)\
DB<3> if ($Fld[1] =~ $muser) {print "TRUE"}
TRUE
As you can see after executing line 21 in the code, the next execution line is 43 (the else statement). Why is the following condition not evaluated as a true, allowing the code to continue with line 23, 24, 25?
if (($state == 0) && ($Fld[1] =~ $muser))
The following line was inserted as a demonstration that the condition should be evaluated as a true:
if ($Fld[1] =~ $muser) {print "TRUE"}
Thanks a lot.
Both problems are related. There is something strange at the end of the line.
First, it looks as if you have a newline character after the slash at the end of the line that you have not chomped off -- at least that is what the debugger shows:
>> p $temp1
(-,user2,t-mobile.co.uk)\
DB<4> n
If there was no newline it would have been
>> p $temp1
(-,user2,t-mobile.co.uk)\
DB<4> n
So, there should be two characters: \ and newline.
Why the weird behavior -- I don't know without looking at your input file.
Also, instead of using string length, you can just do substr( $string, 0, -1 ) -- that will also return all but the last character.
That is probably the reason for your second problem -- I guess that \(-,user1,[^,]+\.co\.uk\)\\ matches and it is the end of line marker $ that causes the mismatch.
use strict please
check the file types (dos, unix, old mac)
view the file in a hex editor
I'm using ActivePerl 5.8 on Windows XP.
use strict;
use warnings;
use Data::Dumper;
There are three subroutines used in my script.
To detect the call stack, I can only insert some print "some location"; and check the print result from console Window.
Is there any good method to monitor it? Thank you.
If it's your code, you might want to use:
Carp::cluck( "And here's the stack:" );
See Carp::cluck. It prints out a warning with a stack trace. It works like the "printf" style of debug output.
Use the debugger's T command.
Example:
$ perl -d -e'
sub foo {}
sub bar { foo; }
bar;
'
Loading DB routines from perl5db.pl version 1.32
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(-e:4): bar;
DB<1> s
main::bar(-e:3): sub bar { foo; }
DB<1> s
main::foo(-e:2): sub foo {}
DB<1> T
. = main::foo() called from -e line 3
. = main::bar() called from -e line 4
DB<1> s
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> q
You weren't specific about why you'd like to monitor the call stack and trace your subs, so answers will have to be broad.
One method is caller:
caller
Returns the context of the current subroutine call. In scalar context, returns the caller's package name if there is a caller, that is, if we're in a subroutine or eval or require, and the undefined value otherwise. In list context, returns
# 0 1 2
($package, $filename, $line) = caller;
With EXPR, it returns some extra information that the debugger uses to print a stack trace. The value of EXPR indicates how many call frames to go back before the current one.
# 0 1 2 3 4
($package, $filename, $line, $subroutine, $hasargs,
# 5 6 7 8 9 10
$wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash)
= caller($i);
You might also use the Devel::Cover module:
Code coverage data are collected using a pluggable runops function which counts how many times each op is executed. These data are then mapped back to reality using the B compiler modules. There is also a statement profiling facility which needs a better backend to be really useful.
The more you tell us about what you want to do, the more helpful to you our answers will be!
You rarely need to directly manage the call stack in Perl. If you do caller is the tool you want. However, it is only rarely needed.
More often, I want to see a stack trace when I am debugging. Good news, its easy to get a stack trace, simply use Carp's confess and cluck functions instead of die and warn.
use strict;
use warnings;
use Carp;
bar(6.1);
bar(1);
sub foo {
confess "Oh noes" unless #_ == 6; # confess is fatal
}
sub bar {
my $count = shift;
cluck "bar is in trouble" unless int $count == $count; # cluck is not fatal
foo( ('a')x $count );
}
This gets you:
dao:~ toad$ perl test.pl
bar is in trouble at test.pl line 14
main::bar(6.1) called at test.pl line 5
Oh noes at test.pl line 9
main::foo('a') called at test.pl line 15
main::bar(1) called at test.pl line 6
Suppose I have a situation where I'm trying to experiment with some Perl code.
perl -d foo.pl
Foo.pl chugs it's merry way around (it's a big script), and I decide I want to rerun a particular subroutine and single step through it, but without restarting the process. How would I do that?
The debugger command b method sets a breakpoint at the beginning of your subroutine.
DB<1> b foo
DB<2> &foo(12)
main::foo(foo.pl:2): my ($x) = #_;
DB<<3>> s
main::foo(foo.pl:3): $x += 3;
DB<<3>> s
main::foo(foo.pl:4): print "x = $x\n";
DB<<3>> _
Sometimes you may have to qualify the subroutine names with a package name.
DB<1> use MyModule
DB<2> b MyModule::MySubroutine
just do: func_name(args)
e.g.
sub foo {
my $arg = shift;
print "hello $arg\n";
}
In perl -d:
DB<1> foo('tom')
hello tom
Responding to the edit regarding wanting to re-step through a subroutine.
This is not entirely the most elegant way of doing this, but I don't have another method off the top of my head and am interested in other people's answers to this question :
my $stop_foo = 0;
while(not $stop_foo) {
foo();
}
sub foo {
my $a = 1 + 1;
}
The debugger will continually execute foo, but you can stop the next loop by executing '$stop_foo++' in the debugger.
Again, I don't really feel like that's the best way but it does get the job done with only minor additions to the debugged code.