Debian Codegraph @

link/uprobe_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 // UprobeMultiOptions defines additional parameters that will be used
17 // when opening a UprobeMulti Link.
18 type UprobeMultiOptions struct {
19 // Symbol addresses. If set, overrides the addresses eventually parsed from
20 // the executable. Mutually exclusive with UprobeMulti's symbols argument.
21 Addresses []uint64
22
23 // Offsets into functions provided by UprobeMulti's symbols argument.
24 // For example: to set uprobes to main+5 and _start+10, call UprobeMulti
25 // with:
26 // symbols: "main", "_start"
27 // opt.Offsets: 5, 10
28 Offsets []uint64
29
30 // Optional list of associated ref counter offsets.
31 RefCtrOffsets []uint64
32
33 // Optional list of associated BPF cookies.
34 Cookies []uint64
35
36 // Only set the uprobe_multi link on the given process ID, zero PID means
37 // system-wide.
38 PID uint32
39 }
40
41 func (ex *Executable) UprobeMulti(symbols []string, prog *ebpf.Program, opts *UprobeMultiOptions) (Link, error) {
42 return ex.uprobeMulti(symbols, prog, opts, 0)
43 }
44
45 func (ex *Executable) UretprobeMulti(symbols []string, prog *ebpf.Program, opts *UprobeMultiOptions) (Link, error) {
46
47 // The return probe is not limited for symbols entry, so there's no special
48 // setup for return uprobes (other than the extra flag). The symbols, opts.Offsets
49 // and opts.Addresses arrays follow the same logic as for entry uprobes.
50 return ex.uprobeMulti(symbols, prog, opts, sys.BPF_F_UPROBE_MULTI_RETURN)
51 }
52
53 func (ex *Executable) uprobeMulti(symbols []string, prog *ebpf.Program, opts *UprobeMultiOptions, flags uint32) (Link, error) {
54 if prog == nil {
55 return nil, errors.New("cannot attach a nil program")
56 }
57
58 if opts == nil {
59 opts = &UprobeMultiOptions{}
60 }
61
62 addresses, err := ex.addresses(symbols, opts.Addresses, opts.Offsets)
63 if err != nil {
64 return nil, err
65 }
66
67 addrs := len(addresses)
68 cookies := len(opts.Cookies)
69 refCtrOffsets := len(opts.RefCtrOffsets)
70
71 if addrs == 0 {
72 return nil, fmt.Errorf("field Addresses is required: %w", errInvalidInput)
73 }
74 if refCtrOffsets > 0 && refCtrOffsets != addrs {
75 return nil, fmt.Errorf("field RefCtrOffsets must be exactly Addresses in length: %w", errInvalidInput)
76 }
77 if cookies > 0 && cookies != addrs {
78 return nil, fmt.Errorf("field Cookies must be exactly Addresses in length: %w", errInvalidInput)
79 }
80
81 attr := &sys.LinkCreateUprobeMultiAttr{
82 Path: sys.NewStringPointer(ex.path),
83 ProgFd: uint32(prog.FD()),
84 AttachType: sys.BPF_TRACE_UPROBE_MULTI,
85 UprobeMultiFlags: flags,
86 Count: uint32(addrs),
87 Offsets: sys.SlicePointer(addresses),
88 Pid: opts.PID,
89 }
90
91 if refCtrOffsets != 0 {
92 attr.RefCtrOffsets = sys.SlicePointer(opts.RefCtrOffsets)
93 }
94 if cookies != 0 {
95 attr.Cookies = sys.SlicePointer(opts.Cookies)
96 }
97
98 fd, err := sys.LinkCreateUprobeMulti(attr)
99 if errors.Is(err, unix.ESRCH) {
100 return nil, fmt.Errorf("%w (specified pid not found?)", os.ErrNotExist)
101 }
102 // Since Linux commit 46ba0e49b642 ("bpf: fix multi-uprobe PID filtering
103 // logic"), if the provided pid overflows MaxInt32 (turning it negative), the
104 // kernel will return EINVAL instead of ESRCH.
105 if errors.Is(err, unix.EINVAL) {
106 return nil, fmt.Errorf("%w (invalid pid, missing symbol or prog's AttachType not AttachTraceUprobeMulti?)", err)
107 }
108
109 if err != nil {
110 if haveFeatErr := features.HaveBPFLinkUprobeMulti(); haveFeatErr != nil {
111 return nil, haveFeatErr
112 }
113 return nil, err
114 }
115
116 return &uprobeMultiLink{RawLink{fd, ""}}, nil
117 }
118
119 func (ex *Executable) addresses(symbols []string, addresses, offsets []uint64) ([]uint64, error) {
120 n := len(symbols)
121 if n == 0 {
122 n = len(addresses)
123 }
124
125 if n == 0 {
126 return nil, fmt.Errorf("%w: neither symbols nor addresses given", errInvalidInput)
127 }
128
129 if symbols != nil && len(symbols) != n {
130 return nil, fmt.Errorf("%w: have %d symbols but want %d", errInvalidInput, len(symbols), n)
131 }
132
133 if addresses != nil && len(addresses) != n {
134 return nil, fmt.Errorf("%w: have %d addresses but want %d", errInvalidInput, len(addresses), n)
135 }
136
137 if offsets != nil && len(offsets) != n {
138 return nil, fmt.Errorf("%w: have %d offsets but want %d", errInvalidInput, len(offsets), n)
139 }
140
141 results := make([]uint64, 0, n)
142 for i := 0; i < n; i++ {
143 var sym string
144 if symbols != nil {
145 sym = symbols[i]
146 }
147
148 var addr, off uint64
149 if addresses != nil {
150 addr = addresses[i]
151 }
152
153 if offsets != nil {
154 off = offsets[i]
155 }
156
157 result, err := ex.address(sym, addr, off)
158 if err != nil {
159 return nil, err
160 }
161
162 results = append(results, result)
163 }
164
165 return results, nil
166 }
167
168 type uprobeMultiLink struct {
169 RawLink
170 }
171
172 var _ Link = (*uprobeMultiLink)(nil)
173
174 func (kml *uprobeMultiLink) Update(_ *ebpf.Program) error {
175 return fmt.Errorf("update uprobe_multi: %w", ErrNotSupported)
176 }
177
178 func (kml *uprobeMultiLink) Info() (*Info, error) {
179 var info sys.UprobeMultiLinkInfo
180 if err := sys.ObjInfo(kml.fd, &info); err != nil {
181 return nil, fmt.Errorf("uprobe multi link info: %s", err)
182 }
183 var (
184 path = make([]byte, info.PathSize)
185 refCtrOffsets = make([]uint64, info.Count)
186 addrs = make([]uint64, info.Count)
187 cookies = make([]uint64, info.Count)
188 )
189 info = sys.UprobeMultiLinkInfo{
190 Path: sys.SlicePointer(path),
191 PathSize: uint32(len(path)),
192 Offsets: sys.SlicePointer(addrs),
193 RefCtrOffsets: sys.SlicePointer(refCtrOffsets),
194 Cookies: sys.SlicePointer(cookies),
195 Count: uint32(len(addrs)),
196 }
197 if err := sys.ObjInfo(kml.fd, &info); err != nil {
198 return nil, fmt.Errorf("uprobe multi link info: %s", err)
199 }
200 if info.Path.IsNil() {
201 path = nil
202 }
203 if info.Cookies.IsNil() {
204 cookies = nil
205 }
206 if info.Offsets.IsNil() {
207 addrs = nil
208 }
209 if info.RefCtrOffsets.IsNil() {
210 refCtrOffsets = nil
211 }
212 extra := &UprobeMultiInfo{
213 Count: info.Count,
214 Flags: info.Flags,
215 pid: info.Pid,
216 offsets: addrs,
217 cookies: cookies,
218 refCtrOffsets: refCtrOffsets,
219 File: unix.ByteSliceToString(path),
220 }
221
222 return &Info{
223 info.Type,
224 info.Id,
225 ebpf.ProgramID(info.ProgId),
226 extra,
227 }, nil
228 }