Skip to content

Commit

Permalink
selftests/bpf: Switch to new btf__type_cnt/btf__raw_data APIs
Browse files Browse the repository at this point in the history
Replace the calls to btf__get_nr_types/btf__get_raw_data in
selftests with new APIs btf__type_cnt/btf__raw_data. The old
APIs will be deprecated in libbpf v0.7+.

Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211022130623.1548429-6-hengqi.chen@gmail.com
  • Loading branch information
Hengqi Chen authored and Andrii Nakryiko committed Oct 22, 2021
1 parent 58fc155 commit 487ef14
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/btf_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ int btf_validate_raw(struct btf *btf, int nr_types, const char *exp_types[])
int i;
bool ok = true;

ASSERT_EQ(btf__get_nr_types(btf), nr_types, "btf_nr_types");
ASSERT_EQ(btf__type_cnt(btf) - 1, nr_types, "btf_nr_types");

for (i = 1; i <= nr_types; i++) {
if (!ASSERT_STREQ(btf_type_raw_dump(btf, i), exp_types[i - 1], "raw_dump"))
Expand Down Expand Up @@ -254,7 +254,7 @@ const char *btf_type_c_dump(const struct btf *btf)
return NULL;
}

for (i = 1; i <= btf__get_nr_types(btf); i++) {
for (i = 1; i < btf__type_cnt(btf); i++) {
err = btf_dump__dump_type(d, i);
if (err) {
fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);
Expand Down
10 changes: 5 additions & 5 deletions tools/testing/selftests/bpf/prog_tests/btf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7274,8 +7274,8 @@ static void do_test_dedup(unsigned int test_num)
goto done;
}

test_btf_data = btf__get_raw_data(test_btf, &test_btf_size);
expect_btf_data = btf__get_raw_data(expect_btf, &expect_btf_size);
test_btf_data = btf__raw_data(test_btf, &test_btf_size);
expect_btf_data = btf__raw_data(expect_btf, &expect_btf_size);
if (CHECK(test_btf_size != expect_btf_size,
"test_btf_size:%u != expect_btf_size:%u",
test_btf_size, expect_btf_size)) {
Expand Down Expand Up @@ -7329,16 +7329,16 @@ static void do_test_dedup(unsigned int test_num)
expect_str_cur += expect_len + 1;
}

test_nr_types = btf__get_nr_types(test_btf);
expect_nr_types = btf__get_nr_types(expect_btf);
test_nr_types = btf__type_cnt(test_btf);
expect_nr_types = btf__type_cnt(expect_btf);
if (CHECK(test_nr_types != expect_nr_types,
"test_nr_types:%u != expect_nr_types:%u",
test_nr_types, expect_nr_types)) {
err = -1;
goto done;
}

for (i = 1; i <= test_nr_types; i++) {
for (i = 1; i < test_nr_types; i++) {
const struct btf_type *test_type, *expect_type;
int test_size, expect_size;

Expand Down
8 changes: 4 additions & 4 deletions tools/testing/selftests/bpf/prog_tests/btf_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static struct btf_dump_test_case {
static int btf_dump_all_types(const struct btf *btf,
const struct btf_dump_opts *opts)
{
size_t type_cnt = btf__get_nr_types(btf);
size_t type_cnt = btf__type_cnt(btf);
struct btf_dump *d;
int err = 0, id;

Expand All @@ -36,7 +36,7 @@ static int btf_dump_all_types(const struct btf *btf,
if (err)
return err;

for (id = 1; id <= type_cnt; id++) {
for (id = 1; id < type_cnt; id++) {
err = btf_dump__dump_type(d, id);
if (err)
goto done;
Expand Down Expand Up @@ -171,7 +171,7 @@ void test_btf_dump_incremental(void)
err = btf__add_field(btf, "x", 2, 0, 0);
ASSERT_OK(err, "field_ok");

for (i = 1; i <= btf__get_nr_types(btf); i++) {
for (i = 1; i < btf__type_cnt(btf); i++) {
err = btf_dump__dump_type(d, i);
ASSERT_OK(err, "dump_type_ok");
}
Expand Down Expand Up @@ -210,7 +210,7 @@ void test_btf_dump_incremental(void)
err = btf__add_field(btf, "s", 3, 32, 0);
ASSERT_OK(err, "field_ok");

for (i = 1; i <= btf__get_nr_types(btf); i++) {
for (i = 1; i < btf__type_cnt(btf); i++) {
err = btf_dump__dump_type(d, i);
ASSERT_OK(err, "dump_type_ok");
}
Expand Down
12 changes: 6 additions & 6 deletions tools/testing/selftests/bpf/prog_tests/btf_endian.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void test_btf_endian() {
ASSERT_EQ(btf__endianness(btf), swap_endian, "endian");

/* Get raw BTF data in non-native endianness... */
raw_data = btf__get_raw_data(btf, &raw_sz);
raw_data = btf__raw_data(btf, &raw_sz);
if (!ASSERT_OK_PTR(raw_data, "raw_data_inverted"))
goto err_out;

Expand All @@ -42,9 +42,9 @@ void test_btf_endian() {
goto err_out;

ASSERT_EQ(btf__endianness(swap_btf), swap_endian, "endian");
ASSERT_EQ(btf__get_nr_types(swap_btf), btf__get_nr_types(btf), "nr_types");
ASSERT_EQ(btf__type_cnt(swap_btf), btf__type_cnt(btf), "nr_types");

swap_raw_data = btf__get_raw_data(swap_btf, &swap_raw_sz);
swap_raw_data = btf__raw_data(swap_btf, &swap_raw_sz);
if (!ASSERT_OK_PTR(swap_raw_data, "swap_raw_data"))
goto err_out;

Expand All @@ -58,7 +58,7 @@ void test_btf_endian() {

/* swap it back to native endianness */
btf__set_endianness(swap_btf, endian);
swap_raw_data = btf__get_raw_data(swap_btf, &swap_raw_sz);
swap_raw_data = btf__raw_data(swap_btf, &swap_raw_sz);
if (!ASSERT_OK_PTR(swap_raw_data, "swap_raw_data"))
goto err_out;

Expand All @@ -75,7 +75,7 @@ void test_btf_endian() {
swap_btf = NULL;

btf__set_endianness(btf, swap_endian);
raw_data = btf__get_raw_data(btf, &raw_sz);
raw_data = btf__raw_data(btf, &raw_sz);
if (!ASSERT_OK_PTR(raw_data, "raw_data_inverted"))
goto err_out;

Expand All @@ -85,7 +85,7 @@ void test_btf_endian() {
goto err_out;

ASSERT_EQ(btf__endianness(swap_btf), swap_endian, "endian");
ASSERT_EQ(btf__get_nr_types(swap_btf), btf__get_nr_types(btf), "nr_types");
ASSERT_EQ(btf__type_cnt(swap_btf), btf__type_cnt(btf), "nr_types");

/* the type should appear as if it was stored in native endianness */
t = btf__type_by_id(swap_btf, var_id);
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/prog_tests/btf_split.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void test_btf_split() {
d = btf_dump__new(btf2, NULL, &opts, btf_dump_printf);
if (!ASSERT_OK_PTR(d, "btf_dump__new"))
goto cleanup;
for (i = 1; i <= btf__get_nr_types(btf2); i++) {
for (i = 1; i < btf__type_cnt(btf2); i++) {
err = btf_dump__dump_type(d, i);
ASSERT_OK(err, "dump_type_ok");
}
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/prog_tests/core_autosize.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void test_core_autosize(void)
if (!ASSERT_OK_PTR(f, "btf_fdopen"))
goto cleanup;

raw_data = btf__get_raw_data(btf, &raw_sz);
raw_data = btf__raw_data(btf, &raw_sz);
if (!ASSERT_OK_PTR(raw_data, "raw_data"))
goto cleanup;
written = fwrite(raw_data, 1, raw_sz, f);
Expand Down
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/prog_tests/core_reloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ static int setup_type_id_case_local(struct core_reloc_test_case *test)
exp->local_anon_void_ptr = -1;
exp->local_anon_arr = -1;

for (i = 1; i <= btf__get_nr_types(local_btf); i++)
for (i = 1; i < btf__type_cnt(local_btf); i++)
{
t = btf__type_by_id(local_btf, i);
/* we are interested only in anonymous types */
Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/prog_tests/resolve_btfids.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ static int resolve_symbols(void)
"Failed to load BTF from btf_data.o\n"))
return -1;

nr = btf__get_nr_types(btf);
nr = btf__type_cnt(btf);

for (type_id = 1; type_id <= nr; type_id++) {
for (type_id = 1; type_id < nr; type_id++) {
if (__resolve_symbol(btf, type_id))
break;
}
Expand Down

0 comments on commit 487ef14

Please sign in to comment.