-
Notifications
You must be signed in to change notification settings - Fork 2
/
minimizer.go
203 lines (184 loc) · 3.96 KB
/
minimizer.go
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package throw
import (
"bytes"
)
func MinimizeStackTrace(st StackTrace, boundPackage string, includeBoundary bool) StackTrace {
var r stackTrace
switch v := st.(type) {
case stackTrace:
r = v
case nil:
return nil
default:
r.data = []byte(v.StackTraceAsText())
r.limit = !v.IsFullStack()
}
min := MinimizePanicStack(r.data, boundPackage, includeBoundary)
if len(min) == len(r.data) {
return st
}
r.data = min
return r
}
func MinimizePanicStack(stackTrace []byte, boundPackage string, includeBoundary bool) []byte {
s := alignStart(stackTrace)
if len(s) == 0 {
return stackTrace
}
pos := skipDebugFrame(s)
s = s[pos:]
prefix := []byte(boundPackage)
// check defer section of panic stack - it will likely start with the boundPackage
start := skipFramesWithPrefix(s, prefix)
if start > 0 {
if start == len(s) {
return s
}
n := skipPanicFrame(s[start:])
if n == 0 {
if bytes.HasPrefix(s[start:], []byte("created by ")) {
return s
}
return minimizeDebugStack(s[start:], boundPackage, includeBoundary)
}
n += start
n += len(minimizeDebugStack(s[n:], boundPackage, includeBoundary))
if n == len(s) {
return s
}
return s[start:n]
}
start = 0
pos = 0
for {
n := skipPanicFrame(s[pos:])
if n > 0 {
pos += n
n = len(minimizeDebugStack(s[pos:], boundPackage, includeBoundary))
return s[start : pos+n]
}
if bytes.HasPrefix(s[pos:], prefix) {
break
}
n = indexOfFrame(s[pos:], 1)
if n <= 0 {
return s
}
pos += n
}
startBound := pos
pos += skipFramesWithPrefix(s[pos:], prefix)
if pos == len(s) {
return s[start:]
}
if !bytes.Contains(s[pos:], []byte("runtime/panic.go")) {
if startBound == start {
return s
}
if includeBoundary {
if n := indexOfFrame(s[startBound:], 1); n > 0 {
startBound += n
}
}
return s[start:startBound]
}
return minimizeDebugStack(s[pos:], boundPackage, includeBoundary)
}
func skipPanicFrame(s []byte) int {
return skipFrameByMethod(s, "panic", "runtime/panic.go")
}
func skipDebugFrame(s []byte) int {
return skipFrameByMethod(s, "runtime/debug.Stack", "debug/stack.go")
}
func MinimizeDebugStack(stackTrace []byte, boundPackage string, includeBoundary bool) []byte {
s := alignStart(stackTrace)
if len(s) == 0 {
return stackTrace
}
pos := skipDebugFrame(s)
return minimizeDebugStack(s[pos:], boundPackage, includeBoundary)
}
func minimizeDebugStack(s []byte, boundPackage string, includeBoundary bool) []byte {
end, ok := skipFramesUntilPrefix(s, []byte(boundPackage))
if end == 0 {
return s
}
if ok && includeBoundary {
if n := indexOfFrame(s[end:], 1); n > 0 {
end += n
}
}
return s[:end]
}
func skipFramesUntilPrefix(s []byte, prefix []byte) (int, bool) {
end := 0
for {
if bytes.HasPrefix(s[end:], prefix) {
return end, true
}
switch n := indexOfFrame(s[end:], 1); {
case n > 0:
end += n
case n == 0:
return len(s), false
default:
return end, false
}
}
}
func skipFramesWithPrefix(s []byte, prefix []byte) int {
end := 0
for {
if !bytes.HasPrefix(s[end:], prefix) {
return end
}
end += len(prefix)
n := indexOfFrame(s[end:], 1)
if n <= 0 {
return len(s)
}
end += n
}
}
func alignStart(s []byte) []byte {
for {
eol := bytes.IndexByte(s, '\n')
if eol <= 0 || eol == len(s)-1 {
return nil
}
if s[eol+1] == '\t' {
return s
}
s = s[eol+1:]
}
}
func skipFrameByMethod(s []byte, methodPrefix, fileSuffix string) int {
if !bytes.HasPrefix(s, []byte(methodPrefix)) {
return 0
}
eol := bytes.IndexByte(s, '\n')
if eol <= 0 || eol == len(s)-1 {
return 0
}
if s[eol+1] != '\t' {
return 0
}
frameEnd := eol
if n := bytes.IndexByte(s[eol+2:], '\n'); n >= 0 {
frameEnd += 3 + n
s = s[:frameEnd]
} else {
frameEnd = len(s)
}
slash := bytes.LastIndexByte(s, '/')
if slash < 0 {
return 0
}
if semicolon := bytes.LastIndexByte(s[slash+1:], ':'); semicolon >= 0 {
s = s[:slash+semicolon+1]
}
if !bytes.HasSuffix(s, []byte(fileSuffix)) {
return 0
}
return frameEnd
}