-
Notifications
You must be signed in to change notification settings - Fork 191
/
stack.go
105 lines (91 loc) · 2.32 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
package goinfo
import (
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/gookit/goutil/basefn"
)
// some commonly consts
var (
DefStackLen = 10000
MaxStackLen = 100000
)
// GetCallStacks stacks is a wrapper for runtime.
// If all is true, Stack that attempts to recover the data for all goroutines.
//
// from glog package
func GetCallStacks(all bool) []byte {
// We don't know how big the traces are, so grow a few times if they don't fit.
// Start large, though.
n := DefStackLen
if all {
n = MaxStackLen
}
// 4<<10 // 4 KB should be enough
var trace []byte
for i := 0; i < 10; i++ {
trace = make([]byte, n)
bts := runtime.Stack(trace, all)
if bts < len(trace) {
return trace[:bts]
}
n *= 2
}
return trace
}
// GetCallerInfo get caller func name and with base filename and line.
//
// returns:
//
// github.com/gookit/goutil/goinfo_test.someFunc2(),stack_test.go:26
func GetCallerInfo(skip int) string {
skip++ // ignore current func
cs := GetCallersInfo(skip, skip+1)
return basefn.FirstOr(cs, "")
}
// SimpleCallersInfo returns an array of strings containing
// the func name, file and line number of each stack frame leading.
func SimpleCallersInfo(skip, num int) []string {
skip++ // ignore current func
return GetCallersInfo(skip, skip+num)
}
// GetCallersInfo returns an array of strings containing
// the func name, file and line number of each stack frame leading.
//
// NOTICE: max should > skip
func GetCallersInfo(skip, max int) []string {
var (
pc uintptr
ok bool
line int
file, name string
)
callers := make([]string, 0, max-skip)
for i := skip; i < max; i++ {
pc, file, line, ok = runtime.Caller(i)
if !ok {
// The breaks below failed to terminate the loop, and we ran off the
// end of the call stack.
break
}
// This is a huge edge case, but it will panic if this is the case
if file == "<autogenerated>" {
break
}
fc := runtime.FuncForPC(pc)
if fc == nil {
break
}
if strings.ContainsRune(file, '/') {
name = fc.Name()
file = filepath.Base(file)
// eg: github.com/gookit/goutil/goinfo_test.someFunc2(),stack_test.go:26
callers = append(callers, name+"(),"+file+":"+strconv.Itoa(line))
}
// Drop the package
// segments := strings.Split(name, ".")
// name = segments[len(segments)-1]
}
return callers
}