-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcallers.go
More file actions
80 lines (72 loc) · 2.1 KB
/
callers.go
File metadata and controls
80 lines (72 loc) · 2.1 KB
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
package errors
import "github.com/secureworks/errors/internal/runtime"
// Caller returns a Frame that describes the proximate frame on the
// caller's stack.
func Caller() Frame {
return getFrame(3)
}
// CallerAt returns a Frame that describes a frame on the caller's
// stack. The argument skipCaller is the number of frames to skip over.
func CallerAt(skipCallers int) Frame {
return getFrame(skipCallers + 3)
}
// CallStack returns all the Frames that describe the caller's stack.
func CallStack() Frames {
st := getStack(3)
ff := make(Frames, len(st))
for i, fr := range st {
ff[i] = fr
}
return ff
}
// CallStackAt returns all the Frames that describe the caller's stack.
// The argument skipCaller is the number of frames to skip over.
func CallStackAt(skipCallers int) Frames {
st := getStack(skipCallers + 3)
ff := make(Frames, len(st))
for i, fr := range st {
ff[i] = fr
}
return ff
}
// CallStackAtMost returns a subset of Frames that describe the caller's
// stack. The argument skipCaller is the number of frames to skip over,
// and the argument maxFrames is the maximum number of frames to return
// (if the entire stack is less than maxFrames, the entireStack is
// returned). maxFrames of zero or fewer is ignored:
//
// CallStackAtMost(0, 0) // ... returns the entire stack for the caller
func CallStackAtMost(skipCallers int, maxFrames int) Frames {
st := getStack(skipCallers + 3)
stackLen := len(st)
if maxFrames > 0 && stackLen > maxFrames {
stackLen = maxFrames
}
ff := make(Frames, stackLen)
for i, fr := range st {
if i == stackLen {
break
}
ff[i] = fr
}
return ff
}
// getFrame translates a runtime.Frame item returned from the internal
// runtime utilities into a frame.
//
//go:noinline
func getFrame(skipCallers int) *frame {
return &frame{pc: runtime.GetFrame(skipCallers).PC}
}
// getStack translates runtime.Frame items returned from the internal
// runtime utilities into frames.
//
//go:noinline
func getStack(skipCallers int) frames {
st := runtime.GetStack(skipCallers)
ff := make([]*frame, len(st))
for i, fr := range st {
ff[i] = &frame{pc: fr.PC}
}
return ff
}