1*f7c14bbaSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
3*f7c14bbaSAndroid Build Coastguard Worker #include <ctype.h>
4*f7c14bbaSAndroid Build Coastguard Worker #include <stdio.h>
5*f7c14bbaSAndroid Build Coastguard Worker #include <stdlib.h>
6*f7c14bbaSAndroid Build Coastguard Worker #include <string.h>
7*f7c14bbaSAndroid Build Coastguard Worker #include <libelf.h>
8*f7c14bbaSAndroid Build Coastguard Worker #include <gelf.h>
9*f7c14bbaSAndroid Build Coastguard Worker #include <unistd.h>
10*f7c14bbaSAndroid Build Coastguard Worker #include <linux/ptrace.h>
11*f7c14bbaSAndroid Build Coastguard Worker #include <linux/kernel.h>
12*f7c14bbaSAndroid Build Coastguard Worker
13*f7c14bbaSAndroid Build Coastguard Worker /* s8 will be marked as poison while it's a reg of riscv */
14*f7c14bbaSAndroid Build Coastguard Worker #if defined(__riscv)
15*f7c14bbaSAndroid Build Coastguard Worker #define rv_s8 s8
16*f7c14bbaSAndroid Build Coastguard Worker #endif
17*f7c14bbaSAndroid Build Coastguard Worker
18*f7c14bbaSAndroid Build Coastguard Worker #include "bpf.h"
19*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf.h"
20*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_common.h"
21*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_internal.h"
22*f7c14bbaSAndroid Build Coastguard Worker #include "hashmap.h"
23*f7c14bbaSAndroid Build Coastguard Worker
24*f7c14bbaSAndroid Build Coastguard Worker /* libbpf's USDT support consists of BPF-side state/code and user-space
25*f7c14bbaSAndroid Build Coastguard Worker * state/code working together in concert. BPF-side parts are defined in
26*f7c14bbaSAndroid Build Coastguard Worker * usdt.bpf.h header library. User-space state is encapsulated by struct
27*f7c14bbaSAndroid Build Coastguard Worker * usdt_manager and all the supporting code centered around usdt_manager.
28*f7c14bbaSAndroid Build Coastguard Worker *
29*f7c14bbaSAndroid Build Coastguard Worker * usdt.bpf.h defines two BPF maps that usdt_manager expects: USDT spec map
30*f7c14bbaSAndroid Build Coastguard Worker * and IP-to-spec-ID map, which is auxiliary map necessary for kernels that
31*f7c14bbaSAndroid Build Coastguard Worker * don't support BPF cookie (see below). These two maps are implicitly
32*f7c14bbaSAndroid Build Coastguard Worker * embedded into user's end BPF object file when user's code included
33*f7c14bbaSAndroid Build Coastguard Worker * usdt.bpf.h. This means that libbpf doesn't do anything special to create
34*f7c14bbaSAndroid Build Coastguard Worker * these USDT support maps. They are created by normal libbpf logic of
35*f7c14bbaSAndroid Build Coastguard Worker * instantiating BPF maps when opening and loading BPF object.
36*f7c14bbaSAndroid Build Coastguard Worker *
37*f7c14bbaSAndroid Build Coastguard Worker * As such, libbpf is basically unaware of the need to do anything
38*f7c14bbaSAndroid Build Coastguard Worker * USDT-related until the very first call to bpf_program__attach_usdt(), which
39*f7c14bbaSAndroid Build Coastguard Worker * can be called by user explicitly or happen automatically during skeleton
40*f7c14bbaSAndroid Build Coastguard Worker * attach (or, equivalently, through generic bpf_program__attach() call). At
41*f7c14bbaSAndroid Build Coastguard Worker * this point, libbpf will instantiate and initialize struct usdt_manager and
42*f7c14bbaSAndroid Build Coastguard Worker * store it in bpf_object. USDT manager is per-BPF object construct, as each
43*f7c14bbaSAndroid Build Coastguard Worker * independent BPF object might or might not have USDT programs, and thus all
44*f7c14bbaSAndroid Build Coastguard Worker * the expected USDT-related state. There is no coordination between two
45*f7c14bbaSAndroid Build Coastguard Worker * bpf_object in parts of USDT attachment, they are oblivious of each other's
46*f7c14bbaSAndroid Build Coastguard Worker * existence and libbpf is just oblivious, dealing with bpf_object-specific
47*f7c14bbaSAndroid Build Coastguard Worker * USDT state.
48*f7c14bbaSAndroid Build Coastguard Worker *
49*f7c14bbaSAndroid Build Coastguard Worker * Quick crash course on USDTs.
50*f7c14bbaSAndroid Build Coastguard Worker *
51*f7c14bbaSAndroid Build Coastguard Worker * From user-space application's point of view, USDT is essentially just
52*f7c14bbaSAndroid Build Coastguard Worker * a slightly special function call that normally has zero overhead, unless it
53*f7c14bbaSAndroid Build Coastguard Worker * is being traced by some external entity (e.g, BPF-based tool). Here's how
54*f7c14bbaSAndroid Build Coastguard Worker * a typical application can trigger USDT probe:
55*f7c14bbaSAndroid Build Coastguard Worker *
56*f7c14bbaSAndroid Build Coastguard Worker * #include <sys/sdt.h> // provided by systemtap-sdt-devel package
57*f7c14bbaSAndroid Build Coastguard Worker * // folly also provide similar functionality in folly/tracing/StaticTracepoint.h
58*f7c14bbaSAndroid Build Coastguard Worker *
59*f7c14bbaSAndroid Build Coastguard Worker * STAP_PROBE3(my_usdt_provider, my_usdt_probe_name, 123, x, &y);
60*f7c14bbaSAndroid Build Coastguard Worker *
61*f7c14bbaSAndroid Build Coastguard Worker * USDT is identified by it's <provider-name>:<probe-name> pair of names. Each
62*f7c14bbaSAndroid Build Coastguard Worker * individual USDT has a fixed number of arguments (3 in the above example)
63*f7c14bbaSAndroid Build Coastguard Worker * and specifies values of each argument as if it was a function call.
64*f7c14bbaSAndroid Build Coastguard Worker *
65*f7c14bbaSAndroid Build Coastguard Worker * USDT call is actually not a function call, but is instead replaced by
66*f7c14bbaSAndroid Build Coastguard Worker * a single NOP instruction (thus zero overhead, effectively). But in addition
67*f7c14bbaSAndroid Build Coastguard Worker * to that, those USDT macros generate special SHT_NOTE ELF records in
68*f7c14bbaSAndroid Build Coastguard Worker * .note.stapsdt ELF section. Here's an example USDT definition as emitted by
69*f7c14bbaSAndroid Build Coastguard Worker * `readelf -n <binary>`:
70*f7c14bbaSAndroid Build Coastguard Worker *
71*f7c14bbaSAndroid Build Coastguard Worker * stapsdt 0x00000089 NT_STAPSDT (SystemTap probe descriptors)
72*f7c14bbaSAndroid Build Coastguard Worker * Provider: test
73*f7c14bbaSAndroid Build Coastguard Worker * Name: usdt12
74*f7c14bbaSAndroid Build Coastguard Worker * Location: 0x0000000000549df3, Base: 0x00000000008effa4, Semaphore: 0x0000000000a4606e
75*f7c14bbaSAndroid Build Coastguard Worker * Arguments: -4@-1204(%rbp) -4@%edi -8@-1216(%rbp) -8@%r8 -4@$5 -8@%r9 8@%rdx 8@%r10 -4@$-9 -2@%cx -2@%ax -1@%sil
76*f7c14bbaSAndroid Build Coastguard Worker *
77*f7c14bbaSAndroid Build Coastguard Worker * In this case we have USDT test:usdt12 with 12 arguments.
78*f7c14bbaSAndroid Build Coastguard Worker *
79*f7c14bbaSAndroid Build Coastguard Worker * Location and base are offsets used to calculate absolute IP address of that
80*f7c14bbaSAndroid Build Coastguard Worker * NOP instruction that kernel can replace with an interrupt instruction to
81*f7c14bbaSAndroid Build Coastguard Worker * trigger instrumentation code (BPF program for all that we care about).
82*f7c14bbaSAndroid Build Coastguard Worker *
83*f7c14bbaSAndroid Build Coastguard Worker * Semaphore above is and optional feature. It records an address of a 2-byte
84*f7c14bbaSAndroid Build Coastguard Worker * refcount variable (normally in '.probes' ELF section) used for signaling if
85*f7c14bbaSAndroid Build Coastguard Worker * there is anything that is attached to USDT. This is useful for user
86*f7c14bbaSAndroid Build Coastguard Worker * applications if, for example, they need to prepare some arguments that are
87*f7c14bbaSAndroid Build Coastguard Worker * passed only to USDTs and preparation is expensive. By checking if USDT is
88*f7c14bbaSAndroid Build Coastguard Worker * "activated", an application can avoid paying those costs unnecessarily.
89*f7c14bbaSAndroid Build Coastguard Worker * Recent enough kernel has built-in support for automatically managing this
90*f7c14bbaSAndroid Build Coastguard Worker * refcount, which libbpf expects and relies on. If USDT is defined without
91*f7c14bbaSAndroid Build Coastguard Worker * associated semaphore, this value will be zero. See selftests for semaphore
92*f7c14bbaSAndroid Build Coastguard Worker * examples.
93*f7c14bbaSAndroid Build Coastguard Worker *
94*f7c14bbaSAndroid Build Coastguard Worker * Arguments is the most interesting part. This USDT specification string is
95*f7c14bbaSAndroid Build Coastguard Worker * providing information about all the USDT arguments and their locations. The
96*f7c14bbaSAndroid Build Coastguard Worker * part before @ sign defined byte size of the argument (1, 2, 4, or 8) and
97*f7c14bbaSAndroid Build Coastguard Worker * whether the argument is signed or unsigned (negative size means signed).
98*f7c14bbaSAndroid Build Coastguard Worker * The part after @ sign is assembly-like definition of argument location
99*f7c14bbaSAndroid Build Coastguard Worker * (see [0] for more details). Technically, assembler can provide some pretty
100*f7c14bbaSAndroid Build Coastguard Worker * advanced definitions, but libbpf is currently supporting three most common
101*f7c14bbaSAndroid Build Coastguard Worker * cases:
102*f7c14bbaSAndroid Build Coastguard Worker * 1) immediate constant, see 5th and 9th args above (-4@$5 and -4@-9);
103*f7c14bbaSAndroid Build Coastguard Worker * 2) register value, e.g., 8@%rdx, which means "unsigned 8-byte integer
104*f7c14bbaSAndroid Build Coastguard Worker * whose value is in register %rdx";
105*f7c14bbaSAndroid Build Coastguard Worker * 3) memory dereference addressed by register, e.g., -4@-1204(%rbp), which
106*f7c14bbaSAndroid Build Coastguard Worker * specifies signed 32-bit integer stored at offset -1204 bytes from
107*f7c14bbaSAndroid Build Coastguard Worker * memory address stored in %rbp.
108*f7c14bbaSAndroid Build Coastguard Worker *
109*f7c14bbaSAndroid Build Coastguard Worker * [0] https://sourceware.org/systemtap/wiki/UserSpaceProbeImplementation
110*f7c14bbaSAndroid Build Coastguard Worker *
111*f7c14bbaSAndroid Build Coastguard Worker * During attachment, libbpf parses all the relevant USDT specifications and
112*f7c14bbaSAndroid Build Coastguard Worker * prepares `struct usdt_spec` (USDT spec), which is then provided to BPF-side
113*f7c14bbaSAndroid Build Coastguard Worker * code through spec map. This allows BPF applications to quickly fetch the
114*f7c14bbaSAndroid Build Coastguard Worker * actual value at runtime using a simple BPF-side code.
115*f7c14bbaSAndroid Build Coastguard Worker *
116*f7c14bbaSAndroid Build Coastguard Worker * With basics out of the way, let's go over less immediately obvious aspects
117*f7c14bbaSAndroid Build Coastguard Worker * of supporting USDTs.
118*f7c14bbaSAndroid Build Coastguard Worker *
119*f7c14bbaSAndroid Build Coastguard Worker * First, there is no special USDT BPF program type. It is actually just
120*f7c14bbaSAndroid Build Coastguard Worker * a uprobe BPF program (which for kernel, at least currently, is just a kprobe
121*f7c14bbaSAndroid Build Coastguard Worker * program, so BPF_PROG_TYPE_KPROBE program type). With the only difference
122*f7c14bbaSAndroid Build Coastguard Worker * that uprobe is usually attached at the function entry, while USDT will
123*f7c14bbaSAndroid Build Coastguard Worker * normally will be somewhere inside the function. But it should always be
124*f7c14bbaSAndroid Build Coastguard Worker * pointing to NOP instruction, which makes such uprobes the fastest uprobe
125*f7c14bbaSAndroid Build Coastguard Worker * kind.
126*f7c14bbaSAndroid Build Coastguard Worker *
127*f7c14bbaSAndroid Build Coastguard Worker * Second, it's important to realize that such STAP_PROBEn(provider, name, ...)
128*f7c14bbaSAndroid Build Coastguard Worker * macro invocations can end up being inlined many-many times, depending on
129*f7c14bbaSAndroid Build Coastguard Worker * specifics of each individual user application. So single conceptual USDT
130*f7c14bbaSAndroid Build Coastguard Worker * (identified by provider:name pair of identifiers) is, generally speaking,
131*f7c14bbaSAndroid Build Coastguard Worker * multiple uprobe locations (USDT call sites) in different places in user
132*f7c14bbaSAndroid Build Coastguard Worker * application. Further, again due to inlining, each USDT call site might end
133*f7c14bbaSAndroid Build Coastguard Worker * up having the same argument #N be located in a different place. In one call
134*f7c14bbaSAndroid Build Coastguard Worker * site it could be a constant, in another will end up in a register, and in
135*f7c14bbaSAndroid Build Coastguard Worker * yet another could be some other register or even somewhere on the stack.
136*f7c14bbaSAndroid Build Coastguard Worker *
137*f7c14bbaSAndroid Build Coastguard Worker * As such, "attaching to USDT" means (in general case) attaching the same
138*f7c14bbaSAndroid Build Coastguard Worker * uprobe BPF program to multiple target locations in user application, each
139*f7c14bbaSAndroid Build Coastguard Worker * potentially having a completely different USDT spec associated with it.
140*f7c14bbaSAndroid Build Coastguard Worker * To wire all this up together libbpf allocates a unique integer spec ID for
141*f7c14bbaSAndroid Build Coastguard Worker * each unique USDT spec. Spec IDs are allocated as sequential small integers
142*f7c14bbaSAndroid Build Coastguard Worker * so that they can be used as keys in array BPF map (for performance reasons).
143*f7c14bbaSAndroid Build Coastguard Worker * Spec ID allocation and accounting is big part of what usdt_manager is
144*f7c14bbaSAndroid Build Coastguard Worker * about. This state has to be maintained per-BPF object and coordinate
145*f7c14bbaSAndroid Build Coastguard Worker * between different USDT attachments within the same BPF object.
146*f7c14bbaSAndroid Build Coastguard Worker *
147*f7c14bbaSAndroid Build Coastguard Worker * Spec ID is the key in spec BPF map, value is the actual USDT spec layed out
148*f7c14bbaSAndroid Build Coastguard Worker * as struct usdt_spec. Each invocation of BPF program at runtime needs to
149*f7c14bbaSAndroid Build Coastguard Worker * know its associated spec ID. It gets it either through BPF cookie, which
150*f7c14bbaSAndroid Build Coastguard Worker * libbpf sets to spec ID during attach time, or, if kernel is too old to
151*f7c14bbaSAndroid Build Coastguard Worker * support BPF cookie, through IP-to-spec-ID map that libbpf maintains in such
152*f7c14bbaSAndroid Build Coastguard Worker * case. The latter means that some modes of operation can't be supported
153*f7c14bbaSAndroid Build Coastguard Worker * without BPF cookie. Such mode is attaching to shared library "generically",
154*f7c14bbaSAndroid Build Coastguard Worker * without specifying target process. In such case, it's impossible to
155*f7c14bbaSAndroid Build Coastguard Worker * calculate absolute IP addresses for IP-to-spec-ID map, and thus such mode
156*f7c14bbaSAndroid Build Coastguard Worker * is not supported without BPF cookie support.
157*f7c14bbaSAndroid Build Coastguard Worker *
158*f7c14bbaSAndroid Build Coastguard Worker * Note that libbpf is using BPF cookie functionality for its own internal
159*f7c14bbaSAndroid Build Coastguard Worker * needs, so user itself can't rely on BPF cookie feature. To that end, libbpf
160*f7c14bbaSAndroid Build Coastguard Worker * provides conceptually equivalent USDT cookie support. It's still u64
161*f7c14bbaSAndroid Build Coastguard Worker * user-provided value that can be associated with USDT attachment. Note that
162*f7c14bbaSAndroid Build Coastguard Worker * this will be the same value for all USDT call sites within the same single
163*f7c14bbaSAndroid Build Coastguard Worker * *logical* USDT attachment. This makes sense because to user attaching to
164*f7c14bbaSAndroid Build Coastguard Worker * USDT is a single BPF program triggered for singular USDT probe. The fact
165*f7c14bbaSAndroid Build Coastguard Worker * that this is done at multiple actual locations is a mostly hidden
166*f7c14bbaSAndroid Build Coastguard Worker * implementation details. This USDT cookie value can be fetched with
167*f7c14bbaSAndroid Build Coastguard Worker * bpf_usdt_cookie(ctx) API provided by usdt.bpf.h
168*f7c14bbaSAndroid Build Coastguard Worker *
169*f7c14bbaSAndroid Build Coastguard Worker * Lastly, while single USDT can have tons of USDT call sites, it doesn't
170*f7c14bbaSAndroid Build Coastguard Worker * necessarily have that many different USDT specs. It very well might be
171*f7c14bbaSAndroid Build Coastguard Worker * that 1000 USDT call sites only need 5 different USDT specs, because all the
172*f7c14bbaSAndroid Build Coastguard Worker * arguments are typically contained in a small set of registers or stack
173*f7c14bbaSAndroid Build Coastguard Worker * locations. As such, it's wasteful to allocate as many USDT spec IDs as
174*f7c14bbaSAndroid Build Coastguard Worker * there are USDT call sites. So libbpf tries to be frugal and performs
175*f7c14bbaSAndroid Build Coastguard Worker * on-the-fly deduplication during a single USDT attachment to only allocate
176*f7c14bbaSAndroid Build Coastguard Worker * the minimal required amount of unique USDT specs (and thus spec IDs). This
177*f7c14bbaSAndroid Build Coastguard Worker * is trivially achieved by using USDT spec string (Arguments string from USDT
178*f7c14bbaSAndroid Build Coastguard Worker * note) as a lookup key in a hashmap. USDT spec string uniquely defines
179*f7c14bbaSAndroid Build Coastguard Worker * everything about how to fetch USDT arguments, so two USDT call sites
180*f7c14bbaSAndroid Build Coastguard Worker * sharing USDT spec string can safely share the same USDT spec and spec ID.
181*f7c14bbaSAndroid Build Coastguard Worker * Note, this spec string deduplication is happening only during the same USDT
182*f7c14bbaSAndroid Build Coastguard Worker * attachment, so each USDT spec shares the same USDT cookie value. This is
183*f7c14bbaSAndroid Build Coastguard Worker * not generally true for other USDT attachments within the same BPF object,
184*f7c14bbaSAndroid Build Coastguard Worker * as even if USDT spec string is the same, USDT cookie value can be
185*f7c14bbaSAndroid Build Coastguard Worker * different. It was deemed excessive to try to deduplicate across independent
186*f7c14bbaSAndroid Build Coastguard Worker * USDT attachments by taking into account USDT spec string *and* USDT cookie
187*f7c14bbaSAndroid Build Coastguard Worker * value, which would complicated spec ID accounting significantly for little
188*f7c14bbaSAndroid Build Coastguard Worker * gain.
189*f7c14bbaSAndroid Build Coastguard Worker */
190*f7c14bbaSAndroid Build Coastguard Worker
191*f7c14bbaSAndroid Build Coastguard Worker #define USDT_BASE_SEC ".stapsdt.base"
192*f7c14bbaSAndroid Build Coastguard Worker #define USDT_SEMA_SEC ".probes"
193*f7c14bbaSAndroid Build Coastguard Worker #define USDT_NOTE_SEC ".note.stapsdt"
194*f7c14bbaSAndroid Build Coastguard Worker #define USDT_NOTE_TYPE 3
195*f7c14bbaSAndroid Build Coastguard Worker #define USDT_NOTE_NAME "stapsdt"
196*f7c14bbaSAndroid Build Coastguard Worker
197*f7c14bbaSAndroid Build Coastguard Worker /* should match exactly enum __bpf_usdt_arg_type from usdt.bpf.h */
198*f7c14bbaSAndroid Build Coastguard Worker enum usdt_arg_type {
199*f7c14bbaSAndroid Build Coastguard Worker USDT_ARG_CONST,
200*f7c14bbaSAndroid Build Coastguard Worker USDT_ARG_REG,
201*f7c14bbaSAndroid Build Coastguard Worker USDT_ARG_REG_DEREF,
202*f7c14bbaSAndroid Build Coastguard Worker };
203*f7c14bbaSAndroid Build Coastguard Worker
204*f7c14bbaSAndroid Build Coastguard Worker /* should match exactly struct __bpf_usdt_arg_spec from usdt.bpf.h */
205*f7c14bbaSAndroid Build Coastguard Worker struct usdt_arg_spec {
206*f7c14bbaSAndroid Build Coastguard Worker __u64 val_off;
207*f7c14bbaSAndroid Build Coastguard Worker enum usdt_arg_type arg_type;
208*f7c14bbaSAndroid Build Coastguard Worker short reg_off;
209*f7c14bbaSAndroid Build Coastguard Worker bool arg_signed;
210*f7c14bbaSAndroid Build Coastguard Worker char arg_bitshift;
211*f7c14bbaSAndroid Build Coastguard Worker };
212*f7c14bbaSAndroid Build Coastguard Worker
213*f7c14bbaSAndroid Build Coastguard Worker /* should match BPF_USDT_MAX_ARG_CNT in usdt.bpf.h */
214*f7c14bbaSAndroid Build Coastguard Worker #define USDT_MAX_ARG_CNT 12
215*f7c14bbaSAndroid Build Coastguard Worker
216*f7c14bbaSAndroid Build Coastguard Worker /* should match struct __bpf_usdt_spec from usdt.bpf.h */
217*f7c14bbaSAndroid Build Coastguard Worker struct usdt_spec {
218*f7c14bbaSAndroid Build Coastguard Worker struct usdt_arg_spec args[USDT_MAX_ARG_CNT];
219*f7c14bbaSAndroid Build Coastguard Worker __u64 usdt_cookie;
220*f7c14bbaSAndroid Build Coastguard Worker short arg_cnt;
221*f7c14bbaSAndroid Build Coastguard Worker };
222*f7c14bbaSAndroid Build Coastguard Worker
223*f7c14bbaSAndroid Build Coastguard Worker struct usdt_note {
224*f7c14bbaSAndroid Build Coastguard Worker const char *provider;
225*f7c14bbaSAndroid Build Coastguard Worker const char *name;
226*f7c14bbaSAndroid Build Coastguard Worker /* USDT args specification string, e.g.:
227*f7c14bbaSAndroid Build Coastguard Worker * "-4@%esi -4@-24(%rbp) -4@%ecx 2@%ax 8@%rdx"
228*f7c14bbaSAndroid Build Coastguard Worker */
229*f7c14bbaSAndroid Build Coastguard Worker const char *args;
230*f7c14bbaSAndroid Build Coastguard Worker long loc_addr;
231*f7c14bbaSAndroid Build Coastguard Worker long base_addr;
232*f7c14bbaSAndroid Build Coastguard Worker long sema_addr;
233*f7c14bbaSAndroid Build Coastguard Worker };
234*f7c14bbaSAndroid Build Coastguard Worker
235*f7c14bbaSAndroid Build Coastguard Worker struct usdt_target {
236*f7c14bbaSAndroid Build Coastguard Worker long abs_ip;
237*f7c14bbaSAndroid Build Coastguard Worker long rel_ip;
238*f7c14bbaSAndroid Build Coastguard Worker long sema_off;
239*f7c14bbaSAndroid Build Coastguard Worker struct usdt_spec spec;
240*f7c14bbaSAndroid Build Coastguard Worker const char *spec_str;
241*f7c14bbaSAndroid Build Coastguard Worker };
242*f7c14bbaSAndroid Build Coastguard Worker
243*f7c14bbaSAndroid Build Coastguard Worker struct usdt_manager {
244*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *specs_map;
245*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *ip_to_spec_id_map;
246*f7c14bbaSAndroid Build Coastguard Worker
247*f7c14bbaSAndroid Build Coastguard Worker int *free_spec_ids;
248*f7c14bbaSAndroid Build Coastguard Worker size_t free_spec_cnt;
249*f7c14bbaSAndroid Build Coastguard Worker size_t next_free_spec_id;
250*f7c14bbaSAndroid Build Coastguard Worker
251*f7c14bbaSAndroid Build Coastguard Worker bool has_bpf_cookie;
252*f7c14bbaSAndroid Build Coastguard Worker bool has_sema_refcnt;
253*f7c14bbaSAndroid Build Coastguard Worker bool has_uprobe_multi;
254*f7c14bbaSAndroid Build Coastguard Worker };
255*f7c14bbaSAndroid Build Coastguard Worker
usdt_manager_new(struct bpf_object * obj)256*f7c14bbaSAndroid Build Coastguard Worker struct usdt_manager *usdt_manager_new(struct bpf_object *obj)
257*f7c14bbaSAndroid Build Coastguard Worker {
258*f7c14bbaSAndroid Build Coastguard Worker static const char *ref_ctr_sysfs_path = "/sys/bus/event_source/devices/uprobe/format/ref_ctr_offset";
259*f7c14bbaSAndroid Build Coastguard Worker struct usdt_manager *man;
260*f7c14bbaSAndroid Build Coastguard Worker struct bpf_map *specs_map, *ip_to_spec_id_map;
261*f7c14bbaSAndroid Build Coastguard Worker
262*f7c14bbaSAndroid Build Coastguard Worker specs_map = bpf_object__find_map_by_name(obj, "__bpf_usdt_specs");
263*f7c14bbaSAndroid Build Coastguard Worker ip_to_spec_id_map = bpf_object__find_map_by_name(obj, "__bpf_usdt_ip_to_spec_id");
264*f7c14bbaSAndroid Build Coastguard Worker if (!specs_map || !ip_to_spec_id_map) {
265*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to find USDT support BPF maps, did you forget to include bpf/usdt.bpf.h?\n");
266*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-ESRCH);
267*f7c14bbaSAndroid Build Coastguard Worker }
268*f7c14bbaSAndroid Build Coastguard Worker
269*f7c14bbaSAndroid Build Coastguard Worker man = calloc(1, sizeof(*man));
270*f7c14bbaSAndroid Build Coastguard Worker if (!man)
271*f7c14bbaSAndroid Build Coastguard Worker return ERR_PTR(-ENOMEM);
272*f7c14bbaSAndroid Build Coastguard Worker
273*f7c14bbaSAndroid Build Coastguard Worker man->specs_map = specs_map;
274*f7c14bbaSAndroid Build Coastguard Worker man->ip_to_spec_id_map = ip_to_spec_id_map;
275*f7c14bbaSAndroid Build Coastguard Worker
276*f7c14bbaSAndroid Build Coastguard Worker /* Detect if BPF cookie is supported for kprobes.
277*f7c14bbaSAndroid Build Coastguard Worker * We don't need IP-to-ID mapping if we can use BPF cookies.
278*f7c14bbaSAndroid Build Coastguard Worker * Added in: 7adfc6c9b315 ("bpf: Add bpf_get_attach_cookie() BPF helper to access bpf_cookie value")
279*f7c14bbaSAndroid Build Coastguard Worker */
280*f7c14bbaSAndroid Build Coastguard Worker man->has_bpf_cookie = kernel_supports(obj, FEAT_BPF_COOKIE);
281*f7c14bbaSAndroid Build Coastguard Worker
282*f7c14bbaSAndroid Build Coastguard Worker /* Detect kernel support for automatic refcounting of USDT semaphore.
283*f7c14bbaSAndroid Build Coastguard Worker * If this is not supported, USDTs with semaphores will not be supported.
284*f7c14bbaSAndroid Build Coastguard Worker * Added in: a6ca88b241d5 ("trace_uprobe: support reference counter in fd-based uprobe")
285*f7c14bbaSAndroid Build Coastguard Worker */
286*f7c14bbaSAndroid Build Coastguard Worker man->has_sema_refcnt = faccessat(AT_FDCWD, ref_ctr_sysfs_path, F_OK, AT_EACCESS) == 0;
287*f7c14bbaSAndroid Build Coastguard Worker
288*f7c14bbaSAndroid Build Coastguard Worker /*
289*f7c14bbaSAndroid Build Coastguard Worker * Detect kernel support for uprobe multi link to be used for attaching
290*f7c14bbaSAndroid Build Coastguard Worker * usdt probes.
291*f7c14bbaSAndroid Build Coastguard Worker */
292*f7c14bbaSAndroid Build Coastguard Worker man->has_uprobe_multi = kernel_supports(obj, FEAT_UPROBE_MULTI_LINK);
293*f7c14bbaSAndroid Build Coastguard Worker return man;
294*f7c14bbaSAndroid Build Coastguard Worker }
295*f7c14bbaSAndroid Build Coastguard Worker
usdt_manager_free(struct usdt_manager * man)296*f7c14bbaSAndroid Build Coastguard Worker void usdt_manager_free(struct usdt_manager *man)
297*f7c14bbaSAndroid Build Coastguard Worker {
298*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR_OR_NULL(man))
299*f7c14bbaSAndroid Build Coastguard Worker return;
300*f7c14bbaSAndroid Build Coastguard Worker
301*f7c14bbaSAndroid Build Coastguard Worker free(man->free_spec_ids);
302*f7c14bbaSAndroid Build Coastguard Worker free(man);
303*f7c14bbaSAndroid Build Coastguard Worker }
304*f7c14bbaSAndroid Build Coastguard Worker
sanity_check_usdt_elf(Elf * elf,const char * path)305*f7c14bbaSAndroid Build Coastguard Worker static int sanity_check_usdt_elf(Elf *elf, const char *path)
306*f7c14bbaSAndroid Build Coastguard Worker {
307*f7c14bbaSAndroid Build Coastguard Worker GElf_Ehdr ehdr;
308*f7c14bbaSAndroid Build Coastguard Worker int endianness;
309*f7c14bbaSAndroid Build Coastguard Worker
310*f7c14bbaSAndroid Build Coastguard Worker if (elf_kind(elf) != ELF_K_ELF) {
311*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized ELF kind %d for '%s'\n", elf_kind(elf), path);
312*f7c14bbaSAndroid Build Coastguard Worker return -EBADF;
313*f7c14bbaSAndroid Build Coastguard Worker }
314*f7c14bbaSAndroid Build Coastguard Worker
315*f7c14bbaSAndroid Build Coastguard Worker switch (gelf_getclass(elf)) {
316*f7c14bbaSAndroid Build Coastguard Worker case ELFCLASS64:
317*f7c14bbaSAndroid Build Coastguard Worker if (sizeof(void *) != 8) {
318*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: attaching to 64-bit ELF binary '%s' is not supported\n", path);
319*f7c14bbaSAndroid Build Coastguard Worker return -EBADF;
320*f7c14bbaSAndroid Build Coastguard Worker }
321*f7c14bbaSAndroid Build Coastguard Worker break;
322*f7c14bbaSAndroid Build Coastguard Worker case ELFCLASS32:
323*f7c14bbaSAndroid Build Coastguard Worker if (sizeof(void *) != 4) {
324*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: attaching to 32-bit ELF binary '%s' is not supported\n", path);
325*f7c14bbaSAndroid Build Coastguard Worker return -EBADF;
326*f7c14bbaSAndroid Build Coastguard Worker }
327*f7c14bbaSAndroid Build Coastguard Worker break;
328*f7c14bbaSAndroid Build Coastguard Worker default:
329*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unsupported ELF class for '%s'\n", path);
330*f7c14bbaSAndroid Build Coastguard Worker return -EBADF;
331*f7c14bbaSAndroid Build Coastguard Worker }
332*f7c14bbaSAndroid Build Coastguard Worker
333*f7c14bbaSAndroid Build Coastguard Worker if (!gelf_getehdr(elf, &ehdr))
334*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
335*f7c14bbaSAndroid Build Coastguard Worker
336*f7c14bbaSAndroid Build Coastguard Worker if (ehdr.e_type != ET_EXEC && ehdr.e_type != ET_DYN) {
337*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unsupported type of ELF binary '%s' (%d), only ET_EXEC and ET_DYN are supported\n",
338*f7c14bbaSAndroid Build Coastguard Worker path, ehdr.e_type);
339*f7c14bbaSAndroid Build Coastguard Worker return -EBADF;
340*f7c14bbaSAndroid Build Coastguard Worker }
341*f7c14bbaSAndroid Build Coastguard Worker
342*f7c14bbaSAndroid Build Coastguard Worker #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
343*f7c14bbaSAndroid Build Coastguard Worker endianness = ELFDATA2LSB;
344*f7c14bbaSAndroid Build Coastguard Worker #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
345*f7c14bbaSAndroid Build Coastguard Worker endianness = ELFDATA2MSB;
346*f7c14bbaSAndroid Build Coastguard Worker #else
347*f7c14bbaSAndroid Build Coastguard Worker # error "Unrecognized __BYTE_ORDER__"
348*f7c14bbaSAndroid Build Coastguard Worker #endif
349*f7c14bbaSAndroid Build Coastguard Worker if (endianness != ehdr.e_ident[EI_DATA]) {
350*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: ELF endianness mismatch for '%s'\n", path);
351*f7c14bbaSAndroid Build Coastguard Worker return -EBADF;
352*f7c14bbaSAndroid Build Coastguard Worker }
353*f7c14bbaSAndroid Build Coastguard Worker
354*f7c14bbaSAndroid Build Coastguard Worker return 0;
355*f7c14bbaSAndroid Build Coastguard Worker }
356*f7c14bbaSAndroid Build Coastguard Worker
find_elf_sec_by_name(Elf * elf,const char * sec_name,GElf_Shdr * shdr,Elf_Scn ** scn)357*f7c14bbaSAndroid Build Coastguard Worker static int find_elf_sec_by_name(Elf *elf, const char *sec_name, GElf_Shdr *shdr, Elf_Scn **scn)
358*f7c14bbaSAndroid Build Coastguard Worker {
359*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *sec = NULL;
360*f7c14bbaSAndroid Build Coastguard Worker size_t shstrndx;
361*f7c14bbaSAndroid Build Coastguard Worker
362*f7c14bbaSAndroid Build Coastguard Worker if (elf_getshdrstrndx(elf, &shstrndx))
363*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
364*f7c14bbaSAndroid Build Coastguard Worker
365*f7c14bbaSAndroid Build Coastguard Worker /* check if ELF is corrupted and avoid calling elf_strptr if yes */
366*f7c14bbaSAndroid Build Coastguard Worker if (!elf_rawdata(elf_getscn(elf, shstrndx), NULL))
367*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
368*f7c14bbaSAndroid Build Coastguard Worker
369*f7c14bbaSAndroid Build Coastguard Worker while ((sec = elf_nextscn(elf, sec)) != NULL) {
370*f7c14bbaSAndroid Build Coastguard Worker char *name;
371*f7c14bbaSAndroid Build Coastguard Worker
372*f7c14bbaSAndroid Build Coastguard Worker if (!gelf_getshdr(sec, shdr))
373*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
374*f7c14bbaSAndroid Build Coastguard Worker
375*f7c14bbaSAndroid Build Coastguard Worker name = elf_strptr(elf, shstrndx, shdr->sh_name);
376*f7c14bbaSAndroid Build Coastguard Worker if (name && strcmp(sec_name, name) == 0) {
377*f7c14bbaSAndroid Build Coastguard Worker *scn = sec;
378*f7c14bbaSAndroid Build Coastguard Worker return 0;
379*f7c14bbaSAndroid Build Coastguard Worker }
380*f7c14bbaSAndroid Build Coastguard Worker }
381*f7c14bbaSAndroid Build Coastguard Worker
382*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
383*f7c14bbaSAndroid Build Coastguard Worker }
384*f7c14bbaSAndroid Build Coastguard Worker
385*f7c14bbaSAndroid Build Coastguard Worker struct elf_seg {
386*f7c14bbaSAndroid Build Coastguard Worker long start;
387*f7c14bbaSAndroid Build Coastguard Worker long end;
388*f7c14bbaSAndroid Build Coastguard Worker long offset;
389*f7c14bbaSAndroid Build Coastguard Worker bool is_exec;
390*f7c14bbaSAndroid Build Coastguard Worker };
391*f7c14bbaSAndroid Build Coastguard Worker
cmp_elf_segs(const void * _a,const void * _b)392*f7c14bbaSAndroid Build Coastguard Worker static int cmp_elf_segs(const void *_a, const void *_b)
393*f7c14bbaSAndroid Build Coastguard Worker {
394*f7c14bbaSAndroid Build Coastguard Worker const struct elf_seg *a = _a;
395*f7c14bbaSAndroid Build Coastguard Worker const struct elf_seg *b = _b;
396*f7c14bbaSAndroid Build Coastguard Worker
397*f7c14bbaSAndroid Build Coastguard Worker return a->start < b->start ? -1 : 1;
398*f7c14bbaSAndroid Build Coastguard Worker }
399*f7c14bbaSAndroid Build Coastguard Worker
parse_elf_segs(Elf * elf,const char * path,struct elf_seg ** segs,size_t * seg_cnt)400*f7c14bbaSAndroid Build Coastguard Worker static int parse_elf_segs(Elf *elf, const char *path, struct elf_seg **segs, size_t *seg_cnt)
401*f7c14bbaSAndroid Build Coastguard Worker {
402*f7c14bbaSAndroid Build Coastguard Worker GElf_Phdr phdr;
403*f7c14bbaSAndroid Build Coastguard Worker size_t n;
404*f7c14bbaSAndroid Build Coastguard Worker int i, err;
405*f7c14bbaSAndroid Build Coastguard Worker struct elf_seg *seg;
406*f7c14bbaSAndroid Build Coastguard Worker void *tmp;
407*f7c14bbaSAndroid Build Coastguard Worker
408*f7c14bbaSAndroid Build Coastguard Worker *seg_cnt = 0;
409*f7c14bbaSAndroid Build Coastguard Worker
410*f7c14bbaSAndroid Build Coastguard Worker if (elf_getphdrnum(elf, &n)) {
411*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
412*f7c14bbaSAndroid Build Coastguard Worker return err;
413*f7c14bbaSAndroid Build Coastguard Worker }
414*f7c14bbaSAndroid Build Coastguard Worker
415*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < n; i++) {
416*f7c14bbaSAndroid Build Coastguard Worker if (!gelf_getphdr(elf, i, &phdr)) {
417*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
418*f7c14bbaSAndroid Build Coastguard Worker return err;
419*f7c14bbaSAndroid Build Coastguard Worker }
420*f7c14bbaSAndroid Build Coastguard Worker
421*f7c14bbaSAndroid Build Coastguard Worker pr_debug("usdt: discovered PHDR #%d in '%s': vaddr 0x%lx memsz 0x%lx offset 0x%lx type 0x%lx flags 0x%lx\n",
422*f7c14bbaSAndroid Build Coastguard Worker i, path, (long)phdr.p_vaddr, (long)phdr.p_memsz, (long)phdr.p_offset,
423*f7c14bbaSAndroid Build Coastguard Worker (long)phdr.p_type, (long)phdr.p_flags);
424*f7c14bbaSAndroid Build Coastguard Worker if (phdr.p_type != PT_LOAD)
425*f7c14bbaSAndroid Build Coastguard Worker continue;
426*f7c14bbaSAndroid Build Coastguard Worker
427*f7c14bbaSAndroid Build Coastguard Worker tmp = libbpf_reallocarray(*segs, *seg_cnt + 1, sizeof(**segs));
428*f7c14bbaSAndroid Build Coastguard Worker if (!tmp)
429*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
430*f7c14bbaSAndroid Build Coastguard Worker
431*f7c14bbaSAndroid Build Coastguard Worker *segs = tmp;
432*f7c14bbaSAndroid Build Coastguard Worker seg = *segs + *seg_cnt;
433*f7c14bbaSAndroid Build Coastguard Worker (*seg_cnt)++;
434*f7c14bbaSAndroid Build Coastguard Worker
435*f7c14bbaSAndroid Build Coastguard Worker seg->start = phdr.p_vaddr;
436*f7c14bbaSAndroid Build Coastguard Worker seg->end = phdr.p_vaddr + phdr.p_memsz;
437*f7c14bbaSAndroid Build Coastguard Worker seg->offset = phdr.p_offset;
438*f7c14bbaSAndroid Build Coastguard Worker seg->is_exec = phdr.p_flags & PF_X;
439*f7c14bbaSAndroid Build Coastguard Worker }
440*f7c14bbaSAndroid Build Coastguard Worker
441*f7c14bbaSAndroid Build Coastguard Worker if (*seg_cnt == 0) {
442*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to find PT_LOAD program headers in '%s'\n", path);
443*f7c14bbaSAndroid Build Coastguard Worker return -ESRCH;
444*f7c14bbaSAndroid Build Coastguard Worker }
445*f7c14bbaSAndroid Build Coastguard Worker
446*f7c14bbaSAndroid Build Coastguard Worker qsort(*segs, *seg_cnt, sizeof(**segs), cmp_elf_segs);
447*f7c14bbaSAndroid Build Coastguard Worker return 0;
448*f7c14bbaSAndroid Build Coastguard Worker }
449*f7c14bbaSAndroid Build Coastguard Worker
parse_vma_segs(int pid,const char * lib_path,struct elf_seg ** segs,size_t * seg_cnt)450*f7c14bbaSAndroid Build Coastguard Worker static int parse_vma_segs(int pid, const char *lib_path, struct elf_seg **segs, size_t *seg_cnt)
451*f7c14bbaSAndroid Build Coastguard Worker {
452*f7c14bbaSAndroid Build Coastguard Worker char path[PATH_MAX], line[PATH_MAX], mode[16];
453*f7c14bbaSAndroid Build Coastguard Worker size_t seg_start, seg_end, seg_off;
454*f7c14bbaSAndroid Build Coastguard Worker struct elf_seg *seg;
455*f7c14bbaSAndroid Build Coastguard Worker int tmp_pid, i, err;
456*f7c14bbaSAndroid Build Coastguard Worker FILE *f;
457*f7c14bbaSAndroid Build Coastguard Worker
458*f7c14bbaSAndroid Build Coastguard Worker *seg_cnt = 0;
459*f7c14bbaSAndroid Build Coastguard Worker
460*f7c14bbaSAndroid Build Coastguard Worker /* Handle containerized binaries only accessible from
461*f7c14bbaSAndroid Build Coastguard Worker * /proc/<pid>/root/<path>. They will be reported as just /<path> in
462*f7c14bbaSAndroid Build Coastguard Worker * /proc/<pid>/maps.
463*f7c14bbaSAndroid Build Coastguard Worker */
464*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(lib_path, "/proc/%d/root%s", &tmp_pid, path) == 2 && pid == tmp_pid)
465*f7c14bbaSAndroid Build Coastguard Worker goto proceed;
466*f7c14bbaSAndroid Build Coastguard Worker
467*f7c14bbaSAndroid Build Coastguard Worker if (!realpath(lib_path, path)) {
468*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to get absolute path of '%s' (err %d), using path as is...\n",
469*f7c14bbaSAndroid Build Coastguard Worker lib_path, -errno);
470*f7c14bbaSAndroid Build Coastguard Worker libbpf_strlcpy(path, lib_path, sizeof(path));
471*f7c14bbaSAndroid Build Coastguard Worker }
472*f7c14bbaSAndroid Build Coastguard Worker
473*f7c14bbaSAndroid Build Coastguard Worker proceed:
474*f7c14bbaSAndroid Build Coastguard Worker sprintf(line, "/proc/%d/maps", pid);
475*f7c14bbaSAndroid Build Coastguard Worker f = fopen(line, "re");
476*f7c14bbaSAndroid Build Coastguard Worker if (!f) {
477*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
478*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to open '%s' to get base addr of '%s': %d\n",
479*f7c14bbaSAndroid Build Coastguard Worker line, lib_path, err);
480*f7c14bbaSAndroid Build Coastguard Worker return err;
481*f7c14bbaSAndroid Build Coastguard Worker }
482*f7c14bbaSAndroid Build Coastguard Worker
483*f7c14bbaSAndroid Build Coastguard Worker /* We need to handle lines with no path at the end:
484*f7c14bbaSAndroid Build Coastguard Worker *
485*f7c14bbaSAndroid Build Coastguard Worker * 7f5c6f5d1000-7f5c6f5d3000 rw-p 001c7000 08:04 21238613 /usr/lib64/libc-2.17.so
486*f7c14bbaSAndroid Build Coastguard Worker * 7f5c6f5d3000-7f5c6f5d8000 rw-p 00000000 00:00 0
487*f7c14bbaSAndroid Build Coastguard Worker * 7f5c6f5d8000-7f5c6f5d9000 r-xp 00000000 103:01 362990598 /data/users/andriin/linux/tools/bpf/usdt/libhello_usdt.so
488*f7c14bbaSAndroid Build Coastguard Worker */
489*f7c14bbaSAndroid Build Coastguard Worker while (fscanf(f, "%zx-%zx %s %zx %*s %*d%[^\n]\n",
490*f7c14bbaSAndroid Build Coastguard Worker &seg_start, &seg_end, mode, &seg_off, line) == 5) {
491*f7c14bbaSAndroid Build Coastguard Worker void *tmp;
492*f7c14bbaSAndroid Build Coastguard Worker
493*f7c14bbaSAndroid Build Coastguard Worker /* to handle no path case (see above) we need to capture line
494*f7c14bbaSAndroid Build Coastguard Worker * without skipping any whitespaces. So we need to strip
495*f7c14bbaSAndroid Build Coastguard Worker * leading whitespaces manually here
496*f7c14bbaSAndroid Build Coastguard Worker */
497*f7c14bbaSAndroid Build Coastguard Worker i = 0;
498*f7c14bbaSAndroid Build Coastguard Worker while (isblank(line[i]))
499*f7c14bbaSAndroid Build Coastguard Worker i++;
500*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(line + i, path) != 0)
501*f7c14bbaSAndroid Build Coastguard Worker continue;
502*f7c14bbaSAndroid Build Coastguard Worker
503*f7c14bbaSAndroid Build Coastguard Worker pr_debug("usdt: discovered segment for lib '%s': addrs %zx-%zx mode %s offset %zx\n",
504*f7c14bbaSAndroid Build Coastguard Worker path, seg_start, seg_end, mode, seg_off);
505*f7c14bbaSAndroid Build Coastguard Worker
506*f7c14bbaSAndroid Build Coastguard Worker /* ignore non-executable sections for shared libs */
507*f7c14bbaSAndroid Build Coastguard Worker if (mode[2] != 'x')
508*f7c14bbaSAndroid Build Coastguard Worker continue;
509*f7c14bbaSAndroid Build Coastguard Worker
510*f7c14bbaSAndroid Build Coastguard Worker tmp = libbpf_reallocarray(*segs, *seg_cnt + 1, sizeof(**segs));
511*f7c14bbaSAndroid Build Coastguard Worker if (!tmp) {
512*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
513*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
514*f7c14bbaSAndroid Build Coastguard Worker }
515*f7c14bbaSAndroid Build Coastguard Worker
516*f7c14bbaSAndroid Build Coastguard Worker *segs = tmp;
517*f7c14bbaSAndroid Build Coastguard Worker seg = *segs + *seg_cnt;
518*f7c14bbaSAndroid Build Coastguard Worker *seg_cnt += 1;
519*f7c14bbaSAndroid Build Coastguard Worker
520*f7c14bbaSAndroid Build Coastguard Worker seg->start = seg_start;
521*f7c14bbaSAndroid Build Coastguard Worker seg->end = seg_end;
522*f7c14bbaSAndroid Build Coastguard Worker seg->offset = seg_off;
523*f7c14bbaSAndroid Build Coastguard Worker seg->is_exec = true;
524*f7c14bbaSAndroid Build Coastguard Worker }
525*f7c14bbaSAndroid Build Coastguard Worker
526*f7c14bbaSAndroid Build Coastguard Worker if (*seg_cnt == 0) {
527*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to find '%s' (resolved to '%s') within PID %d memory mappings\n",
528*f7c14bbaSAndroid Build Coastguard Worker lib_path, path, pid);
529*f7c14bbaSAndroid Build Coastguard Worker err = -ESRCH;
530*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
531*f7c14bbaSAndroid Build Coastguard Worker }
532*f7c14bbaSAndroid Build Coastguard Worker
533*f7c14bbaSAndroid Build Coastguard Worker qsort(*segs, *seg_cnt, sizeof(**segs), cmp_elf_segs);
534*f7c14bbaSAndroid Build Coastguard Worker err = 0;
535*f7c14bbaSAndroid Build Coastguard Worker err_out:
536*f7c14bbaSAndroid Build Coastguard Worker fclose(f);
537*f7c14bbaSAndroid Build Coastguard Worker return err;
538*f7c14bbaSAndroid Build Coastguard Worker }
539*f7c14bbaSAndroid Build Coastguard Worker
find_elf_seg(struct elf_seg * segs,size_t seg_cnt,long virtaddr)540*f7c14bbaSAndroid Build Coastguard Worker static struct elf_seg *find_elf_seg(struct elf_seg *segs, size_t seg_cnt, long virtaddr)
541*f7c14bbaSAndroid Build Coastguard Worker {
542*f7c14bbaSAndroid Build Coastguard Worker struct elf_seg *seg;
543*f7c14bbaSAndroid Build Coastguard Worker int i;
544*f7c14bbaSAndroid Build Coastguard Worker
545*f7c14bbaSAndroid Build Coastguard Worker /* for ELF binaries (both executables and shared libraries), we are
546*f7c14bbaSAndroid Build Coastguard Worker * given virtual address (absolute for executables, relative for
547*f7c14bbaSAndroid Build Coastguard Worker * libraries) which should match address range of [seg_start, seg_end)
548*f7c14bbaSAndroid Build Coastguard Worker */
549*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, seg = segs; i < seg_cnt; i++, seg++) {
550*f7c14bbaSAndroid Build Coastguard Worker if (seg->start <= virtaddr && virtaddr < seg->end)
551*f7c14bbaSAndroid Build Coastguard Worker return seg;
552*f7c14bbaSAndroid Build Coastguard Worker }
553*f7c14bbaSAndroid Build Coastguard Worker return NULL;
554*f7c14bbaSAndroid Build Coastguard Worker }
555*f7c14bbaSAndroid Build Coastguard Worker
find_vma_seg(struct elf_seg * segs,size_t seg_cnt,long offset)556*f7c14bbaSAndroid Build Coastguard Worker static struct elf_seg *find_vma_seg(struct elf_seg *segs, size_t seg_cnt, long offset)
557*f7c14bbaSAndroid Build Coastguard Worker {
558*f7c14bbaSAndroid Build Coastguard Worker struct elf_seg *seg;
559*f7c14bbaSAndroid Build Coastguard Worker int i;
560*f7c14bbaSAndroid Build Coastguard Worker
561*f7c14bbaSAndroid Build Coastguard Worker /* for VMA segments from /proc/<pid>/maps file, provided "address" is
562*f7c14bbaSAndroid Build Coastguard Worker * actually a file offset, so should be fall within logical
563*f7c14bbaSAndroid Build Coastguard Worker * offset-based range of [offset_start, offset_end)
564*f7c14bbaSAndroid Build Coastguard Worker */
565*f7c14bbaSAndroid Build Coastguard Worker for (i = 0, seg = segs; i < seg_cnt; i++, seg++) {
566*f7c14bbaSAndroid Build Coastguard Worker if (seg->offset <= offset && offset < seg->offset + (seg->end - seg->start))
567*f7c14bbaSAndroid Build Coastguard Worker return seg;
568*f7c14bbaSAndroid Build Coastguard Worker }
569*f7c14bbaSAndroid Build Coastguard Worker return NULL;
570*f7c14bbaSAndroid Build Coastguard Worker }
571*f7c14bbaSAndroid Build Coastguard Worker
572*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_note(Elf *elf, const char *path, GElf_Nhdr *nhdr,
573*f7c14bbaSAndroid Build Coastguard Worker const char *data, size_t name_off, size_t desc_off,
574*f7c14bbaSAndroid Build Coastguard Worker struct usdt_note *usdt_note);
575*f7c14bbaSAndroid Build Coastguard Worker
576*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_spec(struct usdt_spec *spec, const struct usdt_note *note, __u64 usdt_cookie);
577*f7c14bbaSAndroid Build Coastguard Worker
collect_usdt_targets(struct usdt_manager * man,Elf * elf,const char * path,pid_t pid,const char * usdt_provider,const char * usdt_name,__u64 usdt_cookie,struct usdt_target ** out_targets,size_t * out_target_cnt)578*f7c14bbaSAndroid Build Coastguard Worker static int collect_usdt_targets(struct usdt_manager *man, Elf *elf, const char *path, pid_t pid,
579*f7c14bbaSAndroid Build Coastguard Worker const char *usdt_provider, const char *usdt_name, __u64 usdt_cookie,
580*f7c14bbaSAndroid Build Coastguard Worker struct usdt_target **out_targets, size_t *out_target_cnt)
581*f7c14bbaSAndroid Build Coastguard Worker {
582*f7c14bbaSAndroid Build Coastguard Worker size_t off, name_off, desc_off, seg_cnt = 0, vma_seg_cnt = 0, target_cnt = 0;
583*f7c14bbaSAndroid Build Coastguard Worker struct elf_seg *segs = NULL, *vma_segs = NULL;
584*f7c14bbaSAndroid Build Coastguard Worker struct usdt_target *targets = NULL, *target;
585*f7c14bbaSAndroid Build Coastguard Worker long base_addr = 0;
586*f7c14bbaSAndroid Build Coastguard Worker Elf_Scn *notes_scn, *base_scn;
587*f7c14bbaSAndroid Build Coastguard Worker GElf_Shdr base_shdr, notes_shdr;
588*f7c14bbaSAndroid Build Coastguard Worker GElf_Ehdr ehdr;
589*f7c14bbaSAndroid Build Coastguard Worker GElf_Nhdr nhdr;
590*f7c14bbaSAndroid Build Coastguard Worker Elf_Data *data;
591*f7c14bbaSAndroid Build Coastguard Worker int err;
592*f7c14bbaSAndroid Build Coastguard Worker
593*f7c14bbaSAndroid Build Coastguard Worker *out_targets = NULL;
594*f7c14bbaSAndroid Build Coastguard Worker *out_target_cnt = 0;
595*f7c14bbaSAndroid Build Coastguard Worker
596*f7c14bbaSAndroid Build Coastguard Worker err = find_elf_sec_by_name(elf, USDT_NOTE_SEC, ¬es_shdr, ¬es_scn);
597*f7c14bbaSAndroid Build Coastguard Worker if (err) {
598*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: no USDT notes section (%s) found in '%s'\n", USDT_NOTE_SEC, path);
599*f7c14bbaSAndroid Build Coastguard Worker return err;
600*f7c14bbaSAndroid Build Coastguard Worker }
601*f7c14bbaSAndroid Build Coastguard Worker
602*f7c14bbaSAndroid Build Coastguard Worker if (notes_shdr.sh_type != SHT_NOTE || !gelf_getehdr(elf, &ehdr)) {
603*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: invalid USDT notes section (%s) in '%s'\n", USDT_NOTE_SEC, path);
604*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
605*f7c14bbaSAndroid Build Coastguard Worker }
606*f7c14bbaSAndroid Build Coastguard Worker
607*f7c14bbaSAndroid Build Coastguard Worker err = parse_elf_segs(elf, path, &segs, &seg_cnt);
608*f7c14bbaSAndroid Build Coastguard Worker if (err) {
609*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to process ELF program segments for '%s': %d\n", path, err);
610*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
611*f7c14bbaSAndroid Build Coastguard Worker }
612*f7c14bbaSAndroid Build Coastguard Worker
613*f7c14bbaSAndroid Build Coastguard Worker /* .stapsdt.base ELF section is optional, but is used for prelink
614*f7c14bbaSAndroid Build Coastguard Worker * offset compensation (see a big comment further below)
615*f7c14bbaSAndroid Build Coastguard Worker */
616*f7c14bbaSAndroid Build Coastguard Worker if (find_elf_sec_by_name(elf, USDT_BASE_SEC, &base_shdr, &base_scn) == 0)
617*f7c14bbaSAndroid Build Coastguard Worker base_addr = base_shdr.sh_addr;
618*f7c14bbaSAndroid Build Coastguard Worker
619*f7c14bbaSAndroid Build Coastguard Worker data = elf_getdata(notes_scn, 0);
620*f7c14bbaSAndroid Build Coastguard Worker off = 0;
621*f7c14bbaSAndroid Build Coastguard Worker while ((off = gelf_getnote(data, off, &nhdr, &name_off, &desc_off)) > 0) {
622*f7c14bbaSAndroid Build Coastguard Worker long usdt_abs_ip, usdt_rel_ip, usdt_sema_off = 0;
623*f7c14bbaSAndroid Build Coastguard Worker struct usdt_note note;
624*f7c14bbaSAndroid Build Coastguard Worker struct elf_seg *seg = NULL;
625*f7c14bbaSAndroid Build Coastguard Worker void *tmp;
626*f7c14bbaSAndroid Build Coastguard Worker
627*f7c14bbaSAndroid Build Coastguard Worker err = parse_usdt_note(elf, path, &nhdr, data->d_buf, name_off, desc_off, ¬e);
628*f7c14bbaSAndroid Build Coastguard Worker if (err)
629*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
630*f7c14bbaSAndroid Build Coastguard Worker
631*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(note.provider, usdt_provider) != 0 || strcmp(note.name, usdt_name) != 0)
632*f7c14bbaSAndroid Build Coastguard Worker continue;
633*f7c14bbaSAndroid Build Coastguard Worker
634*f7c14bbaSAndroid Build Coastguard Worker /* We need to compensate "prelink effect". See [0] for details,
635*f7c14bbaSAndroid Build Coastguard Worker * relevant parts quoted here:
636*f7c14bbaSAndroid Build Coastguard Worker *
637*f7c14bbaSAndroid Build Coastguard Worker * Each SDT probe also expands into a non-allocated ELF note. You can
638*f7c14bbaSAndroid Build Coastguard Worker * find this by looking at SHT_NOTE sections and decoding the format;
639*f7c14bbaSAndroid Build Coastguard Worker * see below for details. Because the note is non-allocated, it means
640*f7c14bbaSAndroid Build Coastguard Worker * there is no runtime cost, and also preserved in both stripped files
641*f7c14bbaSAndroid Build Coastguard Worker * and .debug files.
642*f7c14bbaSAndroid Build Coastguard Worker *
643*f7c14bbaSAndroid Build Coastguard Worker * However, this means that prelink won't adjust the note's contents
644*f7c14bbaSAndroid Build Coastguard Worker * for address offsets. Instead, this is done via the .stapsdt.base
645*f7c14bbaSAndroid Build Coastguard Worker * section. This is a special section that is added to the text. We
646*f7c14bbaSAndroid Build Coastguard Worker * will only ever have one of these sections in a final link and it
647*f7c14bbaSAndroid Build Coastguard Worker * will only ever be one byte long. Nothing about this section itself
648*f7c14bbaSAndroid Build Coastguard Worker * matters, we just use it as a marker to detect prelink address
649*f7c14bbaSAndroid Build Coastguard Worker * adjustments.
650*f7c14bbaSAndroid Build Coastguard Worker *
651*f7c14bbaSAndroid Build Coastguard Worker * Each probe note records the link-time address of the .stapsdt.base
652*f7c14bbaSAndroid Build Coastguard Worker * section alongside the probe PC address. The decoder compares the
653*f7c14bbaSAndroid Build Coastguard Worker * base address stored in the note with the .stapsdt.base section's
654*f7c14bbaSAndroid Build Coastguard Worker * sh_addr. Initially these are the same, but the section header will
655*f7c14bbaSAndroid Build Coastguard Worker * be adjusted by prelink. So the decoder applies the difference to
656*f7c14bbaSAndroid Build Coastguard Worker * the probe PC address to get the correct prelinked PC address; the
657*f7c14bbaSAndroid Build Coastguard Worker * same adjustment is applied to the semaphore address, if any.
658*f7c14bbaSAndroid Build Coastguard Worker *
659*f7c14bbaSAndroid Build Coastguard Worker * [0] https://sourceware.org/systemtap/wiki/UserSpaceProbeImplementation
660*f7c14bbaSAndroid Build Coastguard Worker */
661*f7c14bbaSAndroid Build Coastguard Worker usdt_abs_ip = note.loc_addr;
662*f7c14bbaSAndroid Build Coastguard Worker if (base_addr)
663*f7c14bbaSAndroid Build Coastguard Worker usdt_abs_ip += base_addr - note.base_addr;
664*f7c14bbaSAndroid Build Coastguard Worker
665*f7c14bbaSAndroid Build Coastguard Worker /* When attaching uprobes (which is what USDTs basically are)
666*f7c14bbaSAndroid Build Coastguard Worker * kernel expects file offset to be specified, not a relative
667*f7c14bbaSAndroid Build Coastguard Worker * virtual address, so we need to translate virtual address to
668*f7c14bbaSAndroid Build Coastguard Worker * file offset, for both ET_EXEC and ET_DYN binaries.
669*f7c14bbaSAndroid Build Coastguard Worker */
670*f7c14bbaSAndroid Build Coastguard Worker seg = find_elf_seg(segs, seg_cnt, usdt_abs_ip);
671*f7c14bbaSAndroid Build Coastguard Worker if (!seg) {
672*f7c14bbaSAndroid Build Coastguard Worker err = -ESRCH;
673*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to find ELF program segment for '%s:%s' in '%s' at IP 0x%lx\n",
674*f7c14bbaSAndroid Build Coastguard Worker usdt_provider, usdt_name, path, usdt_abs_ip);
675*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
676*f7c14bbaSAndroid Build Coastguard Worker }
677*f7c14bbaSAndroid Build Coastguard Worker if (!seg->is_exec) {
678*f7c14bbaSAndroid Build Coastguard Worker err = -ESRCH;
679*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: matched ELF binary '%s' segment [0x%lx, 0x%lx) for '%s:%s' at IP 0x%lx is not executable\n",
680*f7c14bbaSAndroid Build Coastguard Worker path, seg->start, seg->end, usdt_provider, usdt_name,
681*f7c14bbaSAndroid Build Coastguard Worker usdt_abs_ip);
682*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
683*f7c14bbaSAndroid Build Coastguard Worker }
684*f7c14bbaSAndroid Build Coastguard Worker /* translate from virtual address to file offset */
685*f7c14bbaSAndroid Build Coastguard Worker usdt_rel_ip = usdt_abs_ip - seg->start + seg->offset;
686*f7c14bbaSAndroid Build Coastguard Worker
687*f7c14bbaSAndroid Build Coastguard Worker if (ehdr.e_type == ET_DYN && !man->has_bpf_cookie) {
688*f7c14bbaSAndroid Build Coastguard Worker /* If we don't have BPF cookie support but need to
689*f7c14bbaSAndroid Build Coastguard Worker * attach to a shared library, we'll need to know and
690*f7c14bbaSAndroid Build Coastguard Worker * record absolute addresses of attach points due to
691*f7c14bbaSAndroid Build Coastguard Worker * the need to lookup USDT spec by absolute IP of
692*f7c14bbaSAndroid Build Coastguard Worker * triggered uprobe. Doing this resolution is only
693*f7c14bbaSAndroid Build Coastguard Worker * possible when we have a specific PID of the process
694*f7c14bbaSAndroid Build Coastguard Worker * that's using specified shared library. BPF cookie
695*f7c14bbaSAndroid Build Coastguard Worker * removes the absolute address limitation as we don't
696*f7c14bbaSAndroid Build Coastguard Worker * need to do this lookup (we just use BPF cookie as
697*f7c14bbaSAndroid Build Coastguard Worker * an index of USDT spec), so for newer kernels with
698*f7c14bbaSAndroid Build Coastguard Worker * BPF cookie support libbpf supports USDT attachment
699*f7c14bbaSAndroid Build Coastguard Worker * to shared libraries with no PID filter.
700*f7c14bbaSAndroid Build Coastguard Worker */
701*f7c14bbaSAndroid Build Coastguard Worker if (pid < 0) {
702*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: attaching to shared libraries without specific PID is not supported on current kernel\n");
703*f7c14bbaSAndroid Build Coastguard Worker err = -ENOTSUP;
704*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
705*f7c14bbaSAndroid Build Coastguard Worker }
706*f7c14bbaSAndroid Build Coastguard Worker
707*f7c14bbaSAndroid Build Coastguard Worker /* vma_segs are lazily initialized only if necessary */
708*f7c14bbaSAndroid Build Coastguard Worker if (vma_seg_cnt == 0) {
709*f7c14bbaSAndroid Build Coastguard Worker err = parse_vma_segs(pid, path, &vma_segs, &vma_seg_cnt);
710*f7c14bbaSAndroid Build Coastguard Worker if (err) {
711*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to get memory segments in PID %d for shared library '%s': %d\n",
712*f7c14bbaSAndroid Build Coastguard Worker pid, path, err);
713*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
714*f7c14bbaSAndroid Build Coastguard Worker }
715*f7c14bbaSAndroid Build Coastguard Worker }
716*f7c14bbaSAndroid Build Coastguard Worker
717*f7c14bbaSAndroid Build Coastguard Worker seg = find_vma_seg(vma_segs, vma_seg_cnt, usdt_rel_ip);
718*f7c14bbaSAndroid Build Coastguard Worker if (!seg) {
719*f7c14bbaSAndroid Build Coastguard Worker err = -ESRCH;
720*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to find shared lib memory segment for '%s:%s' in '%s' at relative IP 0x%lx\n",
721*f7c14bbaSAndroid Build Coastguard Worker usdt_provider, usdt_name, path, usdt_rel_ip);
722*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
723*f7c14bbaSAndroid Build Coastguard Worker }
724*f7c14bbaSAndroid Build Coastguard Worker
725*f7c14bbaSAndroid Build Coastguard Worker usdt_abs_ip = seg->start - seg->offset + usdt_rel_ip;
726*f7c14bbaSAndroid Build Coastguard Worker }
727*f7c14bbaSAndroid Build Coastguard Worker
728*f7c14bbaSAndroid Build Coastguard Worker pr_debug("usdt: probe for '%s:%s' in %s '%s': addr 0x%lx base 0x%lx (resolved abs_ip 0x%lx rel_ip 0x%lx) args '%s' in segment [0x%lx, 0x%lx) at offset 0x%lx\n",
729*f7c14bbaSAndroid Build Coastguard Worker usdt_provider, usdt_name, ehdr.e_type == ET_EXEC ? "exec" : "lib ", path,
730*f7c14bbaSAndroid Build Coastguard Worker note.loc_addr, note.base_addr, usdt_abs_ip, usdt_rel_ip, note.args,
731*f7c14bbaSAndroid Build Coastguard Worker seg ? seg->start : 0, seg ? seg->end : 0, seg ? seg->offset : 0);
732*f7c14bbaSAndroid Build Coastguard Worker
733*f7c14bbaSAndroid Build Coastguard Worker /* Adjust semaphore address to be a file offset */
734*f7c14bbaSAndroid Build Coastguard Worker if (note.sema_addr) {
735*f7c14bbaSAndroid Build Coastguard Worker if (!man->has_sema_refcnt) {
736*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: kernel doesn't support USDT semaphore refcounting for '%s:%s' in '%s'\n",
737*f7c14bbaSAndroid Build Coastguard Worker usdt_provider, usdt_name, path);
738*f7c14bbaSAndroid Build Coastguard Worker err = -ENOTSUP;
739*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
740*f7c14bbaSAndroid Build Coastguard Worker }
741*f7c14bbaSAndroid Build Coastguard Worker
742*f7c14bbaSAndroid Build Coastguard Worker seg = find_elf_seg(segs, seg_cnt, note.sema_addr);
743*f7c14bbaSAndroid Build Coastguard Worker if (!seg) {
744*f7c14bbaSAndroid Build Coastguard Worker err = -ESRCH;
745*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to find ELF loadable segment with semaphore of '%s:%s' in '%s' at 0x%lx\n",
746*f7c14bbaSAndroid Build Coastguard Worker usdt_provider, usdt_name, path, note.sema_addr);
747*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
748*f7c14bbaSAndroid Build Coastguard Worker }
749*f7c14bbaSAndroid Build Coastguard Worker if (seg->is_exec) {
750*f7c14bbaSAndroid Build Coastguard Worker err = -ESRCH;
751*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: matched ELF binary '%s' segment [0x%lx, 0x%lx] for semaphore of '%s:%s' at 0x%lx is executable\n",
752*f7c14bbaSAndroid Build Coastguard Worker path, seg->start, seg->end, usdt_provider, usdt_name,
753*f7c14bbaSAndroid Build Coastguard Worker note.sema_addr);
754*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
755*f7c14bbaSAndroid Build Coastguard Worker }
756*f7c14bbaSAndroid Build Coastguard Worker
757*f7c14bbaSAndroid Build Coastguard Worker usdt_sema_off = note.sema_addr - seg->start + seg->offset;
758*f7c14bbaSAndroid Build Coastguard Worker
759*f7c14bbaSAndroid Build Coastguard Worker pr_debug("usdt: sema for '%s:%s' in %s '%s': addr 0x%lx base 0x%lx (resolved 0x%lx) in segment [0x%lx, 0x%lx] at offset 0x%lx\n",
760*f7c14bbaSAndroid Build Coastguard Worker usdt_provider, usdt_name, ehdr.e_type == ET_EXEC ? "exec" : "lib ",
761*f7c14bbaSAndroid Build Coastguard Worker path, note.sema_addr, note.base_addr, usdt_sema_off,
762*f7c14bbaSAndroid Build Coastguard Worker seg->start, seg->end, seg->offset);
763*f7c14bbaSAndroid Build Coastguard Worker }
764*f7c14bbaSAndroid Build Coastguard Worker
765*f7c14bbaSAndroid Build Coastguard Worker /* Record adjusted addresses and offsets and parse USDT spec */
766*f7c14bbaSAndroid Build Coastguard Worker tmp = libbpf_reallocarray(targets, target_cnt + 1, sizeof(*targets));
767*f7c14bbaSAndroid Build Coastguard Worker if (!tmp) {
768*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
769*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
770*f7c14bbaSAndroid Build Coastguard Worker }
771*f7c14bbaSAndroid Build Coastguard Worker targets = tmp;
772*f7c14bbaSAndroid Build Coastguard Worker
773*f7c14bbaSAndroid Build Coastguard Worker target = &targets[target_cnt];
774*f7c14bbaSAndroid Build Coastguard Worker memset(target, 0, sizeof(*target));
775*f7c14bbaSAndroid Build Coastguard Worker
776*f7c14bbaSAndroid Build Coastguard Worker target->abs_ip = usdt_abs_ip;
777*f7c14bbaSAndroid Build Coastguard Worker target->rel_ip = usdt_rel_ip;
778*f7c14bbaSAndroid Build Coastguard Worker target->sema_off = usdt_sema_off;
779*f7c14bbaSAndroid Build Coastguard Worker
780*f7c14bbaSAndroid Build Coastguard Worker /* notes.args references strings from ELF itself, so they can
781*f7c14bbaSAndroid Build Coastguard Worker * be referenced safely until elf_end() call
782*f7c14bbaSAndroid Build Coastguard Worker */
783*f7c14bbaSAndroid Build Coastguard Worker target->spec_str = note.args;
784*f7c14bbaSAndroid Build Coastguard Worker
785*f7c14bbaSAndroid Build Coastguard Worker err = parse_usdt_spec(&target->spec, ¬e, usdt_cookie);
786*f7c14bbaSAndroid Build Coastguard Worker if (err)
787*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
788*f7c14bbaSAndroid Build Coastguard Worker
789*f7c14bbaSAndroid Build Coastguard Worker target_cnt++;
790*f7c14bbaSAndroid Build Coastguard Worker }
791*f7c14bbaSAndroid Build Coastguard Worker
792*f7c14bbaSAndroid Build Coastguard Worker *out_targets = targets;
793*f7c14bbaSAndroid Build Coastguard Worker *out_target_cnt = target_cnt;
794*f7c14bbaSAndroid Build Coastguard Worker err = target_cnt;
795*f7c14bbaSAndroid Build Coastguard Worker
796*f7c14bbaSAndroid Build Coastguard Worker err_out:
797*f7c14bbaSAndroid Build Coastguard Worker free(segs);
798*f7c14bbaSAndroid Build Coastguard Worker free(vma_segs);
799*f7c14bbaSAndroid Build Coastguard Worker if (err < 0)
800*f7c14bbaSAndroid Build Coastguard Worker free(targets);
801*f7c14bbaSAndroid Build Coastguard Worker return err;
802*f7c14bbaSAndroid Build Coastguard Worker }
803*f7c14bbaSAndroid Build Coastguard Worker
804*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_usdt {
805*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link link;
806*f7c14bbaSAndroid Build Coastguard Worker
807*f7c14bbaSAndroid Build Coastguard Worker struct usdt_manager *usdt_man;
808*f7c14bbaSAndroid Build Coastguard Worker
809*f7c14bbaSAndroid Build Coastguard Worker size_t spec_cnt;
810*f7c14bbaSAndroid Build Coastguard Worker int *spec_ids;
811*f7c14bbaSAndroid Build Coastguard Worker
812*f7c14bbaSAndroid Build Coastguard Worker size_t uprobe_cnt;
813*f7c14bbaSAndroid Build Coastguard Worker struct {
814*f7c14bbaSAndroid Build Coastguard Worker long abs_ip;
815*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *link;
816*f7c14bbaSAndroid Build Coastguard Worker } *uprobes;
817*f7c14bbaSAndroid Build Coastguard Worker
818*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *multi_link;
819*f7c14bbaSAndroid Build Coastguard Worker };
820*f7c14bbaSAndroid Build Coastguard Worker
bpf_link_usdt_detach(struct bpf_link * link)821*f7c14bbaSAndroid Build Coastguard Worker static int bpf_link_usdt_detach(struct bpf_link *link)
822*f7c14bbaSAndroid Build Coastguard Worker {
823*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_usdt *usdt_link = container_of(link, struct bpf_link_usdt, link);
824*f7c14bbaSAndroid Build Coastguard Worker struct usdt_manager *man = usdt_link->usdt_man;
825*f7c14bbaSAndroid Build Coastguard Worker int i;
826*f7c14bbaSAndroid Build Coastguard Worker
827*f7c14bbaSAndroid Build Coastguard Worker bpf_link__destroy(usdt_link->multi_link);
828*f7c14bbaSAndroid Build Coastguard Worker
829*f7c14bbaSAndroid Build Coastguard Worker /* When having multi_link, uprobe_cnt is 0 */
830*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < usdt_link->uprobe_cnt; i++) {
831*f7c14bbaSAndroid Build Coastguard Worker /* detach underlying uprobe link */
832*f7c14bbaSAndroid Build Coastguard Worker bpf_link__destroy(usdt_link->uprobes[i].link);
833*f7c14bbaSAndroid Build Coastguard Worker /* there is no need to update specs map because it will be
834*f7c14bbaSAndroid Build Coastguard Worker * unconditionally overwritten on subsequent USDT attaches,
835*f7c14bbaSAndroid Build Coastguard Worker * but if BPF cookies are not used we need to remove entry
836*f7c14bbaSAndroid Build Coastguard Worker * from ip_to_spec_id map, otherwise we'll run into false
837*f7c14bbaSAndroid Build Coastguard Worker * conflicting IP errors
838*f7c14bbaSAndroid Build Coastguard Worker */
839*f7c14bbaSAndroid Build Coastguard Worker if (!man->has_bpf_cookie) {
840*f7c14bbaSAndroid Build Coastguard Worker /* not much we can do about errors here */
841*f7c14bbaSAndroid Build Coastguard Worker (void)bpf_map_delete_elem(bpf_map__fd(man->ip_to_spec_id_map),
842*f7c14bbaSAndroid Build Coastguard Worker &usdt_link->uprobes[i].abs_ip);
843*f7c14bbaSAndroid Build Coastguard Worker }
844*f7c14bbaSAndroid Build Coastguard Worker }
845*f7c14bbaSAndroid Build Coastguard Worker
846*f7c14bbaSAndroid Build Coastguard Worker /* try to return the list of previously used spec IDs to usdt_manager
847*f7c14bbaSAndroid Build Coastguard Worker * for future reuse for subsequent USDT attaches
848*f7c14bbaSAndroid Build Coastguard Worker */
849*f7c14bbaSAndroid Build Coastguard Worker if (!man->free_spec_ids) {
850*f7c14bbaSAndroid Build Coastguard Worker /* if there were no free spec IDs yet, just transfer our IDs */
851*f7c14bbaSAndroid Build Coastguard Worker man->free_spec_ids = usdt_link->spec_ids;
852*f7c14bbaSAndroid Build Coastguard Worker man->free_spec_cnt = usdt_link->spec_cnt;
853*f7c14bbaSAndroid Build Coastguard Worker usdt_link->spec_ids = NULL;
854*f7c14bbaSAndroid Build Coastguard Worker } else {
855*f7c14bbaSAndroid Build Coastguard Worker /* otherwise concat IDs */
856*f7c14bbaSAndroid Build Coastguard Worker size_t new_cnt = man->free_spec_cnt + usdt_link->spec_cnt;
857*f7c14bbaSAndroid Build Coastguard Worker int *new_free_ids;
858*f7c14bbaSAndroid Build Coastguard Worker
859*f7c14bbaSAndroid Build Coastguard Worker new_free_ids = libbpf_reallocarray(man->free_spec_ids, new_cnt,
860*f7c14bbaSAndroid Build Coastguard Worker sizeof(*new_free_ids));
861*f7c14bbaSAndroid Build Coastguard Worker /* If we couldn't resize free_spec_ids, we'll just leak
862*f7c14bbaSAndroid Build Coastguard Worker * a bunch of free IDs; this is very unlikely to happen and if
863*f7c14bbaSAndroid Build Coastguard Worker * system is so exhausted on memory, it's the least of user's
864*f7c14bbaSAndroid Build Coastguard Worker * concerns, probably.
865*f7c14bbaSAndroid Build Coastguard Worker * So just do our best here to return those IDs to usdt_manager.
866*f7c14bbaSAndroid Build Coastguard Worker * Another edge case when we can legitimately get NULL is when
867*f7c14bbaSAndroid Build Coastguard Worker * new_cnt is zero, which can happen in some edge cases, so we
868*f7c14bbaSAndroid Build Coastguard Worker * need to be careful about that.
869*f7c14bbaSAndroid Build Coastguard Worker */
870*f7c14bbaSAndroid Build Coastguard Worker if (new_free_ids || new_cnt == 0) {
871*f7c14bbaSAndroid Build Coastguard Worker memcpy(new_free_ids + man->free_spec_cnt, usdt_link->spec_ids,
872*f7c14bbaSAndroid Build Coastguard Worker usdt_link->spec_cnt * sizeof(*usdt_link->spec_ids));
873*f7c14bbaSAndroid Build Coastguard Worker man->free_spec_ids = new_free_ids;
874*f7c14bbaSAndroid Build Coastguard Worker man->free_spec_cnt = new_cnt;
875*f7c14bbaSAndroid Build Coastguard Worker }
876*f7c14bbaSAndroid Build Coastguard Worker }
877*f7c14bbaSAndroid Build Coastguard Worker
878*f7c14bbaSAndroid Build Coastguard Worker return 0;
879*f7c14bbaSAndroid Build Coastguard Worker }
880*f7c14bbaSAndroid Build Coastguard Worker
bpf_link_usdt_dealloc(struct bpf_link * link)881*f7c14bbaSAndroid Build Coastguard Worker static void bpf_link_usdt_dealloc(struct bpf_link *link)
882*f7c14bbaSAndroid Build Coastguard Worker {
883*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_usdt *usdt_link = container_of(link, struct bpf_link_usdt, link);
884*f7c14bbaSAndroid Build Coastguard Worker
885*f7c14bbaSAndroid Build Coastguard Worker free(usdt_link->spec_ids);
886*f7c14bbaSAndroid Build Coastguard Worker free(usdt_link->uprobes);
887*f7c14bbaSAndroid Build Coastguard Worker free(usdt_link);
888*f7c14bbaSAndroid Build Coastguard Worker }
889*f7c14bbaSAndroid Build Coastguard Worker
specs_hash_fn(long key,void * ctx)890*f7c14bbaSAndroid Build Coastguard Worker static size_t specs_hash_fn(long key, void *ctx)
891*f7c14bbaSAndroid Build Coastguard Worker {
892*f7c14bbaSAndroid Build Coastguard Worker return str_hash((char *)key);
893*f7c14bbaSAndroid Build Coastguard Worker }
894*f7c14bbaSAndroid Build Coastguard Worker
specs_equal_fn(long key1,long key2,void * ctx)895*f7c14bbaSAndroid Build Coastguard Worker static bool specs_equal_fn(long key1, long key2, void *ctx)
896*f7c14bbaSAndroid Build Coastguard Worker {
897*f7c14bbaSAndroid Build Coastguard Worker return strcmp((char *)key1, (char *)key2) == 0;
898*f7c14bbaSAndroid Build Coastguard Worker }
899*f7c14bbaSAndroid Build Coastguard Worker
allocate_spec_id(struct usdt_manager * man,struct hashmap * specs_hash,struct bpf_link_usdt * link,struct usdt_target * target,int * spec_id,bool * is_new)900*f7c14bbaSAndroid Build Coastguard Worker static int allocate_spec_id(struct usdt_manager *man, struct hashmap *specs_hash,
901*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_usdt *link, struct usdt_target *target,
902*f7c14bbaSAndroid Build Coastguard Worker int *spec_id, bool *is_new)
903*f7c14bbaSAndroid Build Coastguard Worker {
904*f7c14bbaSAndroid Build Coastguard Worker long tmp;
905*f7c14bbaSAndroid Build Coastguard Worker void *new_ids;
906*f7c14bbaSAndroid Build Coastguard Worker int err;
907*f7c14bbaSAndroid Build Coastguard Worker
908*f7c14bbaSAndroid Build Coastguard Worker /* check if we already allocated spec ID for this spec string */
909*f7c14bbaSAndroid Build Coastguard Worker if (hashmap__find(specs_hash, target->spec_str, &tmp)) {
910*f7c14bbaSAndroid Build Coastguard Worker *spec_id = tmp;
911*f7c14bbaSAndroid Build Coastguard Worker *is_new = false;
912*f7c14bbaSAndroid Build Coastguard Worker return 0;
913*f7c14bbaSAndroid Build Coastguard Worker }
914*f7c14bbaSAndroid Build Coastguard Worker
915*f7c14bbaSAndroid Build Coastguard Worker /* otherwise it's a new ID that needs to be set up in specs map and
916*f7c14bbaSAndroid Build Coastguard Worker * returned back to usdt_manager when USDT link is detached
917*f7c14bbaSAndroid Build Coastguard Worker */
918*f7c14bbaSAndroid Build Coastguard Worker new_ids = libbpf_reallocarray(link->spec_ids, link->spec_cnt + 1, sizeof(*link->spec_ids));
919*f7c14bbaSAndroid Build Coastguard Worker if (!new_ids)
920*f7c14bbaSAndroid Build Coastguard Worker return -ENOMEM;
921*f7c14bbaSAndroid Build Coastguard Worker link->spec_ids = new_ids;
922*f7c14bbaSAndroid Build Coastguard Worker
923*f7c14bbaSAndroid Build Coastguard Worker /* get next free spec ID, giving preference to free list, if not empty */
924*f7c14bbaSAndroid Build Coastguard Worker if (man->free_spec_cnt) {
925*f7c14bbaSAndroid Build Coastguard Worker *spec_id = man->free_spec_ids[man->free_spec_cnt - 1];
926*f7c14bbaSAndroid Build Coastguard Worker
927*f7c14bbaSAndroid Build Coastguard Worker /* cache spec ID for current spec string for future lookups */
928*f7c14bbaSAndroid Build Coastguard Worker err = hashmap__add(specs_hash, target->spec_str, *spec_id);
929*f7c14bbaSAndroid Build Coastguard Worker if (err)
930*f7c14bbaSAndroid Build Coastguard Worker return err;
931*f7c14bbaSAndroid Build Coastguard Worker
932*f7c14bbaSAndroid Build Coastguard Worker man->free_spec_cnt--;
933*f7c14bbaSAndroid Build Coastguard Worker } else {
934*f7c14bbaSAndroid Build Coastguard Worker /* don't allocate spec ID bigger than what fits in specs map */
935*f7c14bbaSAndroid Build Coastguard Worker if (man->next_free_spec_id >= bpf_map__max_entries(man->specs_map))
936*f7c14bbaSAndroid Build Coastguard Worker return -E2BIG;
937*f7c14bbaSAndroid Build Coastguard Worker
938*f7c14bbaSAndroid Build Coastguard Worker *spec_id = man->next_free_spec_id;
939*f7c14bbaSAndroid Build Coastguard Worker
940*f7c14bbaSAndroid Build Coastguard Worker /* cache spec ID for current spec string for future lookups */
941*f7c14bbaSAndroid Build Coastguard Worker err = hashmap__add(specs_hash, target->spec_str, *spec_id);
942*f7c14bbaSAndroid Build Coastguard Worker if (err)
943*f7c14bbaSAndroid Build Coastguard Worker return err;
944*f7c14bbaSAndroid Build Coastguard Worker
945*f7c14bbaSAndroid Build Coastguard Worker man->next_free_spec_id++;
946*f7c14bbaSAndroid Build Coastguard Worker }
947*f7c14bbaSAndroid Build Coastguard Worker
948*f7c14bbaSAndroid Build Coastguard Worker /* remember new spec ID in the link for later return back to free list on detach */
949*f7c14bbaSAndroid Build Coastguard Worker link->spec_ids[link->spec_cnt] = *spec_id;
950*f7c14bbaSAndroid Build Coastguard Worker link->spec_cnt++;
951*f7c14bbaSAndroid Build Coastguard Worker *is_new = true;
952*f7c14bbaSAndroid Build Coastguard Worker return 0;
953*f7c14bbaSAndroid Build Coastguard Worker }
954*f7c14bbaSAndroid Build Coastguard Worker
usdt_manager_attach_usdt(struct usdt_manager * man,const struct bpf_program * prog,pid_t pid,const char * path,const char * usdt_provider,const char * usdt_name,__u64 usdt_cookie)955*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *usdt_manager_attach_usdt(struct usdt_manager *man, const struct bpf_program *prog,
956*f7c14bbaSAndroid Build Coastguard Worker pid_t pid, const char *path,
957*f7c14bbaSAndroid Build Coastguard Worker const char *usdt_provider, const char *usdt_name,
958*f7c14bbaSAndroid Build Coastguard Worker __u64 usdt_cookie)
959*f7c14bbaSAndroid Build Coastguard Worker {
960*f7c14bbaSAndroid Build Coastguard Worker unsigned long *offsets = NULL, *ref_ctr_offsets = NULL;
961*f7c14bbaSAndroid Build Coastguard Worker int i, err, spec_map_fd, ip_map_fd;
962*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_uprobe_opts, opts);
963*f7c14bbaSAndroid Build Coastguard Worker struct hashmap *specs_hash = NULL;
964*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link_usdt *link = NULL;
965*f7c14bbaSAndroid Build Coastguard Worker struct usdt_target *targets = NULL;
966*f7c14bbaSAndroid Build Coastguard Worker __u64 *cookies = NULL;
967*f7c14bbaSAndroid Build Coastguard Worker struct elf_fd elf_fd;
968*f7c14bbaSAndroid Build Coastguard Worker size_t target_cnt;
969*f7c14bbaSAndroid Build Coastguard Worker
970*f7c14bbaSAndroid Build Coastguard Worker spec_map_fd = bpf_map__fd(man->specs_map);
971*f7c14bbaSAndroid Build Coastguard Worker ip_map_fd = bpf_map__fd(man->ip_to_spec_id_map);
972*f7c14bbaSAndroid Build Coastguard Worker
973*f7c14bbaSAndroid Build Coastguard Worker err = elf_open(path, &elf_fd);
974*f7c14bbaSAndroid Build Coastguard Worker if (err)
975*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
976*f7c14bbaSAndroid Build Coastguard Worker
977*f7c14bbaSAndroid Build Coastguard Worker err = sanity_check_usdt_elf(elf_fd.elf, path);
978*f7c14bbaSAndroid Build Coastguard Worker if (err)
979*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
980*f7c14bbaSAndroid Build Coastguard Worker
981*f7c14bbaSAndroid Build Coastguard Worker /* normalize PID filter */
982*f7c14bbaSAndroid Build Coastguard Worker if (pid < 0)
983*f7c14bbaSAndroid Build Coastguard Worker pid = -1;
984*f7c14bbaSAndroid Build Coastguard Worker else if (pid == 0)
985*f7c14bbaSAndroid Build Coastguard Worker pid = getpid();
986*f7c14bbaSAndroid Build Coastguard Worker
987*f7c14bbaSAndroid Build Coastguard Worker /* discover USDT in given binary, optionally limiting
988*f7c14bbaSAndroid Build Coastguard Worker * activations to a given PID, if pid > 0
989*f7c14bbaSAndroid Build Coastguard Worker */
990*f7c14bbaSAndroid Build Coastguard Worker err = collect_usdt_targets(man, elf_fd.elf, path, pid, usdt_provider, usdt_name,
991*f7c14bbaSAndroid Build Coastguard Worker usdt_cookie, &targets, &target_cnt);
992*f7c14bbaSAndroid Build Coastguard Worker if (err <= 0) {
993*f7c14bbaSAndroid Build Coastguard Worker err = (err == 0) ? -ENOENT : err;
994*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
995*f7c14bbaSAndroid Build Coastguard Worker }
996*f7c14bbaSAndroid Build Coastguard Worker
997*f7c14bbaSAndroid Build Coastguard Worker specs_hash = hashmap__new(specs_hash_fn, specs_equal_fn, NULL);
998*f7c14bbaSAndroid Build Coastguard Worker if (IS_ERR(specs_hash)) {
999*f7c14bbaSAndroid Build Coastguard Worker err = PTR_ERR(specs_hash);
1000*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
1001*f7c14bbaSAndroid Build Coastguard Worker }
1002*f7c14bbaSAndroid Build Coastguard Worker
1003*f7c14bbaSAndroid Build Coastguard Worker link = calloc(1, sizeof(*link));
1004*f7c14bbaSAndroid Build Coastguard Worker if (!link) {
1005*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
1006*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
1007*f7c14bbaSAndroid Build Coastguard Worker }
1008*f7c14bbaSAndroid Build Coastguard Worker
1009*f7c14bbaSAndroid Build Coastguard Worker link->usdt_man = man;
1010*f7c14bbaSAndroid Build Coastguard Worker link->link.detach = &bpf_link_usdt_detach;
1011*f7c14bbaSAndroid Build Coastguard Worker link->link.dealloc = &bpf_link_usdt_dealloc;
1012*f7c14bbaSAndroid Build Coastguard Worker
1013*f7c14bbaSAndroid Build Coastguard Worker if (man->has_uprobe_multi) {
1014*f7c14bbaSAndroid Build Coastguard Worker offsets = calloc(target_cnt, sizeof(*offsets));
1015*f7c14bbaSAndroid Build Coastguard Worker cookies = calloc(target_cnt, sizeof(*cookies));
1016*f7c14bbaSAndroid Build Coastguard Worker ref_ctr_offsets = calloc(target_cnt, sizeof(*ref_ctr_offsets));
1017*f7c14bbaSAndroid Build Coastguard Worker
1018*f7c14bbaSAndroid Build Coastguard Worker if (!offsets || !ref_ctr_offsets || !cookies) {
1019*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
1020*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
1021*f7c14bbaSAndroid Build Coastguard Worker }
1022*f7c14bbaSAndroid Build Coastguard Worker } else {
1023*f7c14bbaSAndroid Build Coastguard Worker link->uprobes = calloc(target_cnt, sizeof(*link->uprobes));
1024*f7c14bbaSAndroid Build Coastguard Worker if (!link->uprobes) {
1025*f7c14bbaSAndroid Build Coastguard Worker err = -ENOMEM;
1026*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
1027*f7c14bbaSAndroid Build Coastguard Worker }
1028*f7c14bbaSAndroid Build Coastguard Worker }
1029*f7c14bbaSAndroid Build Coastguard Worker
1030*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < target_cnt; i++) {
1031*f7c14bbaSAndroid Build Coastguard Worker struct usdt_target *target = &targets[i];
1032*f7c14bbaSAndroid Build Coastguard Worker struct bpf_link *uprobe_link;
1033*f7c14bbaSAndroid Build Coastguard Worker bool is_new;
1034*f7c14bbaSAndroid Build Coastguard Worker int spec_id;
1035*f7c14bbaSAndroid Build Coastguard Worker
1036*f7c14bbaSAndroid Build Coastguard Worker /* Spec ID can be either reused or newly allocated. If it is
1037*f7c14bbaSAndroid Build Coastguard Worker * newly allocated, we'll need to fill out spec map, otherwise
1038*f7c14bbaSAndroid Build Coastguard Worker * entire spec should be valid and can be just used by a new
1039*f7c14bbaSAndroid Build Coastguard Worker * uprobe. We reuse spec when USDT arg spec is identical. We
1040*f7c14bbaSAndroid Build Coastguard Worker * also never share specs between two different USDT
1041*f7c14bbaSAndroid Build Coastguard Worker * attachments ("links"), so all the reused specs already
1042*f7c14bbaSAndroid Build Coastguard Worker * share USDT cookie value implicitly.
1043*f7c14bbaSAndroid Build Coastguard Worker */
1044*f7c14bbaSAndroid Build Coastguard Worker err = allocate_spec_id(man, specs_hash, link, target, &spec_id, &is_new);
1045*f7c14bbaSAndroid Build Coastguard Worker if (err)
1046*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
1047*f7c14bbaSAndroid Build Coastguard Worker
1048*f7c14bbaSAndroid Build Coastguard Worker if (is_new && bpf_map_update_elem(spec_map_fd, &spec_id, &target->spec, BPF_ANY)) {
1049*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
1050*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to set USDT spec #%d for '%s:%s' in '%s': %d\n",
1051*f7c14bbaSAndroid Build Coastguard Worker spec_id, usdt_provider, usdt_name, path, err);
1052*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
1053*f7c14bbaSAndroid Build Coastguard Worker }
1054*f7c14bbaSAndroid Build Coastguard Worker if (!man->has_bpf_cookie &&
1055*f7c14bbaSAndroid Build Coastguard Worker bpf_map_update_elem(ip_map_fd, &target->abs_ip, &spec_id, BPF_NOEXIST)) {
1056*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
1057*f7c14bbaSAndroid Build Coastguard Worker if (err == -EEXIST) {
1058*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: IP collision detected for spec #%d for '%s:%s' in '%s'\n",
1059*f7c14bbaSAndroid Build Coastguard Worker spec_id, usdt_provider, usdt_name, path);
1060*f7c14bbaSAndroid Build Coastguard Worker } else {
1061*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to map IP 0x%lx to spec #%d for '%s:%s' in '%s': %d\n",
1062*f7c14bbaSAndroid Build Coastguard Worker target->abs_ip, spec_id, usdt_provider, usdt_name,
1063*f7c14bbaSAndroid Build Coastguard Worker path, err);
1064*f7c14bbaSAndroid Build Coastguard Worker }
1065*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
1066*f7c14bbaSAndroid Build Coastguard Worker }
1067*f7c14bbaSAndroid Build Coastguard Worker
1068*f7c14bbaSAndroid Build Coastguard Worker if (man->has_uprobe_multi) {
1069*f7c14bbaSAndroid Build Coastguard Worker offsets[i] = target->rel_ip;
1070*f7c14bbaSAndroid Build Coastguard Worker ref_ctr_offsets[i] = target->sema_off;
1071*f7c14bbaSAndroid Build Coastguard Worker cookies[i] = spec_id;
1072*f7c14bbaSAndroid Build Coastguard Worker } else {
1073*f7c14bbaSAndroid Build Coastguard Worker opts.ref_ctr_offset = target->sema_off;
1074*f7c14bbaSAndroid Build Coastguard Worker opts.bpf_cookie = man->has_bpf_cookie ? spec_id : 0;
1075*f7c14bbaSAndroid Build Coastguard Worker uprobe_link = bpf_program__attach_uprobe_opts(prog, pid, path,
1076*f7c14bbaSAndroid Build Coastguard Worker target->rel_ip, &opts);
1077*f7c14bbaSAndroid Build Coastguard Worker err = libbpf_get_error(uprobe_link);
1078*f7c14bbaSAndroid Build Coastguard Worker if (err) {
1079*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to attach uprobe #%d for '%s:%s' in '%s': %d\n",
1080*f7c14bbaSAndroid Build Coastguard Worker i, usdt_provider, usdt_name, path, err);
1081*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
1082*f7c14bbaSAndroid Build Coastguard Worker }
1083*f7c14bbaSAndroid Build Coastguard Worker
1084*f7c14bbaSAndroid Build Coastguard Worker link->uprobes[i].link = uprobe_link;
1085*f7c14bbaSAndroid Build Coastguard Worker link->uprobes[i].abs_ip = target->abs_ip;
1086*f7c14bbaSAndroid Build Coastguard Worker link->uprobe_cnt++;
1087*f7c14bbaSAndroid Build Coastguard Worker }
1088*f7c14bbaSAndroid Build Coastguard Worker }
1089*f7c14bbaSAndroid Build Coastguard Worker
1090*f7c14bbaSAndroid Build Coastguard Worker if (man->has_uprobe_multi) {
1091*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_uprobe_multi_opts, opts_multi,
1092*f7c14bbaSAndroid Build Coastguard Worker .ref_ctr_offsets = ref_ctr_offsets,
1093*f7c14bbaSAndroid Build Coastguard Worker .offsets = offsets,
1094*f7c14bbaSAndroid Build Coastguard Worker .cookies = cookies,
1095*f7c14bbaSAndroid Build Coastguard Worker .cnt = target_cnt,
1096*f7c14bbaSAndroid Build Coastguard Worker );
1097*f7c14bbaSAndroid Build Coastguard Worker
1098*f7c14bbaSAndroid Build Coastguard Worker link->multi_link = bpf_program__attach_uprobe_multi(prog, pid, path,
1099*f7c14bbaSAndroid Build Coastguard Worker NULL, &opts_multi);
1100*f7c14bbaSAndroid Build Coastguard Worker if (!link->multi_link) {
1101*f7c14bbaSAndroid Build Coastguard Worker err = -errno;
1102*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: failed to attach uprobe multi for '%s:%s' in '%s': %d\n",
1103*f7c14bbaSAndroid Build Coastguard Worker usdt_provider, usdt_name, path, err);
1104*f7c14bbaSAndroid Build Coastguard Worker goto err_out;
1105*f7c14bbaSAndroid Build Coastguard Worker }
1106*f7c14bbaSAndroid Build Coastguard Worker
1107*f7c14bbaSAndroid Build Coastguard Worker free(offsets);
1108*f7c14bbaSAndroid Build Coastguard Worker free(ref_ctr_offsets);
1109*f7c14bbaSAndroid Build Coastguard Worker free(cookies);
1110*f7c14bbaSAndroid Build Coastguard Worker }
1111*f7c14bbaSAndroid Build Coastguard Worker
1112*f7c14bbaSAndroid Build Coastguard Worker free(targets);
1113*f7c14bbaSAndroid Build Coastguard Worker hashmap__free(specs_hash);
1114*f7c14bbaSAndroid Build Coastguard Worker elf_close(&elf_fd);
1115*f7c14bbaSAndroid Build Coastguard Worker return &link->link;
1116*f7c14bbaSAndroid Build Coastguard Worker
1117*f7c14bbaSAndroid Build Coastguard Worker err_out:
1118*f7c14bbaSAndroid Build Coastguard Worker free(offsets);
1119*f7c14bbaSAndroid Build Coastguard Worker free(ref_ctr_offsets);
1120*f7c14bbaSAndroid Build Coastguard Worker free(cookies);
1121*f7c14bbaSAndroid Build Coastguard Worker
1122*f7c14bbaSAndroid Build Coastguard Worker if (link)
1123*f7c14bbaSAndroid Build Coastguard Worker bpf_link__destroy(&link->link);
1124*f7c14bbaSAndroid Build Coastguard Worker free(targets);
1125*f7c14bbaSAndroid Build Coastguard Worker hashmap__free(specs_hash);
1126*f7c14bbaSAndroid Build Coastguard Worker elf_close(&elf_fd);
1127*f7c14bbaSAndroid Build Coastguard Worker return libbpf_err_ptr(err);
1128*f7c14bbaSAndroid Build Coastguard Worker }
1129*f7c14bbaSAndroid Build Coastguard Worker
1130*f7c14bbaSAndroid Build Coastguard Worker /* Parse out USDT ELF note from '.note.stapsdt' section.
1131*f7c14bbaSAndroid Build Coastguard Worker * Logic inspired by perf's code.
1132*f7c14bbaSAndroid Build Coastguard Worker */
parse_usdt_note(Elf * elf,const char * path,GElf_Nhdr * nhdr,const char * data,size_t name_off,size_t desc_off,struct usdt_note * note)1133*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_note(Elf *elf, const char *path, GElf_Nhdr *nhdr,
1134*f7c14bbaSAndroid Build Coastguard Worker const char *data, size_t name_off, size_t desc_off,
1135*f7c14bbaSAndroid Build Coastguard Worker struct usdt_note *note)
1136*f7c14bbaSAndroid Build Coastguard Worker {
1137*f7c14bbaSAndroid Build Coastguard Worker const char *provider, *name, *args;
1138*f7c14bbaSAndroid Build Coastguard Worker long addrs[3];
1139*f7c14bbaSAndroid Build Coastguard Worker size_t len;
1140*f7c14bbaSAndroid Build Coastguard Worker
1141*f7c14bbaSAndroid Build Coastguard Worker /* sanity check USDT note name and type first */
1142*f7c14bbaSAndroid Build Coastguard Worker if (strncmp(data + name_off, USDT_NOTE_NAME, nhdr->n_namesz) != 0)
1143*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1144*f7c14bbaSAndroid Build Coastguard Worker if (nhdr->n_type != USDT_NOTE_TYPE)
1145*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1146*f7c14bbaSAndroid Build Coastguard Worker
1147*f7c14bbaSAndroid Build Coastguard Worker /* sanity check USDT note contents ("description" in ELF terminology) */
1148*f7c14bbaSAndroid Build Coastguard Worker len = nhdr->n_descsz;
1149*f7c14bbaSAndroid Build Coastguard Worker data = data + desc_off;
1150*f7c14bbaSAndroid Build Coastguard Worker
1151*f7c14bbaSAndroid Build Coastguard Worker /* +3 is the very minimum required to store three empty strings */
1152*f7c14bbaSAndroid Build Coastguard Worker if (len < sizeof(addrs) + 3)
1153*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1154*f7c14bbaSAndroid Build Coastguard Worker
1155*f7c14bbaSAndroid Build Coastguard Worker /* get location, base, and semaphore addrs */
1156*f7c14bbaSAndroid Build Coastguard Worker memcpy(&addrs, data, sizeof(addrs));
1157*f7c14bbaSAndroid Build Coastguard Worker
1158*f7c14bbaSAndroid Build Coastguard Worker /* parse string fields: provider, name, args */
1159*f7c14bbaSAndroid Build Coastguard Worker provider = data + sizeof(addrs);
1160*f7c14bbaSAndroid Build Coastguard Worker
1161*f7c14bbaSAndroid Build Coastguard Worker name = (const char *)memchr(provider, '\0', data + len - provider);
1162*f7c14bbaSAndroid Build Coastguard Worker if (!name) /* non-zero-terminated provider */
1163*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1164*f7c14bbaSAndroid Build Coastguard Worker name++;
1165*f7c14bbaSAndroid Build Coastguard Worker if (name >= data + len || *name == '\0') /* missing or empty name */
1166*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1167*f7c14bbaSAndroid Build Coastguard Worker
1168*f7c14bbaSAndroid Build Coastguard Worker args = memchr(name, '\0', data + len - name);
1169*f7c14bbaSAndroid Build Coastguard Worker if (!args) /* non-zero-terminated name */
1170*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1171*f7c14bbaSAndroid Build Coastguard Worker ++args;
1172*f7c14bbaSAndroid Build Coastguard Worker if (args >= data + len) /* missing arguments spec */
1173*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1174*f7c14bbaSAndroid Build Coastguard Worker
1175*f7c14bbaSAndroid Build Coastguard Worker note->provider = provider;
1176*f7c14bbaSAndroid Build Coastguard Worker note->name = name;
1177*f7c14bbaSAndroid Build Coastguard Worker if (*args == '\0' || *args == ':')
1178*f7c14bbaSAndroid Build Coastguard Worker note->args = "";
1179*f7c14bbaSAndroid Build Coastguard Worker else
1180*f7c14bbaSAndroid Build Coastguard Worker note->args = args;
1181*f7c14bbaSAndroid Build Coastguard Worker note->loc_addr = addrs[0];
1182*f7c14bbaSAndroid Build Coastguard Worker note->base_addr = addrs[1];
1183*f7c14bbaSAndroid Build Coastguard Worker note->sema_addr = addrs[2];
1184*f7c14bbaSAndroid Build Coastguard Worker
1185*f7c14bbaSAndroid Build Coastguard Worker return 0;
1186*f7c14bbaSAndroid Build Coastguard Worker }
1187*f7c14bbaSAndroid Build Coastguard Worker
1188*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg, int *arg_sz);
1189*f7c14bbaSAndroid Build Coastguard Worker
parse_usdt_spec(struct usdt_spec * spec,const struct usdt_note * note,__u64 usdt_cookie)1190*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_spec(struct usdt_spec *spec, const struct usdt_note *note, __u64 usdt_cookie)
1191*f7c14bbaSAndroid Build Coastguard Worker {
1192*f7c14bbaSAndroid Build Coastguard Worker struct usdt_arg_spec *arg;
1193*f7c14bbaSAndroid Build Coastguard Worker const char *s;
1194*f7c14bbaSAndroid Build Coastguard Worker int arg_sz, len;
1195*f7c14bbaSAndroid Build Coastguard Worker
1196*f7c14bbaSAndroid Build Coastguard Worker spec->usdt_cookie = usdt_cookie;
1197*f7c14bbaSAndroid Build Coastguard Worker spec->arg_cnt = 0;
1198*f7c14bbaSAndroid Build Coastguard Worker
1199*f7c14bbaSAndroid Build Coastguard Worker s = note->args;
1200*f7c14bbaSAndroid Build Coastguard Worker while (s[0]) {
1201*f7c14bbaSAndroid Build Coastguard Worker if (spec->arg_cnt >= USDT_MAX_ARG_CNT) {
1202*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: too many USDT arguments (> %d) for '%s:%s' with args spec '%s'\n",
1203*f7c14bbaSAndroid Build Coastguard Worker USDT_MAX_ARG_CNT, note->provider, note->name, note->args);
1204*f7c14bbaSAndroid Build Coastguard Worker return -E2BIG;
1205*f7c14bbaSAndroid Build Coastguard Worker }
1206*f7c14bbaSAndroid Build Coastguard Worker
1207*f7c14bbaSAndroid Build Coastguard Worker arg = &spec->args[spec->arg_cnt];
1208*f7c14bbaSAndroid Build Coastguard Worker len = parse_usdt_arg(s, spec->arg_cnt, arg, &arg_sz);
1209*f7c14bbaSAndroid Build Coastguard Worker if (len < 0)
1210*f7c14bbaSAndroid Build Coastguard Worker return len;
1211*f7c14bbaSAndroid Build Coastguard Worker
1212*f7c14bbaSAndroid Build Coastguard Worker arg->arg_signed = arg_sz < 0;
1213*f7c14bbaSAndroid Build Coastguard Worker if (arg_sz < 0)
1214*f7c14bbaSAndroid Build Coastguard Worker arg_sz = -arg_sz;
1215*f7c14bbaSAndroid Build Coastguard Worker
1216*f7c14bbaSAndroid Build Coastguard Worker switch (arg_sz) {
1217*f7c14bbaSAndroid Build Coastguard Worker case 1: case 2: case 4: case 8:
1218*f7c14bbaSAndroid Build Coastguard Worker arg->arg_bitshift = 64 - arg_sz * 8;
1219*f7c14bbaSAndroid Build Coastguard Worker break;
1220*f7c14bbaSAndroid Build Coastguard Worker default:
1221*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unsupported arg #%d (spec '%s') size: %d\n",
1222*f7c14bbaSAndroid Build Coastguard Worker spec->arg_cnt, s, arg_sz);
1223*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1224*f7c14bbaSAndroid Build Coastguard Worker }
1225*f7c14bbaSAndroid Build Coastguard Worker
1226*f7c14bbaSAndroid Build Coastguard Worker s += len;
1227*f7c14bbaSAndroid Build Coastguard Worker spec->arg_cnt++;
1228*f7c14bbaSAndroid Build Coastguard Worker }
1229*f7c14bbaSAndroid Build Coastguard Worker
1230*f7c14bbaSAndroid Build Coastguard Worker return 0;
1231*f7c14bbaSAndroid Build Coastguard Worker }
1232*f7c14bbaSAndroid Build Coastguard Worker
1233*f7c14bbaSAndroid Build Coastguard Worker /* Architecture-specific logic for parsing USDT argument location specs */
1234*f7c14bbaSAndroid Build Coastguard Worker
1235*f7c14bbaSAndroid Build Coastguard Worker #if defined(__x86_64__) || defined(__i386__)
1236*f7c14bbaSAndroid Build Coastguard Worker
calc_pt_regs_off(const char * reg_name)1237*f7c14bbaSAndroid Build Coastguard Worker static int calc_pt_regs_off(const char *reg_name)
1238*f7c14bbaSAndroid Build Coastguard Worker {
1239*f7c14bbaSAndroid Build Coastguard Worker static struct {
1240*f7c14bbaSAndroid Build Coastguard Worker const char *names[4];
1241*f7c14bbaSAndroid Build Coastguard Worker size_t pt_regs_off;
1242*f7c14bbaSAndroid Build Coastguard Worker } reg_map[] = {
1243*f7c14bbaSAndroid Build Coastguard Worker #ifdef __x86_64__
1244*f7c14bbaSAndroid Build Coastguard Worker #define reg_off(reg64, reg32) offsetof(struct pt_regs, reg64)
1245*f7c14bbaSAndroid Build Coastguard Worker #else
1246*f7c14bbaSAndroid Build Coastguard Worker #define reg_off(reg64, reg32) offsetof(struct pt_regs, reg32)
1247*f7c14bbaSAndroid Build Coastguard Worker #endif
1248*f7c14bbaSAndroid Build Coastguard Worker { {"rip", "eip", "", ""}, reg_off(rip, eip) },
1249*f7c14bbaSAndroid Build Coastguard Worker { {"rax", "eax", "ax", "al"}, reg_off(rax, eax) },
1250*f7c14bbaSAndroid Build Coastguard Worker { {"rbx", "ebx", "bx", "bl"}, reg_off(rbx, ebx) },
1251*f7c14bbaSAndroid Build Coastguard Worker { {"rcx", "ecx", "cx", "cl"}, reg_off(rcx, ecx) },
1252*f7c14bbaSAndroid Build Coastguard Worker { {"rdx", "edx", "dx", "dl"}, reg_off(rdx, edx) },
1253*f7c14bbaSAndroid Build Coastguard Worker { {"rsi", "esi", "si", "sil"}, reg_off(rsi, esi) },
1254*f7c14bbaSAndroid Build Coastguard Worker { {"rdi", "edi", "di", "dil"}, reg_off(rdi, edi) },
1255*f7c14bbaSAndroid Build Coastguard Worker { {"rbp", "ebp", "bp", "bpl"}, reg_off(rbp, ebp) },
1256*f7c14bbaSAndroid Build Coastguard Worker { {"rsp", "esp", "sp", "spl"}, reg_off(rsp, esp) },
1257*f7c14bbaSAndroid Build Coastguard Worker #undef reg_off
1258*f7c14bbaSAndroid Build Coastguard Worker #ifdef __x86_64__
1259*f7c14bbaSAndroid Build Coastguard Worker { {"r8", "r8d", "r8w", "r8b"}, offsetof(struct pt_regs, r8) },
1260*f7c14bbaSAndroid Build Coastguard Worker { {"r9", "r9d", "r9w", "r9b"}, offsetof(struct pt_regs, r9) },
1261*f7c14bbaSAndroid Build Coastguard Worker { {"r10", "r10d", "r10w", "r10b"}, offsetof(struct pt_regs, r10) },
1262*f7c14bbaSAndroid Build Coastguard Worker { {"r11", "r11d", "r11w", "r11b"}, offsetof(struct pt_regs, r11) },
1263*f7c14bbaSAndroid Build Coastguard Worker { {"r12", "r12d", "r12w", "r12b"}, offsetof(struct pt_regs, r12) },
1264*f7c14bbaSAndroid Build Coastguard Worker { {"r13", "r13d", "r13w", "r13b"}, offsetof(struct pt_regs, r13) },
1265*f7c14bbaSAndroid Build Coastguard Worker { {"r14", "r14d", "r14w", "r14b"}, offsetof(struct pt_regs, r14) },
1266*f7c14bbaSAndroid Build Coastguard Worker { {"r15", "r15d", "r15w", "r15b"}, offsetof(struct pt_regs, r15) },
1267*f7c14bbaSAndroid Build Coastguard Worker #endif
1268*f7c14bbaSAndroid Build Coastguard Worker };
1269*f7c14bbaSAndroid Build Coastguard Worker int i, j;
1270*f7c14bbaSAndroid Build Coastguard Worker
1271*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(reg_map); i++) {
1272*f7c14bbaSAndroid Build Coastguard Worker for (j = 0; j < ARRAY_SIZE(reg_map[i].names); j++) {
1273*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(reg_name, reg_map[i].names[j]) == 0)
1274*f7c14bbaSAndroid Build Coastguard Worker return reg_map[i].pt_regs_off;
1275*f7c14bbaSAndroid Build Coastguard Worker }
1276*f7c14bbaSAndroid Build Coastguard Worker }
1277*f7c14bbaSAndroid Build Coastguard Worker
1278*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized register '%s'\n", reg_name);
1279*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
1280*f7c14bbaSAndroid Build Coastguard Worker }
1281*f7c14bbaSAndroid Build Coastguard Worker
parse_usdt_arg(const char * arg_str,int arg_num,struct usdt_arg_spec * arg,int * arg_sz)1282*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg, int *arg_sz)
1283*f7c14bbaSAndroid Build Coastguard Worker {
1284*f7c14bbaSAndroid Build Coastguard Worker char reg_name[16];
1285*f7c14bbaSAndroid Build Coastguard Worker int len, reg_off;
1286*f7c14bbaSAndroid Build Coastguard Worker long off;
1287*f7c14bbaSAndroid Build Coastguard Worker
1288*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n", arg_sz, &off, reg_name, &len) == 3) {
1289*f7c14bbaSAndroid Build Coastguard Worker /* Memory dereference case, e.g., -4@-20(%rbp) */
1290*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG_DEREF;
1291*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1292*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1293*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1294*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1295*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1296*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ ( %%%15[^)] ) %n", arg_sz, reg_name, &len) == 2) {
1297*f7c14bbaSAndroid Build Coastguard Worker /* Memory dereference case without offset, e.g., 8@(%rsp) */
1298*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG_DEREF;
1299*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = 0;
1300*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1301*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1302*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1303*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1304*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ %%%15s %n", arg_sz, reg_name, &len) == 2) {
1305*f7c14bbaSAndroid Build Coastguard Worker /* Register read case, e.g., -4@%eax */
1306*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG;
1307*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = 0;
1308*f7c14bbaSAndroid Build Coastguard Worker
1309*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1310*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1311*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1312*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1313*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ $%ld %n", arg_sz, &off, &len) == 2) {
1314*f7c14bbaSAndroid Build Coastguard Worker /* Constant value case, e.g., 4@$71 */
1315*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_CONST;
1316*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1317*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = 0;
1318*f7c14bbaSAndroid Build Coastguard Worker } else {
1319*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
1320*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1321*f7c14bbaSAndroid Build Coastguard Worker }
1322*f7c14bbaSAndroid Build Coastguard Worker
1323*f7c14bbaSAndroid Build Coastguard Worker return len;
1324*f7c14bbaSAndroid Build Coastguard Worker }
1325*f7c14bbaSAndroid Build Coastguard Worker
1326*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__s390x__)
1327*f7c14bbaSAndroid Build Coastguard Worker
1328*f7c14bbaSAndroid Build Coastguard Worker /* Do not support __s390__ for now, since user_pt_regs is broken with -m31. */
1329*f7c14bbaSAndroid Build Coastguard Worker
parse_usdt_arg(const char * arg_str,int arg_num,struct usdt_arg_spec * arg,int * arg_sz)1330*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg, int *arg_sz)
1331*f7c14bbaSAndroid Build Coastguard Worker {
1332*f7c14bbaSAndroid Build Coastguard Worker unsigned int reg;
1333*f7c14bbaSAndroid Build Coastguard Worker int len;
1334*f7c14bbaSAndroid Build Coastguard Worker long off;
1335*f7c14bbaSAndroid Build Coastguard Worker
1336*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(arg_str, " %d @ %ld ( %%r%u ) %n", arg_sz, &off, ®, &len) == 3) {
1337*f7c14bbaSAndroid Build Coastguard Worker /* Memory dereference case, e.g., -2@-28(%r15) */
1338*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG_DEREF;
1339*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1340*f7c14bbaSAndroid Build Coastguard Worker if (reg > 15) {
1341*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized register '%%r%u'\n", reg);
1342*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1343*f7c14bbaSAndroid Build Coastguard Worker }
1344*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = offsetof(user_pt_regs, gprs[reg]);
1345*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ %%r%u %n", arg_sz, ®, &len) == 2) {
1346*f7c14bbaSAndroid Build Coastguard Worker /* Register read case, e.g., -8@%r0 */
1347*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG;
1348*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = 0;
1349*f7c14bbaSAndroid Build Coastguard Worker if (reg > 15) {
1350*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized register '%%r%u'\n", reg);
1351*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1352*f7c14bbaSAndroid Build Coastguard Worker }
1353*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = offsetof(user_pt_regs, gprs[reg]);
1354*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ %ld %n", arg_sz, &off, &len) == 2) {
1355*f7c14bbaSAndroid Build Coastguard Worker /* Constant value case, e.g., 4@71 */
1356*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_CONST;
1357*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1358*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = 0;
1359*f7c14bbaSAndroid Build Coastguard Worker } else {
1360*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
1361*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1362*f7c14bbaSAndroid Build Coastguard Worker }
1363*f7c14bbaSAndroid Build Coastguard Worker
1364*f7c14bbaSAndroid Build Coastguard Worker return len;
1365*f7c14bbaSAndroid Build Coastguard Worker }
1366*f7c14bbaSAndroid Build Coastguard Worker
1367*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__aarch64__)
1368*f7c14bbaSAndroid Build Coastguard Worker
calc_pt_regs_off(const char * reg_name)1369*f7c14bbaSAndroid Build Coastguard Worker static int calc_pt_regs_off(const char *reg_name)
1370*f7c14bbaSAndroid Build Coastguard Worker {
1371*f7c14bbaSAndroid Build Coastguard Worker int reg_num;
1372*f7c14bbaSAndroid Build Coastguard Worker
1373*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(reg_name, "x%d", ®_num) == 1) {
1374*f7c14bbaSAndroid Build Coastguard Worker if (reg_num >= 0 && reg_num < 31)
1375*f7c14bbaSAndroid Build Coastguard Worker return offsetof(struct user_pt_regs, regs[reg_num]);
1376*f7c14bbaSAndroid Build Coastguard Worker } else if (strcmp(reg_name, "sp") == 0) {
1377*f7c14bbaSAndroid Build Coastguard Worker return offsetof(struct user_pt_regs, sp);
1378*f7c14bbaSAndroid Build Coastguard Worker }
1379*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized register '%s'\n", reg_name);
1380*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
1381*f7c14bbaSAndroid Build Coastguard Worker }
1382*f7c14bbaSAndroid Build Coastguard Worker
parse_usdt_arg(const char * arg_str,int arg_num,struct usdt_arg_spec * arg,int * arg_sz)1383*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg, int *arg_sz)
1384*f7c14bbaSAndroid Build Coastguard Worker {
1385*f7c14bbaSAndroid Build Coastguard Worker char reg_name[16];
1386*f7c14bbaSAndroid Build Coastguard Worker int len, reg_off;
1387*f7c14bbaSAndroid Build Coastguard Worker long off;
1388*f7c14bbaSAndroid Build Coastguard Worker
1389*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(arg_str, " %d @ \[ %15[a-z0-9] , %ld ] %n", arg_sz, reg_name, &off, &len) == 3) {
1390*f7c14bbaSAndroid Build Coastguard Worker /* Memory dereference case, e.g., -4@[sp, 96] */
1391*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG_DEREF;
1392*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1393*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1394*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1395*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1396*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1397*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ \[ %15[a-z0-9] ] %n", arg_sz, reg_name, &len) == 2) {
1398*f7c14bbaSAndroid Build Coastguard Worker /* Memory dereference case, e.g., -4@[sp] */
1399*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG_DEREF;
1400*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = 0;
1401*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1402*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1403*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1404*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1405*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ %ld %n", arg_sz, &off, &len) == 2) {
1406*f7c14bbaSAndroid Build Coastguard Worker /* Constant value case, e.g., 4@5 */
1407*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_CONST;
1408*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1409*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = 0;
1410*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ %15[a-z0-9] %n", arg_sz, reg_name, &len) == 2) {
1411*f7c14bbaSAndroid Build Coastguard Worker /* Register read case, e.g., -8@x4 */
1412*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG;
1413*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = 0;
1414*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1415*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1416*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1417*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1418*f7c14bbaSAndroid Build Coastguard Worker } else {
1419*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
1420*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1421*f7c14bbaSAndroid Build Coastguard Worker }
1422*f7c14bbaSAndroid Build Coastguard Worker
1423*f7c14bbaSAndroid Build Coastguard Worker return len;
1424*f7c14bbaSAndroid Build Coastguard Worker }
1425*f7c14bbaSAndroid Build Coastguard Worker
1426*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__riscv)
1427*f7c14bbaSAndroid Build Coastguard Worker
calc_pt_regs_off(const char * reg_name)1428*f7c14bbaSAndroid Build Coastguard Worker static int calc_pt_regs_off(const char *reg_name)
1429*f7c14bbaSAndroid Build Coastguard Worker {
1430*f7c14bbaSAndroid Build Coastguard Worker static struct {
1431*f7c14bbaSAndroid Build Coastguard Worker const char *name;
1432*f7c14bbaSAndroid Build Coastguard Worker size_t pt_regs_off;
1433*f7c14bbaSAndroid Build Coastguard Worker } reg_map[] = {
1434*f7c14bbaSAndroid Build Coastguard Worker { "ra", offsetof(struct user_regs_struct, ra) },
1435*f7c14bbaSAndroid Build Coastguard Worker { "sp", offsetof(struct user_regs_struct, sp) },
1436*f7c14bbaSAndroid Build Coastguard Worker { "gp", offsetof(struct user_regs_struct, gp) },
1437*f7c14bbaSAndroid Build Coastguard Worker { "tp", offsetof(struct user_regs_struct, tp) },
1438*f7c14bbaSAndroid Build Coastguard Worker { "a0", offsetof(struct user_regs_struct, a0) },
1439*f7c14bbaSAndroid Build Coastguard Worker { "a1", offsetof(struct user_regs_struct, a1) },
1440*f7c14bbaSAndroid Build Coastguard Worker { "a2", offsetof(struct user_regs_struct, a2) },
1441*f7c14bbaSAndroid Build Coastguard Worker { "a3", offsetof(struct user_regs_struct, a3) },
1442*f7c14bbaSAndroid Build Coastguard Worker { "a4", offsetof(struct user_regs_struct, a4) },
1443*f7c14bbaSAndroid Build Coastguard Worker { "a5", offsetof(struct user_regs_struct, a5) },
1444*f7c14bbaSAndroid Build Coastguard Worker { "a6", offsetof(struct user_regs_struct, a6) },
1445*f7c14bbaSAndroid Build Coastguard Worker { "a7", offsetof(struct user_regs_struct, a7) },
1446*f7c14bbaSAndroid Build Coastguard Worker { "s0", offsetof(struct user_regs_struct, s0) },
1447*f7c14bbaSAndroid Build Coastguard Worker { "s1", offsetof(struct user_regs_struct, s1) },
1448*f7c14bbaSAndroid Build Coastguard Worker { "s2", offsetof(struct user_regs_struct, s2) },
1449*f7c14bbaSAndroid Build Coastguard Worker { "s3", offsetof(struct user_regs_struct, s3) },
1450*f7c14bbaSAndroid Build Coastguard Worker { "s4", offsetof(struct user_regs_struct, s4) },
1451*f7c14bbaSAndroid Build Coastguard Worker { "s5", offsetof(struct user_regs_struct, s5) },
1452*f7c14bbaSAndroid Build Coastguard Worker { "s6", offsetof(struct user_regs_struct, s6) },
1453*f7c14bbaSAndroid Build Coastguard Worker { "s7", offsetof(struct user_regs_struct, s7) },
1454*f7c14bbaSAndroid Build Coastguard Worker { "s8", offsetof(struct user_regs_struct, rv_s8) },
1455*f7c14bbaSAndroid Build Coastguard Worker { "s9", offsetof(struct user_regs_struct, s9) },
1456*f7c14bbaSAndroid Build Coastguard Worker { "s10", offsetof(struct user_regs_struct, s10) },
1457*f7c14bbaSAndroid Build Coastguard Worker { "s11", offsetof(struct user_regs_struct, s11) },
1458*f7c14bbaSAndroid Build Coastguard Worker { "t0", offsetof(struct user_regs_struct, t0) },
1459*f7c14bbaSAndroid Build Coastguard Worker { "t1", offsetof(struct user_regs_struct, t1) },
1460*f7c14bbaSAndroid Build Coastguard Worker { "t2", offsetof(struct user_regs_struct, t2) },
1461*f7c14bbaSAndroid Build Coastguard Worker { "t3", offsetof(struct user_regs_struct, t3) },
1462*f7c14bbaSAndroid Build Coastguard Worker { "t4", offsetof(struct user_regs_struct, t4) },
1463*f7c14bbaSAndroid Build Coastguard Worker { "t5", offsetof(struct user_regs_struct, t5) },
1464*f7c14bbaSAndroid Build Coastguard Worker { "t6", offsetof(struct user_regs_struct, t6) },
1465*f7c14bbaSAndroid Build Coastguard Worker };
1466*f7c14bbaSAndroid Build Coastguard Worker int i;
1467*f7c14bbaSAndroid Build Coastguard Worker
1468*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(reg_map); i++) {
1469*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(reg_name, reg_map[i].name) == 0)
1470*f7c14bbaSAndroid Build Coastguard Worker return reg_map[i].pt_regs_off;
1471*f7c14bbaSAndroid Build Coastguard Worker }
1472*f7c14bbaSAndroid Build Coastguard Worker
1473*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized register '%s'\n", reg_name);
1474*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
1475*f7c14bbaSAndroid Build Coastguard Worker }
1476*f7c14bbaSAndroid Build Coastguard Worker
parse_usdt_arg(const char * arg_str,int arg_num,struct usdt_arg_spec * arg,int * arg_sz)1477*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg, int *arg_sz)
1478*f7c14bbaSAndroid Build Coastguard Worker {
1479*f7c14bbaSAndroid Build Coastguard Worker char reg_name[16];
1480*f7c14bbaSAndroid Build Coastguard Worker int len, reg_off;
1481*f7c14bbaSAndroid Build Coastguard Worker long off;
1482*f7c14bbaSAndroid Build Coastguard Worker
1483*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(arg_str, " %d @ %ld ( %15[a-z0-9] ) %n", arg_sz, &off, reg_name, &len) == 3) {
1484*f7c14bbaSAndroid Build Coastguard Worker /* Memory dereference case, e.g., -8@-88(s0) */
1485*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG_DEREF;
1486*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1487*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1488*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1489*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1490*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1491*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ %ld %n", arg_sz, &off, &len) == 2) {
1492*f7c14bbaSAndroid Build Coastguard Worker /* Constant value case, e.g., 4@5 */
1493*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_CONST;
1494*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1495*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = 0;
1496*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ %15[a-z0-9] %n", arg_sz, reg_name, &len) == 2) {
1497*f7c14bbaSAndroid Build Coastguard Worker /* Register read case, e.g., -8@a1 */
1498*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG;
1499*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = 0;
1500*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1501*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1502*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1503*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1504*f7c14bbaSAndroid Build Coastguard Worker } else {
1505*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
1506*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1507*f7c14bbaSAndroid Build Coastguard Worker }
1508*f7c14bbaSAndroid Build Coastguard Worker
1509*f7c14bbaSAndroid Build Coastguard Worker return len;
1510*f7c14bbaSAndroid Build Coastguard Worker }
1511*f7c14bbaSAndroid Build Coastguard Worker
1512*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__arm__)
1513*f7c14bbaSAndroid Build Coastguard Worker
calc_pt_regs_off(const char * reg_name)1514*f7c14bbaSAndroid Build Coastguard Worker static int calc_pt_regs_off(const char *reg_name)
1515*f7c14bbaSAndroid Build Coastguard Worker {
1516*f7c14bbaSAndroid Build Coastguard Worker static struct {
1517*f7c14bbaSAndroid Build Coastguard Worker const char *name;
1518*f7c14bbaSAndroid Build Coastguard Worker size_t pt_regs_off;
1519*f7c14bbaSAndroid Build Coastguard Worker } reg_map[] = {
1520*f7c14bbaSAndroid Build Coastguard Worker { "r0", offsetof(struct pt_regs, uregs[0]) },
1521*f7c14bbaSAndroid Build Coastguard Worker { "r1", offsetof(struct pt_regs, uregs[1]) },
1522*f7c14bbaSAndroid Build Coastguard Worker { "r2", offsetof(struct pt_regs, uregs[2]) },
1523*f7c14bbaSAndroid Build Coastguard Worker { "r3", offsetof(struct pt_regs, uregs[3]) },
1524*f7c14bbaSAndroid Build Coastguard Worker { "r4", offsetof(struct pt_regs, uregs[4]) },
1525*f7c14bbaSAndroid Build Coastguard Worker { "r5", offsetof(struct pt_regs, uregs[5]) },
1526*f7c14bbaSAndroid Build Coastguard Worker { "r6", offsetof(struct pt_regs, uregs[6]) },
1527*f7c14bbaSAndroid Build Coastguard Worker { "r7", offsetof(struct pt_regs, uregs[7]) },
1528*f7c14bbaSAndroid Build Coastguard Worker { "r8", offsetof(struct pt_regs, uregs[8]) },
1529*f7c14bbaSAndroid Build Coastguard Worker { "r9", offsetof(struct pt_regs, uregs[9]) },
1530*f7c14bbaSAndroid Build Coastguard Worker { "r10", offsetof(struct pt_regs, uregs[10]) },
1531*f7c14bbaSAndroid Build Coastguard Worker { "fp", offsetof(struct pt_regs, uregs[11]) },
1532*f7c14bbaSAndroid Build Coastguard Worker { "ip", offsetof(struct pt_regs, uregs[12]) },
1533*f7c14bbaSAndroid Build Coastguard Worker { "sp", offsetof(struct pt_regs, uregs[13]) },
1534*f7c14bbaSAndroid Build Coastguard Worker { "lr", offsetof(struct pt_regs, uregs[14]) },
1535*f7c14bbaSAndroid Build Coastguard Worker { "pc", offsetof(struct pt_regs, uregs[15]) },
1536*f7c14bbaSAndroid Build Coastguard Worker };
1537*f7c14bbaSAndroid Build Coastguard Worker int i;
1538*f7c14bbaSAndroid Build Coastguard Worker
1539*f7c14bbaSAndroid Build Coastguard Worker for (i = 0; i < ARRAY_SIZE(reg_map); i++) {
1540*f7c14bbaSAndroid Build Coastguard Worker if (strcmp(reg_name, reg_map[i].name) == 0)
1541*f7c14bbaSAndroid Build Coastguard Worker return reg_map[i].pt_regs_off;
1542*f7c14bbaSAndroid Build Coastguard Worker }
1543*f7c14bbaSAndroid Build Coastguard Worker
1544*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized register '%s'\n", reg_name);
1545*f7c14bbaSAndroid Build Coastguard Worker return -ENOENT;
1546*f7c14bbaSAndroid Build Coastguard Worker }
1547*f7c14bbaSAndroid Build Coastguard Worker
parse_usdt_arg(const char * arg_str,int arg_num,struct usdt_arg_spec * arg,int * arg_sz)1548*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg, int *arg_sz)
1549*f7c14bbaSAndroid Build Coastguard Worker {
1550*f7c14bbaSAndroid Build Coastguard Worker char reg_name[16];
1551*f7c14bbaSAndroid Build Coastguard Worker int len, reg_off;
1552*f7c14bbaSAndroid Build Coastguard Worker long off;
1553*f7c14bbaSAndroid Build Coastguard Worker
1554*f7c14bbaSAndroid Build Coastguard Worker if (sscanf(arg_str, " %d @ \[ %15[a-z0-9] , #%ld ] %n",
1555*f7c14bbaSAndroid Build Coastguard Worker arg_sz, reg_name, &off, &len) == 3) {
1556*f7c14bbaSAndroid Build Coastguard Worker /* Memory dereference case, e.g., -4@[fp, #96] */
1557*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG_DEREF;
1558*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1559*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1560*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1561*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1562*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1563*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ \[ %15[a-z0-9] ] %n", arg_sz, reg_name, &len) == 2) {
1564*f7c14bbaSAndroid Build Coastguard Worker /* Memory dereference case, e.g., -4@[sp] */
1565*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG_DEREF;
1566*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = 0;
1567*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1568*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1569*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1570*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1571*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ #%ld %n", arg_sz, &off, &len) == 2) {
1572*f7c14bbaSAndroid Build Coastguard Worker /* Constant value case, e.g., 4@#5 */
1573*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_CONST;
1574*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = off;
1575*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = 0;
1576*f7c14bbaSAndroid Build Coastguard Worker } else if (sscanf(arg_str, " %d @ %15[a-z0-9] %n", arg_sz, reg_name, &len) == 2) {
1577*f7c14bbaSAndroid Build Coastguard Worker /* Register read case, e.g., -8@r4 */
1578*f7c14bbaSAndroid Build Coastguard Worker arg->arg_type = USDT_ARG_REG;
1579*f7c14bbaSAndroid Build Coastguard Worker arg->val_off = 0;
1580*f7c14bbaSAndroid Build Coastguard Worker reg_off = calc_pt_regs_off(reg_name);
1581*f7c14bbaSAndroid Build Coastguard Worker if (reg_off < 0)
1582*f7c14bbaSAndroid Build Coastguard Worker return reg_off;
1583*f7c14bbaSAndroid Build Coastguard Worker arg->reg_off = reg_off;
1584*f7c14bbaSAndroid Build Coastguard Worker } else {
1585*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: unrecognized arg #%d spec '%s'\n", arg_num, arg_str);
1586*f7c14bbaSAndroid Build Coastguard Worker return -EINVAL;
1587*f7c14bbaSAndroid Build Coastguard Worker }
1588*f7c14bbaSAndroid Build Coastguard Worker
1589*f7c14bbaSAndroid Build Coastguard Worker return len;
1590*f7c14bbaSAndroid Build Coastguard Worker }
1591*f7c14bbaSAndroid Build Coastguard Worker
1592*f7c14bbaSAndroid Build Coastguard Worker #else
1593*f7c14bbaSAndroid Build Coastguard Worker
parse_usdt_arg(const char * arg_str,int arg_num,struct usdt_arg_spec * arg,int * arg_sz)1594*f7c14bbaSAndroid Build Coastguard Worker static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg, int *arg_sz)
1595*f7c14bbaSAndroid Build Coastguard Worker {
1596*f7c14bbaSAndroid Build Coastguard Worker pr_warn("usdt: libbpf doesn't support USDTs on current architecture\n");
1597*f7c14bbaSAndroid Build Coastguard Worker return -ENOTSUP;
1598*f7c14bbaSAndroid Build Coastguard Worker }
1599*f7c14bbaSAndroid Build Coastguard Worker
1600*f7c14bbaSAndroid Build Coastguard Worker #endif
1601