lib/Devel/Trepan/DB.pm

filter: Statementstmt 23.6% 84 / 356 Branchbran 2.2% 3 / 136 Conditioncond 0.0% 0 / 68 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 46.1% 24 / 52 Podpod 28.0% 7 / 25 totaltotal 17.4% 118 / 676 CC 206 SCAR 74.4
CC
206
Cov
16%
DB
3771.9
DB
3771.9
catch
156.0
CRAP 1700.0
↑ Summary lib/Devel/Trepan/DB/Backtrace.pm »
1 # Perl's Core DB.pm library with some corrections, additions,
2 # modifications and code merged from perl5db.pl
3 #
4 # Documentation is after __END__
5 #
6  
7 12 ▶use rlib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use Devel::Callsite;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =pod
12  
13 =head1 C<DB>
14  
15 Devel::Trepan customized DB package. Down the line this should be split off
16 and merged with DB that perl5db.pl and other uses similar ilk.
17  
18 =cut
19  
20 package DB;
21 12 ▶use warnings; no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 12 ▶use Devel::Trepan::DB::Vars;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use Devel::Trepan::DB::Backtrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 ▶use Devel::Trepan::DB::Breakpoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 12 ▶use Devel::Trepan::DB::Eval;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 12 ▶use Devel::Trepan::DB::Sub;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 12 ▶use Devel::Trepan::Terminated;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # "private" globals
33 12 my (@skippkg);
34  
35 12 my $ineval = {};
36  
37 ####
38 #
39 # Globals - must be defined at startup so that clients can refer to
40 # them right after a C<use Devel::Trepan::DB;>
41 #
42 ####
43  
44 12 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
45 12 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 12 $ini_warn = $WARNING;
47  
48 12 $in_debugger = 0;
49 12 @clients = ();
50 12 $ready = 0;
51 12 @DB::saved = ();
52 12 @skippkg = ();
53  
54 # ensure we can share our non-threaded variables or no-op
55 12 ▶ if ($ENV{PERL5DB_THREADED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'PERL5DB_THREADED'}) { } 0 12 12
56 0 require threads;
57 0 require threads::shared;
58 0 import threads::shared qw(share);
59 12 ▶ no strict; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
60 0 $DBGR;
61 0 share(\$DBGR);
62 0 lock($DBGR);
63 12 ▶ use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
64 0 print "Thread support enabled\n";
65 } else {
66 12 ▶ *lock = sub(*) {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
67 12 ▶ *share = sub(*) {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
68 }
69  
70 # Don't print return values on exiting a subroutine.
71 12 $doret = -2;
72  
73 # "Triggers bug (?) in perl if we postpone this until runtime."
74 # XXX No details on this yet, or whether we should fix the bug instead
75 # of work around it. Stay tuned.
76 12 @postponed = @stack = (0);
77  
78 # No extry/exit tracing.
79 12 $frame = 0;
80 12 $HAVE_MODULE{'Devel::Callsite'} = 'call_level_param';
81 }
82  
83 12 ▶END {
Subroutine END: called CC=2 SCAR=10.3
84 0 ▶ unless ($DB::fall_off_on_end) {
Subroutine END: not called CC=2 SCAR=10.3
Branch: 2 branches
BranchTrueFalseTotal
unless ($fall_off_on_end) 12 0 12
unless ($fall_off_on_end) 0 0 0
85 0 $DB::single = 1;
86 0 Devel::Trepan::Terminated::at_exit();
87 }
88 0 $DB::ready = 0;
89 }
90  
91 sub save_vars();
92  
93 ####
94 # DB is called by Perl for every statement
95 #
96 # IMPORTANT NOTE: We allow DB:DB() to get called recursively and as
97 # Father Chrysostomos notes in Perl bug RT #115742 , without the use
98 # of& myDB below, the inner call shares the same pad as the outer
99 # call.
100 #
101 # Under advisement from Ben Morrow, we shouldn't use lexical
102 # variables on versions of Perl before 5.18.0.
103 #
104 sub DB
105 0 ▶{
Subroutine DB: not called CC=62 SCAR=82.4
Pod undocumented
106  
107 # print "+++ in DB single: ${DB::single}\n";
108  
109 # lock the debugger and get the thread id for the prompt
110 0 ▶ if ($ENV{PERL5DB_THREADED}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PERL5DB_THREADED"}) 0 0 0
111 0 lock($DBGR);
112 0 $tid = eval { "[".threads->tid."]" };
113 }
114  
115 0 ▶ return unless $ready && !$in_debugger;
Branch: 1 branch
BranchTrueFalseTotal
unless $ready and not $in_debugger 0 0 0
Condition0%0/3
$ready and not $in_debugger
!ll&&!rl&&r
000
MC/DC0%0/2
$ready and not $in_debugger
$ready not $in_debugger
Truth table0%0/3
A and not(B)
A= $readyB= not $in_debugger
ABresult
0X0
100
111
116 0 local $in_debugger = 1;
117 0 @DB::_ = @_;
118 0 save_vars();
119  
120 # Since DB::DB gets called after every line, we can use caller() to
121 # figure out where we last were executing. Sneaky, eh? This works because
122 # caller is returning all the extra information when called from the
123 # debugger.
124 0 $DB::caller = [CORE::caller];
125 ($DB::package, $DB::filename, $DB::lineno, $DB::subroutine, $DB::hasargs,
126 $DB::wantarray, $DB::evaltext, $DB::is_require, $DB::hints, $DB::bitmask,
127 $DB::hinthash
128 0 ) = @{$DB::caller};
129  
130 # print "++++ $DB::package $DB::filename, $DB::lineno\n";
131 0 local $filename_ini = $filename;
132  
133 0 local $OP_addr = Devel::Callsite::callsite();
134  
135 0 ▶ return if @skippkg and grep { $_ eq $DB::package } @skippkg;
Branch: 1 branch
BranchTrueFalseTotal
if @skippkg and grep {$_ eq $package;} @skippkg 0 0 0
Condition0%0/3
@skippkg and grep {$_ eq $package;} @skippkg
!ll&&!rl&&r
000
MC/DC0%0/2
@skippkg and grep {$_ eq $package;} @skippkg
@skippkg grep {$_ eq $package;} @skippkg
Truth table0%0/3
A and B
A= @skippkgB= grep {$_ eq $package;} @skippkg
ABresult
0X0
100
111
136  
137 # Set package namespace for running eval's in the user context.
138 # However this won't let them modify lexical variables, alas.
139 # This has to be 'local' rather than 'my' to allow recursive
140 # debugging ("debug" command).
141 0 local $namespace_package = "package $DB::package;";
142  
143 0 local(*DB::dbline) = "::_<$DB::filename";
144  
145 # we need to check for pseudofiles on Mac OS (these are files
146 # not attached to a filename, but instead stored in Dev:Pseudo)
147 0 ▶ if ( $OSNAME eq 'MacOS' && $#dbline < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($OSNAME eq "MacOS" and $#dbline < 0) 0 0 0
Condition0%0/3
$OSNAME eq "MacOS" and $#dbline < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$OSNAME eq "MacOS" and $#dbline < 0
$OSNAME eq "MacOS" $#dbline < 0
Truth table0%0/3
A and B
A= $OSNAME eq "MacOS"B= $#dbline < 0
ABresult
0X0
100
111
148 0 $filename_ini = $filename = 'Dev:Pseudo';
149 0 *dbline = $main::{ '_<' . $filename };
150 }
151 0 $DB::brkpt = undef;
152  
153 # Increment debugger nesting level.
154 0 local $DB::level = $DB::level + 1;
155  
156 # Test watch expressions;
157 0 local $watch_triggered = undef;
158 0 local $c;
159 0 for $c (@clients) {
160 0 local @list= @{$c->{watch}->{list}};
161 0 local $wp;
162 0 for $wp (@list) {
163 0 ▶ next unless $wp->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $wp->enabled 0 0 0
164 0 local $opts = {return_type => '$',
165 namespace_package => $namespace_package,
166 fix_file_and_line => 1,
167 hide_position => 0};
168 0 local $new_val = &DB::eval_with_return($wp->expr, $opts, @DB::saved);
169 0 local $old_val = $wp->old_value;
170 12 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
171 0 ▶ next if !defined($old_value) and !defined($new_val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_value or defined $new_val 0 0 0
Condition0%0/3
defined $old_value or defined $new_val
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $old_value or defined $new_val
defined $old_value defined $new_val
Truth table0%0/3
A or B
A= defined $old_valueB= defined $new_val
ABresult
1X1
011
000
172 0 ▶ local $not_same = !defined($old_val) || !defined($new_val);
Condition0%0/3
defined $old_val && defined $new_val
!ll&&!rl&&r
000
MC/DC0%0/2
defined $old_val && defined $new_val
defined $old_val defined $new_val
Truth table0%0/3
A && B
A= defined $old_valB= defined $new_val
ABresult
0X0
100
111
173 0 ▶ if ( $not_same || $new_val ne $wp->old_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_same or $new_val ne $wp->old_value) 0 0 0
Condition0%0/3
$not_same or $new_val ne $wp->old_value
l!l&&r!l&&!r
000
MC/DC0%0/2
$not_same or $new_val ne $wp->old_value
$not_same $new_val ne $wp->old_value
Truth table0%0/3
A or B
A= $not_sameB= $new_val ne $wp->old_value
ABresult
1X1
011
000
174 # Yep! Record change.
175 0 $wp->current_val($new_val);
176 0 $wp->hits($wp->hits+1);
177 0 $watch_triggered = $wp;
178 0 last;
179 }
180 }
181 }
182  
183 # Test for breakpoints and action events.
184 0 local @action = ();
185 0 ▶ if (exists $DB::dbline{$DB::lineno} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dbline{$lineno} and local $brkpts = $dbline{$lineno}) 0 0 0
Condition0%0/3
exists $dbline{$lineno} and local $brkpts = $dbline{$lineno}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $dbline{$lineno} and local $brkpts = $dbline{$lineno}
exists $dbline{$lineno} local $brkpts = $dbline{$lineno}
Truth table0%0/3
A and B
A= exists $dbline{$lineno}B= local $brkpts = $dbline{$lineno}
ABresult
0X0
100
111
186 local $brkpts = $DB::dbline{$DB::lineno}) {
187 0 ▶ for (local $i=0; $i < @$brkpts; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$brkpts) 0 0 0
188 0 local $brkpt = $brkpts->[$i];
189 0 ▶ next unless defined $brkpt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $brkpt 0 0 0
190 0 ▶ if ($brkpt->type eq 'action') {
Branch: 1 branch
BranchTrueFalseTotal
if ($brkpt->type eq "action") 0 0 0
191 0 push @action, $brkpt;
192 0 next ;
193 }
194 0 $stop = 0;
195 0 ▶ if ($brkpt->condition eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($brkpt->condition eq '1') { } 0 0 0
196 # A cheap and simple test for unconditional.
197 0 $stop = 1;
198 } else {
199 0 my $eval_str = sprintf("\$DB::stop = do { %s; }",
200 $brkpt->condition);
201 0 my $opts = {return_type => ';', # ignore return
202 namespace_package => $namespace_package,
203 fix_file_and_line => 1,
204 hide_position => 0};
205 0 &DB::eval_with_return($eval_str, $opts, @DB::saved);
206 }
207 0 ▶ if ($stop && $brkpt->enabled && !($DB::single & RETURN_EVENT)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stop and $brkpt->enabled and not $single & 32) 0 0 0
Condition0%0/6
$stop and $brkpt->enabled and not $single & 32
!ll&&!rl&&r
000
$stop and $brkpt->enabled
!ll&&!rl&&r
000
MC/DC0%0/3
$stop and $brkpt->enabled and not $single & 32
$stop $brkpt->enabled not $single & 32
Truth table0%0/4
A and B and not(C)
A= $stopB= $brkpt->enabledC= not $single & 32
ABCresult
0XX0
10X0
1100
1111
208 0 $DB::signal |= 1;
209 0 $DB::brkpt = $brkpt;
210 0 $event = $brkpt->type;
211 0 ▶ if ($event eq 'tbrkpt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq 'tbrkpt') { } 0 0 0
212 # breakpoint is temporary and remove it.
213 0 undef $brkpts->[$i];
214 } else {
215 0 my $hits = $brkpt->hits + 1;
216 0 $brkpt->hits($hits);
217 }
218 0 last;
219 }
220 }
221 }
222 0 ▶ if ($watch_triggered) {
Branch: 6 branches
BranchTrueFalseTotal
if ($watch_triggered) { } 0 0 0
elsif ($signal) { } 0 0 0
elsif ($single & 32) { } 0 0 0
elsif ($trace) { } 0 0 0
elsif ($single & 3) { } 0 0 0
elsif ($single & 4) { } 0 0 0
223 0 $event = 'watch';
224 } elsif ($DB::signal) {
225 0 ▶ $event ||= 'signal';
Condition0%0/2
$event ||= "signal"
l!l
00
MC/DC0%0/1
$event ||= "signal"
$event
Truth table0%0/2
A
A= $event
Aresult
11
00
226 } elsif ($DB::single & RETURN_EVENT) {
227 0 $event = 'return';
228 } elsif ($DB::trace ) {
229 0 ▶ $event ||= 'trace';
Condition0%0/2
$event ||= "trace"
l!l
00
MC/DC0%0/1
$event ||= "trace"
$event
Truth table0%0/2
A
A= $event
Aresult
11
00
230 } elsif ($DB::single & (SINGLE_STEPPING_EVENT | NEXT_STEPPING_EVENT)) {
231 0 ▶ $event ||= 'line';
Condition0%0/2
$event ||= "line"
l!l
00
MC/DC0%0/1
$event ||= "line"
$event
Truth table0%0/2
A
A= $event
Aresult
11
00
232 } elsif ($DB::single & DEEP_RECURSION_EVENT) {
233 0 ▶ $event ||= 'recurse overflow';
Condition0%0/2
$event ||= "recurse overflow"
l!l
00
MC/DC0%0/1
$event ||= "recurse overflow"
$event
Truth table0%0/2
A
A= $event
Aresult
11
00
234 } else {
235 0 $event = 'unknown';
236 }
237  
238 0 ▶ if ($DB::single || $DB::trace || $DB::signal || $event eq 'watch') {
Branch: 1 branch
BranchTrueFalseTotal
if ($single or $trace or $signal or $event eq "watch") 0 0 0
Condition0%0/9
$single or $trace or $signal or $event eq "watch"
l!l&&r!l&&!r
000
$single or $trace or $signal
l!l&&r!l&&!r
000
$single or $trace
l!l&&r!l&&!r
000
MC/DC0%0/4
$single or $trace or $signal or $event eq "watch"
$single $trace $signal $event eq "watch"
Truth table0%0/5
A or B or C or D
A= $singleB= $traceC= $signalD= $event eq "watch"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
239 0 ▶ $DB::subname = ($DB::sub =~ /\'|::/) ? $DB::sub : "${DB::package}::$DB::sub"; #';
Branch: 1 branch
BranchTrueFalseTotal
$sub =~ /\'|::/ ? : 0 0 0
240 0 loadfile($DB::filename, $DB::lineno);
241 }
242  
243 0 local $action;
244 0 for $action (@action) {
245 0 ▶ &DB::eval_with_return($action->condition, {return_type => '$'},
Branch: 1 branch
BranchTrueFalseTotal
if $action->enabled 0 0 0
246 @DB::saved)
247 if $action->enabled;
248 0 my $hits = $action->hits + 1;
249 0 $action->hits($hits);
250 }
251  
252 0 ▶ if ($DB::single || $DB::signal || $watch_triggered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($single or $signal or $watch_triggered) 0 0 0
Condition0%0/6
$single or $signal or $watch_triggered
l!l&&r!l&&!r
000
$single or $signal
l!l&&r!l&&!r
000
MC/DC0%0/3
$single or $signal or $watch_triggered
$single $signal $watch_triggered
Truth table0%0/4
A or B or C
A= $singleB= $signalC= $watch_triggered
ABCresult
1XX1
01X1
0011
0000
253 0 ▶ _warnall($#stack . " levels deep in subroutine calls.\n") if $DB::single & 4;
Branch: 1 branch
BranchTrueFalseTotal
if $single & 4 0 0 0
254 0 $DB::single = 0;
255 0 $DB::signal = 0;
256 0 $DB::running = 0;
257  
258 # FIXME: give a warning...
259 0 $DB::bt_truncated = defined caller($DB::stack_depth+1);
260 0 ▶ $DB::stack_depth++ while defined caller($DB::stack_depth+1);
Branch: 1 branch
BranchTrueFalseTotal
defined caller $stack_depth + 1 and do { ++$stack_depth } 0 0 0
261  
262 0 local $c;
263 0 for $c (@clients) {
264 # Now sit in an event loop until something sets $running
265 0 local $after_eval = 0;
266 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$running or do { do { local $display_aref = $c->display_lists; local $disp; foreach $disp (@$display_aref) { next unless $disp and $disp->enabled; local $opts = {"return_type", $disp->return_type, "namespace_package", $namespace_package, "fix_file_and_line", 1, "hide_position", 0}; local $eval_result = &eval_with_return($disp->arg, $opts, @saved); local $mess; if (defined $eval_result) { $mess = sprintf("%d: $eval_result", $disp->number); } else { $mess = sprintf("%d: undef", $disp->number); } ; $c->output($mess); } ; if (1 == $after_eval) { $event = "after_eval"; } elsif (2 == $after_eval) { $event = "after_nest"; } ; $c->idle($event, $watch_triggered); $after_eval = 0; if ($running == 2 and defined $eval_str) { local $nest = $eval_opts->{"nest"}; my $return_type = $eval_opts->{"return_type"}; $return_type = "" unless defined $return_type; my $opts = $eval_opts; $opts->{"namespace_package"} = $namespace_package; if ("\@" eq $return_type) { &eval_with_return($eval_str, $opts, @saved); } elsif ("%" eq $return_type) { &eval_with_return($eval_str, $opts, @saved); } else { $eval_result = &eval_with_return($eval_str, $opts, @saved); } ; if ($nest) { $in_debugger = 1; $after_eval = 2; } else { $after_eval = 1; } ; $running = 0; } } } 0 0 0
267 # Show display expresions
268 0 local $display_aref = $c->display_lists;
269 0 local $disp;
270 0 for $disp (@$display_aref) {
271 0 ▶ next unless $disp && $disp->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $disp and $disp->enabled 0 0 0
Condition0%0/3
$disp and $disp->enabled
!ll&&!rl&&r
000
MC/DC0%0/2
$disp and $disp->enabled
$disp $disp->enabled
Truth table0%0/3
A and B
A= $dispB= $disp->enabled
ABresult
0X0
100
111
272 0 local $opts = {return_type => $disp->return_type,
273 namespace_package => $namespace_package,
274 fix_file_and_line => 1,
275 hide_position => 0};
276 # FIXME: allow more than just scalar contexts.
277 0 local $eval_result =
278 &DB::eval_with_return($disp->arg, $opts, @DB::saved);
279 0 local $mess;
280 0 ▶ if (defined($eval_result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $eval_result) { } 0 0 0
281 0 $mess = sprintf("%d: $eval_result", $disp->number);
282 } else {
283 0 $mess = sprintf("%d: undef", $disp->number);
284 }
285 0 $c->output($mess);
286 }
287  
288 0 ▶ if (1 == $after_eval ) {
Branch: 2 branches
BranchTrueFalseTotal
if (1 == $after_eval) { } 0 0 0
elsif (2 == $after_eval) { } 0 0 0
289 0 $event = 'after_eval';
290 } elsif (2 == $after_eval) {
291 0 $event = 'after_nest'
292 }
293  
294 # call client event loop; must not block
295 0 $c->idle($event, $watch_triggered);
296 0 $after_eval = 0;
297 0 ▶ if ($running == 2 && defined($eval_str)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($running == 2 and defined $eval_str) 0 0 0
Condition0%0/3
$running == 2 and defined $eval_str
!ll&&!rl&&r
000
MC/DC0%0/2
$running == 2 and defined $eval_str
$running == 2 defined $eval_str
Truth table0%0/3
A and B
A= $running == 2B= defined $eval_str
ABresult
0X0
100
111
298 # client wants something eval-ed
299 # FIXME: turn into subroutine.
300  
301 0 local $nest = $eval_opts->{nest};
302 0 my $return_type = $eval_opts->{return_type};
303 0 ▶ $return_type = '' unless defined $return_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $return_type 0 0 0
304 0 my $opts = $eval_opts;
305 0 $opts->{namespace_package} = $namespace_package;
306  
307 0 ▶ if ('@' eq $return_type) {
Branch: 2 branches
BranchTrueFalseTotal
if ('@' eq $return_type) { } 0 0 0
elsif ('%' eq $return_type) { } 0 0 0
308 0 &DB::eval_with_return($eval_str, $opts, @DB::saved);
309 } elsif ('%' eq $return_type) {
310 0 &DB::eval_with_return($eval_str, $opts, @DB::saved);
311 } else {
312 0 $eval_result =
313 &DB::eval_with_return($eval_str, $opts, @DB::saved);
314 }
315  
316 0 ▶ if ($nest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nest) { } 0 0 0
317 0 $DB::in_debugger = 1;
318 0 $after_eval = 2;
319 } else {
320 0 $after_eval = 1;
321 }
322 0 $DB::running = 0;
323 }
324 } until $running;
325 }
326 }
327  
328 0 $DB::event = undef;
329 0 ($EVAL_ERROR, $ERRNO, $EXTENDED_OS_ERROR,
330 $OUTPUT_FIELD_SEPARATOR,
331 $INPUT_RECORD_SEPARATOR,
332 $OUTPUT_RECORD_SEPARATOR, $WARNING) = @DB::saved;
333 0 ();
334 }
335  
336 =head1 RESTART SUPPORT
337  
338 These routines are used to store (and restore) lists of items in environment
339 variables during a restart.
340  
341 =head2 set_list
342  
343 Set_list packages up items to be stored in a set of environment variables
344 (VAR_n, containing the number of items, and VAR_0, VAR_1, etc., containing
345 the values). Values outside the standard ASCII charset are stored by encoding
346 then as hexadecimal values.
347  
348 =cut
349  
350 sub set_list
351 0 ▶{
Subroutine set_list: not called CC=2 SCAR=17.9
Pod undocumented
352 0 my ( $stem, @list ) = @_;
353 0 my $val;
354  
355 # VAR_n: how many we have. Scalar assignment gets the number of items.
356 0 $ENV{"${stem}_n"} = @list;
357  
358 # Grab each item in the list, escape the backslashes, encode the non-ASCII
359 # as hex, and then save in the appropriate VAR_0, VAR_1, etc.
360 0 for $i ( 0 .. $#list ) {
361 0 $val = $list[$i];
362 0 $val =~ s/\\/\\\\/g;
363 0 $val =~ s/([\0-\37\177\200-\377])/"\\0x" . unpack('H2',$1)/eg;
364 0 $ENV{"${stem}_$i"} = $val;
365 } ## end for $i (0 .. $#list)
366 } ## end sub set_list
367  
368 =head2 get_list
369  
370 Reverse the set_list operation: grab VAR_n to see how many we should be getting
371 back, and then pull VAR_0, VAR_1. etc. back out.
372  
373 =cut
374  
375 0 ▶sub get_list {
Subroutine get_list: not called CC=3 SCAR=24.8
Pod undocumented
376 0 my $stem = shift;
377 0 my @list;
378 0 my $n = delete $ENV{"${stem}_n"};
379 0 my $val;
380 0 for $i ( 0 .. $n - 1 ) {
381 0 $val = delete $ENV{"${stem}_$i"};
382 0 ▶ $val =~ s/\\((\\)|0x(..))/ $2 ? $2 : pack('H2', $3) /ge;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 0 0 0
383 0 push @list, $val;
384 }
385 0 @list;
386 } ## end sub get_list
387  
388 ###############################################################################
389 # no compile-time subroutine call allowed before this point #
390 ###############################################################################
391  
392 # this can run only after DB() and sub() are defined
393 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
394  
395 # Need this until we replace "save" with "save_vars" in Enbugger/trepan.pm
396 0 ▶sub save { die "Remember to update Enbugger/trepan.pm" };
Subroutine save: not called CC=1 SCAR=6.9
Pod undocumented
397  
398 # Like DB::save from perl5db.pl, but want to use another name to
399 # reduce prototype conflict of save $ vs none if we use perl5db.pl to
400 # debug Devel::Trepan.
401 0 ▶sub save_vars() {
Subroutine save_vars: not called CC=1 SCAR=6.9
Pod undocumented
402 0 @DB::saved = ( $EVAL_ERROR, $ERRNO, $EXTENDED_OS_ERROR,
403 $OUTPUT_FIELD_SEPARATOR,
404 $INPUT_RECORD_SEPARATOR,
405 $OUTPUT_RECORD_SEPARATOR, $WARNING );
406  
407 0 $OUTPUT_FIELD_SEPARATOR = "";
408 0 $INPUT_RECORD_SEPARATOR = "\n";
409 0 $OUTPUT_RECORD_SEPARATOR = "";
410 0 $WARNING = 0; # warnings off
411 }
412  
413 0 ▶sub catch {
Subroutine catch: not called CC=12 SCAR=50.5
Pod undocumented
414 0 @DB::_ = @_;
415 0 $DB::caller = [CORE::caller];
416 ($DB::package, $DB::filename, $DB::lineno, $DB::subroutine, $DB::hasargs,
417 $DB::wantarray, $DB::evaltext, $DB::is_require, $DB::hints, $DB::bitmask,
418 $DB::hinthash
419 0 ) = @{$DB::caller};
420  
421 # Set package namespace for running eval's in the user context.
422 # However this won't let them modify lexical variables, alas.
423 0 my $namespace_package = "package $DB::package;";
424  
425 0 $event = 'post-mortem';
426 0 $running = 0;
427 0 for my $c (@clients) {
428 # Now sit in an event loop until something sets $running
429 0 my $after_eval = 0;
430 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$DB::running or do { do { my $display_aref = $c->display_lists; foreach my $disp (@$display_aref) { next unless $disp and $disp->enabled; my $opts = {"return_type", $disp->return_type, "namespace_package", $namespace_package, "fix_file_and_line", 1, "hide_position", 0}; my $eval_result = &eval_with_return($disp->arg, $opts, @DB::saved); my $mess = sprintf("%d: $eval_result", $disp->number); $c->output($mess); } ; if (1 == $after_eval) { $DB::event = "after_eval"; } elsif (2 == $after_eval) { $DB::event = "after_nest"; } ; $c->idle($DB::event, 0); $after_eval = 0; if ($DB::running == 2 and defined $DB::eval_str) { my $opts = $DB::eval_opts; $opts->{"namespace_package"} = $namespace_package; if ("\@" eq $opts->{"return_type"}) { &eval_with_return($DB::eval_str, $opts, @DB::saved); } elsif ("%" eq $opts->{"return_type"}) { &eval_with_return($DB::eval_str, $opts, @DB::saved); } else { $DB::eval_result = &eval_with_return($DB::eval_str, $opts, @DB::saved); } ; $after_eval = 1; $DB::running = 0; } } } 0 0 0
431 # Show display expresions
432 0 my $display_aref = $c->display_lists;
433 0 for my $disp (@$display_aref) {
434 0 ▶ next unless $disp && $disp->enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $disp and $disp->enabled 0 0 0
Condition0%0/3
$disp and $disp->enabled
!ll&&!rl&&r
000
MC/DC0%0/2
$disp and $disp->enabled
$disp $disp->enabled
Truth table0%0/3
A and B
A= $dispB= $disp->enabled
ABresult
0X0
100
111
435 0 my $opts = {
436 return_type => $disp->return_type,
437 namespace_package => $namespace_package,
438 fix_file_and_line => 1,
439 hide_position => 0};
440 0 my $eval_result = &DB::eval_with_return($disp->arg, $opts,
441 @DB::saved);
442 0 my $mess = sprintf("%d: $eval_result", $disp->number);
443 0 $c->output($mess);
444 }
445  
446 0 ▶ if (1 == $after_eval ) {
Branch: 2 branches
BranchTrueFalseTotal
if (1 == $after_eval) { } 0 0 0
elsif (2 == $after_eval) { } 0 0 0
447 0 $event = 'after_eval';
448 } elsif (2 == $after_eval) {
449 0 $event = 'after_nest'
450 }
451  
452 # call client event loop; must not block
453 0 $c->idle($event, 0);
454 0 $after_eval = 0;
455 0 ▶ if ($running == 2 && defined($eval_str)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DB::running == 2 and defined $DB::eval_str) 0 0 0
Condition0%0/3
$DB::running == 2 and defined $DB::eval_str
!ll&&!rl&&r
000
MC/DC0%0/2
$DB::running == 2 and defined $DB::eval_str
$DB::running == 2 defined $DB::eval_str
Truth table0%0/3
A and B
A= $DB::running == 2B= defined $DB::eval_str
ABresult
0X0
100
111
456 # client wants something eval-ed
457 # FIXME: turn into subroutine.
458  
459 0 my $opts = $eval_opts;
460 0 $opts->{namespace_package} = $namespace_package;
461  
462 0 ▶ if ('@' eq $opts->{return_type}) {
Branch: 2 branches
BranchTrueFalseTotal
if ('@' eq $opts->{'return_type'}) { } 0 0 0
elsif ('%' eq $opts->{'return_type'}) { } 0 0 0
463 0 &DB::eval_with_return($eval_str, $opts, @DB::saved);
464 } elsif ('%' eq $opts->{return_type}) {
465 0 &DB::eval_with_return($eval_str, $opts, @DB::saved);
466 } else {
467 0 $eval_result =
468 &DB::eval_with_return($eval_str, $opts, @DB::saved);
469 }
470  
471 0 $after_eval = 1;
472 0 $running = 0;
473 }
474 } until $running;
475 }
476 }
477  
478 ####
479 #
480 # Client callable (read inheritable) methods defined after this point
481 #
482 ####
483  
484 3 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
485 3 my $s = shift;
486 # $s = _clientname($s) if ref($s);
487 3 push @clients, $s;
488 }
489  
490 0 ▶sub done {
Subroutine done: not called CC=3 SCAR=24.8
Pod documented
491 0 my $s = shift;
492 0 ▶ $s = _clientname($s) if ref($s);
Branch: 1 branch
BranchTrueFalseTotal
if ref $s 0 0 0
493 0 @clients = grep {$_ ne $s} @clients;
494 0 $s->cleanup;
495 # $running = 3 unless @clients;
496 0 ▶ exit(0) unless @clients;
Branch: 1 branch
BranchTrueFalseTotal
unless @DB::clients 0 0 0
497 }
498  
499 0 ▶sub _clientname {
Subroutine _clientname: not called CC=1 SCAR=6.9
500 0 my $name = shift;
501 0 "$name" =~ /^(.+)=[A-Z]+\(.+\)$/;
502 0 return $1;
503 }
504  
505 0 ▶sub step {
Subroutine step: not called CC=1 SCAR=6.9
Pod documented
506 0 my $s = shift;
507 0 $DB::single = SINGLE_STEPPING_EVENT;
508 0 $DB::running = 1;
509 }
510  
511 # cont
512 # cont fn_or_line
513 # cont file line
514 #
515 0 ▶sub cont {
Subroutine cont: not called CC=6 SCAR=37.4
Pod undocumented
516 0 my $s = shift;
517 0 ▶ if (scalar @_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 0) 0 0 0
518 0 my ($file, $line);
519 0 ▶ if (2 == scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 == scalar @_) { } 0 0 0
520 0 ($file, $line) = @_;
521 } else {
522 0 ($file, $line) = ($DB::filename, $_[0]);
523 }
524 0 my $brkpt = $s->set_tbreak($file, $line);
525 0 ▶ return 0 unless $brkpt;
Branch: 1 branch
BranchTrueFalseTotal
unless $brkpt 0 0 0
526 }
527 0 ▶ for (my $i = 0; $i <= $#stack;) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#DB::stack) 0 0 0
528 0 ▶ if (defined $stack[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $DB::stack[$i]) { } 0 0 0
529 0 $stack[$i++] &= ~1 ;
530 } else {
531 # If Enbugger is used $stack[$i] might not be defined
532 0 $stack[$i++] = 0;
533 }
534 }
535 0 $DB::single = 0;
536 0 return $DB::running = 1;
537 }
538  
539 # stop before finishing the current subroutine
540 0 ▶sub finish($;$$) {
Subroutine finish: not called CC=9 SCAR=45.0
Pod undocumented
541 0 my $s = shift;
542 # how many levels to get to DB sub?
543 0 ▶ my $count = scalar @_ >= 1 ? shift : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ >= 1 ? : 0 0 0
544 0 ▶ my $scan_for_DB_sub = scalar @_ >= 1 ? shift : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ >= 1 ? : 0 0 0
545  
546 0 ▶ if ($scan_for_DB_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scan_for_DB_sub) 0 0 0
547 0 my $i = 0;
548 0 ▶ while (my ($pkg, $file, $line, $fn) = CORE::caller($i++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 0 0 0
549 # Note: The function parameter of caller(), $fn, gives the
550 # function that was used rather than the function that the
551 # caller is currently in. Therefore, the implicitly line
552 # calling DB:DB is the one we want to stop at.
553 0 ▶ if ('DB::DB' eq $fn or ('DB' eq $pkg && 'DB' eq $fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("DB::DB" eq $fn or "DB" eq $pkg and "DB" eq $fn) 0 0 0
Condition0%0/6
"DB::DB" eq $fn or "DB" eq $pkg and "DB" eq $fn
l!l&&r!l&&!r
000
"DB" eq $pkg and "DB" eq $fn
!ll&&!rl&&r
000
MC/DC0%0/3
"DB::DB" eq $fn or "DB" eq $pkg and "DB" eq $fn
"DB::DB" eq $fn "DB" eq $pkg "DB" eq $fn
Truth table0%0/4
A or B and C
A= "DB::DB" eq $fnB= "DB" eq $pkgC= "DB" eq $fn
ABCresult
1XX1
0111
00X0
0100
554 # FIXME: This is hoaky. 4 is somehow how far off
555 # @stack is from caller.
556 0 $i -= 4;
557 0 last;
558 }
559 }
560 0 $count += $i;
561 }
562  
563 0 my $index = $#stack-$count;
564 0 ▶ $index = 0 if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 0 0
565 0 $stack[$index] |= RETURN_EVENT;
566 0 $DB::single = RETURN_EVENT;
567 0 $DB::running = 1;
568 }
569  
570 sub return_value($)
571 0 ▶{
Subroutine return_value: not called CC=3 SCAR=24.8
Pod undocumented
572 0 ▶ if ('undef' eq $DB::return_type) {
Branch: 2 branches
BranchTrueFalseTotal
if ('undef' eq $DB::return_type) { } 0 0 0
elsif ('array' eq $DB::return_type) { } 0 0 0
573 0 return undef;
574 } elsif ('array' eq $DB::return_type) {
575 0 return @DB::return_value;
576 } else {
577 0 return $DB::return_value;
578 }
579 }
580  
581 sub return_type($)
582 0 ▶{
Subroutine return_type: not called CC=1 SCAR=6.9
Pod undocumented
583 0 $DB::return_type;
584 }
585  
586 0 ▶sub _outputall {
Subroutine _outputall: not called CC=2 SCAR=17.9
587 0 my $c;
588 0 for $c (@clients) {
589 0 $c->output(@_);
590 }
591 }
592  
593 0 ▶sub _warnall {
Subroutine _warnall: not called CC=2 SCAR=17.9
594 0 my $c;
595 0 for $c (@clients) {
596 0 $c->warning(@_);
597 }
598 }
599  
600 0 ▶sub trace_toggle {
Subroutine trace_toggle: not called CC=1 SCAR=6.9
Pod undocumented
601 0 my $s = shift;
602 0 $DB::trace = !$DB::trace;
603 }
604  
605  
606 ####
607 # first argument is a filename whose subs will be returned
608 # if a filename is not supplied, all subs in the current
609 # filename are returned.
610 #
611 0 ▶sub filesubs {
Subroutine filesubs: not called CC=2 SCAR=17.9
Pod undocumented
612 0 my $s = shift;
613 0 my $fname = shift;
614 0 ▶ $fname = $DB::filename unless $fname;
Branch: 1 branch
BranchTrueFalseTotal
unless $fname 0 0 0
615 0 return grep { $DB::sub{$_} =~ /^$fname/ } keys %DB::sub;
616 }
617  
618 ####
619 # returns a list of all filenames that DB knows about
620 #
621 0 ▶sub files {
Subroutine files: not called CC=1 SCAR=6.9
Pod undocumented
622 0 my $s = shift;
623 0 my(@f) = grep(m|^_<|, keys %main::);
624 0 return map { substr($_,2) } @f;
625 }
626  
627 ####
628 # loadfile($file, $line)
629 #
630 0 ▶sub loadfile {
Subroutine loadfile: not called CC=5 SCAR=34.0
Pod undocumented
631 0 my($file, $line) = @_;
632 0 ▶ if (!defined $main::{'_<' . $file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $main::main::{'_<' . $file}) 0 0 0
633 0 my $try;
634 0 ▶ if (($try) = grep(m|^_<.*$file|, keys %main::)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($try) = grep(/^_<.*$file/, keys %main::main::)) 0 0 0
635 0 $file = substr($try,2);
636 }
637 }
638 0 ▶ if (defined($main::{'_<' . $file})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::main::{'_<' . $file}) 0 0 0
639 0 my $c;
640 # _outputall("Loading file $file..");
641 0 *DB::dbline = "::_<$file";
642 0 $DB::filename = $file;
643 0 for $c (@clients) {
644 # print "2 ", $file, '|', $line, "\n";
645 0 $c->showfile($file, $line);
646 }
647 0 return $file;
648 }
649 0 return undef;
650 }
651  
652 #
653 # "pure virtual" methods
654 #
655  
656 9 ▶sub skippkg {
Subroutine skippkg: called CC=2 SCAR=7.2
Pod undocumented
657 9 my $s = shift;
658 9 ▶ push @skippkg, @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 0 9
659 }
660  
661 0 ▶sub evalcode {
Subroutine evalcode: not called CC=2 SCAR=17.9
Pod documented
662 0 my ($client, $expr) = @_;
663 0 ▶ if (defined $expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expr) 0 0 0
664 0 $DB::running = 2; # hand over to DB() to evaluate in its context
665 0 $ineval->{$client} = $expr;
666 }
667 0 return $ineval->{$client};
668 }
669  
670 3 ▶sub ready {
Subroutine ready: called CC=1 SCAR=0.0
Pod undocumented
671 3 my $s = shift;
672 3 return $ready = 1;
673 }
674  
675 0 ▶sub idle {}
Subroutine idle: not called CC=1 SCAR=0.0
Pod documented
676 0 ▶sub cleanup {}
Subroutine cleanup: not called CC=1 SCAR=0.0
Pod documented
677 0 ▶sub output {}
Subroutine output: not called CC=1 SCAR=0.0
Pod documented
678 0 ▶sub warning {}
Subroutine warning: not called CC=1 SCAR=0.0
Pod undocumented
679 0 ▶sub showfile {}
Subroutine showfile: not called CC=1 SCAR=0.0
Pod undocumented
680  
681 12 $SIG{'INT'} = \&DB::catch;
682  
683 12 1;
684 __END__
↑ Summary lib/Devel/Trepan/DB/Backtrace.pm »