bpftool: Fix handling enum64 in btf dump sorting
Mykyta Yatsenko [email protected]
PATCH 1/1 bpftool: Fix handling enum64 in btf dump sorting
From: Mykyta Yatsenko <[email protected]>
Wrong function is used to access the first enum64 element. Substituting btf_enum(t) with btf_enum64(t) for BTF_KIND_ENUM64.
--- tools/bpf/bpftool/btf.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
tools/bpf/bpftool/btf.c +10 -3
@@ -557,16 +557,23 @@ static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool fconst struct btf_type *t =btf__type_by_id (btf, index);switch (btf_kind (t)) { -case BTF_KIND_ENUM : -case BTF_KIND_ENUM64 : { +case BTF_KIND_ENUM : {int name_off = t->name_off;/* Use name of the first element for anonymous enums if allowed */ -if (!from_ref && !t->name_off &&btf_vlen (t)) +if (!from_ref && !name_off &&btf_vlen (t)) name_off =btf_enum (t)->name_off;return btf__name_by_offset (btf, name_off); } +case BTF_KIND_ENUM64 : { +int name_off = t->name_off; + +if (!from_ref && !name_off &&btf_vlen (t)) + name_off =btf_enum64 (t)->name_off; + +return btf__name_by_offset (btf, name_off); + }case BTF_KIND_ARRAY :return btf_type_sort_name (btf,btf_array (t)->type, true);case BTF_KIND_TYPE_TAG :
Signature · 1 line
2.46.0
Daniel Borkmann
maintainerQuoted 20 lines · On 9/1/24 11:30 PM, "Mykyta Yatsenko mykyta.yatsenko5"@gmail.com wrote:
From: Mykyta Yatsenko <[email protected]>
Wrong function is used to access the first enum64 element. Substituting
btf_enum(t)withbtf_enum64(t)forBTF_KIND_ENUM64.Signed-off-by Mykyta Yatsenko <[email protected]>--- tools/bpf/bpftool/btf.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)tools/bpf/bpftool/btf.c +0
-2@@ -557,16 +557,23 @@ static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool fconst struct btf_type *t =btf__type_by_id (btf, index);switch (btf_kind (t)) { -case BTF_KIND_ENUM : -case BTF_KIND_ENUM64 : {
tools/bpf/bpftool/btf.c static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool f · line 568
Quoted 10 lines
tools/bpf/bpftool/btf.c +2
-1+case BTF_KIND_ENUM : {int name_off = t->name_off;/* Use name of the first element for anonymous enums if allowed */ -if (!from_ref && !t->name_off &&btf_vlen (t)) +if (!from_ref && !name_off &&btf_vlen (t)) name_off =btf_enum (t)->name_off;return btf__name_by_offset (btf, name_off); }
Small nit, could we consolidate the logic into the below? Still somewhat nicer than duplicating all of the rest.
tools/bpf/bpftool/btf.c +4 -2
@@ -562,8 +562,10 @@ static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool fint name_off = t->name_off;/* Use name of the first element for anonymous enums if allowed */ -if (!from_ref && !t->name_off &&btf_vlen (t)) - name_off =btf_enum (t)->name_off; +if (!from_ref && !name_off &&btf_vlen (t)) + name_off =btf_kind (t) ==BTF_KIND_ENUM64 ? +btf_enum64 (t)->name_off : +btf_enum (t)->name_off;return btf__name_by_offset (btf, name_off); }
Thanks,
Daniel
Quoted 24 lines · Daniel Borkmann
Quoted 23 lines · On 9/1/24 11:30 PM, "Mykyta Yatsenko mykyta.yatsenko5"@gmail.com wrote:
From: Mykyta Yatsenko <[email protected]>
Wrong function is used to access the first enum64 element. Substituting
btf_enum(t)withbtf_enum64(t)forBTF_KIND_ENUM64.Signed-off-by Mykyta Yatsenko <[email protected]>---
tools/bpf/bpftool/btf.c| 13 ++++++++++---1 file changed, 10 insertions(+), 3 deletions(-)tools/bpf/bpftool/btf.c +0
-0@@ -557,16 +557,23 @@ static const char *btf_type_sort_name(conststruct btf *btf,
__u32index, bool f
const structbtf_type*t =btf__type_by_id(btf, index); switch (btf_kind(t)) {
- caseBTF_KIND_ENUM:
- caseBTF_KIND_ENUM64: {
+ caseBTF_KIND_ENUM: {
intname_off=t->name_off;
/* Use name of the first element for anonymous enums if allowed */
Quoted 8 lines
Quoted 5 lines
- if (!from_ref && !t->name_off &&
btf_vlen(t)) + if (!from_ref && !name_off &&btf_vlen(t))name_off= btf_enum(t)->name_off; returnbtf__name_by_offset(btf, name_off); }Small nit, could we consolidate the logic into the below? Still somewhat nicer than duplicating all of the rest.
Thanks for the suggestion, this makes sense, I will apply it.
Quoted 23 lines
tools/bpf/bpftool/btf.c +0
-0@@ -562,8 +562,10 @@ static const char *btf_type_sort_name(conststruct btf *btf,
__u32index, bool f
intname_off=t->name_off;/* Use name of the first element for anonymous enums if allowed */ - if (!from_ref && !t->name_off &&
btf_vlen(t))
-name_off= btf_enum(t)->name_off;
+ if (!from_ref && !name_off &&btf_vlen(t))
+name_off=btf_kind(t)==BTF_KIND_ENUM64?
+ btf_enum64(t)->name_off :
+ btf_enum(t)->name_off;return
btf__name_by_offset(btf, name_off); }Thanks,
Daniel
Quoted 41 lines · Daniel Borkmann
Quoted 20 lines · On 9/1/24 11:30 PM, "Mykyta Yatsenko mykyta.yatsenko5"@gmail.com wrote:
From: Mykyta Yatsenko <[email protected]>
Wrong function is used to access the first enum64 element. Substituting
btf_enum(t)withbtf_enum64(t)forBTF_KIND_ENUM64.Signed-off-by Mykyta Yatsenko <[email protected]>--- tools/bpf/bpftool/btf.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)tools/bpf/bpftool/btf.c +0
-2@@ -557,16 +557,23 @@ static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool fconst struct btf_type *t =btf__type_by_id (btf, index);switch (btf_kind (t)) { -case BTF_KIND_ENUM : -case BTF_KIND_ENUM64 : {tools/bpf/bpftool/btf.c static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool f · line 568
Quoted 10 lines
tools/bpf/bpftool/btf.c +2
-1+case BTF_KIND_ENUM : {int name_off = t->name_off;/* Use name of the first element for anonymous enums if allowed */ -if (!from_ref && !t->name_off &&btf_vlen (t)) +if (!from_ref && !name_off &&btf_vlen (t)) name_off =btf_enum (t)->name_off;return btf__name_by_offset (btf, name_off); }Small nit, could we consolidate the logic into the below? Still somewhat nicer than duplicating all of the rest.
tools/bpf/bpftool/btf.c +0
-0
tools/bpf/bpftool/btf.c static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool f · line 566
Quoted 7 lines
tools/bpf/bpftool/btf.c +2
-2@@ -562,8 +562,10 @@ static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool fint name_off = t->name_off;/* Use name of the first element for anonymous enums if allowed */ -if (!from_ref && !t->name_off &&btf_vlen (t)) - name_off =btf_enum (t)->name_off; +if (!from_ref && !name_off &&btf_vlen (t)) + name_off = btf_kind(t) == BTF_KIND_ENUM64 ?
Just fyi for the future (I don't think we need to fix this or anything like that), but using BTF_KIND_xxx constants in btf_kind(t) comparisons should be rare. Libbpf provides a full set of shorter btf_is_xxx(t) helpers for this. So this would be just btf_is_enum64(t). What you did is not wrong, it's just more open-coded and verbose.
Quoted 8 lines
tools/bpf/bpftool/btf.c +2
-0+ btf_enum64(t)->name_off : +btf_enum (t)->name_off;return btf__name_by_offset (btf, name_off); }Thanks,
Daniel
Daniel Borkmann
maintainerQuoted 44 lines · Andrii Nakryiko
Quoted 43 lines · Daniel Borkmann
Quoted 20 lines · On 9/1/24 11:30 PM, "Mykyta Yatsenko mykyta.yatsenko5"@gmail.com wrote:
From: Mykyta Yatsenko <[email protected]>
Wrong function is used to access the first enum64 element. Substituting
btf_enum(t)withbtf_enum64(t)forBTF_KIND_ENUM64.Signed-off-by Mykyta Yatsenko <[email protected]>--- tools/bpf/bpftool/btf.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)tools/bpf/bpftool/btf.c +0
-2@@ -557,16 +557,23 @@ static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool fconst struct btf_type *t =btf__type_by_id (btf, index);switch (btf_kind (t)) { -case BTF_KIND_ENUM : -case BTF_KIND_ENUM64 : {tools/bpf/bpftool/btf.c static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool f · line 568
Quoted 10 lines
tools/bpf/bpftool/btf.c +2
-1+case BTF_KIND_ENUM : {int name_off = t->name_off;/* Use name of the first element for anonymous enums if allowed */ -if (!from_ref && !t->name_off &&btf_vlen (t)) +if (!from_ref && !name_off &&btf_vlen (t)) name_off =btf_enum (t)->name_off;return btf__name_by_offset (btf, name_off); }Small nit, could we consolidate the logic into the below? Still somewhat nicer than duplicating all of the rest.
tools/bpf/bpftool/btf.c +0
-0@@ -562,8 +562,10 @@ static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool fint name_off = t->name_off;
Quoted 12 lines
tools/bpf/bpftool/btf.c static const char *btf_type_sort_name(const struct btf *btf, __u32 index, bool f · line 566
Quoted 5 lines
tools/bpf/bpftool/btf.c +2
-2/* Use name of the first element for anonymous enums if allowed */ -if (!from_ref && !t->name_off &&btf_vlen (t)) - name_off =btf_enum (t)->name_off; +if (!from_ref && !name_off &&btf_vlen (t)) + name_off = btf_kind(t) == BTF_KIND_ENUM64 ?Just fyi for the future (I don't think we need to fix this or anything like that), but using
BTF_KIND_xxxconstants inbtf_kind(t)comparisons should be rare. Libbpf provides a full set of shorterbtf_is_xxx(t)helpers for this. So this would be justbtf_is_enum64(t). What you did is not wrong, it's just more open-coded and verbose.
Noted, that would have been better agree.
Thanks,
Daniel