-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.go
75 lines (66 loc) · 1.64 KB
/
debug.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
package elio
import (
"bytes"
"fmt"
"runtime/debug"
"strings"
"github.com/maruel/panicparse/stack"
)
// PanicParse panic parse
func PanicParse() (string, error) {
stack := string(debug.Stack())
out, err := StackParse(stack)
if nil != err {
return stack, err
}
return out, err
}
// StackParse stack parse
func StackParse(in string) (out string, err error) {
i := bytes.NewBufferString(in)
o := &bytes.Buffer{}
var c *stack.Context
if c, err = stack.ParseDump(i, o, true); nil != err {
return "", err
}
// Find out similar goroutine traces and group them into buckets.
buckets := stack.Aggregate(c.Goroutines, stack.AnyValue)
// Calculate alignment.
srcLen := 0
pkgLen := 0
for _, bucket := range buckets {
for _, line := range bucket.Signature.Stack.Calls {
if l := len(line.SrcLine()); l > srcLen {
srcLen = l
}
if l := len(line.Func.PkgName()); l > pkgLen {
pkgLen = l
}
}
}
var builder strings.Builder
for _, bucket := range buckets {
// Print the goroutine header.
extra := ""
if s := bucket.SleepString(); s != "" {
extra += " [" + s + "]"
}
if bucket.Locked {
extra += " [locked]"
}
if c := bucket.CreatedByString(false); c != "" {
extra += " [Created by " + c + "]"
}
builder.WriteString(fmt.Sprintf("%d: %s%s\n", len(bucket.IDs), bucket.State, extra))
// Print the stack lines.
for _, line := range bucket.Stack.Calls {
builder.WriteString(fmt.Sprintf(" %-*s %-*s %s(%s)\n",
pkgLen, line.Func.PkgName(), srcLen, line.SrcLine(),
line.Func.Name(), &line.Args))
}
if bucket.Stack.Elided {
builder.WriteString(" (...)\n")
}
}
return builder.String(), nil
}