-
Notifications
You must be signed in to change notification settings - Fork 5
/
stack.go
200 lines (172 loc) · 4.34 KB
/
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
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
package errors
import (
"fmt"
"io"
"path"
"runtime"
"strings"
"sync/atomic"
)
// Frame represents a program counter inside a stack frame.
type Frame uintptr
// pc returns the program counter for this frame; multiple frames may have the
// same PC value.
func (f Frame) pc() uintptr { return uintptr(f) }
func (f Frame) source() (string, int, string) {
return sourceForPC(f.pc())
}
func (f Frame) file() string {
file, _, _ := f.source()
return file
}
func (f Frame) line() int {
_, line, _ := f.source()
return line
}
func (f Frame) name() string {
_, _, name := f.source()
return name
}
// Format formats the frame according to the fmt.Formatter interface.
//
// %s source file
// %d source line
// %n function name
// %v equivalent to %s:%d
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+s path of source file relative to the compile time GOPATH
// %#s function name and path of source file
// %+n function name prefixed by its package name
// %#n function name prefixed by its full package path
// %+v equivalent to %+s:%d
// %#v equivalent to %#s:%d
func (f Frame) Format(s fmt.State, verb rune) {
pc := f.pc()
switch verb {
case 's':
switch {
case s.Flag('#'):
fn := runtime.FuncForPC(pc)
if fn == nil {
fmt.Fprintf(s, "(unknown)\n\t%#x", pc)
} else {
file, _ := fn.FileLine(pc)
fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file)
}
case s.Flag('+'):
io.WriteString(s, f.file())
default:
io.WriteString(s, path.Base(f.file()))
}
case 'd':
fmt.Fprintf(s, "%d", f.line())
case 'n':
funcName := f.name()
switch {
case s.Flag('#'):
case s.Flag('+'):
funcName = longFuncName(funcName)
default:
funcName = shortFuncName(funcName)
}
io.WriteString(s, funcName)
case 'v':
f.Format(s, 's')
io.WriteString(s, ":")
f.Format(s, 'd')
case 'x':
switch {
case s.Flag('#'):
fmt.Fprintf(s, "%#x", pc)
default:
fmt.Fprintf(s, "%x", pc)
}
case 'X':
switch {
case s.Flag('#'):
fmt.Fprintf(s, "%#X", pc)
default:
fmt.Fprintf(s, "%X", pc)
}
default:
fmt.Fprintf(s, "%%!%c(errors.Frame=%#x)", verb, pc)
}
}
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
type StackTrace []Frame
// Format formats the stack of Frames according to the fmt.Formatter interface.
//
// %s lists source files for each Frame in the stack
// %v lists the source file and line number for each Frame in the stack
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+v Prints filename, function, and line number for each Frame in the stack.
func (st StackTrace) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case s.Flag('#'):
for _, f := range st {
fmt.Fprintf(s, "\n%#v", f)
}
case s.Flag('+'):
fmt.Fprintf(s, "%+v", []Frame(st))
default:
fmt.Fprintf(s, "%v", []Frame(st))
}
case 's':
fmt.Fprintf(s, "%s", []Frame(st))
default:
fmt.Fprintf(s, "%%!%c(errors.StackFrame=%#x)", verb, []Frame(st))
}
}
func shortFuncName(name string) string {
name = longFuncName(name)
if i := strings.Index(name, "."); i >= 0 {
name = name[i+1:]
}
return name
}
func longFuncName(name string) string {
if i := strings.LastIndex(name, "/"); i >= 0 {
name = name[i+1:]
}
return name
}
// CaptureStackTrace walks the call stack that led to this function and records
// it as a StackTrace value. The skip argument is the number of stacks frames to
// skip, the frame for captureStackTrace is never included in the returned trace.
func CaptureStackTrace(skip int) StackTrace {
frames := make([]uintptr, 100)
length := runtime.Callers(skip+2, frames[:])
if init := initializing(); init {
for _, f := range frames {
if init = strings.HasPrefix(shortFuncName(Frame(f).name()), "init."); init {
break
}
}
if init {
return nil
}
completeInitialization()
}
return makeStackTrace(frames[:length])
}
func makeStackTrace(frames []uintptr) StackTrace {
stackTrace := make(StackTrace, len(frames))
for i, pc := range frames {
stackTrace[i] = Frame(pc)
}
return stackTrace
}
// Atomic variable used to check if the initialization phase is complete, so
var initialized uint32
func initializing() bool {
return atomic.LoadUint32(&initialized) == 0
}
func completeInitialization() {
atomic.StoreUint32(&initialized, 1)
}