-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (100 loc) · 2.54 KB
/
main.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
package main
import (
"bytes"
_ "embed"
"fmt"
"log/slog"
manager "github.com/DataDog/ebpf-manager"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/features"
)
//go:generate clang -Wall -O2 -g -target bpf -DUSE_PERF_BUF -c bpf/probe.bpf.c -Ibpf/include/ -o bpf/bin/probe_perf.bpf.o
//go:generate clang -Wall -O2 -g -target bpf -DUSE_RING_BUF -c bpf/probe.bpf.c -Ibpf/include/ -o bpf/bin/probe_ring.bpf.o
//go:embed bpf/bin/probe_perf.bpf.o
var ProbePerf []byte
//go:embed bpf/bin/probe_ring.bpf.o
var ProbeRing []byte
func RingEventHandler(_ int, _ []byte, _ *manager.RingBuffer, _ *manager.Manager) {
fmt.Println("data received from ring")
}
func PerfEventHandler(_ int, _ []byte, _ *manager.PerfMap, _ *manager.Manager) {
fmt.Println("data received from perf")
}
func main() {
if err := run(); err != nil {
slog.Error("unable to run", "error", err)
}
}
func HaveRingBuffers() bool {
return features.HaveMapType(ebpf.RingBuf) == nil
}
func run() error {
var m *manager.Manager
if HaveRingBuffers() {
m = &manager.Manager{
Probes: []*manager.Probe{
{
ProbeIdentificationPair: manager.ProbeIdentificationPair{
EBPFFuncName: "sys_enter_sendto",
},
},
},
RingBuffers: []*manager.RingBuffer{
{
Map: manager.Map{
Name: "events",
},
RingBufferOptions: manager.RingBufferOptions{
DataHandler: RingEventHandler,
},
},
},
}
if err := m.Init(bytes.NewReader(ProbeRing)); err != nil {
slog.Error("unable to init manager", "error", err)
return err
}
defer func() {
if err := m.Stop(manager.CleanAll); err != nil {
slog.Error("unable to stop manager", "error", err)
}
}()
} else {
m = &manager.Manager{
Probes: []*manager.Probe{
{
ProbeIdentificationPair: manager.ProbeIdentificationPair{
EBPFFuncName: "sys_enter_sendto",
},
},
},
PerfMaps: []*manager.PerfMap{
{
Map: manager.Map{
Name: "perf_events",
},
PerfMapOptions: manager.PerfMapOptions{
DataHandler: PerfEventHandler,
},
},
},
}
if err := m.Init(bytes.NewReader(ProbePerf)); err != nil {
slog.Error("unable to init manager", "error", err)
return err
}
defer func() {
if err := m.Stop(manager.CleanAll); err != nil {
slog.Error("unable to stop manager", "error", err)
}
}()
}
slog.Info("manager declared", "manager", m)
if err := m.Start(); err != nil {
slog.Error("unable to start", "error", err)
return err
}
slog.Info("successfully started, run (sudo bpftool prog tracelog)")
select {}
return nil
}