Debian Codegraph @

link/tracing.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "errors"
7 "fmt"
8
9 "github.com/cilium/ebpf"
10 "github.com/cilium/ebpf/btf"
11 "github.com/cilium/ebpf/internal/sys"
12 "github.com/cilium/ebpf/internal/unix"
13 )
14
15 type tracing struct {
16 RawLink
17 }
18
19 func (f *tracing) Update(_ *ebpf.Program) error {
20 return fmt.Errorf("tracing update: %w", ErrNotSupported)
21 }
22
23 func (f *tracing) Info() (*Info, error) {
24 var info sys.TracingLinkInfo
25 if err := sys.ObjInfo(f.fd, &info); err != nil {
26 return nil, fmt.Errorf("tracing link info: %s", err)
27 }
28 extra := &TracingInfo{
29 TargetObjectId: info.TargetObjId,
30 TargetBtfId: info.TargetBtfId,
31 AttachType: info.AttachType,
32 }
33
34 return &Info{
35 info.Type,
36 info.Id,
37 ebpf.ProgramID(info.ProgId),
38 extra,
39 }, nil
40 }
41
42 // AttachFreplace attaches the given eBPF program to the function it replaces.
43 //
44 // The program and name can either be provided at link time, or can be provided
45 // at program load time. If they were provided at load time, they should be nil
46 // and empty respectively here, as they will be ignored by the kernel.
47 // Examples:
48 //
49 // AttachFreplace(dispatcher, "function", replacement)
50 // AttachFreplace(nil, "", replacement)
51 func AttachFreplace(targetProg *ebpf.Program, name string, prog *ebpf.Program) (Link, error) {
52 if (name == "") != (targetProg == nil) {
53 return nil, fmt.Errorf("must provide both or neither of name and targetProg: %w", errInvalidInput)
54 }
55 if prog == nil {
56 return nil, fmt.Errorf("prog cannot be nil: %w", errInvalidInput)
57 }
58 if prog.Type() != ebpf.Extension {
59 return nil, fmt.Errorf("eBPF program type %s is not an Extension: %w", prog.Type(), errInvalidInput)
60 }
61
62 var (
63 target int
64 typeID btf.TypeID
65 )
66 if targetProg != nil {
67 btfHandle, err := targetProg.Handle()
68 if err != nil {
69 return nil, err
70 }
71 defer btfHandle.Close()
72
73 spec, err := btfHandle.Spec(nil)
74 if err != nil {
75 return nil, err
76 }
77
78 var function *btf.Func
79 if err := spec.TypeByName(name, &function); err != nil {
80 return nil, err
81 }
82
83 target = targetProg.FD()
84 typeID, err = spec.TypeID(function)
85 if err != nil {
86 return nil, err
87 }
88 }
89
90 link, err := AttachRawLink(RawLinkOptions{
91 Target: target,
92 Program: prog,
93 Attach: ebpf.AttachNone,
94 BTF: typeID,
95 })
96 if errors.Is(err, sys.ENOTSUPP) {
97 // This may be returned by bpf_tracing_prog_attach via bpf_arch_text_poke.
98 return nil, fmt.Errorf("create raw tracepoint: %w", ErrNotSupported)
99 }
100 if err != nil {
101 return nil, err
102 }
103
104 return &tracing{*link}, nil
105 }
106
107 type TracingOptions struct {
108 // Program must be of type Tracing with attach type
109 // AttachTraceFEntry/AttachTraceFExit/AttachModifyReturn or
110 // AttachTraceRawTp.
111 Program *ebpf.Program
112 // Program attach type. Can be one of:
113 // - AttachTraceFEntry
114 // - AttachTraceFExit
115 // - AttachModifyReturn
116 // - AttachTraceRawTp
117 // This field is optional.
118 AttachType ebpf.AttachType
119 // Arbitrary value that can be fetched from an eBPF program
120 // via `bpf_get_attach_cookie()`.
121 Cookie uint64
122 }
123
124 type LSMOptions struct {
125 // Program must be of type LSM with attach type
126 // AttachLSMMac.
127 Program *ebpf.Program
128 // Arbitrary value that can be fetched from an eBPF program
129 // via `bpf_get_attach_cookie()`.
130 Cookie uint64
131 }
132
133 // attachBTFID links all BPF program types (Tracing/LSM) that they attach to a btf_id.
134 func attachBTFID(program *ebpf.Program, at ebpf.AttachType, cookie uint64) (Link, error) {
135 if program.FD() < 0 {
136 return nil, fmt.Errorf("invalid program %w", sys.ErrClosedFd)
137 }
138
139 var (
140 fd *sys.FD
141 err error
142 )
143 switch at {
144 case ebpf.AttachTraceFEntry, ebpf.AttachTraceFExit, ebpf.AttachTraceRawTp,
145 ebpf.AttachModifyReturn, ebpf.AttachLSMMac:
146 // Attach via BPF link
147 fd, err = sys.LinkCreateTracing(&sys.LinkCreateTracingAttr{
148 ProgFd: uint32(program.FD()),
149 AttachType: sys.AttachType(at),
150 Cookie: cookie,
151 })
152 if err == nil {
153 break
154 }
155 if !errors.Is(err, unix.EINVAL) && !errors.Is(err, sys.ENOTSUPP) {
156 return nil, fmt.Errorf("create tracing link: %w", err)
157 }
158 fallthrough
159 case ebpf.AttachNone:
160 // Attach via RawTracepointOpen
161 if cookie > 0 {
162 return nil, fmt.Errorf("create raw tracepoint with cookie: %w", ErrNotSupported)
163 }
164
165 fd, err = sys.RawTracepointOpen(&sys.RawTracepointOpenAttr{
166 ProgFd: uint32(program.FD()),
167 })
168 if errors.Is(err, sys.ENOTSUPP) {
169 // This may be returned by bpf_tracing_prog_attach via bpf_arch_text_poke.
170 return nil, fmt.Errorf("create raw tracepoint: %w", ErrNotSupported)
171 }
172 if err != nil {
173 return nil, fmt.Errorf("create raw tracepoint: %w", err)
174 }
175 default:
176 return nil, fmt.Errorf("invalid attach type: %s", at.String())
177 }
178
179 raw := RawLink{fd: fd}
180 info, err := raw.Info()
181 if err != nil {
182 raw.Close()
183 return nil, err
184 }
185
186 if info.Type == RawTracepointType {
187 // Sadness upon sadness: a Tracing program with AttachRawTp returns
188 // a raw_tracepoint link. Other types return a tracing link.
189 return &rawTracepoint{raw}, nil
190 }
191 return &tracing{raw}, nil
192 }
193
194 // AttachTracing links a tracing (fentry/fexit/fmod_ret) BPF program or
195 // a BTF-powered raw tracepoint (tp_btf) BPF Program to a BPF hook defined
196 // in kernel modules.
197 func AttachTracing(opts TracingOptions) (Link, error) {
198 if t := opts.Program.Type(); t != ebpf.Tracing {
199 return nil, fmt.Errorf("invalid program type %s, expected Tracing", t)
200 }
201
202 switch opts.AttachType {
203 case ebpf.AttachTraceFEntry, ebpf.AttachTraceFExit, ebpf.AttachModifyReturn,
204 ebpf.AttachTraceRawTp, ebpf.AttachNone:
205 default:
206 return nil, fmt.Errorf("invalid attach type: %s", opts.AttachType.String())
207 }
208
209 return attachBTFID(opts.Program, opts.AttachType, opts.Cookie)
210 }
211
212 // AttachLSM links a Linux security module (LSM) BPF Program to a BPF
213 // hook defined in kernel modules.
214 func AttachLSM(opts LSMOptions) (Link, error) {
215 if t := opts.Program.Type(); t != ebpf.LSM {
216 return nil, fmt.Errorf("invalid program type %s, expected LSM", t)
217 }
218
219 return attachBTFID(opts.Program, ebpf.AttachLSMMac, opts.Cookie)
220 }