usdt.c 51 KB

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