mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
BTF-playground: Cannot get kernel BTF obj ID this way
Signed-off-by: Jesper Dangaard Brouer <netoptimizer@brouer.com>
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <bpf/bpf.h>
|
||||
#include <bpf/btf.h> /* Notice libbpf BTF include */
|
||||
|
||||
#include <linux/err.h>
|
||||
@@ -32,11 +33,42 @@ int print_all_levels(enum libbpf_print_level level,
|
||||
#define pr_err(fmt, ...) \
|
||||
fprintf(stderr, "%s:%d - " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
|
||||
|
||||
int fail1_get_kernel_btf_obj_id(struct btf *btf_obj)
|
||||
{
|
||||
/* *** DOES NOT WORK ***
|
||||
*
|
||||
* The struct btf returned from btf__load_module_btf() doesn't keep the
|
||||
* file descriptor open. Thus, we never reach bpf_obj_get_info_by_fd().
|
||||
*
|
||||
*/
|
||||
struct bpf_btf_info info;
|
||||
__u32 len = sizeof(info);
|
||||
int btf_fd;
|
||||
int err;
|
||||
|
||||
memset(&info, 0, sizeof(info));
|
||||
|
||||
btf_fd = btf__fd(btf_obj);
|
||||
if (btf_fd < 0) {
|
||||
printf("ERR: No FD(%d) in btf_obj:%p\n", btf_fd, btf_obj);
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = bpf_obj_get_info_by_fd(btf_fd, &info, &len); /* Privileged op */
|
||||
if (err) {
|
||||
printf("ERR(%d): Can't get BTF object info on FD(%d): %s\n",
|
||||
errno, btf_fd, strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
return info.id;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct btf *vmlinux_btf, *module_btf = NULL;
|
||||
int opt, longindex = 0;
|
||||
__u32 btf_obj_id;
|
||||
__s32 type_id;
|
||||
int err = 0;
|
||||
|
||||
@@ -78,8 +110,12 @@ int main(int argc, char **argv)
|
||||
goto out;
|
||||
}
|
||||
|
||||
printf("Module:%s Symbol:%s have BTF id:%d\n",
|
||||
module_name, symbol_name, type_id);
|
||||
/* Wanted to get BTF object ID used by kernel that ident BTF */
|
||||
//btf_obj_id = fail1_get_kernel_btf_obj_id(vmlinux_btf);
|
||||
btf_obj_id = fail1_get_kernel_btf_obj_id(module_btf);
|
||||
|
||||
printf("Module:%s (BTF-obj ID:%d) Symbol:%s have BTF type id:%d\n",
|
||||
module_name, btf_obj_id, symbol_name, type_id);
|
||||
|
||||
out:
|
||||
btf__free(module_btf);
|
||||
|
Reference in New Issue
Block a user