-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.go
More file actions
54 lines (44 loc) · 1.23 KB
/
setup.go
File metadata and controls
54 lines (44 loc) · 1.23 KB
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
package bpf
import (
"fmt"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"go-ebp-logger/constants"
)
func SetupBPF(bpfObj string) (*ebpf.Map, *ebpf.Map, func()) {
spec, err := ebpf.LoadCollectionSpec(bpfObj)
if err != nil {
fmt.Printf("Failed to load BPF collection spec: %v\n", err)
}
coll, err := ebpf.NewCollection(spec)
if err != nil {
fmt.Printf("Failed to load BPF collection: %v\n", err)
}
var links []*link.Link
for progName, fn := range constants.ProgsToFuncs {
prog := coll.Programs[progName]
if prog == nil {
fmt.Printf("Program '%s' not found\n", progName)
}
kp, err := link.Kprobe(fn, prog, nil)
if err != nil {
fmt.Printf("Failed to attach kprobe to %s: %v\n", fn, err)
}
links = append(links, &kp)
}
events := coll.Maps["events"]
if events == nil {
fmt.Printf("Map 'events' not found\n")
}
renameEvents := coll.Maps["rename_events"]
if renameEvents == nil {
fmt.Printf("Map 'rename_events' not found\n")
}
cleanup := func() {
for _, l := range links {
(*l).Close()
}
coll.Close()
}
return events, renameEvents, cleanup
}