Debian Codegraph @

link/kprobe_multi.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "errors"
7 "fmt"
8 "os"
9
10 "github.com/cilium/ebpf"
11 "github.com/cilium/ebpf/features"
12 "github.com/cilium/ebpf/internal/sys"
13 "github.com/cilium/ebpf/internal/unix"
14 )
15
16 // KprobeMultiOptions defines additional parameters that will be used
17 // when opening a KprobeMulti Link.
18 type KprobeMultiOptions struct {
19 // Symbols takes a list of kernel symbol names to attach an ebpf program to.
20 //
21 // Mutually exclusive with Addresses.
22 Symbols []string
23
24 // Addresses takes a list of kernel symbol addresses in case they can not
25 // be referred to by name.
26 //
27 // Note that only start addresses can be specified, since the fprobe API
28 // limits the attach point to the function entry or return.
29 //
30 // Mutually exclusive with Symbols.
31 Addresses []uintptr
32
33 // Cookies specifies arbitrary values that can be fetched from an eBPF
34 // program via `bpf_get_attach_cookie()`.
35 //
36 // If set, its length should be equal to the length of Symbols or Addresses.
37 // Each Cookie is assigned to the Symbol or Address specified at the
38 // corresponding slice index.
39 Cookies []uint64
40
41 // Session must be true when attaching Programs with the
42 // [ebpf.AttachTraceKprobeSession] attach type.
43 //
44 // This makes a Kprobe execute on both function entry and return. The entry
45 // program can share a cookie value with the return program and can decide
46 // whether the return program gets executed.
47 Session bool
48 }
49
50 // KprobeMulti attaches the given eBPF program to the entry point of a given set
51 // of kernel symbols.
52 //
53 // The difference with Kprobe() is that multi-kprobe accomplishes this in a
54 // single system call, making it significantly faster than attaching many
55 // probes one at a time.
56 //
57 // Requires at least Linux 5.18.
58 func KprobeMulti(prog *ebpf.Program, opts KprobeMultiOptions) (Link, error) {
59 return kprobeMulti(prog, opts, 0)
60 }
61
62 // KretprobeMulti attaches the given eBPF program to the return point of a given
63 // set of kernel symbols.
64 //
65 // The difference with Kretprobe() is that multi-kprobe accomplishes this in a
66 // single system call, making it significantly faster than attaching many
67 // probes one at a time.
68 //
69 // Requires at least Linux 5.18.
70 func KretprobeMulti(prog *ebpf.Program, opts KprobeMultiOptions) (Link, error) {
71 return kprobeMulti(prog, opts, sys.BPF_F_KPROBE_MULTI_RETURN)
72 }
73
74 func kprobeMulti(prog *ebpf.Program, opts KprobeMultiOptions, flags uint32) (Link, error) {
75 if prog == nil {
76 return nil, errors.New("cannot attach a nil program")
77 }
78
79 syms := uint32(len(opts.Symbols))
80 addrs := uint32(len(opts.Addresses))
81 cookies := uint32(len(opts.Cookies))
82
83 if syms == 0 && addrs == 0 {
84 return nil, fmt.Errorf("one of Symbols or Addresses is required: %w", errInvalidInput)
85 }
86 if syms != 0 && addrs != 0 {
87 return nil, fmt.Errorf("fields Symbols and Addresses are mutually exclusive: %w", errInvalidInput)
88 }
89 if cookies > 0 && cookies != syms && cookies != addrs {
90 return nil, fmt.Errorf("field Cookies must be exactly Symbols or Addresses in length: %w", errInvalidInput)
91 }
92
93 attachType := sys.BPF_TRACE_KPROBE_MULTI
94 if opts.Session {
95 attachType = sys.BPF_TRACE_KPROBE_SESSION
96 }
97
98 attr := &sys.LinkCreateKprobeMultiAttr{
99 ProgFd: uint32(prog.FD()),
100 AttachType: attachType,
101 KprobeMultiFlags: flags,
102 }
103
104 switch {
105 case syms != 0:
106 attr.Count = syms
107 attr.Syms = sys.NewStringSlicePointer(opts.Symbols)
108
109 case addrs != 0:
110 attr.Count = addrs
111 attr.Addrs = sys.SlicePointer(opts.Addresses)
112 }
113
114 if cookies != 0 {
115 attr.Cookies = sys.SlicePointer(opts.Cookies)
116 }
117
118 fd, err := sys.LinkCreateKprobeMulti(attr)
119 if err == nil {
120 return &kprobeMultiLink{RawLink{fd, ""}}, nil
121 }
122
123 if errors.Is(err, unix.ESRCH) {
124 return nil, fmt.Errorf("couldn't find one or more symbols: %w", os.ErrNotExist)
125 }
126
127 if opts.Session {
128 if haveFeatErr := features.HaveBPFLinkKprobeSession(); haveFeatErr != nil {
129 return nil, haveFeatErr
130 }
131 } else {
132 if haveFeatErr := features.HaveBPFLinkKprobeMulti(); haveFeatErr != nil {
133 return nil, haveFeatErr
134 }
135 }
136
137 // Check EINVAL after running feature probes, since it's also returned when
138 // the kernel doesn't support the multi/session attach types.
139 if errors.Is(err, unix.EINVAL) {
140 return nil, fmt.Errorf("%w (missing kernel symbol or prog's AttachType not %s?)", err, ebpf.AttachType(attachType))
141 }
142
143 return nil, err
144 }
145
146 type kprobeMultiLink struct {
147 RawLink
148 }
149
150 var _ Link = (*kprobeMultiLink)(nil)
151
152 func (kml *kprobeMultiLink) Update(_ *ebpf.Program) error {
153 return fmt.Errorf("update kprobe_multi: %w", ErrNotSupported)
154 }
155
156 func (kml *kprobeMultiLink) Info() (*Info, error) {
157 var info sys.KprobeMultiLinkInfo
158 if err := sys.ObjInfo(kml.fd, &info); err != nil {
159 return nil, fmt.Errorf("kprobe multi link info: %w", err)
160 }
161 var addrs = make([]uint64, info.Count)
162 var cookies = make([]uint64, info.Count)
163 info = sys.KprobeMultiLinkInfo{
164 Addrs: sys.SlicePointer(addrs),
165 Cookies: sys.SlicePointer(cookies),
166 Count: uint32(len(addrs)),
167 }
168 if err := sys.ObjInfo(kml.fd, &info); err != nil {
169 return nil, fmt.Errorf("kprobe multi link info: %w", err)
170 }
171 if info.Addrs.IsNil() {
172 addrs = nil
173 }
174 if info.Cookies.IsNil() {
175 cookies = nil
176 }
177 extra := &KprobeMultiInfo{
178 Count: info.Count,
179 Flags: info.Flags,
180 Missed: info.Missed,
181 addrs: addrs,
182 cookies: cookies,
183 }
184
185 return &Info{
186 info.Type,
187 info.Id,
188 ebpf.ProgramID(info.ProgId),
189 extra,
190 }, nil
191 }