Debian Codegraph @

examples/tracepoint_in_go/main.go

raw symbols SCIP
1 //go:build linux
2
3 // This program demonstrates how to attach an eBPF program to a tracepoint.
4 // The program is attached to the syscall/sys_enter_openat tracepoint and
5 // prints out the integer 123 every time the syscall is entered.
6 package main
7
8 import (
9 "errors"
10 "log"
11 "os"
12 "os/signal"
13 "syscall"
14
15 "github.com/cilium/ebpf"
16 "github.com/cilium/ebpf/asm"
17 "github.com/cilium/ebpf/link"
18 "github.com/cilium/ebpf/perf"
19 "github.com/cilium/ebpf/rlimit"
20 )
21
22 // Metadata for the eBPF program used in this example.
23 var progSpec = &ebpf.ProgramSpec{
24 Name: "my_trace_prog", // non-unique name, will appear in `bpftool prog list` while attached
25 Type: ebpf.TracePoint, // only TracePoint programs can be attached to trace events created by link.Tracepoint()
26 License: "GPL", // license must be GPL for calling kernel helpers like perf_event_output
27 }
28
29 func main() {
30
31 // Subscribe to signals for terminating the program.
32 stopper := make(chan os.Signal, 1)
33 signal.Notify(stopper, os.Interrupt, syscall.SIGTERM)
34
35 // Allow the current process to lock memory for eBPF resources.
36 if err := rlimit.RemoveMemlock(); err != nil {
37 log.Fatal(err)
38 }
39
40 // Create a perf event array for the kernel to write perf records to.
41 // These records will be read by userspace below.
42 events, err := ebpf.NewMap(&ebpf.MapSpec{
43 Type: ebpf.PerfEventArray,
44 Name: "my_perf_array",
45 })
46 if err != nil {
47 log.Fatalf("creating perf event array: %s", err)
48 }
49 defer events.Close()
50
51 // Open a perf reader from userspace into the perf event array
52 // created earlier.
53 rd, err := perf.NewReader(events, os.Getpagesize())
54 if err != nil {
55 log.Fatalf("creating event reader: %s", err)
56 }
57 defer rd.Close()
58
59 // Close the reader when the process receives a signal, which will exit
60 // the read loop.
61 go func() {
62 <-stopper
63 rd.Close()
64 }()
65
66 // Minimal program that writes the static value '123' to the perf ring on
67 // each event. Note that this program refers to the file descriptor of
68 // the perf event array created above, which needs to be created prior to the
69 // program being verified by and inserted into the kernel.
70 progSpec.Instructions = asm.Instructions{
71 // store the integer 123 at FP[-8]
72 asm.Mov.Imm(asm.R2, 123),
73 asm.StoreMem(asm.RFP, -8, asm.R2, asm.Word),
74
75 // load registers with arguments for call of FnPerfEventOutput
76 asm.LoadMapPtr(asm.R2, events.FD()), // file descriptor of the perf event array
77 asm.LoadImm(asm.R3, 0xffffffff, asm.DWord),
78 asm.Mov.Reg(asm.R4, asm.RFP),
79 asm.Add.Imm(asm.R4, -8),
80 asm.Mov.Imm(asm.R5, 4),
81
82 // call FnPerfEventOutput, an eBPF kernel helper
83 asm.FnPerfEventOutput.Call(),
84
85 // set exit code to 0
86 asm.Mov.Imm(asm.R0, 0),
87 asm.Return(),
88 }
89
90 // Instantiate and insert the program into the kernel.
91 prog, err := ebpf.NewProgram(progSpec)
92 if err != nil {
93 log.Fatalf("creating ebpf program: %s", err)
94 }
95 defer prog.Close()
96
97 // Open a trace event based on a pre-existing kernel hook (tracepoint).
98 // Each time a userspace program uses the 'openat()' syscall, the eBPF
99 // program specified above will be executed and a '123' value will appear
100 // in the perf ring.
101 tp, err := link.Tracepoint("syscalls", "sys_enter_openat", prog, nil)
102 if err != nil {
103 log.Fatalf("opening tracepoint: %s", err)
104 }
105 defer tp.Close()
106
107 log.Println("Waiting for events..")
108
109 for {
110 record, err := rd.Read()
111 if err != nil {
112 if errors.Is(err, perf.ErrClosed) {
113 log.Println("Received signal, exiting..")
114 return
115 }
116 log.Printf("reading from reader: %s", err)
117 continue
118 }
119
120 log.Println("Record:", record)
121 }
122 }