|
|
243a81 |
From e9ae2826b8712d491362771233ed6bf21ae1a07a Mon Sep 17 00:00:00 2001
|
|
|
243a81 |
From: Jerome Marchand <jmarchan@redhat.com>
|
|
|
243a81 |
Date: Thu, 19 May 2022 16:37:40 +0200
|
|
|
243a81 |
Subject: [PATCH 3/3] libbpf: Allow kernel_struct_has_field to reach field in
|
|
|
243a81 |
unnamed struct or union
|
|
|
243a81 |
|
|
|
243a81 |
Some field can belong to unnamed struct or union. In C, they are
|
|
|
243a81 |
accessed as if their belong directly to the parent struct or union but
|
|
|
243a81 |
this is not the case for BTF.
|
|
|
243a81 |
|
|
|
243a81 |
When looking for a field, kernel_struct_has_field should also look
|
|
|
243a81 |
reccursively into unnamed structs or unions.
|
|
|
243a81 |
---
|
|
|
243a81 |
src/cc/libbpf.c | 28 ++++++++++++++++++----------
|
|
|
243a81 |
1 file changed, 18 insertions(+), 10 deletions(-)
|
|
|
243a81 |
|
|
|
243a81 |
diff --git a/src/cc/libbpf.c b/src/cc/libbpf.c
|
|
|
243a81 |
index 7410ae1a..e98b8852 100644
|
|
|
243a81 |
--- a/src/cc/libbpf.c
|
|
|
243a81 |
+++ b/src/cc/libbpf.c
|
|
|
243a81 |
@@ -1302,12 +1302,27 @@ bool bpf_has_kernel_btf(void)
|
|
|
243a81 |
return true;
|
|
|
243a81 |
}
|
|
|
243a81 |
|
|
|
243a81 |
+static int find_member_by_name(struct btf *btf, const struct btf_type *btf_type, const char *field_name) {
|
|
|
243a81 |
+ const struct btf_member *btf_member = btf_members(btf_type);
|
|
|
243a81 |
+ int i;
|
|
|
243a81 |
+
|
|
|
243a81 |
+ for (i = 0; i < btf_vlen(btf_type); i++, btf_member++) {
|
|
|
243a81 |
+ const char *name = btf__name_by_offset(btf, btf_member->name_off);
|
|
|
243a81 |
+ if (!strcmp(name, field_name)) {
|
|
|
243a81 |
+ return 1;
|
|
|
243a81 |
+ } else if (name[0] == '\0') {
|
|
|
243a81 |
+ if (find_member_by_name(btf, btf__type_by_id(btf, btf_member->type), field_name))
|
|
|
243a81 |
+ return 1;
|
|
|
243a81 |
+ }
|
|
|
243a81 |
+ }
|
|
|
243a81 |
+ return 0;
|
|
|
243a81 |
+}
|
|
|
243a81 |
+
|
|
|
243a81 |
int kernel_struct_has_field(const char *struct_name, const char *field_name)
|
|
|
243a81 |
{
|
|
|
243a81 |
const struct btf_type *btf_type;
|
|
|
243a81 |
- const struct btf_member *btf_member;
|
|
|
243a81 |
struct btf *btf;
|
|
|
243a81 |
- int i, ret, btf_id;
|
|
|
243a81 |
+ int ret, btf_id;
|
|
|
243a81 |
|
|
|
243a81 |
btf = libbpf_find_kernel_btf();
|
|
|
243a81 |
ret = libbpf_get_error(btf);
|
|
|
243a81 |
@@ -1321,14 +1336,7 @@ int kernel_struct_has_field(const char *struct_name, const char *field_name)
|
|
|
243a81 |
}
|
|
|
243a81 |
|
|
|
243a81 |
btf_type = btf__type_by_id(btf, btf_id);
|
|
|
243a81 |
- btf_member = btf_members(btf_type);
|
|
|
243a81 |
- for (i = 0; i < btf_vlen(btf_type); i++, btf_member++) {
|
|
|
243a81 |
- if (!strcmp(btf__name_by_offset(btf, btf_member->name_off), field_name)) {
|
|
|
243a81 |
- ret = 1;
|
|
|
243a81 |
- goto cleanup;
|
|
|
243a81 |
- }
|
|
|
243a81 |
- }
|
|
|
243a81 |
- ret = 0;
|
|
|
243a81 |
+ ret = find_member_by_name(btf, btf_type, field_name);
|
|
|
243a81 |
|
|
|
243a81 |
cleanup:
|
|
|
243a81 |
btf__free(btf);
|
|
|
243a81 |
--
|
|
|
243a81 |
2.35.3
|
|
|
243a81 |
|