-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
bpfdebug_linux.go
103 lines (91 loc) · 2.91 KB
/
bpfdebug_linux.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package ebpftest is utilities for tests against eBPF
package ebpftest
import (
"os"
"strconv"
"testing"
"github.com/stretchr/testify/require"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
)
// LogTracePipe logs all messages read from /sys/kernel/[debug/]/tracing/trace_pipe during the test.
// This function will set the environment variable BPF_DEBUG=true for the duration of the test.
func LogTracePipe(t *testing.T) {
logTracePipe(t, nil)
}
// LogTracePipeSelf logs only messages from the current process read from /sys/kernel/[debug/]/tracing/trace_pipe during the test.
// This function will set the environment variable BPF_DEBUG=true for the duration of the test.
func LogTracePipeSelf(t *testing.T) {
subtask := make(map[uint32]struct{})
mypid := getpid()
pidstr := strconv.Itoa(int(mypid))
t.Logf("filtering to %d and child tasks", mypid)
logTracePipe(t, func(ev *TraceEvent) bool {
if ev.PID == mypid {
return true
}
// check if a thread group of current process
if _, ok := subtask[ev.PID]; ok {
return true
}
_, err := os.Stat(kernel.HostProc(pidstr, "task", strconv.Itoa(int(ev.PID))))
if err == nil {
// cache result for faster lookup
subtask[ev.PID] = struct{}{}
return true
}
return false
})
}
// LogTracePipeProcess logs only messages from the provided process read from /sys/kernel/[debug/]/tracing/trace_pipe during the test.
// This function will set the environment variable BPF_DEBUG=true for the duration of the test.
func LogTracePipeProcess(t *testing.T, pid uint32) {
logTracePipe(t, func(ev *TraceEvent) bool {
return ev.PID == pid
})
}
// LogTracePipeFilter logs only messages that pass `filterFn` read from /sys/kernel/[debug/]/tracing/trace_pipe during the test.
// This function will set the environment variable BPF_DEBUG=true for the duration of the test.
func LogTracePipeFilter(t *testing.T, filterFn func(ev *TraceEvent) bool) {
logTracePipe(t, filterFn)
}
func getpid() uint32 {
p, err := os.Readlink(kernel.HostProc("/self"))
if err == nil {
if pid, err := strconv.ParseInt(p, 10, 32); err == nil {
return uint32(pid)
}
}
return uint32(os.Getpid())
}
func logTracePipe(t *testing.T, filterFn func(ev *TraceEvent) bool) {
t.Setenv("BPF_DEBUG", "true")
tp, err := NewTracePipe()
require.NoError(t, err)
t.Cleanup(func() { _ = tp.Close() })
ready := make(chan struct{})
go func() {
close(ready)
logs, errs := tp.Channel()
for {
select {
case log, ok := <-logs:
if !ok {
return
}
if filterFn == nil || filterFn(log) {
t.Logf("trace_pipe: %s", log)
}
case err, ok := <-errs:
if !ok {
return
}
t.Logf("trace_pipe: error: %s\n", err)
}
}
}()
<-ready
}