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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
package internal
import "C"
import (
"context"
"fmt"
"os"
"path/filepath"
"syscall"
"time"
"ior/internal/event"
"ior/internal/file"
"ior/internal/flags"
"ior/internal/flamegraph"
"ior/internal/types"
. "ior/internal/types"
)
// TOOD: read and write syscalls: can also collect amount of bytes!
type eventLoop struct {
flags flags.Flags
filter *eventFilter
enterEvs map[uint32]*event.Pair // Temp. store of sys_enter tracepoints per Tid.
files map[int32]file.File // Track all open files by file descriptor..
comms map[uint32]string // Program or thread name of the current Tid.
prevPairs map[uint32]*event.Pair // Previous event (to calculate time differences between two events)
flamegraph flamegraph.Flamegraph // Storing all paths in a map structure for analysis
// Statistics
numTracepoints uint
numTracepointMismatches uint
numSyscalls uint
numSyscallsAfterFilter uint
startTime time.Time
done chan struct{}
}
func newEventLoop(flags flags.Flags) *eventLoop {
return &eventLoop{
flags: flags,
filter: newEventFilter(flags),
enterEvs: make(map[uint32]*event.Pair),
files: make(map[int32]file.File),
comms: make(map[uint32]string),
prevPairs: make(map[uint32]*event.Pair),
flamegraph: flamegraph.New(),
done: make(chan struct{}),
}
}
func (e *eventLoop) stats() string {
fmt.Println("Waiting for stats to be ready")
<-e.done
duration := time.Since(e.startTime)
return "Statistics:\n" +
fmt.Sprintf("\tduration:%v\n", duration) +
fmt.Sprintf("\ttracepoints:%v (%.2f/s) with %d mismatches (%.2f%%)\n", e.numTracepoints, float64(e.numTracepoints)/duration.Seconds(), e.numTracepointMismatches, (float64(e.numTracepointMismatches)/float64(e.numTracepoints))*100) +
fmt.Sprintf("\tsyscalls:%d (%.2f/s)\n",
e.numSyscalls, float64(e.numSyscalls)/duration.Seconds()) +
fmt.Sprintf("\tsyscalls after filter:%d (%.2f/s)\n",
e.numSyscallsAfterFilter, float64(e.numSyscallsAfterFilter)/duration.Seconds())
}
func (e *eventLoop) run(ctx context.Context, rawCh <-chan []byte) {
defer close(e.done)
if e.flags.FlamegraphEnable {
fmt.Println("Collecting flame graph stats, press Ctrl+C to stop")
e.flamegraph.Start(ctx)
}
if e.flags.PprofEnable {
fmt.Println("Profiling, press Ctrl+C to stop")
}
if !e.flags.FlamegraphEnable && !e.flags.PprofEnable {
fmt.Println(event.EventStreamHeader)
}
e.startTime = time.Now()
for ev := range e.events(ctx, rawCh) {
switch {
case e.flags.FlamegraphEnable:
e.flamegraph.Ch <- ev
case e.flags.PprofEnable:
ev.RecyclePrev()
default:
fmt.Println(ev.String())
ev.RecyclePrev()
}
e.numSyscallsAfterFilter++
}
if e.flags.FlamegraphEnable {
fmt.Println("Waiting for flamegraph")
<-e.flamegraph.Done
}
}
func (e *eventLoop) events(ctx context.Context, rawCh <-chan []byte) <-chan *event.Pair {
ch := make(chan *event.Pair)
go func() {
defer close(ch)
for {
select {
case raw := <-rawCh:
if len(raw) == 0 {
continue
}
e.processRawEvent(raw, ch)
case <-ctx.Done():
fmt.Println("Stopping event loop")
return
default:
time.Sleep(time.Millisecond * 10)
}
}
}()
return ch
}
func (e *eventLoop) processRawEvent(raw []byte, ch chan<- *event.Pair) {
e.numTracepoints++
// TODO: Would a map be faster than a big switch-case statement?
switch EventType(raw[0]) {
case ENTER_OPEN_EVENT:
if ev, ok := e.filter.openEvent(NewOpenEvent(raw)); ok {
e.syscallEnter(ev)
}
case EXIT_OPEN_EVENT:
e.syscallExit(NewFdEvent(raw), ch)
case ENTER_FD_EVENT:
e.syscallEnter(NewFdEvent(raw))
case EXIT_FD_EVENT:
e.syscallExit(NewFdEvent(raw), ch)
case ENTER_NULL_EVENT:
e.syscallEnter(NewNullEvent(raw))
case EXIT_NULL_EVENT:
e.syscallExit(NewNullEvent(raw), ch)
case EXIT_RET_EVENT:
e.syscallExit(NewRetEvent(raw), ch)
case ENTER_NAME_EVENT:
if ev, ok := e.filter.nameEvent(NewNameEvent(raw)); ok {
e.syscallEnter(ev)
}
case ENTER_PATH_EVENT:
if ev, ok := e.filter.pathEvent(NewPathEvent(raw)); ok {
e.syscallEnter(ev)
}
case ENTER_FCNTL_EVENT:
e.syscallEnter(NewFcntlEvent(raw))
default:
panic(fmt.Sprintf("unhandled event type %v: %v", EventType(raw[0]), raw))
}
}
func (e *eventLoop) syscallEnter(enterEv event.Event) {
tid := enterEv.GetTid()
if !e.filter.commFilterEnable {
e.enterEvs[tid] = event.NewPair(enterEv)
return
}
switch enterEv.(type) {
case *OpenEvent:
e.enterEvs[tid] = event.NewPair(enterEv)
default:
// Only, when we have a comm name
if _, ok := e.comms[tid]; ok {
e.enterEvs[tid] = event.NewPair(enterEv)
} else {
// Probably not an issue.
fmt.Println("WARN: No comm name for", enterEv, "process probably already vanished?")
}
}
}
func (e *eventLoop) syscallExit(exitEv event.Event, ch chan<- *event.Pair) {
ev, ok := e.enterEvs[exitEv.GetTid()]
if !ok {
exitEv.Recycle()
return
}
delete(e.enterEvs, exitEv.GetTid())
ev.ExitEv = exitEv
e.numSyscalls++
// Expect ID one lower, otherwise, enter and exit tracepoints
// don't match up. E.g.:
// enterEv:SYS_ENTER_OPEN => exitEv:SYS_EXIT_OPEN
if ev.EnterEv.GetTraceId()-1 != ev.ExitEv.GetTraceId() {
e.numTracepointMismatches++
ev.Recycle()
return
}
switch v := ev.EnterEv.(type) {
case *OpenEvent:
openEv := ev.EnterEv.(*OpenEvent)
fd := int32(ev.ExitEv.(*RetEvent).Ret)
file := file.NewFd(fd, openEv.Filename[:], v.Flags)
if file.Flags == -1 {
// Issue here is that some open_event's aren't really open_event's
// need to double check all tracepoints
fmt.Println("DEBUG with -1 flags", openEv, file)
}
if fd >= 0 {
e.files[fd] = file
}
ev.File = file
e.comms[openEv.Tid] = string(openEv.Comm[:])
case *NameEvent:
nameEvent := ev.EnterEv.(*NameEvent)
ev.File = file.NewOldnameNewname(nameEvent.Oldname[:], nameEvent.Newname[:])
ev.Comm = e.comm(ev.EnterEv.GetTid())
case *PathEvent:
nameEvent := ev.EnterEv.(*PathEvent)
ev.File = file.NewPathname(nameEvent.Pathname[:])
ev.Comm = e.comm(ev.EnterEv.GetTid())
case *FdEvent:
fd := ev.EnterEv.(*FdEvent).Fd
if file_, ok := e.files[fd]; ok {
ev.File = file_
if ev.Is(SYS_ENTER_CLOSE) {
delete(e.files, fd)
}
} else {
ev.File = file.NewFdWithPid(fd, v.Pid)
}
ev.Comm = e.comm(ev.EnterEv.GetTid())
if !e.filter.eventPair(ev) {
ev.Recycle()
return
}
case *NullEvent:
ev.Comm = e.comm(ev.EnterEv.GetTid())
if !e.filter.eventPair(ev) {
ev.Recycle()
return
}
case *FcntlEvent:
ev.Comm = e.comm(ev.EnterEv.GetTid())
fd := int32(v.Fd)
if file_, ok := e.files[fd]; ok {
ev.File = file_
} else {
ev.File = file.NewFdWithPid(fd, v.Pid)
}
if !e.filter.eventPair(ev) {
ev.Recycle()
return
}
retEvent, ok := exitEv.(*types.RetEvent)
if !ok {
panic("expected *types.RetEvent")
}
// Syscall returned -1, nothing was changed with the fd
if retEvent.Ret == -1 {
break
}
fdFile, ok := ev.File.(file.FdFile)
if !ok {
panic("expected a file.FdFile")
}
// See fcntl(2) for implementation details
switch v.Cmd {
case syscall.F_SETFL:
canChange := syscall.O_APPEND | syscall.O_ASYNC | syscall.O_DIRECT | syscall.O_NOATIME | syscall.O_NONBLOCK
fdFile.Flags |= (int32(v.Arg) & int32(canChange))
ev.File = fdFile
e.files[fd] = fdFile
case syscall.F_DUPFD:
newFd := int32(retEvent.Ret)
e.files[newFd] = fdFile.Dup(newFd)
case syscall.F_DUPFD_CLOEXEC:
newFd := int32(retEvent.Ret)
duppedFd := fdFile.Dup(newFd)
duppedFd.Flags |= syscall.O_CLOEXEC
e.files[newFd] = duppedFd
}
default:
panic(fmt.Sprintf("unknown type: %v", v))
}
// TODO: implement sync(2)
// TODO: implement dup syscall
// TODO: implement dup2 syscall
// TODO: implement dup3 syscall
// TODO: implement readlink syscall
// TODO: implement readv(2)
// TODO: implement copy_file_range
// TODO: open_by_handle_at
// TODO: name_to_handle_at
// TODO: mmap, msync...
// TODO: getcwd?
// TODO: syslog(2) for auditd debugging
// TODO: truncate
// TODO: sync_file_range
// TODO: readahead
// TODO: fallocate
ev.PrevPair, _ = e.prevPairs[ev.EnterEv.GetTid()]
ev.CalculateDurations()
e.prevPairs[ev.EnterEv.GetTid()] = ev
ch <- ev
}
func (e *eventLoop) comm(tid uint32) string {
if comm, ok := e.comms[tid]; ok {
return comm
}
if linkName, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", tid)); err == nil {
linkName = filepath.Base(linkName)
e.comms[tid] = linkName
return linkName
}
return ""
}
|