-
Notifications
You must be signed in to change notification settings - Fork 0
/
etwstacks.go
84 lines (77 loc) · 1.59 KB
/
etwstacks.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
package etwstacks
import (
"runtime"
"strings"
"github.com/Microsoft/go-winio/pkg/etw"
"github.com/Microsoft/go-winio/pkg/guid"
)
var p *etw.Provider
func init() {
// Provider ID: fc009c55-f069-5ee5-b70f-fa2fa7b2dd20
p, _ = etw.NewProvider("etwstacks", etwCallback)
}
func writeError(err error) {
p.WriteEvent(
"StackDumpError",
etw.WithEventOpts(
etw.WithLevel(etw.LevelError),
),
etw.WithFields(
etw.StringField("err", err.Error()),
),
)
}
func etwCallback(sourceID guid.GUID, state etw.ProviderState, level etw.Level, matchAnyKeyword uint64, matchAllKeyword uint64, filterData uintptr) {
if state == etw.ProviderStateCaptureState {
stacks := getStacks()
g, err := guid.NewV4()
if err != nil {
writeError(err)
return
}
p.WriteEvent(
"BeginStackDump",
etw.WithEventOpts(
etw.WithOpcode(etw.OpcodeStart),
etw.WithActivityID(g),
),
etw.WithFields(
etw.IntField("stackCount", len(stacks)),
),
)
for i, stack := range stacks {
p.WriteEvent(
"StackDump",
etw.WithEventOpts(
etw.WithActivityID(g),
),
etw.WithFields(
etw.IntField("stackIndex", i),
etw.StringField("stack", stack),
),
)
}
p.WriteEvent(
"EndStackDump",
etw.WithEventOpts(
etw.WithOpcode(etw.OpcodeStop),
etw.WithActivityID(g),
),
nil,
)
}
}
func getStacks() []string {
var (
buf []byte
stackSize int
)
bufferLen := 16384
for stackSize == len(buf) {
buf = make([]byte, bufferLen)
stackSize = runtime.Stack(buf, true)
bufferLen *= 2
}
buf = buf[:stackSize]
return strings.Split(string(buf), "\n\n")
}