libbpf 开发指南:查找具有给定名称和附加类型的内核 BTF(BPF Type Format)类型 ID

目录

函数原型与解释

代码demo

makefile

cmake

期望输出


函数原型与解释

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ym影子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值