forked from aquasecurity/libbpfgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libbpf_cb.go
30 lines (25 loc) · 845 Bytes
/
libbpf_cb.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
package libbpfgo
import (
"C"
"unsafe"
)
// This callback definition needs to be in a different file from where it is declared in C
// Otherwise, multiple definition compilation error will occur
//export perfCallback
func perfCallback(ctx unsafe.Pointer, cpu C.int, data unsafe.Pointer, size C.int) {
pb := eventChannels.get(uint(uintptr(ctx))).(*PerfBuffer)
pb.eventsChan <- C.GoBytes(data, size)
}
//export perfLostCallback
func perfLostCallback(ctx unsafe.Pointer, cpu C.int, cnt C.ulonglong) {
pb := eventChannels.get(uint(uintptr(ctx))).(*PerfBuffer)
if pb.lostChan != nil {
pb.lostChan <- uint64(cnt)
}
}
//export ringbufferCallback
func ringbufferCallback(ctx unsafe.Pointer, data unsafe.Pointer, size C.int) C.int {
ch := eventChannels.get(uint(uintptr(ctx))).(chan []byte)
ch <- C.GoBytes(data, size)
return C.int(0)
}