-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacktrace.go
67 lines (59 loc) · 1016 Bytes
/
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
package runtime
import (
"runtime"
)
type StackFrame struct {
Pc uintptr
Name string
File string
Line int
}
func (sf StackFrame) ParsedName() ParsedName {
return ParseName(sf.Name)
}
func GetStackFrame(ofs int) StackFrame {
var pcs [1]uintptr
_ = runtime.Callers(
ofs+2,
pcs[:],
)
frs := runtime.CallersFrames(pcs[:])
fr, _ := frs.Next()
return StackFrame{
Pc: fr.PC,
File: fr.File,
Line: fr.Line,
Name: fr.Function,
}
}
func GetStackTrace(n, ofs int) []StackFrame {
pcs := make([]uintptr, n)
nf := runtime.Callers(
ofs+2,
pcs,
)
pcs = pcs[:nf]
frs := runtime.CallersFrames(pcs)
sfs := make([]StackFrame, 0, nf)
for {
fr, more := frs.Next()
sfs = append(sfs, StackFrame{
Pc: fr.PC,
File: fr.File,
Line: fr.Line,
Name: fr.Function,
})
if !more {
break
}
}
return sfs
}
func GetCaller(ofs int) ParsedName {
return GetStackFrame(ofs + 1).ParsedName()
}
func GetPc() uintptr {
var pcs [1]uintptr
runtime.Callers(1, pcs[:])
return pcs[0]
}