-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherror.go
101 lines (84 loc) · 1.9 KB
/
error.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
package stack
import (
"errors"
"runtime"
)
func (e *withStack) Error() string {
return e.origin.Error()
}
func (e *withStack) Unwrap() error {
return errors.Unwrap(e.origin)
}
func (e *withStack) StackTrace() []runtime.Frame {
return e.stack.StackTrace()
}
type withStack struct {
origin error
*stack
}
// 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()
}
func (f frame) runtime() runtime.Frame {
rf := runtime.Frame{
PC: f.pc(),
File: f.file(),
Line: f.line(),
}
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return rf
}
rf.Func = fn
rf.Function = fn.Name()
rf.Entry = fn.Entry()
return rf
}
// stack represents a stack of program counters.
type stack []uintptr
func (s *stack) StackTrace() []runtime.Frame {
f := make([]runtime.Frame, len(*s))
for i := 0; i < len(f); i++ {
f[i] = frame((*s)[i]).runtime()
}
return f
}
func callers() *stack {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
var st stack = pcs[0:n]
return &st
}