目录
函数原型与解释
LIBBPF_API int libbpf_find_vmlinux_btf_id (const char *name, enum bpf_attach_type attach_type)
参数解释:
- name:表示 BPF 附加类型的字符串名称,例如 "kprobe"、"tracepoint" 等。
- attach_type:一个指向 enum bpf_attach_type 类型的指针,用于存储查找到的 BPF 附加类型。
返回值:
- 如果找到了与给定名称匹配的 BPF 附加类型,则返回 0。如果未找到匹配项,则返回 -1。
代码demo
#include <bpf/bpf_helpers.h> #include <linux/bpf.h> SEC("kprobe/__x64_sys_getpid") int bpf_prog1(struct pt_regs *ctx) { return 0; } char _license[] SEC("license") = "GPL";
#include <stdio.h> #include <bpf/libbpf.h> int main(void) { const char *type_name = "task_struct"; enum bpf_attach_type attach_type = BPF_PROG_TYPE_TRACEPOINT; int btf_id; btf_id = libbpf_find_vmlinux_btf_id(type_name, attach_type); if (btf_id < 0) { fprintf(stderr, "Error finding BTF ID: %d\n", btf_id); return 1; } printf("BTF ID for '%s': %d\n", type_name, btf_id); return 0; }
makefile
CC = gcc CFLAGS = -Wall -O2 LDLIBS = -lbpf all: find_btf_id find_btf_id: find_btf_id.o clean: rm -f find_btf_id find_btf_id.o
cmake
cmake_minimum_required(VERSION 3.10) project(find_btf_id C) set(CMAKE_C_STANDARD 99) add_executable(find_btf_id find_btf_id.c) target_link_libraries(find_btf_id bpf)
期望输出
BTF ID for 'task_struct': 141