StackTrace.pm

filter: Statementstmt 87.7% 129 / 147 Branchbran 73.2% 41 / 56 Conditioncond 72.7% 24 / 33 MC/DCmcdc 63.1% 12 / 19 Subroutinesub 95.6% 22 / 23 Podpod 66.6% 4 / 6 totaltotal 81.6% 232 / 284 CC 42 SCAR 20.1
CC
42
Cov
82%
parse
25.1
_handle_block
12.5
_parse_text
9.0
CRAP 7.5
↑ Summary StackTrace/Type/GDB/Frame.pm »
1 package Parse::StackTrace;
2 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Parse::StackTrace::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Exception::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '0.08';
10  
11 3 has 'threads' => (is => 'ro', isa => 'ArrayRef[Parse::StackTrace::Thread]',
12 required => 1);
13 3 has 'binary' => (is => 'ro', isa => 'Str|Undef');
14 3 has 'text_lines' => (is => 'ro', isa => 'ArrayRef[Str]', required => 1);
15  
16 # Defaults for StackTrace types that don't define these.
17 3 ▶use constant BIN_REGEX => '';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use constant IGNORE_LINES => {};
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 our $WHITESPACE_ONLY = qr/^\s*$/;
21  
22 #####################
23 # Parsing Functions #
24 #####################
25  
26 25 ▶sub parse {
Subroutine parse: called CC=6 SCAR=32.2
Pod documented
27 25 my $class = shift;
28 # If you call parse() directly on this class, then we use the "type"
29 # parameter to determine what type of StackTrace we're parsing.
30 25 ▶ if ($class eq 'Parse::StackTrace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "Parse::StackTrace") 0 25 25
31 0 my %params = @_;
32 0 my $types = $params{'types'};
33 0 ▶ die "You must specify trace types" if !$types || !scalar @$types;
Branch: 1 branch
BranchTrueFalseTotal
unless $types and scalar @$types 0 0 0
Condition0%0/3
$types and scalar @$types
!ll&&!rl&&r
000
MC/DC0%0/2
$types and scalar @$types
$types scalar @$types
Truth table0%0/3
A and B
A= $typesB= scalar @$types
ABresult
0X0
100
111
34 0 my $trace;
35 0 foreach my $type (@$types) {
36 0 my $parser = $class->_class("Type::$type");
37 0 $trace = $parser->parse(@_);
38 0 ▶ return $trace if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 0 0
39 }
40
41 0 return undef;
42 }
43
44 # For subclasses
45 25 return $class->_do_parse(@_);
46 }
47  
48 25 ▶sub _do_parse {
Subroutine _do_parse: called CC=5 SCAR=16.2
49 25 my ($class, %params) = @_;
50 25 my $text = $params{text};
51 25 my $debug = $params{debug};
52
53 25 ▶ die "You must specify a value for the 'text' argument" if !defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 25 25
54 25 ▶ if ($text !~ $class->HAS_TRACE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text =~ $class->HAS_TRACE) 0 25 25
55 0 return undef;
56 }
57
58 25 my $binary;
59 25 ▶ if ($class->BIN_REGEX and $text =~ $class->BIN_REGEX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->BIN_REGEX and $text =~ $class->BIN_REGEX) 6 19 25
Condition100%3/3
$class->BIN_REGEX and $text =~ $class->BIN_REGEX
!ll&&!rl&&r
5146
MC/DC100%2/2
$class->BIN_REGEX and $text =~ $class->BIN_REGEX
$class->BIN_REGEX $text =~ $class->BIN_REGEX
Truth table100%3/3
A and B
A= $class->BIN_REGEXB= $text =~ $class->BIN_REGEX
ABresult
0X0
100
111
60 6 $binary = $1;
61 }
62  
63 25 my ($threads, $trace_lines) = $class->_parse_text($text, $debug);
64 25 my $trace = $class->new(threads => $threads, binary => $binary,
65 text_lines => $trace_lines);
66 25 return $trace;
67 }
68  
69 25 ▶sub _parse_text {
Subroutine _parse_text: called CC=9 SCAR=22.0
70 25 my ($class, $text, $debug) = @_;
71
72 25 my @lines = split(/\r?\n/, $text);
73 25 my @threads;
74 25 my $default_thread = $class->thread_class->new();
75 25 my $current_thread = $default_thread;
76 25 my $current_end_line = 0;
77 25 ▶ print STDERR "Current Thread: Default\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 25 25
78 25 ▶ while (scalar @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @lines) 2777 25 2802
79 2777 my $lines_start_size = scalar @lines;
80
81 2777 my $frame_lines = $class->_get_next_trace_block(\@lines);
82
83 2777 my $lines_read = $lines_start_size - scalar(@lines);
84 2777 my $current_start_line = $current_end_line + 1;
85 2777 $current_end_line += $lines_read;
86
87 2777 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 2777 2777
88 0 my $joined_lines = join("\n", @$frame_lines);
89 0 print STDERR "Trace Line(s) $current_start_line-$current_end_line:",
90 " [$joined_lines]\n";
91 }
92
93 2777 ▶ next if (scalar(@$frame_lines) == 1
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$frame_lines == 1 and $frame_lines->[0] =~ /$Parse::StackTrace::WHITESPACE_ONLY/ || $class->_ignore_line($frame_lines->[0]) 721 2056 2777
Condition100%6/6
scalar @$frame_lines == 1 and $frame_lines->[0] =~ /$Parse::StackTrace::WHITESPACE_ONLY/ || $class->_ignore_line($frame_lines->[0])
!ll&&!rl&&r
5171539721
$frame_lines->[0] =~ /$Parse::StackTrace::WHITESPACE_ONLY/ or $class->_ignore_line($frame_lines->[0])
l!l&&r!l&&!r
1445771539
MC/DC100%3/3
scalar @$frame_lines == 1 and $frame_lines->[0] =~ /$Parse::StackTrace::WHITESPACE_ONLY/ || $class->_ignore_line($frame_lines->[0])
scalar @$frame_lines == 1 $frame_lines->[0] =~ /$Parse::StackTrace::WHITESPACE_ONLY/ $class->_ignore_line($frame_lines->[0])
Truth table100%4/4
A and B or C
A= scalar @$frame_lines == 1B= $frame_lines->[0] =~ /$Parse::StackTrace::WHITESPACE_ONLY/C= $class->_ignore_line($frame_lines->[0])
ABCresult
0XX0
1000
11X1
1011
94 and ($frame_lines->[0] =~ $WHITESPACE_ONLY
95 or $class->_ignore_line($frame_lines->[0])));
96  
97 2056 $current_thread = $class->_handle_block(
98 start_line_number => $current_start_line,
99 end_line_number => $current_end_line,
100 frame_lines => $frame_lines,
101 thread => $current_thread,
102 threads => \@threads,
103 debug => $debug,
104 lines => \@lines,
105 );
106 }
107
108 # Don't include any threads that don't have frames.
109 25 @threads = grep { scalar @{ $_->frames } } @threads;
110
111 25 ▶ @threads = ($default_thread) if not @threads;
Branch: 1 branch
BranchTrueFalseTotal
unless @threads 14 11 25
112  
113 25 my @thread_starts = $threads[0]->starting_line;
114 # Sometimes default_thread isn't in the final @threads, but if we parsed
115 # frames into it, then it should be considered part of the trace text.
116 25 ▶ if ($default_thread->has_starting_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_thread->has_starting_line) 20 5 25
117 20 push(@thread_starts, $default_thread->starting_line);
118 }
119 25 my $trace_start = min(@thread_starts) - 1;
120 25 my $trace_end = $threads[-1]->ending_line - 1;
121 25 my @trace_lines = (split(/\r?\n/, $text))[$trace_start..$trace_end];
122  
123 25 return (\@threads, \@trace_lines);
124 }
125  
126 2051 ▶sub _handle_block {
Subroutine _handle_block: called CC=11 SCAR=25.3
127 2051 my ($class, %params) = @_;
128 my ($frame_lines, $current_thread, $threads, $start, $end, $lines, $debug) =
129 2051 @params{qw(frame_lines thread threads start_line_number end_line_number
130 lines debug)};
131  
132 2051 ▶ if (my ($number, $description) = $class->_line_starts_thread($frame_lines->[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = $class->_line_starts_thread($frame_lines->[0])) { } 91 1960 2051
elsif ($frame_lines->[0] =~ $class->HAS_TRACE) { } 1620 340 1960
133 91 $current_thread = $class->thread_class->new(
134 number => $number, description => $description,
135 starting_line => $start);
136 91 push(@$threads, $current_thread);
137 91 ▶ print STDERR "Current Thread: " . $current_thread->number . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 91 91
138 if $debug;
139 }
140 elsif ($frame_lines->[0] =~ $class->HAS_TRACE) {
141 # For the default thread, we have to set its starting line as soon
142 # as we parse a frame in it.
143 1620 ▶ if (!$current_thread->has_starting_line) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current_thread->has_starting_line) 20 1600 1620
144 20 $current_thread->starting_line($start);
145 }
146
147 1620 my $thread_end = $end;
148 # If the next line is ignored, it's still part of the thread
149 # in terms of its textual content, so we want to make the thread
150 # end on that line instead.
151 1620 ▶ if ($lines->[0] and $class->_ignore_line($lines->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines->[0] and $class->_ignore_line($lines->[0])) 568 1052 1620
Condition100%3/3
$lines->[0] and $class->_ignore_line($lines->[0])
!ll&&!rl&&r
401012568
MC/DC100%2/2
$lines->[0] and $class->_ignore_line($lines->[0])
$lines->[0] $class->_ignore_line($lines->[0])
Truth table100%3/3
A and B
A= $lines->[0]B= $class->_ignore_line($lines->[0])
ABresult
0X0
100
111
152 568 $thread_end++;
153 }
154 1620 $current_thread->ending_line($thread_end);
155  
156 1620 my $frame;
157 1620 eval {
158 1620 $frame = $class->frame_class->parse(lines => $frame_lines,
159 debug => $debug);
160 };
161 1620 my $e;
162 1620 ▶ if ($e = Exception::Class->caught('Parse::StackTrace::Exception::NotAFrame')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e = 'Exception::Class'->caught('Parse::StackTrace::Exception::NotAFrame')) { } 0 1620 1620
elsif ($e = 'Exception::Class'->caught) { } 0 1620 1620
163 0 warn $e;
164 }
165 elsif ($e = Exception::Class->caught) {
166 0 ▶ (blessed $e and $e->isa('Exception::Class')) ? $e->rethrow : die $e;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($e) && $e->isa('Exception::Class') ? : 0 0 0
Condition0%0/3
builtin::blessed($e) and $e->isa("Exception::Class")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($e) and $e->isa("Exception::Class")
builtin::blessed($e) $e->isa("Exception::Class")
Truth table0%0/3
A and B
A= builtin::blessed($e)B= $e->isa("Exception::Class")
ABresult
0X0
100
111
167 }
168 else {
169 1620 $current_thread->add_frame($frame);
170 }
171 }
172
173 2051 return $current_thread;
174 }
175  
176 2779 ▶sub _get_next_trace_block {
Subroutine _get_next_trace_block: called CC=4 SCAR=13.9
177 2779 my ($class, $lines) = @_;
178 2779 my @frame_lines = (shift @$lines);
179 2779 ▶ if ($frame_lines[0] =~ $class->HAS_TRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame_lines[0] =~ $class->HAS_TRACE) 1622 1157 2779
180 1622 ▶ while (@$lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 3673 9 3682
181 3673 my $next_line = $class->_get_next_frame_line($lines);
182 3673 ▶ last if not $next_line;
Branch: 1 branch
BranchTrueFalseTotal
unless $next_line 1613 2060 3673
183 2060 push(@frame_lines, $next_line);
184 }
185 }
186
187 2779 return \@frame_lines;
188 }
189  
190 # Returns the next line only if it's a *fragment* of a stack frame.
191 # If it's the start of a new frame, a new thread, or a line that we
192 # ignore, then we return nothing.
193 3673 ▶sub _get_next_frame_line {
Subroutine _get_next_frame_line: called CC=2 SCAR=6.9
194 3673 my ($class, $lines) = @_;
195 3673 my $line = $lines->[0];
196 3673 ▶ if ($class->_next_line_ends_frame($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_next_line_ends_frame($line)) 1613 2060 3673
197 return undef
198 1613 }
199 2060 return shift @$lines
200 }
201  
202 3682 ▶sub _next_line_ends_frame {
Subroutine _next_line_ends_frame: called CC=5 SCAR=16.1
203 3682 my ($class, $line) = @_;
204 3682 ▶ if ($line =~ $WHITESPACE_ONLY or $class->_ignore_line($line)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$Parse::StackTrace::WHITESPACE_ONLY/ or $class->_ignore_line($line) or $line =~ $class->HAS_TRACE or $class->_line_starts_thread($line)) 1617 2065 3682
Condition88%8/9
$line =~ /$Parse::StackTrace::WHITESPACE_ONLY/ or $class->_ignore_line($line) or $line =~ $class->HAS_TRACE or $class->_line_starts_thread($line)
l!l&&r!l&&!r
161702065
$line =~ /$Parse::StackTrace::WHITESPACE_ONLY/ or $class->_ignore_line($line) or $line =~ $class->HAS_TRACE
l!l&&r!l&&!r
60310142065
$line =~ /$Parse::StackTrace::WHITESPACE_ONLY/ or $class->_ignore_line($line)
l!l&&r!l&&!r
345693079
MC/DC75%3/4
$line =~ /$Parse::StackTrace::WHITESPACE_ONLY/ or $class->_ignore_line($line) or $line =~ $class->HAS_TRACE or $class->_line_starts_thread($line)
$line =~ /$Parse::StackTrace::WHITESPACE_ONLY/ $class->_ignore_line($line) $line =~ $class->HAS_TRACE $class->_line_starts_thread($line)
Truth table80%4/5
A or B or C or D
A= $line =~ /$Parse::StackTrace::WHITESPACE_ONLY/B= $class->_ignore_line($line)C= $line =~ $class->HAS_TRACED= $class->_line_starts_thread($line)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
205 or $line =~ $class->HAS_TRACE
206 or $class->_line_starts_thread($line))
207 {
208 1617 return 1;
209 }
210 2065 return 0;
211 }
212  
213 # By default we don't support threads.
214 97 ▶sub _line_starts_thread { return (); }
Subroutine _line_starts_thread: called CC=1 SCAR=0.0
215  
216 7344 ▶sub _ignore_line {
Subroutine _ignore_line: called CC=2 SCAR=6.9
217 7344 my ($class, $line) = @_;
218 7344 ▶ return grep($_ eq $line, $class->IGNORE_LINES) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep(($_ eq $line), $class->IGNORE_LINES) ? : 1699 5645 7344
219 }
220  
221 #####################
222 # Complex Accessors #
223 #####################
224  
225 0 ▶sub text {
Subroutine text: not called CC=1 SCAR=6.9
Pod documented
226 0 my $self = shift;
227 0 return join("\n", @{ $self->text_lines });
228 }
229  
230 31 ▶sub thread_number {
Subroutine thread_number: called CC=2 SCAR=6.9
Pod documented
231 31 my ($self, $number) = @_;
232 189 ▶ my ($thread) = grep { defined $_->number and $_->number == $number }
Condition100%3/3
defined $_->number && $_->number == $number
!ll&&!rl&&r
916911
MC/DC100%2/2
defined $_->number && $_->number == $number
defined $_->number $_->number == $number
Truth table100%3/3
A && B
A= defined $_->numberB= $_->number == $number
ABresult
0X0
100
111
233 31 @{ $self->threads };
234 31 return $thread;
235 }
236  
237 7 ▶sub thread_with_crash {
Subroutine thread_with_crash: called CC=3 SCAR=11.1
Pod documented
238 7 my $self = shift;
239 7 foreach my $thread (@{ $self->threads }) {
240 16 ▶ return $thread if defined $thread->frame_with_crash;
Branch: 1 branch
BranchTrueFalseTotal
if defined $thread->frame_with_crash 7 9 16
241 }
242 0 return undef;
243 }
244  
245 ####################
246 # Subclass Helpers #
247 ####################
248  
249 116 ▶sub thread_class { return shift->_class('Thread') }
Subroutine thread_class: called CC=1 SCAR=0.0
Pod undocumented
250 1620 ▶sub frame_class { return shift->_class('Frame') }
Subroutine frame_class: called CC=1 SCAR=0.0
Pod undocumented
251  
252 1736 ▶sub _class {
Subroutine _class: called CC=3 SCAR=11.3
253 1736 my ($invocant, $what) = @_;
254 1736 ▶ my $class = ref $invocant || $invocant;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
017360
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
255 1736 my $module = $class . '::' . $what;
256 1736 my $file = $module;
257 1736 $file =~ s{::}{/}g;
258 1736 ▶ eval { require "$file.pm" }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require "$file.pm" } } or die "Error requiring ${module}: $@" 1736 0 1736
259 || die("Error requiring $module: $@");
260 1736 return $module;
261 }
262  
263 3 __PACKAGE__->meta->make_immutable;
264  
265 3 1;
266  
267 __END__
↑ Summary StackTrace/Type/GDB/Frame.pm »