Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathstackframe.go
More file actions
Latest commit
122 lines (104 loc) · 2.97 KB
/
Copy pathstackframe.go
File metadata and controls
122 lines (104 loc) · 2.97 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
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
package errors
import (
"bufio"
"bytes"
"fmt"
"os"
"runtime"
"strings"
)
// A StackFrame contains all necessary information about to generate a line
// in a callstack.
typeStackFramestruct {
// The path to the file containing this ProgramCounter
Filestring
// The LineNumber in that file
LineNumberint
// The Name of the function that contains this ProgramCounter
Namestring
// The Package that contains this function
Packagestring
// The underlying ProgramCounter
ProgramCounteruintptr
}
// NewStackFrame popoulates a stack frame object from the program counter.
funcNewStackFrame(pcuintptr) (frameStackFrame) {
frame=StackFrame{ProgramCounter: pc}
ifframe.Func() ==nil {
return
}
frame.Package, frame.Name=packageAndName(frame.Func())
// pc -1 because the program counters we use are usually return addresses,
// and we want to show the line that corresponds to the function call
frame.File, frame.LineNumber=frame.Func().FileLine(pc-1)
return
}
// Func returns the function that contained this frame.
func (frame*StackFrame) Func() *runtime.Func {
ifframe.ProgramCounter==0 {
returnnil
}
returnruntime.FuncForPC(frame.ProgramCounter)
}
// String returns the stackframe formatted in the same way as go does
// in runtime/debug.Stack()
func (frame*StackFrame) String() string {
str:=fmt.Sprintf("%s:%d (0x%x)\n", frame.File, frame.LineNumber, frame.ProgramCounter)
source, err:=frame.sourceLine()
iferr!=nil {
returnstr
}
returnstr+fmt.Sprintf("\t%s: %s\n", frame.Name, source)
}
// SourceLine gets the line of code (from File and Line) of the original source if possible.
func (frame*StackFrame) SourceLine() (string, error) {
source, err:=frame.sourceLine()
iferr!=nil {
returnsource, New(err)
}
returnsource, err
}
func (frame*StackFrame) sourceLine() (string, error) {
ifframe.LineNumber<=0 {
return"???", nil
}
file, err:=os.Open(frame.File)
iferr!=nil {
return"", err
}
deferfile.Close()
scanner:=bufio.NewScanner(file)
currentLine:=1
forscanner.Scan() {
ifcurrentLine==frame.LineNumber {
returnstring(bytes.Trim(scanner.Bytes(), " \t")), nil
}
currentLine++
}
iferr:=scanner.Err(); err!=nil {
return"", err
}
return"???", nil
}
funcpackageAndName(fn*runtime.Func) (string, string) {
name:=fn.Name()
pkg:=""
// The name includes the path name to the package, which is unnecessary
// since the file name is already included. Plus, it has center dots.
// That is, we see
// runtime/debug.*T·ptrmethod
// and want
// *T.ptrmethod
// Since the package path might contains dots (e.g. code.google.com/...),
// we first remove the path prefix if there is one.
iflastslash:=strings.LastIndex(name, "/"); lastslash>=0 {
pkg+=name[:lastslash] +"/"
name=name[lastslash+1:]
}
ifperiod:=strings.Index(name, "."); period>=0 {
pkg+=name[:period]
name=name[period+1:]
}
name=strings.Replace(name, "·", ".", -1)
returnpkg, name
}