lib/Devel/Trepan/DB/LineCache.pm

filter: Statementstmt 32.9% 304 / 924 Branchbran 19.1% 81 / 424 Conditioncond 13.4% 17 / 126 MC/DCmcdc 2.7% 2 / 74 Subroutinesub 48.1% 53 / 110 Podpod 69.4% 50 / 72 totaltotal 29.3% 507 / 1730 CC 273 SCAR 43.9
CC
273
Cov
27%
update_cache
258.6
update_cache
258.6
getline
135.2
CRAP 80.2
« lib/Devel/Trepan/CmdProcessor/Command/Complete.pm ↑ Summary blib/lib/Devel/Trepan/DB/SelfLoader.pm »
1 #!/usr/bin/env perl
2 #
3 # Copyright (C) 2011-2014 Rocky Bernstein <rocky@cpan.org>
4 #
5 #
6 14 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 14 ▶use version; $VERSION = '1.0.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package Devel::Trepan::DB::LineCache;
12  
13 # FIXME: Figure out where to put this
14 # *pod
15 #
16 # I<eval_ok($code)> => I<boolean>
17 #
18 # Evaluate I<$code> and return true if there's no error.
19 # *cut
20 sub eval_ok ($)
21 0 ▶{
Subroutine eval_ok: not called CC=2 SCAR=13.0
Pod undocumented
22 0 ▶ my $code = shift;
Subroutine eval_ok: not called CC=2 SCAR=13.0
Pod undocumented
23 14 ▶ no strict; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
24 0 ▶ $DB::namespace_package = 'package main' unless $DB::namespace_package;
Branch: 2 branches
BranchTrueFalseTotal
unless $DB::namespace_package 0 0 0
unless $DB::namespace_package 0 0 0
25 0 my $wrapped = "$DB::namespace_package; sub { $code }";
26 0 my %orig_sub = %DB::sub;
27 0 eval $wrapped;
28 0 %DB::sub = %orig_sub;
29 # print $@, "\n" if $@;
30 0 return !$@;
31 }
32  
33 14 ▶use rlib '../../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 =head1 NAME
36  
37 Devel::Trepan::DB::LineCache - package to read and cache lines of a Perl program.
38  
39 =head1 SYNOPSIS
40  
41 The LineCache package allows one to get any line from any file,
42 caching lines of the file on first access to the file. Although the
43 file may be any file, the common use is when the file is a Perl
44 script since parsing of the file is done to figure out where the
45 statement boundaries are.
46  
47 The routines here may be is useful when a small random sets of lines
48 are read from a single file, in particular in a debugger to show
49 source lines.
50  
51 use Devel::Trepan::DB::LineCache;
52 $lines = getlines('/tmp/myperl.pl')
53  
54 # The following lines have same effect as the above.
55 unshift @INC, '/tmp';
56 $lines = getlines('myperl.pl');
57 shift @INC;
58  
59 chdir '/tmp';
60 $lines = getlines('myperl.pl')
61  
62 $line = getline('/tmp/myperl.pl', 6)
63 # Note lines[6] == line (if /tmp/myperl.pl has 6 lines)
64  
65 clear_file_cache
66 update_cache # Check for modifications of all cached files.
67  
68 =cut
69  
70 14 our(@ISA, @EXPORT, $VERSION);
71 14 require Exporter;
72 14 @ISA = qw(Exporter);
73 14 @EXPORT = qw(cache_file clear_file_format_cache
74 clear_cache update_cache
75 file_list getlines
76 filename_is_eval getline map_file is_cached
77 highlight_string cache_file
78 load_file map_script map_file_line remap_file
79 remap_dbline_to_file remap_string_to_tempfile %script_cache
80 trace_line_numbers update_script_cache
81 );
82 14 $VERSION = "1.0.0";
83  
84 14 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
85 14 ▶use vars qw(%file_cache %script_cache);
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 14 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
88 14 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 14 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 14 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
92 14 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 14 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 14 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 ## struct(stat => '$', lines => '%', path => '$', sha1 => '$');
97  
98 # The file cache. The key is a name as would be given by Perl for
99 # __FILE__. The value is a LineCacheInfo object.
100  
101  
102 # Maps a string filename (a String) to a key in %file_cache (a
103 # String).
104 #
105 # One important use of %file2file_remap is mapping the a full path
106 # of a file into the name stored in %file_cache or given by Perl's
107 # __FILE__. Applications such as those that get input from users,
108 # may want canonicalize a file name before looking it up. This map
109 # gives a way to do that.
110 #
111 # Another related use is when a template system is used. Here we'll
112 # probably want to remap not only the file name but also line
113 # ranges. Will probably use this for that, but I'm not sure.
114  
115 14 my %file2file_remap;
116 my %file2file_remap_lines;
117 14 my %script2file;
118 14 my @tempfiles;
119  
120 =head1 SUBROUTINES
121  
122 I<Note:> in what follows we use I<$file_or_script> to refer to either
123 a filename which generally should be a Perl file, or a psuedo-file
124 name that is created in an I<eval()> string. Often, the filename does
125 not have to be fully qualified. In some cases I<@INC> will be used to
126 find the file.
127  
128 =cut
129  
130 ## FIXME:: Make conditional
131 14 ▶use Devel::Trepan::DB::Colors;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 14 my $perl_formatter;
133  
134 14 ▶sub color_setup {
Subroutine color_setup: called CC=1 SCAR=1.2
Pod undocumented
135 0 ▶ $perl_formatter = Devel::Trepan::DB::Colors::setup(@_);
Subroutine color_setup: not called CC=1 SCAR=1.2
Pod undocumented
136 }
137 14 color_setup('lightbg');
138  
139 sub remove_temps()
140 14 ▶{
Subroutine remove_temps: called CC=5 SCAR=27.4
Pod undocumented
141 0 ▶ for my $filename (values %script2file) {
Subroutine remove_temps: not called CC=5 SCAR=27.4
Pod undocumented
142 0 ▶ unlink($filename) if -f $filename;
Branch: 2 branches
BranchTrueFalseTotal
if -f $filename 1 0 1
if -f $filename 0 0 0
143 }
144 0 for my $filename (@tempfiles) {
145 0 ▶ unlink($filename) if -f $filename;
Branch: 2 branches
BranchTrueFalseTotal
if -f $filename 0 0 0
if -f $filename 0 0 0
146 }
147 }
148  
149 14 ▶END {
Subroutine END: called CC=1 SCAR=1.2
150 0 ▶ $DB::ready = 0;
Subroutine END: not called CC=1 SCAR=1.2
151 0 remove_temps
152 };
153  
154 =head2 clear_file_cache
155  
156 B<clear_file_cache()>
157  
158 B<clear_file_cache(I<$filename>)>
159  
160  
161 Clear the file cache of I<$filename>. If I<$filename>
162 is not given, clear all files in the cache.
163  
164 =cut
165  
166 sub clear_file_cache(;$)
167 1 ▶{
Subroutine clear_file_cache: called CC=3 SCAR=19.7
Pod documented
168 0 ▶ if (scalar @_ == 1) {
Subroutine clear_file_cache: not called CC=3 SCAR=19.7
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 1 1
if (scalar @_ == 1) { } 0 0 0
169 0 my $filename = shift;
170 0 ▶ if ($file_cache{$filename}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Devel::Trepan::DB::LineCache::file_cache{$filename}) 0 0 0
if ($Devel::Trepan::DB::LineCache::file_cache{$filename}) 0 0 0
171 0 delete $file_cache{$filename};
172 }
173 } else {
174 0 %file_cache = ();
175 0 %file2file_remap = ();
176 0 %file2file_remap_lines = ();
177 }
178 }
179  
180 =head2 clear_file_format_cache
181  
182 B<clear_file_format_cache()>
183  
184 Remove syntax-formatted lines in the cache. Use this when you change
185 the L<Syntax::Highlight::Perl> colors and want to redo how files may
186 have previously been syntax marked.
187  
188 =cut
189  
190 sub clear_file_format_cache()
191 0 ▶{
Subroutine clear_file_format_cache: not called CC=4 SCAR=30.0
Pod documented
192 0 ▶ while (my ($fname, $cache_info) = each %file_cache) {
Subroutine clear_file_format_cache: not called CC=4 SCAR=30.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %Devel::Trepan::DB::LineCache::file_cache) 0 0 0
if (() = each %Devel::Trepan::DB::LineCache::file_cache) 0 0 0
193 0 ▶ while (my($format, $lines) = each %{$cache_info->{lines_href}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %{$$cache_info{"lines_href"};}) 0 0 0
if (() = each %{$$cache_info{"lines_href"};}) 0 0 0
194 0 ▶ next if 'plain' eq $format;
Branch: 2 branches
BranchTrueFalseTotal
if "plain" eq $format 0 0 0
if "plain" eq $format 0 0 0
195 0 my $ref = $file_cache{$fname};
196 0 $ref->{lines_href}->{$format} = undef;
197 }
198 }
199 }
200  
201 =pod
202  
203 =head2 clear_script_cache
204  
205 B<clear_script_cache()>
206  
207 Clear the script cache entirely.
208  
209 =cut
210  
211 0 ▶sub clear_script_cache() {
Subroutine clear_script_cache: not called CC=1 SCAR=6.9
Pod documented
212 0 ▶ %script_cache = {};
Subroutine clear_script_cache: not called CC=1 SCAR=6.9
Pod documented
213 }
214  
215 =pod
216  
217 =head2 cached_files
218  
219 B<cached_files()> => I<list of files>
220  
221 Return an array of cached file names
222  
223 =cut
224  
225 0 ▶sub cached_files() {
Subroutine cached_files: not called CC=1 SCAR=6.9
Pod documented
226 0 ▶ keys %file_cache;
Subroutine cached_files: not called CC=1 SCAR=6.9
Pod documented
227 }
228  
229 =pod
230  
231 =head2 checkcache
232  
233 B<checkcache()> => I<list-of-filenames>
234  
235 B<checkcache(I<$filename> [, $opts])> => I<list-of-filenames>
236  
237 Discard cache entries that are out of date. If I<$filename>is I<undef>,
238 all entries in the file cache are checked.
239  
240 If we did not previously have I<stat()> information about a file, it
241 will be added. Return a list of invalidated filenames. I<undef> is
242 returned if a filename was given but not found cached.
243  
244 =cut
245  
246 sub checkcache(;$$)
247 0 ▶{
Subroutine checkcache: not called CC=11 SCAR=48.8
Pod documented
248 0 ▶ my ($filename, $opts) = @_;
Subroutine checkcache: not called CC=11 SCAR=48.8
Pod documented
249 0 ▶ $opts = {} unless defined $opts;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $opts 0 0 0
unless defined $opts 0 0 0
250  
251 0 my $use_perl_d_file = $opts->{use_perl_d_file};
252  
253 0 my @filenames;
254 0 ▶ if (defined $filename) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $filename) { } 0 0 0
elsif (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 0 0 0
if (defined $filename) { } 0 0 0
elsif (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 0 0 0
255 0 @filenames = keys %file_cache;
256 } elsif (exists $file_cache{$filename}) {
257 0 @filenames = ($filename);
258 } else {
259 0 return undef;
260 }
261  
262 0 my @result = ();
263 0 for my $filename (@filenames) {
264 0 ▶ next unless exists $file_cache{$filename};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $Devel::Trepan::DB::LineCache::file_cache{$filename} 0 0 0
unless exists $Devel::Trepan::DB::LineCache::file_cache{$filename} 0 0 0
265 0 my $path = $file_cache{$filename}{path};
266 0 ▶ if (-f $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $path) { } 0 0 0
if (-f $path) { } 0 0 0
267 0 my $cache_info = $file_cache{$filename}{stat};
268 0 my $stat = File::stat::stat($path);
269 0 ▶ if ($cache_info) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cache_info) 0 0 0
if ($cache_info) 0 0 0
270 0 ▶ if ($stat &&
Branch: 2 branches
BranchTrueFalseTotal
if ($stat and $cache_info->{"size"} != $stat->size || $cache_info->{"mtime"} != $stat->mtime) 0 0 0
if ($stat and $cache_info->{"size"} != $stat->size || $cache_info->{"mtime"} != $stat->mtime) 0 0 0
Condition0%0/12
$stat and $cache_info->{"size"} != $stat->size || $cache_info->{"mtime"} != $stat->mtime
!ll&&!rl&&r
000
$cache_info->{"size"} != $stat->size or $cache_info->{"mtime"} != $stat->mtime
l!l&&r!l&&!r
000
$stat and $cache_info->{"size"} != $stat->size || $cache_info->{"mtime"} != $stat->mtime
!ll&&!rl&&r
000
$cache_info->{"size"} != $stat->size or $cache_info->{"mtime"} != $stat->mtime
l!l&&r!l&&!r
000
MC/DC0%0/3
$stat and $cache_info->{"size"} != $stat->size || $cache_info->{"mtime"} != $stat->mtime
$stat $cache_info->{"size"} != $stat->size $cache_info->{"mtime"} != $stat->mtime
MC/DC0%0/3
$stat and $cache_info->{"size"} != $stat->size || $cache_info->{"mtime"} != $stat->mtime
$stat $cache_info->{"size"} != $stat->size $cache_info->{"mtime"} != $stat->mtime
Truth table0%0/4
A and B or C
A= $statB= $cache_info->{"size"} != $stat->sizeC= $cache_info->{"mtime"} != $stat->mtime
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
A and B or C
A= $statB= $cache_info->{"size"} != $stat->sizeC= $cache_info->{"mtime"} != $stat->mtime
ABCresult
0XX0
1000
11X1
1011
271 ($cache_info->{size} != $stat->size or
272 $cache_info->{mtime} != $stat->mtime)) {
273 0 push @result, $filename;
274 0 update_cache($filename, $opts);
275 }
276 }
277 } else {
278 0 push @result, $filename;
279 0 update_cache($filename, $opts);
280 }
281 }
282 0 return @result;
283 }
284  
285 =pod
286  
287 =head2 cache_script
288  
289 B<cache_script(I<$script> [, I<$opts>]) > => I<script>
290  
291 Cache psuedo eval-string for a pseudo eval-string if it's not already cached.
292  
293 =cut
294  
295 sub cache_script($;$)
296 0 ▶{
Subroutine cache_script: not called CC=3 SCAR=24.8
Pod documented
297 0 ▶ my ($script, $opts) = @_;
Subroutine cache_script: not called CC=3 SCAR=24.8
Pod documented
298 0 ▶ $opts = {} unless defined $opts;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $opts 0 0 0
unless defined $opts 0 0 0
299 0 ▶ if (exists $script_cache{$script}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $Devel::Trepan::DB::LineCache::script_cache{$script}) { } 0 0 0
if (exists $Devel::Trepan::DB::LineCache::script_cache{$script}) { } 0 0 0
300 0 return 1;
301 } else {
302 0 return update_script_cache($script, $opts);
303 }
304 }
305  
306 =pod
307  
308 =head2 cache
309  
310 B<cache(I<$file_or_script> [, I<$reload_on_change>]) > => I<filename>
311  
312 Cache file name or script object if it's not already cached.
313  
314 Return the expanded filename for it in the cache if a filename,
315 or the script, or I<undef> if we can't find the file.
316  
317 =cut
318  
319 sub cache($;$)
320 0 ▶{
Subroutine cache: not called CC=1 SCAR=6.9
Pod documented
321 0 ▶ my ($file_or_script, $reload_on_change) = @_;
Subroutine cache: not called CC=1 SCAR=6.9
Pod documented
322 0 cache_file($file_or_script, $reload_on_change)
323 }
324  
325 =pod
326  
327 =head2 cache_file
328  
329 B<cache(I<$file_or_script> [, I<$reload_on_change>, $opts]) > => I<filename>
330  
331 Cache I<$filename_or_script> if it's not already cached.
332  
333 Return the expanded filename for I<$file_or_script> if it is in the
334 cache or I<undef> if we can't find it.
335  
336 =cut
337  
338 sub cache_file($;$$)
339 1 ▶{
Subroutine cache_file: called CC=6 SCAR=30.2
Pod documented
340 0 ▶ my ($filename, $reload_on_change, $opts) = @_;
Subroutine cache_file: not called CC=6 SCAR=30.2
Pod documented
341 0 ▶ $opts = {} unless defined $opts;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $opts 1 0 1
unless defined $opts 0 0 0
342 0 ▶ if (exists $file_cache{$filename}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 1 0 1
if (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 0 0 0
343 0 ▶ checkcache($filename) if $reload_on_change;
Branch: 2 branches
BranchTrueFalseTotal
if $reload_on_change 0 1 1
if $reload_on_change 0 0 0
344 } else {
345 0 ▶ $opts->{use_perl_d_file} = 1 unless defined $opts->{use_perl_d_file};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $opts->{"use_perl_d_file"} 0 0 0
unless defined $opts->{"use_perl_d_file"} 0 0 0
346 0 update_cache($filename, $opts);
347 }
348 0 ▶ if (exists $file_cache{$filename}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 1 0 1
if (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 0 0 0
349 0 $file_cache{$filename}{path};
350 } else {
351 0 return undef;
352 }
353 }
354  
355 =pod
356  
357 =head2 is_cached
358  
359 B<cache(I<$file_or_script>)> => I<boolean>
360  
361 Return I<true> if I<$file_or_script> is cached.
362  
363 =cut
364  
365 sub is_cached($)
366 0 ▶{
Subroutine is_cached: not called CC=2 SCAR=17.9
Pod documented
367 0 ▶ my $file_or_script = shift;
Subroutine is_cached: not called CC=2 SCAR=17.9
Pod documented
368 0 ▶ return undef unless defined $file_or_script;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $file_or_script 0 0 0
unless defined $file_or_script 0 0 0
369 0 exists $file_cache{map_file($file_or_script)};
370 }
371  
372 sub is_cached_script($)
373 0 ▶{
Subroutine is_cached_script: not called CC=1 SCAR=6.9
Pod undocumented
374 0 ▶ my $filename = shift;
Subroutine is_cached_script: not called CC=1 SCAR=6.9
Pod undocumented
375 0 my $name = map_file($filename);
376 0 scalar @{"_<$name"};
377 }
378  
379 sub is_empty($)
380 0 ▶{
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod undocumented
381 0 ▶ my $filename = shift;
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod undocumented
382 0 $filename=map_file($filename);
383 0 my $ref = $file_cache{$filename};
384 0 $ref->{lines_href}{plain};
385 }
386  
387 sub file_list()
388 0 ▶{
Subroutine file_list: not called CC=1 SCAR=6.9
Pod undocumented
389 0 ▶ my @list = (cached_files(), keys(%file2file_remap));
Subroutine file_list: not called CC=1 SCAR=6.9
Pod undocumented
390 0 my %seen;
391 0 my @uniq = grep { ! $seen{$_} ++ } @list;
392 0 sort(@uniq);
393 }
394  
395 =pod
396  
397 =head2 getline
398  
399 B<getline($file_or_script, $line_number [, $opts])> => I<string>
400  
401 Get line I<$line_number> from I<$file_script>. Return I<undef> if
402 there was a problem. If a file named I<$file_or_script> is not found, the
403 function will look for it in the I<@INC> array.
404  
405 =cut
406  
407 sub getline($$;$)
408 4 ▶{
Subroutine getline: called CC=16 SCAR=49.1
Pod documented
409 0 ▶ my ($file_or_script, $line_number, $opts) = @_;
Subroutine getline: not called CC=16 SCAR=49.1
Pod documented
410 0 ▶ $opts = {} unless defined $opts;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $opts 2 2 4
unless defined $opts 0 0 0
411 0 my $reload_on_change = $opts->{reload_on_change};
412 0 my $filename = map_file($file_or_script);
413 0 ($filename, $line_number) = map_file_line($filename, $line_number);
414 0 my $lines = getlines($filename, $opts);
415 # Adjust for 0-origin arrays vs 1 origin line numbers
416 0 ▶ return undef unless $lines;
Branch: 2 branches
BranchTrueFalseTotal
unless $lines 0 4 4
unless $lines 0 0 0
417 0 my $max_index = scalar(@$lines) - 1;
418 0 my $index = $line_number - 1;
419 0 ▶ if (defined $lines && @$lines && $index >= 0 && $index <= $max_index) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $lines and @$lines and $index >= 0 and $index <= $max_index) { } 4 0 4
if (defined $lines and @$lines and $index >= 0 and $index <= $max_index) { } 0 0 0
Condition16%3/18
defined $lines and @$lines and $index >= 0 and $index <= $max_index
!ll&&!rl&&r
004
defined $lines and @$lines and $index >= 0
!ll&&!rl&&r
004
defined $lines and @$lines
!ll&&!rl&&r
004
defined $lines and @$lines and $index >= 0 and $index <= $max_index
!ll&&!rl&&r
000
defined $lines and @$lines and $index >= 0
!ll&&!rl&&r
000
defined $lines and @$lines
!ll&&!rl&&r
000
MC/DC0%0/4
defined $lines and @$lines and $index >= 0 and $index <= $max_index
defined $lines @$lines $index >= 0 $index <= $max_index
MC/DC0%0/4
defined $lines and @$lines and $index >= 0 and $index <= $max_index
defined $lines @$lines $index >= 0 $index <= $max_index
Truth table20%1/5
A and B and C and D
A= defined $linesB= @$linesC= $index >= 0D= $index <= $max_index
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table0%0/5
A and B and C and D
A= defined $linesB= @$linesC= $index >= 0D= $index <= $max_index
ABCDresult
0XXX0
10XX0
110X0
11100
11111
420 0 ▶ my $max_continue = $opts->{maxlines} || 1;
Condition25%1/4
$opts->{'maxlines'} || 1
l!l
04
$opts->{'maxlines'} || 1
l!l
00
MC/DC0%0/1
$opts->{'maxlines'} || 1
$opts->{'maxlines'}
MC/DC0%0/1
$opts->{'maxlines'} || 1
$opts->{'maxlines'}
Truth table50%1/2
A
A= $opts->{'maxlines'}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{'maxlines'}
Aresult
11
00
421 0 my $line = $lines->[$index];
422 0 ▶ return undef unless defined $line;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $line 0 4 4
unless defined $line 0 0 0
423 0 ▶ if ($max_continue > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($max_continue > 1) 0 4 4
if ($max_continue > 1) 0 0 0
424 0 my $plain_lines = getlines($filename, {output => 'plain'});
425 # FIXME: should cache results
426 0 ▶ my $sep = ($plain_lines eq $lines) ? '' : "\n";
Branch: 2 branches
BranchTrueFalseTotal
$plain_lines eq $lines ? : 0 0 0
$plain_lines eq $lines ? : 0 0 0
427 0 my $plain_line = $plain_lines->[$index];
428 0 ▶ while (--$max_continue && !eval_ok($plain_line)) {
Branch: 2 branches
BranchTrueFalseTotal
if (--$max_continue and not &eval_ok($plain_line)) 0 0 0
if (--$max_continue and not &eval_ok($plain_line)) 0 0 0
Condition0%0/6
--$max_continue and not &eval_ok($plain_line)
!ll&&!rl&&r
000
--$max_continue and not &eval_ok($plain_line)
!ll&&!rl&&r
000
MC/DC0%0/2
--$max_continue and not &eval_ok($plain_line)
--$max_continue not &eval_ok($plain_line)
MC/DC0%0/2
--$max_continue and not &eval_ok($plain_line)
--$max_continue not &eval_ok($plain_line)
Truth table0%0/3
A and not(B)
A= --$max_continueB= not &eval_ok($plain_line)
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= --$max_continueB= not &eval_ok($plain_line)
ABresult
0X0
100
111
429 0 my $next_line = $lines->[++$index];
430 0 ▶ last unless $next_line;
Branch: 2 branches
BranchTrueFalseTotal
unless $next_line 0 0 0
unless $next_line 0 0 0
431 0 $line .= ($sep . $next_line);
432 0 $plain_line .= $plain_lines->[$index];
433 0 ▶ last if $file_cache{$filename}{trace_nums}{$index+1};
Branch: 2 branches
BranchTrueFalseTotal
if $Devel::Trepan::DB::LineCache::file_cache{$filename}{"trace_nums"}{$index + 1} 0 0 0
if $Devel::Trepan::DB::LineCache::file_cache{$filename}{"trace_nums"}{$index + 1} 0 0 0
434 }
435 }
436 0 ▶ chomp $line if defined $line;
Branch: 2 branches
BranchTrueFalseTotal
if defined $line 4 0 4
if defined $line 0 0 0
437 0 return $line;
438 } else {
439 0 return undef;
440 }
441 }
442  
443 =pod
444  
445 =head2 getlines
446  
447 B<getlines($filename, [$opts])> => I<string>
448  
449 Read lines of I<$filename> and cache the results. However
450 if I<$filename> was previously cached use the results from the
451 cache. Return I<undef> if we can't get lines.
452  
453 B<Examples:>
454  
455 $lines = getline('/tmp/myfile.pl')
456 # Same as above
457 push @INC, '/tmp';
458 $lines = getlines('myfile.pl')
459  
460 =cut
461  
462 sub getlines($;$);
463 sub getlines($;$)
464 7 ▶{
Subroutine getlines: called CC=14 SCAR=47.2
Pod documented
465 0 ▶ my ($filename, $opts) = @_;
Subroutine getlines: not called CC=14 SCAR=47.2
Pod documented
466 0 ▶ $opts = {use_perl_d_file => 1} unless defined $opts;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $opts 1 6 7
unless defined $opts 0 0 0
467 my ($reload_on_change, $use_perl_d_file) =
468 0 ($opts->{reload_on_change}, $opts->{use_perl_d_file});
469 0 ▶ checkcache($filename) if $reload_on_change;
Branch: 2 branches
BranchTrueFalseTotal
if $reload_on_change 0 7 7
if $reload_on_change 0 0 0
470 0 ▶ my $format = $opts->{output} || 'plain';
Condition50%2/4
$opts->{'output'} || 'plain'
l!l
25
$opts->{'output'} || 'plain'
l!l
00
MC/DC100%1/1
$opts->{'output'} || 'plain'
$opts->{'output'}
MC/DC0%0/1
$opts->{'output'} || 'plain'
$opts->{'output'}
Truth table100%2/2
A
A= $opts->{'output'}
Aresult
11
00
Truth table0%0/2
A
A= $opts->{'output'}
Aresult
11
00
471 0 ▶ if (exists $file_cache{$filename}) {
Branch: 4 branches
BranchTrueFalseTotal
if (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 5 2 7
elsif (exists $Devel::Trepan::DB::LineCache::script_cache{$filename}) { } 0 2 2
if (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 0 0 0
elsif (exists $Devel::Trepan::DB::LineCache::script_cache{$filename}) { } 0 0 0
472 0 my $lines_href = $file_cache{$filename}{lines_href};
473 0 my $lines_aref = $lines_href->{$format};
474 0 ▶ return $lines_href->{plain} if $format eq 'plain';
Branch: 2 branches
BranchTrueFalseTotal
if $format eq "plain" 5 0 5
if $format eq "plain" 0 0 0
475 0 ▶ if ($opts->{output} && !defined $lines_aref) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'output'} and not defined $lines_aref) { } 0 0 0
if ($opts->{'output'} and not defined $lines_aref) { } 0 0 0
Condition0%0/6
$opts->{"output"} and not defined $lines_aref
!ll&&!rl&&r
000
$opts->{"output"} and not defined $lines_aref
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"output"} and not defined $lines_aref
$opts->{"output"} not defined $lines_aref
MC/DC0%0/2
$opts->{"output"} and not defined $lines_aref
$opts->{"output"} not defined $lines_aref
Truth table0%0/3
A and not(B)
A= $opts->{"output"}B= not defined $lines_aref
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $opts->{"output"}B= not defined $lines_aref
ABresult
0X0
100
111
476 0 my @formatted_lines = ();
477 0 $lines_aref = $lines_href->{plain};
478 0 for my $line (@$lines_aref) {
479 0 push @formatted_lines, highlight_string($line);
480 ## print $formatted_text;
481 }
482 0 $lines_href->{$format} = \@formatted_lines;
483 0 return \@formatted_lines;
484 } else {
485 0 return $lines_aref;
486 }
487 } elsif (exists $script_cache{$filename}) {
488 ### FIXME: combine with above...
489 ### print "+++IS IN SCRIPT CACHE\n";
490 0 my $lines_href = $script_cache{$filename}{lines_href};
491 0 my $lines_aref = $lines_href->{$format};
492 0 ▶ if ($opts->{output} && !defined $lines_aref) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'output'} and not defined $lines_aref) { } 0 0 0
if ($opts->{'output'} and not defined $lines_aref) { } 0 0 0
Condition0%0/6
$opts->{"output"} and not defined $lines_aref
!ll&&!rl&&r
000
$opts->{"output"} and not defined $lines_aref
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"output"} and not defined $lines_aref
$opts->{"output"} not defined $lines_aref
MC/DC0%0/2
$opts->{"output"} and not defined $lines_aref
$opts->{"output"} not defined $lines_aref
Truth table0%0/3
A and not(B)
A= $opts->{"output"}B= not defined $lines_aref
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $opts->{"output"}B= not defined $lines_aref
ABresult
0X0
100
111
493 0 my @formatted_lines = ();
494 0 my $lines_aref = $lines_href->{plain};
495 0 for my $line (@$lines_aref) {
496 0 push @formatted_lines, highlight_string($line);
497 ## print $formatted_text;
498 }
499 0 $lines_href->{$format} = \@formatted_lines;
500 0 return \@formatted_lines;
501 } else {
502 0 return $lines_aref;
503 }
504 } else {
505 0 $opts->{use_perl_d_file} = 1;
506 0 update_cache($filename, $opts);
507 0 ▶ if (exists $file_cache{$filename}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 2 0 2
if (exists $Devel::Trepan::DB::LineCache::file_cache{$filename}) { } 0 0 0
508 0 return getlines($filename, $opts);
509 } else {
510 0 return undef;
511 }
512 }
513 }
514  
515 =head2 highlight_string
516  
517 B<highlight_string($string)> => I<marked-up-string>
518  
519 Add syntax-formatting characters via
520 L<Syntax::Highlight::Perl::Improved> to I<marked-up-string> according to table
521 given in L<Devel::Trepan::DB::Colors>.
522  
523 =cut
524  
525 sub highlight_string($)
526 0 ▶{
Subroutine highlight_string: not called CC=1 SCAR=6.9
Pod documented
527 0 ▶ my ($string) = shift;
Subroutine highlight_string: not called CC=1 SCAR=6.9
Pod documented
528 0 $string = $perl_formatter->format_string($string);
529 0 chomp $string;
530 0 $string;
531 }
532  
533 =head2 path
534  
535 B<path($filename)> => I<string>
536  
537 Return full filename path for I<$filename>.
538  
539 =cut
540  
541 sub path($)
542 0 ▶{
Subroutine path: not called CC=2 SCAR=17.9
Pod documented
543 0 ▶ my $filename = shift;
Subroutine path: not called CC=2 SCAR=17.9
Pod documented
544 0 $filename = map_file($filename);
545 0 ▶ return undef unless exists $file_cache{$filename};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $Devel::Trepan::DB::LineCache::file_cache{$filename} 0 0 0
unless exists $Devel::Trepan::DB::LineCache::file_cache{$filename} 0 0 0
546 0 $file_cache{$filename}->path();
547 }
548  
549 =pod
550  
551 =head2 remap_file
552  
553 B<remap_file($from_file, $to_file)> => $to_file
554  
555 Set to make any lookups retriving lines from of I<$from_file> refer to
556 I<$to_file>.
557  
558 B<Example>:
559  
560 Running:
561  
562 use Devel::Trepan::DB::LineCache;
563 remap_file('another_name', __FILE__);
564 print getline('another_name', __LINE__), "\n";
565  
566 gives:
567  
568 print getline('another_name', __LINE__), "\n";
569  
570 =cut
571  
572 sub remap_file($$)
573 1 ▶{
Subroutine remap_file: called CC=1 SCAR=1.2
Pod documented
574 0 ▶ my ($from_file, $to_file) = @_;
Subroutine remap_file: not called CC=1 SCAR=1.2
Pod documented
575 0 $file2file_remap{$from_file} = $to_file;
576 0 cache_file($to_file);
577 }
578  
579 sub remap_string_to_tempfile($)
580 0 ▶{
Subroutine remap_string_to_tempfile: not called CC=1 SCAR=6.9
Pod undocumented
581 0 ▶ my $string = shift;
Subroutine remap_string_to_tempfile: not called CC=1 SCAR=6.9
Pod undocumented
582 0 my ($fh, $tempfile) = tempfile('XXXX', SUFFIX=>'.pl',
583 TMPDIR => 1);
584 0 push @tempfiles, $tempfile;
585 0 $fh->autoflush(1);
586 0 print $fh $string;
587 0 $fh->close();
588 0 return $tempfile;
589 }
590  
591 =pod
592  
593 =head2 remap_dbline_to_file
594  
595 I<remap_dbline_to_file()>
596  
597 When we run C<trepan.pl -e> ... or C<perl -d:Trepan -e ...> we have
598 data in internal an "line" array I<@DB::dbline> but no external
599 file. Here, we will create a temporary file and store the data in
600 that.
601  
602 =cut
603  
604 sub remap_dbline_to_file()
605 0 ▶{
Subroutine remap_dbline_to_file: not called CC=2 SCAR=14.4
Pod documented
606 14 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
607 0 ▶ my @lines = @DB::dbline;
Subroutine remap_dbline_to_file: not called CC=2 SCAR=17.9
Pod documented
608 0 ▶ shift @lines if $lines[0] eq "use Devel::Trepan;\n";
Branch: 2 branches
BranchTrueFalseTotal
if $lines[0] eq "use Devel::Trepan;\n" 0 0 0
if $lines[0] eq "use Devel::Trepan;\n" 0 0 0
609 0 my $string = join('', @lines);
610 0 my $tempfile = remap_string_to_tempfile $string;
611 0 remap_file('-e', $tempfile);
612 }
613  
614 sub remap_file_lines($$$$)
615 0 ▶{
Subroutine remap_file_lines: not called CC=3 SCAR=24.8
Pod undocumented
616 0 ▶ my ($from_file, $to_file, $range_ref, $start) = @_;
Subroutine remap_file_lines: not called CC=3 SCAR=24.8
Pod undocumented
617 0 my @range = @$range_ref;
618 0 ▶ $to_file = $from_file unless $to_file;
Branch: 2 branches
BranchTrueFalseTotal
unless $to_file 0 0 0
unless $to_file 0 0 0
619 0 my $ary_ref = ${$file2file_remap_lines{$to_file}};
620 0 ▶ $ary_ref = [] unless defined $ary_ref;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $ary_ref 0 0 0
unless defined $ary_ref 0 0 0
621 # FIXME: need to check for overwriting ranges: whether
622 # they intersect or one encompasses another.
623 0 push @$ary_ref, [$from_file, @range, $start];
624 }
625  
626 =pod
627  
628 =head2 sha1
629  
630 I<sha1($filename)> => I<string>
631  
632 Return SHA1 for I<$filename>.
633  
634 B<Example>:
635  
636 In file C</tmp/foo.pl>:
637  
638 use Devel::Trepan::DB::LineCache;
639 cache_file(__FILE__);
640 printf "SHA1 of %s is:\n%s\n", __FILE__, Devel::Trepan::DB::LineCache::sha1(__FILE__);
641  
642 gives:
643  
644 SHA1 of /tmp/foo.pl is:
645 719b1aa8d559e64bd0de70b325beff79beac32f5
646  
647 =cut
648  
649 sub Devel::Trepan::DB::LineCache::sha1($)
650 1 ▶{
Subroutine sha1: called CC=5 SCAR=23.0
Pod documented
651 0 ▶ my $filename = shift;
Subroutine sha1: not called CC=5 SCAR=23.0
Pod documented
652 0 $filename = map_file($filename);
653 0 ▶ return undef unless exists $file_cache{$filename};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $Devel::Trepan::DB::LineCache::file_cache{$filename} 0 1 1
unless exists $Devel::Trepan::DB::LineCache::file_cache{$filename} 0 0 0
654 0 my $sha1 = $file_cache{$filename}{sha1};
655 0 ▶ return $sha1->hexdigest if exists $file_cache{$filename}{sha1};
Branch: 2 branches
BranchTrueFalseTotal
if exists $Devel::Trepan::DB::LineCache::file_cache{$filename}{"sha1"} 0 1 1
if exists $Devel::Trepan::DB::LineCache::file_cache{$filename}{"sha1"} 0 0 0
656 0 $sha1 = Digest::SHA->new('sha1');
657 0 my $line_ary = $file_cache{$filename}{lines_href}{plain};
658 0 for my $line (@$line_ary) {
659 0 ▶ next unless defined $line;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $line 0 107 107
unless defined $line 0 0 0
660 0 $sha1->add($line);
661 }
662 0 $file_cache{$filename}{sha1} = $sha1;
663 0 $sha1->hexdigest;
664 }
665  
666 =pod
667  
668 =head2 size
669  
670 I<size($filename_or_script)> => I<string>
671  
672 Return the number of lines in I<$filename_or_script>.
673  
674 B<Example>:
675  
676 In file C</tmp/foo.pl>:
677  
678 use :Devel::Trepan::DB::LineCache;
679 cache_file(__FILE__);
680 printf "%s has %d lines\n", __FILE__, Devel::Trepan::DB::LineCache::size(__FILE__);
681  
682 gives:
683  
684 /tmp/foo.pl has 3 lines
685  
686 =cut
687  
688 sub size($)
689 0 ▶{
Subroutine size: not called CC=3 SCAR=24.8
Pod documented
690 0 ▶ my $file_or_script = shift;
Subroutine size: not called CC=3 SCAR=24.8
Pod documented
691 0 $file_or_script = map_file($file_or_script);
692 0 cache($file_or_script);
693 0 ▶ return undef unless exists $file_cache{$file_or_script};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $Devel::Trepan::DB::LineCache::file_cache{$file_or_script} 0 0 0
unless exists $Devel::Trepan::DB::LineCache::file_cache{$file_or_script} 0 0 0
694 0 my $lines_href = $file_cache{$file_or_script}{lines_href};
695 0 ▶ return undef unless defined $lines_href;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $lines_href 0 0 0
unless defined $lines_href 0 0 0
696 0 scalar @{$lines_href->{plain}};
697 }
698  
699 =pod
700  
701 =head2 stat
702  
703 B<stat(I<$filename>)> => I<stat-info>
704  
705 Return file I<stat()> info in the cache for I<$filename>.
706  
707 B<Example>:
708  
709 In file C</tmp/foo.pl>:
710  
711 use Devel::Trepan::DB::LineCache;
712 cache_file(__FILE__);
713 printf("stat() info for %s is:
714 dev ino mode nlink uid gid rdev size atime ctime ...
715 %4d %8d %7o %3d %4d %4d %4d %4d %d %d",
716 __FILE__,
717 @{Devel::Trepan::DB::LineCache::stat(__FILE__)});
718  
719 gives:
720  
721 stat() info for /tmp/foo.pl is:
722 dev ino mode nlink uid gid rdev size atime ctime ...
723 2056 5242974 100664 1 1000 1000 0 266 1347890102 1347890101
724  
725 =cut
726  
727 sub Devel::Trepan::DB::LineCache::stat($)
728 0 ▶{
Subroutine stat: not called CC=2 SCAR=17.9
729 0 ▶ my $filename = shift;
Subroutine stat: not called CC=2 SCAR=17.9
730 0 ▶ return undef unless exists $file_cache{$filename};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $Devel::Trepan::DB::LineCache::file_cache{$filename} 0 0 0
unless exists $Devel::Trepan::DB::LineCache::file_cache{$filename} 0 0 0
731 0 $file_cache{$filename}{stat};
732 }
733  
734 =pod
735  
736 =head2 trace_line_numbers
737  
738 B<trace_line_numbers($filename [, $reload_on_change])> => I<list-of-numbers>
739  
740 Return an array of line numbers in (control opcodes) COP in
741 $I<filename>. These line numbers are the places where a breakpoint
742 might be set in a debugger.
743  
744 We get this information from the Perl run-time, so that should have
745 been set up for this to take effect. See L<B::CodeLines> for a way to
746 get this information, basically by running an Perl invocation that has
747 this set up.
748  
749 =cut
750  
751 sub trace_line_numbers($;$)
752 1 ▶{
Subroutine trace_line_numbers: called CC=2 SCAR=10.0
Pod documented
753 0 ▶ my ($filename, $reload_on_change) = @_;
Subroutine trace_line_numbers: not called CC=2 SCAR=10.0
Pod documented
754 0 my $fullname = update_cache($filename, $reload_on_change);
755 0 ▶ return undef unless $fullname;
Branch: 2 branches
BranchTrueFalseTotal
unless $fullname 0 1 1
unless $fullname 0 0 0
756 0 return sort {$a <=> $b} keys %{$file_cache{$filename}{trace_nums}};
757 }
758  
759 =pod
760  
761 =head2 is_trace_line
762  
763 B<is_trace_line($filename, $line_num [,$reload_on_change])> => I<boolean>
764  
765 Return I<true> if I<$line_num> is a trace line number of I<$filename>.
766  
767 See the comment in L<trace_line_numbers> regarding run-time setup that
768 needs to take place for this to work.
769  
770 =cut
771  
772 sub is_trace_line($$;$)
773 0 ▶{
Subroutine is_trace_line: not called CC=2 SCAR=17.9
Pod documented
774 0 ▶ my ($filename, $line_num, $reload_on_change) = @_;
Subroutine is_trace_line: not called CC=2 SCAR=17.9
Pod documented
775 0 my $fullname = cache($filename, $reload_on_change);
776 0 ▶ return undef unless $fullname;
Branch: 2 branches
BranchTrueFalseTotal
unless $fullname 0 0 0
unless $fullname 0 0 0
777 0 return !!$file_cache{$filename}{trace_nums}{$line_num};
778 }
779  
780 =pod
781  
782 =head2 map_file
783  
784 B<map_file($filename)> => string
785  
786 A previous invocation of I<remap_file()> could have mapped
787 I<$filename> into something else. If that is the case we return the
788 name that I<$filename> was mapped into. Otherwise we return I<$filename>
789  
790 =cut
791  
792 sub map_file($)
793 6 ▶{
Subroutine map_file: called CC=7 SCAR=32.4
Pod documented
794 0 ▶ my $filename = shift;
Subroutine map_file: not called CC=7 SCAR=32.4
Pod documented
795 0 ▶ return undef unless defined($filename);
Branch: 2 branches
BranchTrueFalseTotal
unless defined $filename 0 6 6
unless defined $filename 0 0 0
796 0 ▶ if ($file2file_remap{$filename}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($file2file_remap{$filename}) { } 1 5 6
elsif ($script2file{$filename}) { } 0 5 5
if ($file2file_remap{$filename}) { } 0 0 0
elsif ($script2file{$filename}) { } 0 0 0
797 0 $file2file_remap{$filename};
798 } elsif ($script2file{$filename}) {
799 0 $script2file{$filename};
800 } else {
801 0 ▶ return $filename if -r $filename;
Branch: 2 branches
BranchTrueFalseTotal
if -r $filename 5 0 5
if -r $filename 0 0 0
802 0 ▶ my $testpath = glob $filename;
Subroutine BEGIN: called CC=1 SCAR=0.6
803 0 ▶ return $testpath if defined $testpath && -r $testpath;
Branch: 2 branches
BranchTrueFalseTotal
if defined $testpath and -r $testpath 0 0 0
if defined $testpath and -r $testpath 0 0 0
Condition0%0/6
defined $testpath and -r $testpath
!ll&&!rl&&r
000
defined $testpath and -r $testpath
!ll&&!rl&&r
000
MC/DC0%0/2
defined $testpath and -r $testpath
defined $testpath -r $testpath
MC/DC0%0/2
defined $testpath and -r $testpath
defined $testpath -r $testpath
Truth table0%0/3
A and B
A= defined $testpathB= -r $testpath
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $testpathB= -r $testpath
ABresult
0X0
100
111
804  
805 0 return $filename;
806 }
807 }
808  
809 =pod
810  
811 =head2 map_script
812  
813 B<map_script($script, $string)> => string
814  
815 Note that a previous invocation of I<remap_file()> could have mapped I<$script>
816 (a pseudo-file name that I<eval()> uses) into something else.
817  
818 Return the temporary file name that I<$script> was mapped to.
819  
820 =cut
821  
822 14 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
823 sub map_script($$;$)
824 1 ▶{
Subroutine map_script: called CC=4 SCAR=20.3
Pod documented
825 0 ▶ my ($script, $string, $opts) = @_;
Subroutine map_script: not called CC=4 SCAR=20.3
Pod documented
826 0 ▶ if (exists $script2file{$script}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $script2file{$script}) 0 1 1
if (exists $script2file{$script}) 0 0 0
827 0 return $script2file{$script};
828 }
829  
830 0 my ($fh, $tempfile) = tempfile('XXXX', SUFFIX=>'.pl',
831 TMPDIR => 1);
832 0 ▶ return undef unless defined($string);
Branch: 2 branches
BranchTrueFalseTotal
unless defined $string 0 1 1
unless defined $string 0 0 0
833 0 print $fh $string;
834 0 $fh->close();
835 0 ▶ $opts ||= {};
Condition25%1/4
$opts ||= {}
l!l
01
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
Truth table0%0/2
A
A= $opts
Aresult
11
00
836 0 $opts->{use_perl_d_file} = 0;
837 0 update_cache($tempfile, $opts);
838 0 $script2file{$script} = $tempfile;
839  
840 0 return $tempfile;
841 }
842  
843 sub map_file_line($$)
844 4 ▶{
Subroutine map_file_line: called CC=6 SCAR=34.7
Pod undocumented
845 0 ▶ my ($file, $line) = @_;
Subroutine map_file_line: not called CC=6 SCAR=34.7
Pod undocumented
846 0 ▶ if (exists $file2file_remap_lines{$file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $file2file_remap_lines{$file}) 0 4 4
if (exists $file2file_remap_lines{$file}) 0 0 0
847 0 my $triplet_ref = $file2file_remap_lines{$file};
848 0 for my $triplet (@$triplet_ref) {
849 0 my ($from_file, $range_ref, $start) = @$triplet;
850 0 my @range = @$range_ref;
851 0 ▶ if ( $range[0] >= $line && $range[-1] <= $line) {
Branch: 2 branches
BranchTrueFalseTotal
if ($range[0] >= $line and $range[-1] <= $line) 0 0 0
if ($range[0] >= $line and $range[-1] <= $line) 0 0 0
Condition0%0/6
$range[0] >= $line and $range[-1] <= $line
!ll&&!rl&&r
000
$range[0] >= $line and $range[-1] <= $line
!ll&&!rl&&r
000
MC/DC0%0/2
$range[0] >= $line and $range[-1] <= $line
$range[0] >= $line $range[-1] <= $line
MC/DC0%0/2
$range[0] >= $line and $range[-1] <= $line
$range[0] >= $line $range[-1] <= $line
Truth table0%0/3
A and B
A= $range[0] >= $lineB= $range[-1] <= $line
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $range[0] >= $lineB= $range[-1] <= $line
ABresult
0X0
100
111
852 0 ▶ my $from_file = $from_file || $file;
Condition0%0/6
$from_file || $file
l!l&&r!l&&!r
000
$from_file || $file
l!l&&r!l&&!r
000
MC/DC0%0/2
$from_file || $file
$from_file $file
MC/DC0%0/2
$from_file || $file
$from_file $file
Truth table0%0/3
A || B
A= $from_fileB= $file
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $from_fileB= $file
ABresult
1X1
011
000
853 0 return [$from_file, $start+$line-$range[0]];
854 }
855 }
856 }
857 0 return ($file, $line);
858 }
859  
860 =pod
861  
862 =head2 filename_is_eval
863  
864 B<filename_is_eval($filename)> => I<boolean>
865  
866 Return I<true> if $filename matches one of the pseudo-filename strings
867 that get created for by I<eval()>.
868  
869 =cut
870  
871 sub filename_is_eval($)
872 10 ▶{
Subroutine filename_is_eval: called CC=3 SCAR=16.5
Pod documented
873 0 ▶ my $filename = shift;
Subroutine filename_is_eval: not called CC=3 SCAR=16.5
Pod documented
874 0 ▶ return 0 unless defined $filename;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $filename 0 10 10
unless defined $filename 0 0 0
875 return !!
876 0 ▶ ($filename =~ /^\(eval \d+\)|-e$/
Condition33%2/6
$filename =~ /^\(eval \d+\)|-e$/ || $filename =~ /^sub \S+::\S+/
l!l&&r!l&&!r
307
$filename =~ /^\(eval \d+\)|-e$/ || $filename =~ /^sub \S+::\S+/
l!l&&r!l&&!r
000
MC/DC50%1/2
$filename =~ /^\(eval \d+\)|-e$/ || $filename =~ /^sub \S+::\S+/
$filename =~ /^\(eval \d+\)|-e$/ $filename =~ /^sub \S+::\S+/
MC/DC0%0/2
$filename =~ /^\(eval \d+\)|-e$/ || $filename =~ /^sub \S+::\S+/
$filename =~ /^\(eval \d+\)|-e$/ $filename =~ /^sub \S+::\S+/
Truth table66%2/3
A || B
A= $filename =~ /^\(eval \d+\)|-e$/B= $filename =~ /^sub \S+::\S+/
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $filename =~ /^\(eval \d+\)|-e$/B= $filename =~ /^sub \S+::\S+/
ABresult
1X1
011
000
877 # SelfLoader does this:
878 || $filename =~ /^sub \S+::\S+/
879 );
880 }
881  
882 =pod
883  
884 =head2 update_script_cache
885  
886 B<update_script_cache($script, $opts)> => I<boolean>
887  
888 Update a cache entry for an pseudo eval-string file name. If something
889 is wrong, return I<undef>. Return I<true> if the cache was updated and
890 I<false> if not.
891  
892 =cut
893  
894 sub update_script_cache($$)
895 1 ▶{
Subroutine update_script_cache: called CC=10 SCAR=41.3
Pod documented
896 0 ▶ my ($script, $opts) = @_;
Subroutine update_script_cache: not called CC=10 SCAR=41.3
Pod documented
897 0 ▶ return 0 unless filename_is_eval($script);
Branch: 2 branches
BranchTrueFalseTotal
unless &filename_is_eval($script) 0 1 1
unless &filename_is_eval($script) 0 0 0
898 0 my $string = $opts->{string};
899 0 my $lines_href = {};
900 0 ▶ if (defined($string)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $string) { } 1 0 1
if (defined $string) { } 0 0 0
901 0 my @lines = split(/\n/, $string);
902 0 $lines_href->{plain} = \@lines;
903 } else {
904 0 ▶ if ($script eq $DB::filename) {
Branch: 2 branches
BranchTrueFalseTotal
if ($script eq $DB::filename) { } 0 0 0
if ($script eq $DB::filename) { } 0 0 0
905 ## SelfLoader evals
906 0 ▶ if (!@DB::line && $script =~/^sub (\S+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @DB::line and $script =~ /^sub (\S+)/) 0 0 0
if (not @DB::line and $script =~ /^sub (\S+)/) 0 0 0
Condition0%0/6
not @DB::line and $script =~ /^sub (\S+)/
!ll&&!rl&&r
000
not @DB::line and $script =~ /^sub (\S+)/
!ll&&!rl&&r
000
MC/DC0%0/2
not @DB::line and $script =~ /^sub (\S+)/
not @DB::line $script =~ /^sub (\S+)/
MC/DC0%0/2
not @DB::line and $script =~ /^sub (\S+)/
not @DB::line $script =~ /^sub (\S+)/
Truth table0%0/3
not(A) and B
A= not @DB::lineB= $script =~ /^sub (\S+)/
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not @DB::lineB= $script =~ /^sub (\S+)/
ABresult
0X0
100
111
907 0 my $func = $1;
908 0 my $string = $Devel::Trepan::SelfLoader::Cache{$func};
909 0 ▶ return 0 unless $string;
Branch: 2 branches
BranchTrueFalseTotal
unless $string 0 0 0
unless $string 0 0 0
910 0 $string =~ s/^\n#line 1.+\n//;
911 0 @DB::line = split(/\n/, $string);
912 }
913  
914 # Should be the same as the else case,
915 # but just in case...
916 0 $lines_href->{plain} = \@DB::line;
917 0 $string = join("\n", @DB::line);
918 } else {
919 14 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
920 0 $lines_href->{plain} = \@{"_<$script"};
921 0 $string = join("\n", @{"_<$script"});
922 }
923 0 ▶ return 0 unless length($string);
Branch: 2 branches
BranchTrueFalseTotal
unless length $string 0 0 0
unless length $string 0 0 0
924 }
925 $lines_href->{$opts->{output}} = highlight_string($string) if
926 0 ▶ $opts->{output} && $opts->{output} ne 'plain';
Branch: 2 branches
BranchTrueFalseTotal
if $opts->{"output"} and $opts->{"output"} ne "plain" 0 1 1
if $opts->{"output"} and $opts->{"output"} ne "plain" 0 0 0
Condition16%1/6
$opts->{"output"} and $opts->{"output"} ne "plain"
!ll&&!rl&&r
100
$opts->{"output"} and $opts->{"output"} ne "plain"
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"output"} and $opts->{"output"} ne "plain"
$opts->{"output"} $opts->{"output"} ne "plain"
MC/DC0%0/2
$opts->{"output"} and $opts->{"output"} ne "plain"
$opts->{"output"} $opts->{"output"} ne "plain"
Truth table33%1/3
A and B
A= $opts->{"output"}B= $opts->{"output"} ne "plain"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $opts->{"output"}B= $opts->{"output"} ne "plain"
ABresult
0X0
100
111
927  
928 0 my $entry = {
929 lines_href => $lines_href,
930 };
931 0 $script_cache{$script} = $entry;
932 0 return 1;
933 }
934  
935 =head2
936  
937 B<dualvar_lines($file_or_string, $is_file, $mark_trace)> =>
938 # I<list of dual-var strings>
939  
940 # Routine to create dual numeric/string values for
941 # C<$file_or_string>. A list reference is returned. In string context
942 # it is the line with a trailing "\n". In a numeric context it is 0 or
943 # 1 if $mark_trace is set and B::CodeLines determines it is a trace
944 # line.
945 #
946 # Note: Perl implementations seem to put a COP address inside
947 # @DB::db_line when there are trace lines. I am not sure if this is
948 # specified as part of the API. We don't do that here but (and might
949 # even if it is not officially defined in the API.) Instead put value
950 # 1.
951 #
952 =cut
953  
954 # FIXME: $mark_trace may be something of a hack. Without it we can
955 # get into infinite regress in marking %INC modules.
956  
957 1 ▶sub dualvar_lines($$;$$) {
Subroutine dualvar_lines: called CC=9 SCAR=32.5
Pod undocumented
958 0 ▶ my ($file_or_string, $dualvar_lines, $is_file, $mark_trace) = @_;
Subroutine dualvar_lines: not called CC=9 SCAR=32.5
Pod undocumented
959 0 my @break_line = ();
960 0 local $INPUT_RECORD_SEPARATOR = "\n";
961  
962 # Setup for B::CodeLines and for reading file lines
963 0 my ($cmd, @text);
964 0 my $fh;
965 0 my $filename;
966 0 ▶ if ($is_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_file) { } 1 0 1
if ($is_file) { } 0 0 0
967 0 $filename = $file_or_string;
968 0 ▶ return () unless open($fh, '<', $filename);
Branch: 2 branches
BranchTrueFalseTotal
unless open $fh, "<", $filename 0 1 1
unless open $fh, "<", $filename 0 0 0
969 0 @text = readline $fh;
970 0 $cmd = "$^X -MO=CodeLines $filename";
971 0 close $fh;
972 } else {
973 0 @text = split("\n", $file_or_string);
974 0 $cmd = "$^X -MO=CodeLines,-exec -e '$file_or_string'";
975 }
976  
977 # Make text data be 1-origin rather than 0-origin.
978 0 unshift @text, undef;
979  
980 # Get trace lines from B::CodeLines
981 0 ▶ if ($mark_trace and open($fh, '-|', "$cmd 2>/dev/null")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mark_trace and open $fh, "-|", "$cmd 2>/dev/null") 1 0 1
if ($mark_trace and open $fh, "-|", "$cmd 2>/dev/null") 0 0 0
Condition16%1/6
$mark_trace and open $fh, "-|", "$cmd 2>/dev/null"
!ll&&!rl&&r
001
$mark_trace and open $fh, "-|", "$cmd 2>/dev/null"
!ll&&!rl&&r
000
MC/DC0%0/2
$mark_trace and open $fh, "-|", "$cmd 2>/dev/null"
$mark_trace open $fh, "-|", "$cmd 2>/dev/null"
MC/DC0%0/2
$mark_trace and open $fh, "-|", "$cmd 2>/dev/null"
$mark_trace open $fh, "-|", "$cmd 2>/dev/null"
Truth table33%1/3
A and B
A= $mark_traceB= open $fh, "-|", "$cmd 2>/dev/null"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $mark_traceB= open $fh, "-|", "$cmd 2>/dev/null"
ABresult
0X0
100
111
982 0 ▶ while (my $line=<$fh>) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 6 1 7
if (defined(my $line = readline $fh)) 0 0 0
983 0 ▶ next unless $line =~ /^\d+$/;
Branch: 2 branches
BranchTrueFalseTotal
unless $line =~ /^\d+$/ 0 6 6
unless $line =~ /^\d+$/ 0 0 0
984 0 $break_line[$line] = $line;
985 }
986 }
987 # Create dual variable array and hash.
988 0 my $trace_nums = {};
989 0 ▶ for (my $i = 1; $i < scalar @text; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < scalar @text) 15 1 16
if ($i < scalar @text) 0 0 0
990 0 ▶ my $num = exists $break_line[$i] ? $mark_trace : 0;
Branch: 2 branches
BranchTrueFalseTotal
exists $break_line[$i] ? : 6 9 15
exists $break_line[$i] ? : 0 0 0
991 0 $trace_nums->{$i} = -$i;
992 0 $dualvar_lines->[$i] = Scalar::Util::dualvar($num, $text[$i] . "\n");
993 }
994 0 $file_cache{$filename}{trace_nums} = $trace_nums;
995 0 return $dualvar_lines;
996 }
997  
998 =head2
999  
1000 B<load_file(I<$filename>)> => I<list of strings>
1001  
1002 Somewhat simulates what Perl does in reading a file when debugging is
1003 turned on. We return the file contents as a list of strings in
1004 I<_E<gt>$filename>. But also entry is a dual variable. In numeric
1005 context, each entry of the list is I<true> if that line is traceable
1006 or break-pointable (is the address of a COP instruction). In a
1007 non-numeric context, each entry is a string of the line contents
1008 including the trailing C<\n>.
1009  
1010 I<Note:> something similar exists in L<Enbugger> and it is useful when
1011 a debugger is called via Enbugger which turn on debugging late so source
1012 files might not have been read in.
1013  
1014 =cut
1015 1 ▶sub load_file($;$) {
Subroutine load_file: called CC=3 SCAR=15.7
Pod undocumented
1016 0 ▶ my ($filename, $eval_string) = @_;
Subroutine load_file: not called CC=3 SCAR=15.7
Pod undocumented
1017  
1018 # The symbols by which we'll know ye.
1019 0 my $base_symname = "_<$filename";
1020 0 my $symname = "main::$base_symname";
1021  
1022 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1023  
1024 # Note: dualvar_lines updates @$synmame;
1025 0 ▶ if (defined($eval_string)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $eval_string) { } 0 1 1
if (defined $eval_string) { } 0 0 0
1026 0 dualvar_lines($eval_string, \@$symname, 0, 1);
1027 } else {
1028 0 dualvar_lines($filename, \@$symname, 1, 1);
1029 }
1030  
1031 0 ▶ $$symname ||= $filename;
Condition16%1/6
$$symname ||= $filename
l!l&&r!l&&!r
010
$$symname ||= $filename
l!l&&r!l&&!r
000
MC/DC0%0/2
$$symname ||= $filename
$$symname $filename
MC/DC0%0/2
$$symname ||= $filename
$$symname $filename
Truth table33%1/3
A ||= B
A= $$symnameB= $filename
ABresult
1X1
011
000
Truth table0%0/3
A ||= B
A= $$symnameB= $filename
ABresult
1X1
011
000
1032  
1033 0 return;
1034 }
1035  
1036 =head2 readlines
1037  
1038 B<readlines(I<$filename>)> => I<list of strings>
1039  
1040 Return a a list of strings for I<$filename>. If we can't read
1041 I<$filename> retun I<undef>. Each line will have a "\n" at the end.
1042  
1043 =cut
1044  
1045 sub readlines($)
1046 10 ▶{
Subroutine readlines: called CC=2 SCAR=10.4
Pod documented
1047 0 ▶ my $path = shift;
Subroutine readlines: not called CC=2 SCAR=10.4
Pod documented
1048 0 ▶ if (-r $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-r $path) { } 10 0 10
if (-r $path) { } 0 0 0
1049 0 my $fh;
1050 0 open($fh, '<', $path);
1051 0 seek $fh, 0, 0;
1052 0 my @lines = <$fh>;
1053 0 close $fh;
1054 0 return @lines;
1055 } else {
1056 0 return undef;
1057 }
1058 }
1059  
1060 =head2 update_cache
1061  
1062 B<update_cache($filename, [, $opts]>
1063  
1064 Update a cache entry. If something's wrong, return I<undef>. Return
1065 the expanded file name if the cache was updated and I<false> if not. If
1066 $I<$opts-E<gt>{use_perl_d_file}> is I<true>, use that as the source for the
1067 lines of the file.
1068  
1069 =cut
1070  
1071 sub update_cache($;$)
1072 5 ▶{
Subroutine update_cache: called CC=32 SCAR=55.6
Pod documented
1073 0 ▶ my ($filename, $opts) = @_;
Subroutine update_cache: not called CC=32 SCAR=55.6
Pod documented
1074 0 my $read_file = 0;
1075 0 ▶ $opts = {} unless defined $opts;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $opts 1 4 5
unless defined $opts 0 0 0
1076 0 my $use_perl_d_file = $opts->{use_perl_d_file};
1077 0 ▶ $use_perl_d_file = 1 unless defined $use_perl_d_file;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $use_perl_d_file 1 4 5
unless defined $use_perl_d_file 0 0 0
1078  
1079 0 ▶ return undef unless $filename;
Branch: 2 branches
BranchTrueFalseTotal
unless $filename 0 5 5
unless $filename 0 0 0
1080  
1081 0 delete $file_cache{$filename};
1082  
1083 0 my $is_eval = filename_is_eval($filename);
1084 0 my $path = $filename;
1085 0 ▶ unless ($is_eval) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($is_eval) 5 0 5
unless ($is_eval) 0 0 0
1086 0 ▶ $path = abs_path($filename) if -f $filename;
Branch: 2 branches
BranchTrueFalseTotal
if -f $filename 5 0 5
if -f $filename 0 0 0
1087 }
1088 0 my $lines_href;
1089 0 my $trace_nums = {};
1090 0 my $stat;
1091 0 ▶ if ($use_perl_d_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($use_perl_d_file) 4 1 5
if ($use_perl_d_file) 0 0 0
1092 0 my @list = ($filename);
1093 0 ▶ if ($is_eval) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_eval) 0 4 4
if ($is_eval) 0 0 0
1094 0 cache_script($filename);
1095 ## FIXME: create a temporary file in script2file;
1096 }
1097 0 ▶ push @list, $file2file_remap{$path} if exists $file2file_remap{$path};
Branch: 2 branches
BranchTrueFalseTotal
if exists $file2file_remap{$path} 1 3 4
if exists $file2file_remap{$path} 0 0 0
1098 0 for my $name (@list) {
1099 14 ▶ no strict; # Avoid string as ARRAY ref error message
Subroutine BEGIN: called CC=1 SCAR=0.0
1100 0 ▶ if (scalar @{"main::_<$name"}) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @{"main::_<$name";}) 5 0 5
if (scalar @{"main::_<$name";}) 0 0 0
1101 0 $stat = File::stat::stat($path);
1102 }
1103 0 my $raw_lines = \@{"main::_<$name"};
1104  
1105 # Perl sometimes doesn't seem to save all file data, such
1106 # as those intended for POD or possibly those after
1107 # __END__. But we want these, so we'll have to read the
1108 # file the old-fashioned way and check lines. Variable
1109 # $incomplete records if there was a mismatch.
1110 0 my $incomplete = 0;
1111 0 ▶ if (-r $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-r $path) 5 0 5
if (-r $path) 0 0 0
1112 0 my @lines_check = readlines($path);
1113 0 my @lines = @$raw_lines;
1114 0 my $totally_empty = 1;
1115 0 ▶ for (my $i=1; $i<=$#lines; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i <= $#lines) 27 0 27
if ($i <= $#lines) 0 0 0
1116 0 ▶ if (defined $raw_lines->[$i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $raw_lines->[$i]) 5 22 27
if (defined $raw_lines->[$i]) 0 0 0
1117 0 $totally_empty = 0;
1118 0 last;
1119 }
1120 }
1121 0 ▶ if ($totally_empty) {
Branch: 2 branches
BranchTrueFalseTotal
if ($totally_empty) { } 0 5 5
if ($totally_empty) { } 0 0 0
1122 0 load_file($filename);
1123 0 $trace_nums = $file_cache{$filename}{trace_nums};
1124 } else {
1125 0 ▶ for (my $i=1; $i<=$#lines; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i <= $#lines) 181 5 186
if ($i <= $#lines) 0 0 0
1126 0 ▶ if (defined $raw_lines->[$i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $raw_lines->[$i]) { } 159 22 181
if (defined $raw_lines->[$i]) { } 0 0 0
1127 14 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1128 0 ▶ $trace_nums->{$i} = (-$raw_lines->[$i]) if
Branch: 2 branches
BranchTrueFalseTotal
if $raw_lines->[$i] != 0 18 141 159
if $raw_lines->[$i] != 0 0 0 0
1129 (+$raw_lines->[$i]) != 0;
1130 0 ▶ $incomplete = 1 if $raw_lines->[$i] ne $lines[$i];
Branch: 2 branches
BranchTrueFalseTotal
if $raw_lines->[$i] ne $lines[$i] 0 159 159
if $raw_lines->[$i] ne $lines[$i] 0 0 0
1131 } else {
1132 0 $raw_lines->[$i] = $lines_check[$i-1]
1133 }
1134 }
1135 }
1136 }
1137 14 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1138 0 $lines_href = {};
1139 0 $lines_href->{plain} = $raw_lines;
1140 0 ▶ if ($opts->{output} && $opts->{output} ne 'plain' && defined($raw_lines)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{"output"} and $opts->{"output"} ne "plain" and defined $raw_lines) 0 5 5
if ($opts->{"output"} and $opts->{"output"} ne "plain" and defined $raw_lines) 0 0 0
Condition25%3/12
$opts->{"output"} and $opts->{"output"} ne "plain" and defined $raw_lines
!ll&&!rl&&r
500
$opts->{"output"} and $opts->{"output"} ne "plain"
!ll&&!rl&&r
410
$opts->{"output"} and $opts->{"output"} ne "plain" and defined $raw_lines
!ll&&!rl&&r
000
$opts->{"output"} and $opts->{"output"} ne "plain"
!ll&&!rl&&r
000
MC/DC0%0/3
$opts->{"output"} and $opts->{"output"} ne "plain" and defined $raw_lines
$opts->{"output"} $opts->{"output"} ne "plain" defined $raw_lines
MC/DC0%0/3
$opts->{"output"} and $opts->{"output"} ne "plain" and defined $raw_lines
$opts->{"output"} $opts->{"output"} ne "plain" defined $raw_lines
Truth table50%2/4
A and B and C
A= $opts->{"output"}B= $opts->{"output"} ne "plain"C= defined $raw_lines
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $opts->{"output"}B= $opts->{"output"} ne "plain"C= defined $raw_lines
ABCresult
0XX0
10X0
1100
1111
1141 # Some lines in $raw_lines may be undefined
1142 14 ▶ no strict; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1143 0 local $WARNING=0;
1144 0 my $highlight_lines = highlight_string(join('', @$raw_lines));
1145 0 my @highlight_lines = split(/\n/, $highlight_lines);
1146 0 $lines_href->{$opts->{output}} = \@highlight_lines;
1147 14 ▶ use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1148 }
1149 0 $read_file = 1;
1150 }
1151 }
1152  
1153 # File based reading is done here.
1154 0 ▶ if (-f $path ) {
Branch: 4 branches
BranchTrueFalseTotal
if (-f $path) { } 5 0 5
elsif (not $read_file) { } 0 0 0
if (-f $path) { } 0 0 0
elsif (not $read_file) { } 0 0 0
1155 0 ▶ $stat = File::stat::stat($path) unless defined $stat;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $stat 1 4 5
unless defined $stat 0 0 0
1156 } elsif (!$read_file) {
1157 0 ▶ if (basename($filename) eq $filename) {
Branch: 2 branches
BranchTrueFalseTotal
if (basename($filename) eq $filename) 0 0 0
if (basename($filename) eq $filename) 0 0 0
1158 # try looking through the search path.
1159 0 for my $dirname (@INC) {
1160 0 $path = File::Spec->catfile($dirname, $filename);
1161 0 ▶ if ( -f $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $path) 0 0 0
if (-f $path) 0 0 0
1162 0 $stat = File::stat::stat($path);
1163 0 last;
1164 }
1165 }
1166 }
1167 0 ▶ return 0 unless defined $stat;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $stat 0 0 0
unless defined $stat 0 0 0
1168 }
1169 0 ▶ if ( -r $path ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-r $path) 5 0 5
if (-r $path) 0 0 0
1170 0 my @lines = readlines($path);
1171 0 $lines_href = {plain => \@lines};
1172 0 ▶ if ($opts->{output} && $opts->{output} ne 'plain') {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{"output"} and $opts->{"output"} ne "plain") 0 5 5
if ($opts->{"output"} and $opts->{"output"} ne "plain") 0 0 0
Condition33%2/6
$opts->{"output"} and $opts->{"output"} ne "plain"
!ll&&!rl&&r
410
$opts->{"output"} and $opts->{"output"} ne "plain"
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"output"} and $opts->{"output"} ne "plain"
$opts->{"output"} $opts->{"output"} ne "plain"
MC/DC0%0/2
$opts->{"output"} and $opts->{"output"} ne "plain"
$opts->{"output"} $opts->{"output"} ne "plain"
Truth table66%2/3
A and B
A= $opts->{"output"}B= $opts->{"output"} ne "plain"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $opts->{"output"}B= $opts->{"output"} ne "plain"
ABresult
0X0
100
111
1173 0 my $highlight_lines = highlight_string(join('', @lines));
1174 0 my @highlight_lines = split(/\n/, $highlight_lines);
1175 0 $lines_href->{$opts->{output}} = \@highlight_lines;
1176 }
1177 }
1178 0 my $entry = {
1179 stat => $stat,
1180 lines_href => $lines_href,
1181 path => $path,
1182 incomplete => 0,
1183 trace_nums => $trace_nums,
1184 };
1185 0 $file_cache{$filename} = $entry;
1186 14 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1187 0 $file2file_remap{$path} = $filename;
1188 0 return $path;
1189 }
1190  
1191 # example usage
1192 14 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 14 14
1193 14 ▶ BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
1194 14 ▶ use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
1195 14 $PERLDB |= 0x400;
1196 }; # Turn on saving @{_<$filename};
1197 0 my $file=__FILE__;
1198 0 my $fullfile = abs_path($file);
1199 14 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1200 0 print scalar(@{"main::_<$file"}), "\n";
1201 14 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1202  
1203 0 my $script_name = '(eval 234)';
1204 0 update_script_cache($script_name, {string => "now\nis\nthe\ntime"});
1205 0 print join(', ', keys %script_cache), "\n";
1206 0 my $lines = $script_cache{$script_name}{lines_href}{plain};
1207 0 print join("\n", @{$lines}), "\n";
1208 0 $lines = getlines($script_name);
1209 0 printf "%s has %d lines\n", $script_name, scalar @$lines;
1210 0 printf("Line 1 of $script_name is:\n%s\n",
1211 getline($script_name, 1));
1212 0 my $max_line = size($script_name);
1213 0 printf("%s has %d lines via size\n",
1214 $script_name, scalar @$lines);
1215 0 do __FILE__;
1216 0 my @line_nums = trace_line_numbers(__FILE__);
1217  
1218 ### FIXME: add more of this stuff into unit test.
1219 0 printf("Breakpoints for: %s:\n%s\n",
1220 __FILE__, join(', ', @line_nums[0..30]));
1221 0 $lines = getlines(__FILE__);
1222 0 printf "%s has %d lines\n", __FILE__, scalar @$lines;
1223 0 my $full_file = abs_path(__FILE__);
1224 0 $lines = getlines(__FILE__);
1225 0 printf "%s still has %d lines\n", __FILE__, scalar @$lines;
1226 0 $lines = getlines(__FILE__);
1227 0 printf "%s also has %d lines\n", $full_file, scalar @$lines;
1228 0 my $line_number = __LINE__;
1229 0 my $line = getline(__FILE__, $line_number);
1230 0 printf "The %d line is:\n%s\n", $line_number, $line ;
1231 0 remap_file('another_name', __FILE__);
1232 0 print getline('another_name', __LINE__), "\n";
1233 0 printf "Files cached: %s\n", join(', ', cached_files);
1234 0 update_cache(__FILE__);
1235 0 printf "I said %s has %d lines!\n", __FILE__, size(__FILE__);
1236 0 printf "SHA1 of %s is:\n%s\n", __FILE__, sha1(__FILE__);
1237  
1238 0 my $stat = stat(__FILE__);
1239 0 printf("stat info size: %d, ctime %s, mode %o\n",
1240 $stat->size, $stat->ctime, $stat->mode);
1241  
1242 0 my $lines_aref = getlines(__FILE__, {output=>'term'});
1243 0 ▶ print join("\n", @$lines_aref[0..5,50..55]), "\n" if defined $lines_aref;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lines_aref 0 0 0
1244 0 $DB::filename = '(eval 4)';
1245 0 my $filename = map_script($DB::filename, "\$x=1;\n\$y=2;\n\$z=3;\n");
1246 0 print "mapped eval is $filename\n";
1247 0 printf("%s is a trace line? %d\n", __FILE__,
1248 is_trace_line(__FILE__, __LINE__-1));
1249 0 printf("%s is a trace line? %d\n", __FILE__,
1250 is_trace_line(__FILE__, __LINE__));
1251 0 eval "printf \"filename_is_eval: %s, %d\n\", __FILE__,
1252 filename_is_eval(__FILE__);";
1253 0 printf("filename_is_eval: %s, %d\n", __FILE__, filename_is_eval(__FILE__));
1254 0 printf("filename_is_eval: %s, %d\n", '-e', filename_is_eval('-e'));
1255  
1256 #$DB::filename = 'bogus';
1257 #eval {
1258 # print '+++', is_cached_script(__FILE__), "\n";
1259 #};
1260  
1261 0 $lines_aref = getlines(__FILE__, {output=>'term'});
1262 # print("trace nums again: ", join(', ',
1263 # trace_line_numbers(__FILE__)),
1264 # "\n");
1265 0 $line = getline(__FILE__, __LINE__,
1266 {output=>'term',
1267 maxlines => 6});
1268 0 print '-' x 30, "\n";
1269 0 print "$line\n";
1270 0 $line = getline(__FILE__, __LINE__,
1271 {output=>'plain',
1272 maxlines => 5});
1273 0 print '-' x 30, "\n";
1274 0 print "$line\n";
1275 0 print '-' x 30, "\n";
1276  
1277 0 my $dirname = File::Basename::dirname(__FILE__);
1278 0 my $colors_file = File::Spec->catfile($dirname, 'Colors.pm');
1279 0 load_file($colors_file);
1280 0 @line_nums = trace_line_numbers($colors_file);
1281 0 print join(', ', @line_nums, "\n");
1282 }
1283  
1284 14 1;
« lib/Devel/Trepan/CmdProcessor/Command/Complete.pm ↑ Summary blib/lib/Devel/Trepan/DB/SelfLoader.pm »