Existing BTF_ID_LIST used a local static variable to store btf_ids. This patch provided a new macro BTF_ID_LIST_GLOBAL to store btf_ids in a global variable which can be shared among multiple files. The existing BTF_ID_LIST is still retained. Two reasons. First, BTF_ID_LIST is also used to build btf_ids for helper arguments which typically is an array of 5. Since typically different helpers have different signature, it makes little sense to share them. Second, some current computed btf_ids are indeed local. If later those btf_ids are shared between different files, they can use BTF_ID_LIST_GLOBAL then. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Jiri Olsa <jolsa@redhat.com> Link: https://lore.kernel.org/bpf/20200720163401.1393159-1-yhs@fb.com
129 lines
2.6 KiB
C
129 lines
2.6 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <linux/err.h>
|
|
#include <string.h>
|
|
#include <bpf/btf.h>
|
|
#include <bpf/libbpf.h>
|
|
#include <linux/btf.h>
|
|
#include <linux/kernel.h>
|
|
#define CONFIG_DEBUG_INFO_BTF
|
|
#include <linux/btf_ids.h>
|
|
#include "test_progs.h"
|
|
|
|
static int duration;
|
|
|
|
struct symbol {
|
|
const char *name;
|
|
int type;
|
|
int id;
|
|
};
|
|
|
|
struct symbol test_symbols[] = {
|
|
{ "unused", BTF_KIND_UNKN, 0 },
|
|
{ "S", BTF_KIND_TYPEDEF, -1 },
|
|
{ "T", BTF_KIND_TYPEDEF, -1 },
|
|
{ "U", BTF_KIND_TYPEDEF, -1 },
|
|
{ "S", BTF_KIND_STRUCT, -1 },
|
|
{ "U", BTF_KIND_UNION, -1 },
|
|
{ "func", BTF_KIND_FUNC, -1 },
|
|
};
|
|
|
|
BTF_ID_LIST(test_list_local)
|
|
BTF_ID_UNUSED
|
|
BTF_ID(typedef, S)
|
|
BTF_ID(typedef, T)
|
|
BTF_ID(typedef, U)
|
|
BTF_ID(struct, S)
|
|
BTF_ID(union, U)
|
|
BTF_ID(func, func)
|
|
|
|
extern __u32 test_list_global[];
|
|
BTF_ID_LIST_GLOBAL(test_list_global)
|
|
BTF_ID_UNUSED
|
|
BTF_ID(typedef, S)
|
|
BTF_ID(typedef, T)
|
|
BTF_ID(typedef, U)
|
|
BTF_ID(struct, S)
|
|
BTF_ID(union, U)
|
|
BTF_ID(func, func)
|
|
|
|
static int
|
|
__resolve_symbol(struct btf *btf, int type_id)
|
|
{
|
|
const struct btf_type *type;
|
|
const char *str;
|
|
unsigned int i;
|
|
|
|
type = btf__type_by_id(btf, type_id);
|
|
if (!type) {
|
|
PRINT_FAIL("Failed to get type for ID %d\n", type_id);
|
|
return -1;
|
|
}
|
|
|
|
for (i = 0; i < ARRAY_SIZE(test_symbols); i++) {
|
|
if (test_symbols[i].id != -1)
|
|
continue;
|
|
|
|
if (BTF_INFO_KIND(type->info) != test_symbols[i].type)
|
|
continue;
|
|
|
|
str = btf__name_by_offset(btf, type->name_off);
|
|
if (!str) {
|
|
PRINT_FAIL("Failed to get name for BTF ID %d\n", type_id);
|
|
return -1;
|
|
}
|
|
|
|
if (!strcmp(str, test_symbols[i].name))
|
|
test_symbols[i].id = type_id;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int resolve_symbols(void)
|
|
{
|
|
struct btf *btf;
|
|
int type_id;
|
|
__u32 nr;
|
|
|
|
btf = btf__parse_elf("btf_data.o", NULL);
|
|
if (CHECK(libbpf_get_error(btf), "resolve",
|
|
"Failed to load BTF from btf_data.o\n"))
|
|
return -1;
|
|
|
|
nr = btf__get_nr_types(btf);
|
|
|
|
for (type_id = 1; type_id <= nr; type_id++) {
|
|
if (__resolve_symbol(btf, type_id))
|
|
break;
|
|
}
|
|
|
|
btf__free(btf);
|
|
return 0;
|
|
}
|
|
|
|
int test_resolve_btfids(void)
|
|
{
|
|
__u32 *test_list, *test_lists[] = { test_list_local, test_list_global };
|
|
unsigned int i, j;
|
|
int ret = 0;
|
|
|
|
if (resolve_symbols())
|
|
return -1;
|
|
|
|
/* Check BTF_ID_LIST(test_list_local) and
|
|
* BTF_ID_LIST_GLOBAL(test_list_global) IDs
|
|
*/
|
|
for (j = 0; j < ARRAY_SIZE(test_lists); j++) {
|
|
test_list = test_lists[j];
|
|
for (i = 0; i < ARRAY_SIZE(test_symbols) && !ret; i++) {
|
|
ret = CHECK(test_list[i] != test_symbols[i].id,
|
|
"id_check",
|
|
"wrong ID for %s (%d != %d)\n",
|
|
test_symbols[i].name,
|
|
test_list[i], test_symbols[i].id);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|