-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
167 lines (147 loc) · 3.15 KB
/
example_test.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package trace_test
import (
"fmt"
"io"
"os"
"reflect"
"runtime"
"sync"
"time"
"github.com/cstockton/go-trace"
"github.com/cstockton/go-trace/encoding"
"github.com/cstockton/go-trace/event"
)
func Example() {
f, err := os.Open(`internal/tracefile/testdata/go1.8/log.trace`)
if err != nil {
fmt.Println(`Err:`, err)
return
}
defer f.Close()
var (
evt event.Event
d = encoding.NewDecoder(f)
)
for d.More() {
evt.Reset()
if err := d.Decode(&evt); err != nil {
break
}
if evt.Type == event.EvGoSysCall {
fmt.Println(evt.Type) // print syscall events
}
}
if err := d.Err(); err != nil {
fmt.Println(`Err: `, err)
return
}
// Output:
// event.GoSysCall
// event.GoSysCall
// event.GoSysCall
// event.GoSysCall
// event.GoSysCall
// event.GoSysCall
// event.GoSysCall
// event.GoSysCall
// event.GoSysCall
// event.GoSysCall
// event.GoSysCall
}
func Example_runtimeDecoding() {
sleepFn := func(wg *sync.WaitGroup) {
defer wg.Done()
<-time.After(time.Millisecond * 100)
}
r, w := io.Pipe()
trace.Start(w)
var wg sync.WaitGroup
wg.Add(3)
go sleepFn(&wg)
go sleepFn(&wg)
go sleepFn(&wg)
wg.Wait()
go func() {
defer w.Close()
trace.Stop()
}()
var (
dec = encoding.NewDecoder(r)
evt event.Event
evts []*event.Event
)
v, err := dec.Version()
if err != nil {
fmt.Println(`Err:`, err)
return
}
tr, err := event.NewTrace(v)
if err != nil {
fmt.Println(`Err:`, err)
return
}
for dec.More() {
evt.Reset()
if err := dec.Decode(&evt); err != nil {
break
}
if err := tr.Visit(&evt); err != nil {
fmt.Println(`Err:`, err)
}
evts = append(evts, evt.Copy())
}
if err := dec.Err(); err != nil {
fmt.Println(`Err: `, err)
return
}
// Lets make sure a goroutine is started for this func
findPC := uint64(reflect.ValueOf(sleepFn).Pointer())
findName := runtime.FuncForPC(uintptr(findPC)).Name()
for _, e := range evts {
if e.Type != event.EvGoCreate {
continue
}
// We want a stack for the new StackID
stack := tr.Stacks[e.Get(`NewStackID`)]
if len(stack) < 1 {
continue
}
name := runtime.FuncForPC(uintptr(stack[0].PC())).Name()
if findName == name {
stack, ok := tr.Stacks[e.Get(`StackID`)]
if !ok {
fmt.Println(`No stack exists for event:`, e)
}
// can't print stack in a unit test due to lack of determnism
// fmt.Println(stack)
fmt.Printf("\nFound EvGoCreate for `sleepFn` with new stack:\n====\n")
for _, frame := range stack {
fmt.Printf(" %v\n", frame.Func())
}
}
}
// Output:
// Found EvGoCreate for `sleepFn` with new stack:
// ====
// github.com/cstockton/go-trace_test.Example_runtimeDecoding
// testing.runExample
// testing.runExamples
// testing.(*M).Run
// main.main
//
// Found EvGoCreate for `sleepFn` with new stack:
// ====
// github.com/cstockton/go-trace_test.Example_runtimeDecoding
// testing.runExample
// testing.runExamples
// testing.(*M).Run
// main.main
//
// Found EvGoCreate for `sleepFn` with new stack:
// ====
// github.com/cstockton/go-trace_test.Example_runtimeDecoding
// testing.runExample
// testing.runExamples
// testing.(*M).Run
// main.main
}