Commit 59f2a29c authored by Andrii Nakryiko's avatar Andrii Nakryiko
Browse files

Merge branch 'libbpf: Add btf__type_cnt() and btf__raw_data() APIs'



Hengqi Chen says:

====================

Add btf__type_cnt() and btf__raw_data() APIs and deprecate
btf__get_nr_type() and btf__get_raw_data() since the old APIs
don't follow libbpf naming convention. Also update tools/selftests
to use these new APIs. This is part of effort towards libbpf v1.0

v1->v2:
 - Update commit message, deprecate the old APIs in libbpf v0.7 (Andrii)
 - Separate changes in tools/ to individual patches (Andrii)
====================

Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents 1000298c 487ef148
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -329,7 +329,7 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
				printf("\n\ttype_id=%u offset=%u size=%u",
				       v->type, v->offset, v->size);

				if (v->type <= btf__get_nr_types(btf)) {
				if (v->type < btf__type_cnt(btf)) {
					vt = btf__type_by_id(btf, v->type);
					printf(" (%s '%s')",
					       btf_kind_str[btf_kind_safe(btf_kind(vt))],
@@ -390,14 +390,14 @@ static int dump_btf_raw(const struct btf *btf,
		}
	} else {
		const struct btf *base;
		int cnt = btf__get_nr_types(btf);
		int cnt = btf__type_cnt(btf);
		int start_id = 1;

		base = btf__base_btf(btf);
		if (base)
			start_id = btf__get_nr_types(base) + 1;
			start_id = btf__type_cnt(base);

		for (i = start_id; i <= cnt; i++) {
		for (i = start_id; i < cnt; i++) {
			t = btf__type_by_id(btf, i);
			dump_btf_type(btf, i, t);
		}
@@ -440,9 +440,9 @@ static int dump_btf_c(const struct btf *btf,
				goto done;
		}
	} else {
		int cnt = btf__get_nr_types(btf);
		int cnt = btf__type_cnt(btf);

		for (i = 1; i <= cnt; i++) {
		for (i = 1; i < cnt; i++) {
			err = btf_dump__dump_type(d, i);
			if (err)
				goto done;
+2 −2
Original line number Diff line number Diff line
@@ -211,7 +211,7 @@ static int codegen_datasec_def(struct bpf_object *obj,
static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
{
	struct btf *btf = bpf_object__btf(obj);
	int n = btf__get_nr_types(btf);
	int n = btf__type_cnt(btf);
	struct btf_dump *d;
	struct bpf_map *map;
	const struct btf_type *sec;
@@ -233,7 +233,7 @@ static int codegen_datasecs(struct bpf_object *obj, const char *obj_name)
			continue;

		sec = NULL;
		for (i = 1; i <= n; i++) {
		for (i = 1; i < n; i++) {
			const struct btf_type *t = btf__type_by_id(btf, i);
			const char *name;

+2 −2
Original line number Diff line number Diff line
@@ -502,12 +502,12 @@ static int symbols_resolve(struct object *obj)
	}

	err = -1;
	nr_types = btf__get_nr_types(btf);
	nr_types = btf__type_cnt(btf);

	/*
	 * Iterate all the BTF types and search for collected symbol IDs.
	 */
	for (type_id = 1; type_id <= nr_types; type_id++) {
	for (type_id = 1; type_id < nr_types; type_id++) {
		const struct btf_type *type;
		struct rb_root *root;
		struct btf_id *id;
+22 −14
Original line number Diff line number Diff line
@@ -57,7 +57,7 @@ struct btf {
	 * representation is broken up into three independently allocated
	 * memory regions to be able to modify them independently.
	 * raw_data is nulled out at that point, but can be later allocated
	 * and cached again if user calls btf__get_raw_data(), at which point
	 * and cached again if user calls btf__raw_data(), at which point
	 * raw_data will contain a contiguous copy of header, types, and
	 * strings:
	 *
@@ -435,6 +435,11 @@ __u32 btf__get_nr_types(const struct btf *btf)
	return btf->start_id + btf->nr_types - 1;
}

__u32 btf__type_cnt(const struct btf *btf)
{
	return btf->start_id + btf->nr_types;
}

const struct btf *btf__base_btf(const struct btf *btf)
{
	return btf->base_btf;
@@ -466,8 +471,8 @@ static int determine_ptr_size(const struct btf *btf)
	if (btf->base_btf && btf->base_btf->ptr_sz > 0)
		return btf->base_btf->ptr_sz;

	n = btf__get_nr_types(btf);
	for (i = 1; i <= n; i++) {
	n = btf__type_cnt(btf);
	for (i = 1; i < n; i++) {
		t = btf__type_by_id(btf, i);
		if (!btf_is_int(t))
			continue;
@@ -684,12 +689,12 @@ int btf__resolve_type(const struct btf *btf, __u32 type_id)

__s32 btf__find_by_name(const struct btf *btf, const char *type_name)
{
	__u32 i, nr_types = btf__get_nr_types(btf);
	__u32 i, nr_types = btf__type_cnt(btf);

	if (!strcmp(type_name, "void"))
		return 0;

	for (i = 1; i <= nr_types; i++) {
	for (i = 1; i < nr_types; i++) {
		const struct btf_type *t = btf__type_by_id(btf, i);
		const char *name = btf__name_by_offset(btf, t->name_off);

@@ -703,12 +708,12 @@ __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
static __s32 btf_find_by_name_kind(const struct btf *btf, int start_id,
				   const char *type_name, __u32 kind)
{
	__u32 i, nr_types = btf__get_nr_types(btf);
	__u32 i, nr_types = btf__type_cnt(btf);

	if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
		return 0;

	for (i = start_id; i <= nr_types; i++) {
	for (i = start_id; i < nr_types; i++) {
		const struct btf_type *t = btf__type_by_id(btf, i);
		const char *name;

@@ -781,7 +786,7 @@ static struct btf *btf_new_empty(struct btf *base_btf)

	if (base_btf) {
		btf->base_btf = base_btf;
		btf->start_id = btf__get_nr_types(base_btf) + 1;
		btf->start_id = btf__type_cnt(base_btf);
		btf->start_str_off = base_btf->hdr->str_len;
	}

@@ -831,7 +836,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf)

	if (base_btf) {
		btf->base_btf = base_btf;
		btf->start_id = btf__get_nr_types(base_btf) + 1;
		btf->start_id = btf__type_cnt(base_btf);
		btf->start_str_off = base_btf->hdr->str_len;
	}

@@ -1224,7 +1229,7 @@ static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endi
	return NULL;
}

const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size)
const void *btf__raw_data(const struct btf *btf_ro, __u32 *size)
{
	struct btf *btf = (struct btf *)btf_ro;
	__u32 data_sz;
@@ -1232,7 +1237,7 @@ const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size)

	data = btf_get_raw_data(btf, &data_sz, btf->swapped_endian);
	if (!data)
		return errno = -ENOMEM, NULL;
		return errno = ENOMEM, NULL;

	btf->raw_size = data_sz;
	if (btf->swapped_endian)
@@ -1243,6 +1248,9 @@ const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size)
	return data;
}

__attribute__((alias("btf__raw_data")))
const void *btf__get_raw_data(const struct btf *btf, __u32 *size);

const char *btf__str_by_offset(const struct btf *btf, __u32 offset)
{
	if (offset < btf->start_str_off)
@@ -1651,7 +1659,7 @@ int btf__add_btf(struct btf *btf, const struct btf *src_btf)
	old_strs_len = btf->hdr->str_len;

	data_sz = src_btf->hdr->type_len;
	cnt = btf__get_nr_types(src_btf);
	cnt = btf__type_cnt(src_btf) - 1;

	/* pre-allocate enough memory for new types */
	t = btf_add_type_mem(btf, data_sz);
@@ -1968,7 +1976,7 @@ int btf__add_union(struct btf *btf, const char *name, __u32 byte_sz)

static struct btf_type *btf_last_type(struct btf *btf)
{
	return btf_type_by_id(btf, btf__get_nr_types(btf));
	return btf_type_by_id(btf, btf__type_cnt(btf) - 1);
}

/*
@@ -3174,7 +3182,7 @@ static struct btf_dedup *btf_dedup_new(struct btf *btf, struct btf_ext *btf_ext,
		goto done;
	}

	type_cnt = btf__get_nr_types(btf) + 1;
	type_cnt = btf__type_cnt(btf);
	d->map = malloc(sizeof(__u32) * type_cnt);
	if (!d->map) {
		err = -ENOMEM;
+4 −0
Original line number Diff line number Diff line
@@ -132,7 +132,9 @@ LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
				   const char *type_name);
LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
					const char *type_name, __u32 kind);
LIBBPF_DEPRECATED_SINCE(0, 7, "use btf__type_cnt() instead; note that btf__get_nr_types() == btf__type_cnt() - 1")
LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
LIBBPF_API __u32 btf__type_cnt(const struct btf *btf);
LIBBPF_API const struct btf *btf__base_btf(const struct btf *btf);
LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
						  __u32 id);
@@ -145,7 +147,9 @@ LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
LIBBPF_API int btf__fd(const struct btf *btf);
LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
LIBBPF_DEPRECATED_SINCE(0, 7, "use btf__raw_data() instead")
LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
LIBBPF_API const void *btf__raw_data(const struct btf *btf, __u32 *size);
LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset);
LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
Loading