/
Backtrace.pm
186 lines (164 loc) Β· 6.31 KB
/
Backtrace.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
my class Exception { ... }
my class Backtrace { ... }
my class Backtrace::Frame {
has Str $.file;
has Int $.line;
has Mu $.code;
has Str $.subname;
method subtype(Backtrace::Frame:D:) {
my $s = $!code.^name.lc.split('+', 2)[0];
$s eq 'mu' ?? '' !! $s;
}
method package(Backtrace::Frame:D:) {
$.code.package;
}
multi method Str(Backtrace::Frame:D:) {
my $s = self.subtype;
$s ~= ' ' if $s.chars;
my $text = " in {$s}$.subname at {$.file}:$.line\n";
if +(%*ENV<RAKUDO_VERBOSE_STACKFRAME> // 0) -> $extra {
my $io = $!file.IO;
if $io.e {
my @lines := $io.lines;
my $from = max $!line - $extra, 1;
my $to = min $!line + $extra, +@lines;
for $from..$to -> $line {
my $star = $line == $!line ?? '*' !! ' ';
$text ~= "$line.fmt('%5d')$star @lines[$line - 1]\n";
}
$text ~= "\n";
}
}
$text;
}
method is-hidden(Backtrace::Frame:D:) { $!code.?is-hidden-from-backtrace }
method is-routine(Backtrace::Frame:D:) { nqp::istype($!code,Routine) }
method is-setting(Backtrace::Frame:D:) { $!file.ends-with("CORE.setting") }
}
my class Backtrace is List {
proto method new(|) {*}
multi method new(Mu $e, Int $offset = 0) {
$e.^name eq 'BOOTException'
?? self.new(nqp::backtrace(nqp::decont($e)), $offset)
!! self.new(nqp::backtrace(nqp::getattr(nqp::decont($e), Exception, '$!ex')), $offset);
}
multi method new(Int $offset = 0) {
try { die() };
self.new($!, 2 + $offset);
}
# note that backtraces are nqp::list()s, marshalled to us as Parcel
multi method new(Parcel $bt, Int $offset = 0) {
my $new = self.bless();
for $offset .. $bt.elems - 1 {
next unless defined $bt[$_]<sub>;
my Mu $sub := nqp::getattr(nqp::decont($bt[$_]<sub>), ForeignCode, '$!do');
next if nqp::isnull($sub);
my $code;
try {
$code := nqp::getcodeobj($sub);
$code := Any unless nqp::istype($code, Mu);
};
my $line = $bt[$_]<annotations><line>;
my $file = $bt[$_]<annotations><file>;
next unless $line && $file;
# now *that's* an evil hack
next if $file.ends-with('BOOTSTRAP.nqp')
|| $file.ends-with('QRegex.nqp');
last if $file.ends-with('NQPHLL.nqp');
my $subname = nqp::p6box_s(nqp::getcodename($sub));
$subname = '<anon>' if $subname.starts-with("_block");
last if $subname eq 'handle-begin-time-exceptions';
$new.push: Backtrace::Frame.new(
:line($line.Int),
:$file,
:$subname,
:$code,
);
}
$new;
}
method next-interesting-index(Backtrace:D:
Int $idx is copy = 0, :$named, :$noproto, :$setting) {
++$idx;
# NOTE: the < $.end looks like an off-by-one error
# but it turns out that a simple perl6 -e 'die "foo"'
# has two bt frames from the mainline; so it's OK to never
# consider the last one
loop (; $idx < self.end; ++$idx) {
my $cand = self.AT-POS($idx);
next if $cand.is-hidden; # hidden is never interesting
next if $named && !$cand.subname; # only want named ones
next if $noproto # no proto's please
&& $cand.code.?is_dispatcher; # if a dispatcher
next if !$setting # no settings please
&& $cand.is-setting; # and in setting
return $idx;
}
Int;
}
method outer-caller-idx(Backtrace:D: Int $startidx) {
my %print;
my $start = self.AT-POS($startidx).code;
return $startidx.list unless $start;
my $current = $start.outer;
my %outers;
while $current.DEFINITE {
%outers{$current.static_id} = $start;
$current = $current.outer;
}
my @outers;
loop (my Int $i = $startidx; $i < $.end; ++$i) {
if self.AT-POS($i).code.DEFINITE && %outers{self.AT-POS($i).code.static_id}.DEFINITE {
@outers.push: $i;
return @outers if self.AT-POS($i).is-routine;
}
}
@outers;
}
method nice(Backtrace:D: :$oneline) {
my $setting = %*ENV<RAKUDO_BACKTRACE_SETTING>;
try {
my @frames;
my Int $i = self.next-interesting-index(-1);
while $i.defined {
$i = self.next-interesting-index($i, :$setting) if $oneline;
last unless $i.defined;
my $prev = self.AT-POS($i);
if $prev.is-routine {
@frames.push: $prev;
} else {
my @outer_callers := self.outer-caller-idx($i);
my ($target_idx) = @outer_callers.keys.grep({self.AT-POS($i).code.^isa(Routine)});
$target_idx ||= @outer_callers[0] || $i;
my $current = self.AT-POS($target_idx);
@frames.push: $current.clone(line => $prev.line);
$i = $target_idx;
}
last if $oneline;
$i = self.next-interesting-index($i, :$setting);
}
CATCH {
default {
return "<Internal error while creating backtrace: $_.message() $_.backtrace.full().\n"
~ "Please report this as a bug (mail to rakudobug@perl.org)\n",
~ "and re-run with the --ll-exception command line option\n"
~ "to get more information about your error>";
}
}
@frames.join;
}
}
method concise(Backtrace:D:) {
self.grep({ !.is-hidden && .is-routine && !.is-setting }).join
}
multi method Str(Backtrace:D:) {
self.nice;
}
method full(Backtrace:D:) {
self.join
}
method summary(Backtrace:D:) {
self.grep({ !.is-hidden && (.is-routine || !.is-setting )}).join
}
}
# vim: ft=perl6 expandtab sw=4