Skip to content

Commit 71d29c2

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
selftests/bpf: Test libbpf API function btf__add_tag()
Add btf_write tests with btf__add_tag() function. Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5c07f2f commit 71d29c2

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

tools/testing/selftests/bpf/btf_helpers.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ static const char * const btf_kind_str_mapping[] = {
2424
[BTF_KIND_VAR] = "VAR",
2525
[BTF_KIND_DATASEC] = "DATASEC",
2626
[BTF_KIND_FLOAT] = "FLOAT",
27+
[BTF_KIND_TAG] = "TAG",
2728
};
2829

2930
static const char *btf_kind_str(__u16 kind)
3031
{
31-
if (kind > BTF_KIND_DATASEC)
32+
if (kind > BTF_KIND_TAG)
3233
return "UNKNOWN";
3334
return btf_kind_str_mapping[kind];
3435
}
@@ -177,6 +178,10 @@ int fprintf_btf_type_raw(FILE *out, const struct btf *btf, __u32 id)
177178
case BTF_KIND_FLOAT:
178179
fprintf(out, " size=%u", t->size);
179180
break;
181+
case BTF_KIND_TAG:
182+
fprintf(out, " type_id=%u component_idx=%d",
183+
t->type, btf_tag(t)->component_idx);
184+
break;
180185
default:
181186
break;
182187
}

tools/testing/selftests/bpf/prog_tests/btf_write.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,26 @@ void test_btf_write() {
281281
"[17] DATASEC 'datasec1' size=12 vlen=1\n"
282282
"\ttype_id=1 offset=4 size=8", "raw_dump");
283283

284+
/* TAG */
285+
id = btf__add_tag(btf, "tag1", 16, -1);
286+
ASSERT_EQ(id, 18, "tag_id");
287+
t = btf__type_by_id(btf, 18);
288+
ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "tag1", "tag_value");
289+
ASSERT_EQ(btf_kind(t), BTF_KIND_TAG, "tag_kind");
290+
ASSERT_EQ(t->type, 16, "tag_type");
291+
ASSERT_EQ(btf_tag(t)->component_idx, -1, "tag_component_idx");
292+
ASSERT_STREQ(btf_type_raw_dump(btf, 18),
293+
"[18] TAG 'tag1' type_id=16 component_idx=-1", "raw_dump");
294+
295+
id = btf__add_tag(btf, "tag2", 14, 1);
296+
ASSERT_EQ(id, 19, "tag_id");
297+
t = btf__type_by_id(btf, 19);
298+
ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "tag2", "tag_value");
299+
ASSERT_EQ(btf_kind(t), BTF_KIND_TAG, "tag_kind");
300+
ASSERT_EQ(t->type, 14, "tag_type");
301+
ASSERT_EQ(btf_tag(t)->component_idx, 1, "tag_component_idx");
302+
ASSERT_STREQ(btf_type_raw_dump(btf, 19),
303+
"[19] TAG 'tag2' type_id=14 component_idx=1", "raw_dump");
304+
284305
btf__free(btf);
285306
}

0 commit comments

Comments
 (0)