blib/lib/dbgp-helper/DB/DbgrContext.pm

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 266 CC 57 SCAR 53.3
CC
57
Cov
0%
getContextProperties
420.0
getProximityVarsViaB
240.0
_hasActiveIterator
56.0
CRAP 205.7
« blib/lib/dbgp-helper/DB/DbgrProperties.pm ↑ Summary blib/lib/dbgp-helper/DB/CGI/Util.pm »
1 # DbgrProperties.pm -- Move all the property-handling code
2 # into this module.
3 #
4 # Copyright (c) 1998-2006 ActiveState Software Inc.
5 # All rights reserved.
6 #
7 # Xdebug compatibility, UNIX domain socket support and misc fixes
8 # by Mattia Barbon <mattia@barbon.org>
9 #
10 # This software (the Perl-DBGP package) is covered by the Artistic License
11 # (http://www.opensource.org/licenses/artistic-license.php).
12  
13 package DB::DbgrContext;
14  
15 use strict qw(vars subs);
16  
17 require Exporter;
18 our @ISA = qw(Exporter);
19 our @EXPORT = qw(
20 emitContextNames
21 emitContextProperties
22 getContextProperties
23 getProximityVarsViaPadWalker
24 hasPadWalker
25 GlobalVars
26 LocalVars
27 FunctionArguments
28 PunctuationVariables
29 );
30 our @EXPORT_OK = ();
31  
32 use DB::DbgrCommon;
33 use DB::DbgrProperties;
34  
35 use constant LocalVars => 0;
36 use constant GlobalVars => 1;
37 use constant FunctionArguments => 2;
38 use constant PunctuationVariables => 3;
39  
40 our $ldebug = 0;
41  
42 my %contextProperties = (
43 Globals => GlobalVars,
44 Locals => LocalVars,
45 Arguments => FunctionArguments,
46 Special => PunctuationVariables,
47 );
48 my @contextProperties = sort { $contextProperties{$a} <=> $contextProperties{$b} } keys %contextProperties;
49  
50 my @_punctuationVariables = ('$_', '$?', '$@', '$.', '@+', '@-', '$+', '$!', '$$', '$0');
51 # $`, $& and $' are special-cased to avoid the performance penalty
52 my @_rxPunctuationVariables = ('`', '&', '\'');
53  
54 sub emitContextNames($$) {
55 0 ▶ my ($cmd, $transactionID) = @_;
Subroutine emitContextNames: not called CC=2 SCAR=17.9
Pod undocumented
56 0 my $res = sprintf(qq(%s\n<response %s command="%s"
57 transaction_id="%s" >),
58 xmlHeader(),
59 namespaceAttr(),
60 $cmd,
61 $transactionID);
62 # todo: the spec suggests that locals be the default,
63 # but globals (package globals) make more sense for Perl.
64 0 ▶ for (my $i = 0; $i <= $#contextProperties; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#contextProperties) 0 0 0
65 0 $res .= sprintf(qq(<context name="%s" id="%d" />\n),
66 $contextProperties[$i],
67 $i);
68 }
69 0 $res .= "\n</response>\n";
70 0 printWithLength($res);
71 }
72  
73 # Return a ref to an array of [name, value, needValue] triples
74 #
75 # Some values can be evaluated in this scope, but non-package values
76 # and locals at the top-level will need to be evaluated in the
77 # debugger's main loop.
78  
79 sub getContextProperties($$) {
80 0 ▶ my ($context_id, $packageName) = @_;
Subroutine getContextProperties: not called CC=20 SCAR=60.4
Pod undocumented
81  
82 # Here just show the top-level.
83 0 local $settings{max_depth}[0] = 0;
84 0 ▶ if ($context_id == GlobalVars) {
Branch: 2 branches
BranchTrueFalseTotal
if ($context_id == 1) { } 0 0 0
elsif ($context_id == 3) { } 0 0 0
85 0 ▶ require B if $] >= 5.010;
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.01" 0 0 0
86 # Globals
87 # Variables on the calling frame
88 0 my $stash = \%{"${packageName}::"};
89 0 my @results;
90 0 for my $key (keys %$stash) {
91 0 ▶ next if $key =~ /^(?:_<|[^0a-zA-Z_])/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^(?:_<|[^0a-zA-Z_])/ 0 0 0
92 0 ▶ next if $key =~ /::$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /::$/ 0 0 0
93 0 my $glob = \($stash->{$key});
94 0 my ($has_scalar, $is_glob);
95 0 ▶ if ($] >= 5.010) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.01) { } 0 0 0
96 0 my $gv = B::svref_2object($glob);
97 0 $is_glob = $gv->isa("B::GV");
98 0 ▶ $has_scalar = $is_glob && !$gv->SV->isa('B::SPECIAL');
Condition0%0/3
$is_glob && !$gv->SV->isa('B::SPECIAL')
!ll&&!rl&&r
000
MC/DC0%0/2
$is_glob && !$gv->SV->isa('B::SPECIAL')
$is_glob !$gv->SV->isa('B::SPECIAL')
Truth table0%0/3
A && not(B)
A= $is_globB= !$gv->SV->isa('B::SPECIAL')
ABresult
0X0
100
111
99 } else {
100 0 $is_glob = 1;
101 0 $has_scalar = defined ${*{$glob}{SCALAR}};
102 }
103 0 ▶ my $array = $is_glob && *{$glob}{ARRAY};
Condition0%0/2
$is_glob && *{$glob;}{'ARRAY'}
!ll
00
MC/DC0%0/1
$is_glob && *{$glob;}{'ARRAY'}
$is_glob
Truth table0%0/2
A
A= $is_glob
Aresult
00
11
104 0 ▶ my $hash = $is_glob && *{$glob}{HASH};
Condition0%0/2
$is_glob && *{$glob;}{'HASH'}
!ll
00
MC/DC0%0/1
$is_glob && *{$glob;}{'HASH'}
$is_glob
Truth table0%0/2
A
A= $is_glob
Aresult
00
11
105 0 ▶ next unless $has_scalar || $array || $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_scalar or $array or $hash 0 0 0
Condition0%0/6
$has_scalar or $array or $hash
l!l&&r!l&&!r
000
$has_scalar or $array
l!l&&r!l&&!r
000
MC/DC0%0/3
$has_scalar or $array or $hash
$has_scalar $array $hash
Truth table0%0/4
A or B or C
A= $has_scalarB= $arrayC= $hash
ABCresult
1XX1
01X1
0011
0000
106 0 ▶ push @results, ["\$${key}", ${*{$glob}{SCALAR}}, 0] if $has_scalar;
Branch: 1 branch
BranchTrueFalseTotal
if $has_scalar 0 0 0
107 0 ▶ push @results, ["\@${key}", $array, 0] if $array;
Branch: 1 branch
BranchTrueFalseTotal
if $array 0 0 0
108 0 ▶ push @results, ["\%${key}", $hash, 0] if $hash;
Branch: 1 branch
BranchTrueFalseTotal
if $hash 0 0 0
109 }
110 0 return \@results;
111 } elsif ($context_id == PunctuationVariables) {
112 0 my @results;
113 0 foreach my $pv (@_punctuationVariables) {
114 0 push (@results, [$pv, undef, 1]);
115 }
116 0 foreach my $pv (@_rxPunctuationVariables) {
117 # somebody might use @' and trigger the condition, I'm willing to risk it
118 0 ▶ push (@results, ["\$$pv", undef, exists $main::{$pv} ? 1 : 0]);
Branch: 1 branch
BranchTrueFalseTotal
exists $main::main::{$pv} ? : 0 0 0
119 }
120 0 return \@results;
121 } else {
122 0 die sprintf("code:%d:error:%s",
123 302,
124 ("Not ready to evaluate "
125 . $contextProperties[$context_id]
126 . ' variables'));
127 }
128 }
129  
130 sub emitContextProperties($$$$;$) {
131 0 ▶ my ($cmd,
Subroutine emitContextProperties: not called CC=4 SCAR=30.0
Pod undocumented
132 $transactionID,
133 $context_id,
134 $nameValuesARef,
135 $maxDataSize) = @_;
136
137 0 my $res = sprintf(qq(%s\n<response %s command="%s"
138 context_id="%d"
139 transaction_id="%s" >),
140 xmlHeader(),
141 namespaceAttr(),
142 $cmd,
143 $context_id,
144 $transactionID);
145 0 my @results = @$nameValuesARef;
146 0 my $numVars = scalar @results;
147 0 ▶ for (my $i = 0; $i < $numVars; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $numVars) 0 0 0
148 0 my $result = $results[$i];
149 0 my $name = $result->[0];
150 0 my $val = $result->[1];
151 0 eval {
152 0 my $property = getFullPropertyInfoByValue($name,
153 $name,
154 $val,
155 $maxDataSize,
156 0,
157 0);
158 # dblog("emitContextProperties: getFullPropertyInfoByValue => $property") if $ldebug;
159 0 $res .= $property;
160 };
161 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
162 0 ▶ dblog("emitContextProperties: error [$@]") if $ldebug;
Branch: 1 branch
BranchTrueFalseTotal
if $ldebug 0 0 0
163 }
164 }
165 0 $res .= "\n</response>";
166 0 printWithLength($res);
167 }
168  
169 sub _hasActiveArrayIterator {
170 0 ▶ my ($b) = @_;
Subroutine _hasActiveArrayIterator: not called CC=6 SCAR=37.4
171 0 ▶ for (my $magic = $b->MAGIC ; $magic; $magic = $magic->MOREMAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($magic) 0 0 0
172 0 ▶ next if $magic->TYPE ne '@';
Branch: 1 branch
BranchTrueFalseTotal
if $magic->TYPE ne "\@" 0 0 0
173 # undocumented internals? which undocumented internals?
174 return $Config::Config{ivsize} == 4 ||
175 ($] >= 5.027006 &&
176 0 ▶ $Config::Config{sizesize} == $Config::Config{ivsize}) ?
Branch: 1 branch
BranchTrueFalseTotal
$Config::Config{'ivsize'} == 4 || $] >= 5.027006 && $Config::Config{'sizesize'} == $Config::Config{'ivsize'} ? : 0 0 0
Condition0%0/6
$Config::Config{"ivsize"} == 4 or $] >= "5.027006" and $Config::Config{"sizesize"} == $Config::Config{"ivsize"}
l!l&&r!l&&!r
000
$] >= "5.027006" and $Config::Config{"sizesize"} == $Config::Config{"ivsize"}
!ll&&!rl&&r
000
MC/DC0%0/3
$Config::Config{"ivsize"} == 4 or $] >= "5.027006" and $Config::Config{"sizesize"} == $Config::Config{"ivsize"}
$Config::Config{"ivsize"} == 4 $] >= "5.027006" $Config::Config{"sizesize"} == $Config::Config{"ivsize"}
Truth table0%0/4
A or B and C
A= $Config::Config{"ivsize"} == 4B= $] >= "5.027006"C= $Config::Config{"sizesize"} == $Config::Config{"ivsize"}
ABCresult
1XX1
0111
00X0
0100
177 $magic->LENGTH :
178 unpack('j', $magic->PTR) != 0;
179 }
180 0 return 0;
181 }
182  
183 sub _hasActiveIterator {
184 0 ▶ my ($sigil, $vref) = @_;
Subroutine _hasActiveIterator: not called CC=7 SCAR=40.3
185 0 require B;
186 0 ▶ if ($sigil eq '$' && (my $kind = ref $$vref)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sigil eq '$' and my $kind = ref $$vref) { } 0 0 0
elsif ($sigil eq '@') { } 0 0 0
elsif ($sigil eq '%') { } 0 0 0
Condition0%0/3
$sigil eq "\$" and my $kind = ref $$vref
!ll&&!rl&&r
000
MC/DC0%0/2
$sigil eq "\$" and my $kind = ref $$vref
$sigil eq "\$" my $kind = ref $$vref
Truth table0%0/3
A and B
A= $sigil eq "\$"B= my $kind = ref $$vref
ABresult
0X0
100
111
187 0 ▶ if ($kind eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'ARRAY') { } 0 0 0
elsif ($kind eq 'HASH') { } 0 0 0
188 0 return _hasActiveArrayIterator(B::svref_2object($$vref));
189 } elsif ($kind eq 'HASH') {
190 0 return B::svref_2object($$vref)->RITER != -1;
191 }
192 } elsif ($sigil eq '@') {
193 0 return _hasActiveArrayIterator(B::svref_2object($vref));
194 } elsif ($sigil eq '%') {
195 0 return B::svref_2object($vref)->RITER != -1;
196 }
197 }
198  
199 sub getProximityVarsViaPadWalker($$$$) {
200 0 ▶ my ($pkg, $filename, $line, $stackDepth) = @_;
Subroutine getProximityVarsViaPadWalker: not called CC=7 SCAR=40.3
Pod undocumented
201 0 $stackDepth += 2; # Because we're two levels above the user code here.
202 0 my $my_var_hash = PadWalker::peek_my($stackDepth);
203 0 my $our_var_hash = PadWalker::peek_our($stackDepth);
204 0 my %merged_vars = (%$my_var_hash, %$our_var_hash);
205 0 our @dbline;
206 0 local *dbline = $main::{'_<' . $filename};
207 0 my $sourceText = join("\n", @dbline);
208  
209 0 my @results = ();
210 0 ▶ while(my($k, $v) = each %merged_vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %merged_vars) 0 0 0
211 0 my $sigil = substr($k, 0, 1);
212 0 ▶ if (!_hasActiveIterator($sigil, $v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not _hasActiveIterator($sigil, $v)) { } 0 0 0
elsif ($ldebug) { } 0 0 0
213 0 ▶ push(@results, [$k, $sigil eq '$' ? $$v : $v, 0]);
Branch: 1 branch
BranchTrueFalseTotal
$sigil eq '$' ? : 0 0 0
214 } elsif ($ldebug) {
215 0 dblog("Skipping $k because it has an active iterator");
216 }
217 }
218 0 ▶ if (! exists $merged_vars{'$_'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $merged_vars{"\$_"}) 0 0 0
219 0 my $dollar_under_val = eval('$_');
220 0 ▶ if (defined $dollar_under_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dollar_under_val) 0 0 0
221 0 push(@results, ['$_', $dollar_under_val, 0]);
222 }
223 }
224 0 return \@results;
225 }
226  
227 {
228 # needs to be defined in package DB, otherwise eval "" sees a package
229 # which is not "DB" and uses it to get the lexical context
230 package DB;
231  
232 sub getProximityVarsViaB($$$$) {
233 package DB::DbgrContext;
234 0 ▶ my ($pkg, $filename, $line, $stackDepth) = @_;
Subroutine getProximityVarsViaB: not called CC=15 SCAR=54.8
Pod undocumented
235 # there is no accurate way of getting these without PadWalker, and
236 # there is not way to get the values without PadWalker anyway
237 0 ▶ return [] if $stackDepth != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $stackDepth != 0 0 0 0
238 0 require B;
239 0 undef *lex_var_hook;
240 0 my $b_cv = eval "sub DB::lex_var_hook {};
241 B::svref_2object(\\&DB::lex_var_hook)->OUTSIDE->OUTSIDE";
242 0 my ($evaltext, %vars, @vars) = ('');
243 0 ▶ for ( ; $b_cv && !$b_cv->isa('B::SPECIAL'); $b_cv = $b_cv->OUTSIDE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b_cv and not $b_cv->isa("B::SPECIAL")) 0 0 0
Condition0%0/3
$b_cv and not $b_cv->isa("B::SPECIAL")
!ll&&!rl&&r
000
MC/DC0%0/2
$b_cv and not $b_cv->isa("B::SPECIAL")
$b_cv not $b_cv->isa("B::SPECIAL")
Truth table0%0/3
A and not(B)
A= $b_cvB= not $b_cv->isa("B::SPECIAL")
ABresult
0X0
100
111
244 0 my $pad = $b_cv->PADLIST->ARRAYelt(0);
245 0 ▶ for my $i (1 .. ($] < 5.022 ? $pad->FILL : $pad->MAX)) {
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.022 ? : 0 0 0
246 0 my $v = $pad->ARRAYelt($i);
247 0 ▶ next if $v->isa('B::SPECIAL') || !$v->LEN;
Branch: 1 branch
BranchTrueFalseTotal
if $v->isa("B::SPECIAL") or not $v->LEN 0 0 0
Condition0%0/3
$v->isa("B::SPECIAL") or not $v->LEN
l!l&&r!l&&!r
000
MC/DC0%0/2
$v->isa("B::SPECIAL") or not $v->LEN
$v->isa("B::SPECIAL") not $v->LEN
Truth table0%0/3
A or not(B)
A= $v->isa("B::SPECIAL")B= not $v->LEN
ABresult
1X1
011
000
248 0 ▶ my $name = $] < 5.022 ? ${$v->object_2svref} : $v->PV;
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.022 ? : 0 0 0
249 0 ▶ next if $vars{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $vars{$name} 0 0 0
250 0 $vars{$name} = 1;
251 0 push @vars, $name;
252 # take a reference to avoid resetting hash iterators
253 0 $evaltext .= "scalar eval '\\$name',\n";
254 }
255 }
256 0 DB::simple_eval("use strict; \@DB::lex_vars_list = ($evaltext)");
257 0 my @results;
258 0 for my $i (0 .. $#DB::lex_vars_list) {
259 0 ▶ next unless my $value = $DB::lex_vars_list[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless my $value = $DB::lex_vars_list[$i] 0 0 0
260 0 my $sigil = substr($vars[$i], 0, 1);
261 0 ▶ if (!_hasActiveIterator($sigil, $value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not _hasActiveIterator($sigil, $value)) { } 0 0 0
elsif ($ldebug) { } 0 0 0
262 0 ▶ push @results, [$vars[$i], $sigil eq '$' ? $$value : $value, 0];
Branch: 1 branch
BranchTrueFalseTotal
$sigil eq '$' ? : 0 0 0
263 } elsif ($ldebug) {
264 0 dblog("Skipping $vars[$i] because it has an active iterator");
265 }
266 }
267 0 return \@results;
268 }
269 }
270  
271 # -1: unknown, 0: no, 1:yes #### Do not init as 1, only -1 or 0.
272 # bug 93570 - allow padwalker detection/use to be disabled
273 my $havePadWalker = $ENV{DBGP_PERL_IGNORE_PADWALKER} ? 0 : -1;
274  
275 sub hasPadWalker {
276 0 ▶ if ($havePadWalker == -1) {
Subroutine hasPadWalker: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if ($havePadWalker == -1) 0 0 0
277 0 local $@;
278 0 eval {
279 0 require PadWalker;
280 0 PadWalker->VERSION(0.08);
281 0 $havePadWalker = 1;
282 };
283 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
284 0 $havePadWalker = 0;
285 }
286 }
287 0 return $havePadWalker;
288 }
289  
290 1;
« blib/lib/dbgp-helper/DB/DbgrProperties.pm ↑ Summary blib/lib/dbgp-helper/DB/CGI/Util.pm »