forked from aquasecurity/libbpfgo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-iterator.go
81 lines (65 loc) · 1.36 KB
/
module-iterator.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
package libbpfgo
/*
#cgo LDFLAGS: -lelf -lz
#include "libbpfgo.h"
*/
import "C"
//
// BPFObjectIterator (Module Iterator)
//
// BPFObjectProgramIterator iterates over programs and maps in a BPF object
type BPFObjectIterator struct {
m *Module
prevProg *BPFProg
prevMap *BPFMap
}
func (it *BPFObjectIterator) NextMap() *BPFMap {
var startMapC *C.struct_bpf_map
if it.prevMap != nil && it.prevMap.bpfMap != nil {
startMapC = it.prevMap.bpfMap
}
bpfMapC, errno := C.bpf_object__next_map(it.m.obj, startMapC)
if bpfMapC == nil {
_ = errno // intentionally ignored
return nil
}
bpfMap := &BPFMap{
bpfMap: bpfMapC,
module: it.m,
}
it.prevMap = bpfMap
if !bpfMap.module.loaded {
bpfMap.bpfMapLow = &BPFMapLow{
fd: -1,
info: &BPFMapInfo{},
}
return bpfMap
}
fd := bpfMap.FileDescriptor()
info, err := GetMapInfoByFD(fd)
if err != nil {
return nil
}
bpfMap.bpfMapLow = &BPFMapLow{
fd: fd,
info: info,
}
return bpfMap
}
func (it *BPFObjectIterator) NextProgram() *BPFProg {
var startProg *C.struct_bpf_program
if it.prevProg != nil && it.prevProg.prog != nil {
startProg = it.prevProg.prog
}
progC, errno := C.bpf_object__next_program(it.m.obj, startProg)
if progC == nil {
_ = errno // intentionally ignored
return nil
}
prog := &BPFProg{
prog: progC,
module: it.m,
}
it.prevProg = prog
return prog
}