-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacktrace.go
More file actions
64 lines (59 loc) · 1.66 KB
/
Copy pathstacktrace.go
File metadata and controls
64 lines (59 loc) · 1.66 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
package groundcover
import (
"runtime"
"strings"
)
// captureStack walks the current goroutine's stack, skipping the first skip
// frames (relative to captureStack's caller) and capturing at most maxDepth
// frames. inAppPrefix is the main module path used to classify in-app frames.
func captureStack(skip, maxDepth int, inAppPrefix string) []Frame {
if maxDepth <= 0 {
maxDepth = 128
}
// +2 to skip runtime.Callers and captureStack itself.
pcs := make([]uintptr, maxDepth)
n := runtime.Callers(skip+2, pcs)
if n == 0 {
return nil
}
frames := runtime.CallersFrames(pcs[:n])
out := make([]Frame, 0, n)
for {
fr, more := frames.Next()
if fr.Function != "" || fr.File != "" {
out = append(out, Frame{
Function: fr.Function,
File: fr.File,
Line: fr.Line,
InApp: isInApp(fr.Function, fr.File, inAppPrefix),
})
}
if !more {
break
}
}
return out
}
// isInApp reports whether a frame belongs to the application: its function lives
// under the main module path and it is not vendored or part of the runtime.
func isInApp(function, file, inAppPrefix string) bool {
if strings.Contains(file, "/vendor/") {
return false
}
if inAppPrefix == "" {
// Without a known module path, treat non-stdlib, non-runtime frames as
// in-app on a best-effort basis.
return !strings.HasPrefix(function, "runtime.") && strings.Contains(function, ".")
}
return strings.HasPrefix(function, inAppPrefix)
}
// inAppFrames returns only the in-app frames of e, preserving order.
func inAppFrames(frames []Frame) []Frame {
out := make([]Frame, 0, len(frames))
for _, f := range frames {
if f.InApp {
out = append(out, f)
}
}
return out
}