-
Notifications
You must be signed in to change notification settings - Fork 28
/
stack.go
34 lines (27 loc) · 861 Bytes
/
stack.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
package routine
import (
"runtime"
"strings"
)
const (
runtimePkgPrefix = "runtime."
runtimePanic = "panic"
)
func captureStackTrace(skip int, depth int) []uintptr {
pcs := make([]uintptr, depth)
return pcs[:runtime.Callers(skip+2, pcs)]
}
func showFrame(name string) bool {
return strings.IndexByte(name, '.') >= 0 && (!strings.HasPrefix(name, runtimePkgPrefix) || isExportedRuntime(name))
}
func skipFrame(name string, skipped bool) bool {
return !skipped && isPanicRuntime(name)
}
func isExportedRuntime(name string) bool {
const n = len(runtimePkgPrefix)
return len(name) > n && name[:n] == runtimePkgPrefix && 'A' <= name[n] && name[n] <= 'Z'
}
func isPanicRuntime(name string) bool {
const n = len(runtimePkgPrefix)
return len(name) > n && name[:n] == runtimePkgPrefix && strings.Contains(strings.ToLower(name[n:]), runtimePanic)
}