tools/lib/bpf/usdt.c
Source file repositories/reference/linux-study-clean/tools/lib/bpf/usdt.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/bpf/usdt.c- Extension
.c- Size
- 55202 bytes
- Lines
- 1697
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
ctype.hstdio.hstdlib.hstring.hlibelf.hgelf.hunistd.hlinux/ptrace.hlinux/kernel.hbpf.hlibbpf.hlibbpf_common.hlibbpf_internal.hhashmap.h
Detected Declarations
struct usdt_arg_specstruct usdt_specstruct usdt_notestruct usdt_targetstruct usdt_managerstruct elf_segstruct bpf_link_usdtenum usdt_arg_typefunction usdt_manager_freefunction sanity_check_usdt_elffunction find_elf_sec_by_namefunction cmp_elf_segsfunction parse_elf_segsfunction parse_vma_segsfunction has_nop_combofunction has_nop_combofunction collect_usdt_targetsfunction nop5function bpf_link_usdt_detachfunction bpf_link_usdt_deallocfunction specs_hash_fnfunction specs_equal_fnfunction allocate_spec_idfunction parse_usdt_notefunction parse_usdt_specfunction calc_pt_regs_offfunction parse_usdt_argfunction parse_usdt_argfunction calc_pt_regs_offfunction parse_usdt_argfunction calc_pt_regs_offfunction parse_usdt_argfunction calc_pt_regs_offfunction parse_usdt_argfunction parse_usdt_arg
Annotated Snippet
struct usdt_arg_spec {
__u64 val_off;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
enum usdt_arg_type arg_type: 8;
__u16 idx_reg_off: 12;
__u16 scale_bitshift: 4;
__u8 __reserved: 8; /* keep reg_off offset stable */
#else
__u8 __reserved: 8; /* keep reg_off offset stable */
__u16 idx_reg_off: 12;
__u16 scale_bitshift: 4;
enum usdt_arg_type arg_type: 8;
#endif
short reg_off;
bool arg_signed;
char arg_bitshift;
};
/* should match BPF_USDT_MAX_ARG_CNT in usdt.bpf.h */
#define USDT_MAX_ARG_CNT 12
/* should match struct __bpf_usdt_spec from usdt.bpf.h */
struct usdt_spec {
struct usdt_arg_spec args[USDT_MAX_ARG_CNT];
__u64 usdt_cookie;
short arg_cnt;
};
struct usdt_note {
const char *provider;
const char *name;
/* USDT args specification string, e.g.:
* "-4@%esi -4@-24(%rbp) -4@%ecx 2@%ax 8@%rdx"
*/
const char *args;
long loc_addr;
long base_addr;
long sema_addr;
};
struct usdt_target {
long abs_ip;
long rel_ip;
long sema_off;
struct usdt_spec spec;
const char *spec_str;
};
struct usdt_manager {
struct bpf_map *specs_map;
struct bpf_map *ip_to_spec_id_map;
int *free_spec_ids;
size_t free_spec_cnt;
size_t next_free_spec_id;
bool has_bpf_cookie;
bool has_sema_refcnt;
bool has_uprobe_multi;
bool has_uprobe_syscall;
};
struct usdt_manager *usdt_manager_new(struct bpf_object *obj)
{
static const char *ref_ctr_sysfs_path = "/sys/bus/event_source/devices/uprobe/format/ref_ctr_offset";
struct usdt_manager *man;
struct bpf_map *specs_map, *ip_to_spec_id_map;
specs_map = bpf_object__find_map_by_name(obj, "__bpf_usdt_specs");
ip_to_spec_id_map = bpf_object__find_map_by_name(obj, "__bpf_usdt_ip_to_spec_id");
if (!specs_map || !ip_to_spec_id_map) {
pr_warn("usdt: failed to find USDT support BPF maps, did you forget to include bpf/usdt.bpf.h?\n");
return ERR_PTR(-ESRCH);
}
man = calloc(1, sizeof(*man));
if (!man)
return ERR_PTR(-ENOMEM);
man->specs_map = specs_map;
man->ip_to_spec_id_map = ip_to_spec_id_map;
/* Detect if BPF cookie is supported for kprobes.
* We don't need IP-to-ID mapping if we can use BPF cookies.
* Added in: 7adfc6c9b315 ("bpf: Add bpf_get_attach_cookie() BPF helper to access bpf_cookie value")
*/
man->has_bpf_cookie = kernel_supports(obj, FEAT_BPF_COOKIE);
/* Detect kernel support for automatic refcounting of USDT semaphore.
* If this is not supported, USDTs with semaphores will not be supported.
Annotation
- Immediate include surface: `ctype.h`, `stdio.h`, `stdlib.h`, `string.h`, `libelf.h`, `gelf.h`, `unistd.h`, `linux/ptrace.h`.
- Detected declarations: `struct usdt_arg_spec`, `struct usdt_spec`, `struct usdt_note`, `struct usdt_target`, `struct usdt_manager`, `struct elf_seg`, `struct bpf_link_usdt`, `enum usdt_arg_type`, `function usdt_manager_free`, `function sanity_check_usdt_elf`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.