lib/Devel/Trepan/DB/Eval.pm

filter: Statementstmt 37.3% 62 / 166 Branchbran 18.1% 12 / 66 Conditioncond 5.5% 1 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 63.1% 12 / 19 Podpod 0.0% 0 / 10 totaltotal 30.1% 87 / 289 CC 39 SCAR 32.7
CC
39
Cov
30%
caller_levels_skip
56.0
caller_levels_skip
56.0
eval_with_return
30.1
CRAP 26.4
« lib/Devel/Trepan/CmdProcessor/Command/Disable.pm ↑ Summary lib/Devel/Trepan/Complete.pm »
1 # Eval part of Perl's Core DB.pm library and perl5db.pl with modification.
2  
3 package DB;
4 13 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # FIXME: remove these
8 13 ▶use vars qw($eval_result @eval_result);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # This is the flag that says "a debugger is running, please call
11 # DB::DB and DB::sub". We will turn it on forcibly before we try to
12 # execute anything in the user's context, because we always want to
13 # get control back.
14 13 ▶use constant db_stop => 1 << 30;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 # When we want to evaluate a string in the context of the running
18 # program we use these:
19 13 $DB::eval_result = undef; # Place for result if scalar;
20 13 @DB::eval_result = (); # place for result if array
21 13 %DB::eval_result = (); # place for result if hash
22 }
23  
24 # Like builtin caller but we strip off DB:: routines which are presumably
25 # are calls from inside inside the debugger (package DB).
26 # NOTE: we assume the original builtin caller has been saved inside
27 # local-declared *orig_caller. See below in eval_with_return.
28  
29 # no critic
30  
31 0 ▶sub caller_levels_skip() {
Subroutine caller_levels_skip: not called CC=7 SCAR=40.3
Pod undocumented
32 0 ▶ my $skip=0;
Subroutine caller_levels_skip: not called CC=7 SCAR=40.3
Pod undocumented
33 0 ▶ my $db_fn = ($DB::event eq 'post-mortem') ? 'catch' : 'DB';
Branch: 2 branches
BranchTrueFalseTotal
$DB::event eq 'post-mortem' ? : 0 0 0
$DB::event eq 'post-mortem' ? : 0 0 0
34  
35 0 ▶ while (my ($pkg, $file, $line, $fn) = caller($skip++)) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = caller $skip++) 0 0 0
if (() = caller $skip++) 0 0 0
36 # Note: The function parameter of caller(), $fn, gives the
37 # function that was used rather than the function that the
38 # caller is currently in. Therefore, the implicitly line
39 # calling DB:DB is the one we want to stop at.
40 0 ▶ if ("DB::$db_fn" eq $fn or ('DB' eq $pkg && $db_fn eq $fn)) {
Branch: 2 branches
BranchTrueFalseTotal
if ("DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn) 0 0 0
if ("DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn) 0 0 0
Condition0%0/12
"DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn
l!l&&r!l&&!r
000
"DB" eq $pkg and $db_fn eq $fn
!ll&&!rl&&r
000
"DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn
l!l&&r!l&&!r
000
"DB" eq $pkg and $db_fn eq $fn
!ll&&!rl&&r
000
MC/DC0%0/3
"DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn
"DB::$db_fn" eq $fn "DB" eq $pkg $db_fn eq $fn
MC/DC0%0/3
"DB::$db_fn" eq $fn or "DB" eq $pkg and $db_fn eq $fn
"DB::$db_fn" eq $fn "DB" eq $pkg $db_fn eq $fn
Truth table0%0/4
A or B and C
A= "DB::$db_fn" eq $fnB= "DB" eq $pkgC= $db_fn eq $fn
ABCresult
1XX1
0111
00X0
0100
Truth table0%0/4
A or B and C
A= "DB::$db_fn" eq $fnB= "DB" eq $pkgC= $db_fn eq $fn
ABCresult
1XX1
0111
00X0
0100
41 ## print("XXX $skip\n");
42 0 $skip--;
43 0 last ;
44 }
45 }
46 0 ▶ $skip-- if $skip > 0;
Branch: 2 branches
BranchTrueFalseTotal
if $skip > 0 0 0 0
if $skip > 0 0 0 0
47 0 return $skip;
48 }
49  
50 # Provide a replacement for built-in CORE::caller
51 0 ▶sub caller_sans_DB(;$) {
Subroutine caller_sans_DB: not called CC=4 SCAR=30.0
Pod undocumented
52 0 ▶ my $levels = shift;
Subroutine caller_sans_DB: not called CC=4 SCAR=30.0
Pod undocumented
53 0 ▶ $levels = 0 unless defined($levels);
Branch: 2 branches
BranchTrueFalseTotal
unless defined $levels 0 0 0
unless defined $levels 0 0 0
54 0 my $skip = caller_levels_skip();
55 0 my @caller = CORE::caller($skip+$levels);
56  
57 0 ▶ return if ! @caller; # empty
Branch: 2 branches
BranchTrueFalseTotal
unless @caller 0 0 0
unless @caller 0 0 0
58 0 ▶ return $caller[0] if ! wantarray; # scalar context
Branch: 2 branches
BranchTrueFalseTotal
unless wantarray 0 0 0
unless wantarray 0 0 0
59 0 return @caller[0..2]; # outside of DB, array info just gives 3 itmes
60  
61 }
62  
63 # evaluate $eval_str in the context of $package_namespace (a package name).
64 # @saved contains an ordered list of saved global variables.
65 # $return_type indicates the return context:
66 # @ for array context,
67 # $ for scalar context,
68 # % save result in a hash variable
69 #
70 3 ▶sub eval_with_return {
Subroutine eval_with_return: called CC=9 SCAR=34.1
Pod undocumented
71 0 ▶ my ($eval_str, $opts, @saved) = @_;
Subroutine eval_with_return: not called CC=9 SCAR=34.1
Pod undocumented
72 13 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 0 ($EVAL_ERROR, $ERRNO, $EXTENDED_OS_ERROR,
74 $OUTPUT_FIELD_SEPARATOR,
75 $INPUT_RECORD_SEPARATOR,
76 $OUTPUT_RECORD_SEPARATOR, $WARNING) = @saved;
77  
78 {
79 0 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.1
80 # Try to keep the user code from messing with us. Save these so that
81 # even if the eval'ed code changes them, we can put them back again.
82 # Needed because the user could refer directly to the debugger's
83 # package globals (and any 'my' variables in this containing scope)
84 # inside the eval(), and we want to try to stay safe.
85 0 local $otrace = $DB::trace;
86 0 local $osingle = $DB::single;
87 0 local $od = $DEBUGGING;
88  
89 # Set package namespace for running eval's in the namespace
90 # of the debugged program.
91 0 ▶ my $eval_setup = $opts->{namespace_package} || $DB::namespace_package;
Condition16%1/6
$opts->{'namespace_package'} || $namespace_package
l!l&&r!l&&!r
300
$opts->{'namespace_package'} || $namespace_package
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'namespace_package'} || $namespace_package
$opts->{'namespace_package'} $namespace_package
MC/DC0%0/2
$opts->{'namespace_package'} || $namespace_package
$opts->{'namespace_package'} $namespace_package
Truth table33%1/3
A || B
A= $opts->{'namespace_package'}B= $namespace_package
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $opts->{'namespace_package'}B= $namespace_package
ABresult
1X1
011
000
92 0 $eval_setup .= ";\n\@_ = \@DB::_;";
93  
94 # Make sure __FILE__ and __LINE__ are set correctly
95 0 ▶ if( $opts->{fix_file_and_line}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{"fix_file_and_line"}) 0 3 3
if ($opts->{"fix_file_and_line"}) 0 0 0
96 0 my $position_str = "\n# line $DB::lineno \"$DB::filename\"\n";
97 0 $eval_setup .= $position_str ;
98 }
99  
100 0 my $return_type = $opts->{return_type};
101  
102 # Override caller inside the eval below. Many thanks to Toby
103 # Inkster and educated_foo via
104 # http://www.perlmonks.org/?node_id=1065502
105  
106 0 local *CORE::GLOBAL::caller = \&caller_sans_DB;
107  
108 # Note: our code shouldn't use caller for itself below (or if
109 # it is needed use it by the name CORE::caller, since we've
110 # overwritten it above.
111  
112 0 ▶ if ('$' eq $return_type) {
Branch: 6 branches
BranchTrueFalseTotal
if ('$' eq $return_type) { } 1 2 3
elsif ('@' eq $return_type) { } 1 1 2
elsif ('%' eq $return_type) { } 1 0 1
if ('$' eq $return_type) { } 0 0 0
elsif ('@' eq $return_type) { } 0 0 0
elsif ('%' eq $return_type) { } 0 0 0
113 # print "+++ eval $return: $eval_setup \$DB::eval_result=$eval_str\n";
114 0 eval "$eval_setup \$DB::eval_result=$eval_str\n";
115 } elsif ('@' eq $return_type) {
116 # print "+++ eval @return: $eval_setup \@DB::eval_result=$eval_str\n";
117 0 eval "$eval_setup \@DB::eval_result=$eval_str\n";
118 } elsif ('%' eq $return_type) {
119 0 eval "$eval_setup \%DB::eval_result=$eval_str\n";
120 # } elsif ('>' eq $return_type) {
121 # ($eval_result, $stderr, @result) = capture {
122 # eval "$eval_setup $eval_str\n";
123 # };
124 # } elsif ('2>&1' eq $return_type) {
125 # $eval_result = capture_merged {
126 # eval "$eval_setup $eval_str\n";
127 } else {
128 # print "+++ eval $eval_setup $eval_str\n";
129 0 $eval_result = eval "$eval_setup $eval_str\n";
130 };
131  
132 # Restore those old values.
133 0 $DB::trace = $otrace;
134 0 $DB::single = $osingle;
135 0 $DEBUGGING = $od;
136  
137 0 my $msg = $EVAL_ERROR;
138 0 ▶ if ($msg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($msg) { } 0 3 3
if ($msg) { } 0 0 0
139 0 chomp $msg;
140 0 ▶ if ($opts->{hide_position}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{"hide_position"}) 0 0 0
if ($opts->{"hide_position"}) 0 0 0
141 0 $msg =~ s/ at .* line \d+[.,]//;
142 0 $msg =~ s/ line \d+,//;
143 0 $msg =~ s/ at EOF$/ at end of string/;
144 }
145 0 _warnall($msg);
146 0 $eval_str = '';
147 0 return undef;
148 } else {
149 0 ▶ if ('@' eq $return_type) {
Branch: 2 branches
BranchTrueFalseTotal
if ('@' eq $return_type) { } 1 2 3
if ('@' eq $return_type) { } 0 0 0
150 0 return @eval_result;
151 } else {
152 0 return $eval_result;
153 }
154 }
155 }
156 }
157  
158 # Evaluate the argument and return 0 if there's no error.
159 # If there is an error we return the error message.
160 sub eval_not_ok ($)
161 7 ▶{
Subroutine eval_not_ok: called CC=2 SCAR=8.3
Pod undocumented
162 0 ▶ my $code = shift;
Subroutine eval_not_ok: not called CC=2 SCAR=8.3
Pod undocumented
163 0 my $wrapped = "$DB::namespace_package; sub { $code }";
164 13 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 0 ▶ eval $wrapped;
Subroutine BEGIN: called CC=1 SCAR=0.4
166 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 4 3 7
if ($@) { } 0 0 0
167 0 my $msg = $@;
168 0 $msg =~ s/ at .* line \d+[.,]//g;
169 0 $msg =~ s/ at EOF$/ at end of string/;
170 0 return $msg;
171 } else {
172 0 return 0;
173 }
174 }
175  
176 13 ▶unless (CORE::caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 13 13
177 eval {
178 0 ▶ sub doit($) {
Subroutine doit: not called CC=2 SCAR=17.9
Pod undocumented
179 0 ▶ my $code = shift;
Pod undocumented
180 0 my $msg = eval_not_ok($code);
181 0 print "code: $code\n";
182 0 ▶ if ($msg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($msg) { } 0 0 0
if ($msg) { } 0 0 0
183 0 print "$msg";
184 } else {
185 0 print "code ok\n";
186 }
187 }
188 0 };
189  
190 0 $DB::namespace_package = 'package DB;';
191 0 doit 'doit(1,2,3)';
192 0 doit "1+";
193 0 doit '$x+2';
194 0 doit "foo(";
195 0 doit '$foo =';
196 0 doit 'BEGIN { $x = 1; ';
197 0 doit 'package foo; 1';
198  
199 }
200  
201 # doit '$x = 1; __END__ $y=';
202  
203  
204 13 1;
« lib/Devel/Trepan/CmdProcessor/Command/Disable.pm ↑ Summary lib/Devel/Trepan/Complete.pm »