-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstack.go
More file actions
37 lines (34 loc) · 728 Bytes
/
stack.go
File metadata and controls
37 lines (34 loc) · 728 Bytes
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
package que
import (
"runtime"
"strconv"
"strings"
)
// Stack reads and formats caller stack frames.
// The argument skip is the number of stack frames to skip.
func Stack(skip int) string {
pcs := callers(skip)
var b strings.Builder
for _, pc := range pcs {
pc = pc - 1
fn := runtime.FuncForPC(pc)
if fn == nil {
b.WriteString("unknown")
} else {
file, line := fn.FileLine(pc)
b.WriteString(fn.Name())
b.WriteString("\n\t")
b.WriteString(file)
b.WriteByte(':')
b.WriteString(strconv.FormatInt(int64(line), 10))
}
b.WriteByte('\n')
}
return b.String()
}
func callers(skip int) []uintptr {
const deep = 32
var pcs [deep]uintptr
n := runtime.Callers(skip, pcs[:])
return pcs[0:n]
}