-
Notifications
You must be signed in to change notification settings - Fork 2
/
stacktrace.go
214 lines (183 loc) · 4.96 KB
/
stacktrace.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
204
205
206
207
208
209
210
211
212
213
214
package throw
import (
"bytes"
"io"
"runtime"
"runtime/debug"
"strconv"
"strings"
)
const StackTracePrefix = "Stack trace:"
const stackTracePrintPrefix = StackTracePrefix + "\n"
type StackTrace interface {
StackTraceAsText() string
WriteStackTraceTo(writer io.Writer) error
IsFullStack() bool
}
// CaptureStack captures whole stack
// When (skipFrames) are more than stack depth then only "created by" entry will be returned
func CaptureStack(skipFrames int) StackTrace {
return stackTrace{captureStack(skipFrames+1, false), false}
}
// CaptureStackTop is an optimized version to capture a limited info about top-level stack entry only
// When (skipFrames) are more than stack depth then only "created by" entry will be returned
func CaptureStackTop(skipFrames int) StackTrace {
return stackTrace{captureStack(skipFrames+1, true), true}
}
func ExtractStackTop(st StackTrace, skipFrames int) StackTrace {
var data []byte
switch vv := st.(type) {
case nil:
return nil
case stackTrace:
if vv.limit {
return st
}
data = vv.data
default:
if !vv.IsFullStack() {
return st
}
data = []byte(vv.StackTraceAsText())
}
start := indexOfFrame(data, skipFrames)
if start < 0 || start == 0 && skipFrames > 0 {
return stackTrace{data, true}
}
start += skipPanicFrame(data)
end := indexOfFrame(data[start:], 1)
if end > 0 {
data = data[start : start+end]
} else {
data = data[start:]
}
return stackTrace{append([]byte(nil), data...), true}
}
func IsInSystemPanic(skipFrames int) bool {
pc := make([]uintptr, 1)
if runtime.Callers(skipFrames+2, pc) != 1 {
return false
}
n := runtime.FuncForPC(pc[0]).Name()
return n == "runtime.preprintpanics"
}
type stackTrace struct {
data []byte
limit bool
}
func (v stackTrace) IsFullStack() bool {
return !v.limit
}
func (v stackTrace) WriteStackTraceTo(w io.Writer) error {
_, err := w.Write(v.data)
return err
}
func (v stackTrace) StackTraceAsText() string {
return string(v.data)
}
func (v stackTrace) LogString() string {
return string(v.data)
}
func (v stackTrace) String() string {
return stackTracePrintPrefix + string(v.data)
}
func captureStack(skipFrames int, limitFrames bool) []byte {
skipFrames++
if limitFrames {
// provides a bit less info, but is 10x times faster
result := captureStackByCallers(skipFrames, true)
if len(result) > 0 {
return result
}
// result will be empty at stack top that we can't capture by runtime.Callers() so we will fallback
}
return captureStackByDebug(skipFrames, limitFrames)
}
const topFrameLimit = 1 // MUST be 1, otherwise comparison of stack vs stack top may not work properly
func captureStackByDebug(skipFrames int, limitFrames bool) []byte {
stackBytes := debug.Stack()
capacity := cap(stackBytes)
if i := bytes.IndexByte(stackBytes, '\n'); i > 0 {
stackBytes = stackBytes[i+1:]
} else {
// strange result, let be safe
return stackBytes
}
const serviceFrames = 2 // debug.Stack() + captureStackByDebug()
if i := indexOfFrame(stackBytes, skipFrames+serviceFrames); i > 0 {
stackBytes = stackBytes[i:]
if limitFrames {
if i := indexOfFrame(stackBytes, topFrameLimit); i > 0 {
stackBytes = stackBytes[:i]
}
}
}
stackBytes = bytes.TrimSpace(stackBytes)
return trimCapacity(capacity, stackBytes)
}
func trimCapacity(actualCapacity int, b []byte) []byte {
n := len(b) << 1
if actualCapacity > n || cap(b) > n {
return append(make([]byte, 0, n), b...)
}
return b
}
func indexOfFrame(stackBytes []byte, skipFrames int) int {
offset := 0
for ; skipFrames > 0; skipFrames-- {
prevOffset := offset
if i := bytes.Index(stackBytes[offset:], frameFileSep); i > 0 {
offset += i + len(frameFileSep)
if j := bytes.IndexByte(stackBytes[offset:], '\n'); j > 0 {
offset += j + 1
if offset == len(stackBytes) {
return prevOffset
}
continue
}
return prevOffset
}
return -1
}
return offset
}
const frameFileSeparator = "\n\t"
var frameFileSep = []byte(frameFileSeparator)
func captureStackByCallers(skipFrames int, limitFrames bool) []byte {
var pcs []uintptr
if limitFrames {
pcs = make([]uintptr, topFrameLimit)
} else {
pcs = make([]uintptr, 50) // maxStackDepth
}
const serviceFrames = 2 // runtime.Callers() + captureStackByCallers()
pcs = pcs[:runtime.Callers(skipFrames+serviceFrames, pcs)]
result := make([]byte, 0, len(pcs)<<7)
for i, pc := range pcs {
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
fName := fn.Name()
if i == len(pcs)-1 && fName == "runtime.goexit" {
break
}
// imitation of debug.Stack() format
if len(result) > 0 {
result = append(result, '\n')
}
result = append(result, fName...)
result = append(result, frameFileSeparator...)
fName, line := fn.FileLine(pc)
result = append(result, fName...)
result = append(result, ':')
result = strconv.AppendInt(result, int64(line), 10)
}
return trimCapacity(0, result)
}
func TrimStackTrace(s string) string {
if i := strings.Index(s, "\n"+stackTracePrintPrefix); i >= 0 {
return s[:i]
}
return s
}