-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (56 loc) · 1.51 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
package main
import (
"bufio"
"context"
"errors"
"fmt"
"io/fs"
"log"
"os"
"os/signal"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
)
// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf bpf_prog.c
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
// Allow the current process to lock memory for eBPF resources.
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatal(err)
}
bpfObj := bpfObjects{}
if err := loadBpfObjects(&bpfObj, nil); err != nil {
log.Fatal(err)
}
defer bpfObj.Close()
l, err := link.Tracepoint("syscalls", "sys_enter_execve", bpfObj.BpfProg, nil)
if err != nil {
log.Fatal(err)
}
defer l.Close()
// /sys/kernel/debug/tracing/trace_pipe is a special file in the /sys/kernel/debug
// filesystem that provides access to the trace data generated by the Linux kernel's
// dynamic tracing facility
tracePipeFile := "/sys/kernel/debug/tracing/trace_pipe"
tracePipe, err := os.Open(tracePipeFile)
if err != nil {
log.Fatalf("open %s: %s\n", tracePipeFile, err)
}
defer tracePipe.Close()
go func() {
// Create a bufio.Scanner to read the trace data.
scanner := bufio.NewScanner(tracePipe)
// Read and print the trace data.
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
if !errors.Is(err, fs.ErrClosed) {
log.Println(err)
}
}
}()
<-ctx.Done()
}