-
Notifications
You must be signed in to change notification settings - Fork 9
/
stacktrace.go
180 lines (162 loc) · 4.45 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
// Copyright (c) 2015, Dave Cheney <dave@cheney.net>
// All rights reserved.
// Adapted from https://github.com/pkg/errors.
package stacktrace
import (
"fmt"
"io"
"path"
"runtime"
"strconv"
"strings"
)
// Frame represents a program counter inside a stack frame.
// For historical reasons if Frame is interpreted as a uintptr
// its value represents the program counter + 1.
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) - 1 }
// File returns the full path to the file that contains the
// function for this Frame's pc.
func (f Frame) File() string {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return "unknown"
}
file, _ := fn.FileLine(f.pc())
return file
}
// Line returns the line number of source code of the
// function for this Frame's pc.
func (f Frame) Line() int {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return 0
}
_, line := fn.FileLine(f.pc())
return line
}
// Name returns the name of this function, if known.
func (f Frame) Name() string {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return "unknown"
}
return fn.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 function name and path of source file relative to the compile time
// GOPATH separated by \n\t (<funcname>\n\t<path>)
// %+v equivalent to %+s:%d
func (f Frame) Format(s fmt.State, verb rune) {
switch verb {
case 's':
switch {
case s.Flag('+'):
writeString(s, f.Name())
writeString(s, "\n\t")
writeString(s, f.File())
default:
writeString(s, path.Base(f.File()))
}
case 'd':
writeString(s, strconv.Itoa(f.Line()))
case 'n':
writeString(s, funcname(f.Name()))
case 'v':
f.Format(s, 's')
writeString(s, ":")
f.Format(s, 'd')
}
}
// errcheck safe wrapper for io.WriteString.
func writeString(w io.Writer, s string) {
_, _ = io.WriteString(w, s)
}
// MarshalText formats a stacktrace Frame as a text string. The output is the
// same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.
func (f Frame) MarshalText() ([]byte, error) {
name := f.Name()
if name == "unknown" {
return []byte(name), nil
}
return []byte(fmt.Sprintf("%s %s:%d", name, f.File(), f.Line())), nil
}
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
type StackTrace []Frame
// New returns a new StackTrace after filtering out uninteresting frames
// from runtime.Callers.
func New() StackTrace {
return NewWithSkip(1)
}
// New returns a new StackTrace after filtering out uninteresting frames
// from runtime.Callers.
func NewWithSkip(k int) StackTrace {
const depth = 32
var pcs [depth]uintptr
// We skip 3 frames: 0 identifies the frame for Callers,
// 1 identifies the call below, and 2 identifies the `New`
// invocation above all of which are uninteresting for the
// user.
n := runtime.Callers(k+3, pcs[:])
pcslice := pcs[0:n]
frames := make([]Frame, len(pcslice))
for i := 0; i < len(frames); i++ {
frames[i] = Frame((pcslice)[i])
}
return frames
}
// 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 {
writeString(s, "\n")
f.Format(s, verb)
}
case s.Flag('#'):
fmt.Fprintf(s, "%#v", []Frame(st))
default:
st.formatSlice(s, verb)
}
case 's':
st.formatSlice(s, verb)
}
}
// formatSlice will format this StackTrace into the given buffer as a slice of
// Frame, only valid when called with '%s' or '%v'.
func (st StackTrace) formatSlice(s fmt.State, verb rune) {
writeString(s, "[")
for i, f := range st {
if i > 0 {
writeString(s, " ")
}
f.Format(s, verb)
}
writeString(s, "]")
}
// funcname removes the path prefix component of a function's name reported by func.Name().
func funcname(name string) string {
i := strings.LastIndex(name, "/")
name = name[i+1:]
i = strings.Index(name, ".")
return name[i+1:]
}