From mboxrd@z Thu Jan 1 00:00:00 1970 From: Song Liu Subject: Re: [PATCH bpf-next 1/3] bpf: btf: export btf types and name by offset from lib Date: Wed, 20 Jun 2018 22:40:19 +0000 Message-ID: References: <20180620203051.223156973@fb.com> <20180620203702.996737905@fb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Cc: Daniel Borkmann , Martin Lau , "Alexei Starovoitov" , Yonghong Song , Quentin Monnet , Jakub Kicinski , "David S. Miller" , Networking , Kernel Team , "linux-kernel@vger.kernel.org" To: Okash Khawaja Return-path: In-Reply-To: <20180620203702.996737905@fb.com> Content-Language: en-US Content-ID: Sender: linux-kernel-owner@vger.kernel.org List-Id: netdev.vger.kernel.org > On Jun 20, 2018, at 1:30 PM, Okash Khawaja wrote: >=20 > This patch introduces btf__resolve_type() function and exports two > existing functions from libbpf. btf__resolve_type follows modifier > types like const and typedef until it hits a type which actually takes > up memory, and then returns it. This function follows similar pattern > to btf__resolve_size but instead of computing size, it just returns > the type. >=20 > These functions will be used in the followig patch which parses > information inside array of `struct btf_type *`. btf_name_by_offset is > used for printing variable names. >=20 > Signed-off-by: Okash Khawaja > Acked-by: Martin KaFai Lau >=20 > --- > tools/lib/bpf/btf.c | 65 ++++++++++++++++++++++++++++++++++++----------= ------ > tools/lib/bpf/btf.h | 3 ++ > 2 files changed, 48 insertions(+), 20 deletions(-) >=20 > --- a/tools/lib/bpf/btf.c > +++ b/tools/lib/bpf/btf.c > @@ -17,6 +17,11 @@ >=20 > #define BTF_MAX_NR_TYPES 65535 >=20 > +#define IS_MODIFIER(k) (((k) =3D=3D BTF_KIND_TYPEDEF) || \ > + ((k) =3D=3D BTF_KIND_VOLATILE) || \ > + ((k) =3D=3D BTF_KIND_CONST) || \ > + ((k) =3D=3D BTF_KIND_RESTRICT)) > + > static struct btf_type btf_void; >=20 > struct btf { > @@ -33,14 +38,6 @@ struct btf { > int fd; > }; >=20 > -static const char *btf_name_by_offset(const struct btf *btf, uint32_t of= fset) > -{ > - if (offset < btf->hdr->str_len) > - return &btf->strings[offset]; > - else > - return NULL; > -} > - > static int btf_add_type(struct btf *btf, struct btf_type *t) > { > if (btf->types_size - btf->nr_types < 2) { > @@ -190,15 +187,6 @@ static int btf_parse_type_sec(struct btf > return 0; > } >=20 > -static const struct btf_type *btf_type_by_id(const struct btf *btf, > - uint32_t type_id) > -{ > - if (type_id > btf->nr_types) > - return NULL; > - > - return btf->types[type_id]; > -} nit: Why do we need to move these two functions to later of the file?=20 Other than this, Acked-by: Song Liu > - > static bool btf_type_is_void(const struct btf_type *t) > { > return t =3D=3D &btf_void || BTF_INFO_KIND(t->info) =3D=3D BTF_KIND_FWD; > @@ -234,7 +222,7 @@ int64_t btf__resolve_size(const struct b > int64_t size =3D -1; > int i; >=20 > - t =3D btf_type_by_id(btf, type_id); > + t =3D btf__type_by_id(btf, type_id); > for (i =3D 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t); > i++) { > size =3D btf_type_size(t); > @@ -259,7 +247,7 @@ int64_t btf__resolve_size(const struct b > return -EINVAL; > } >=20 > - t =3D btf_type_by_id(btf, type_id); > + t =3D btf__type_by_id(btf, type_id); > } >=20 > if (size < 0) > @@ -271,6 +259,26 @@ int64_t btf__resolve_size(const struct b > return nelems * size; > } >=20 > +int32_t btf__resolve_type(const struct btf *btf, uint32_t type_id) > +{ > + const struct btf_type *t; > + int depth =3D 0; > + > + t =3D btf__type_by_id(btf, type_id); > + while (depth < MAX_RESOLVE_DEPTH && > + !btf_type_is_void_or_null(t) && > + IS_MODIFIER(BTF_INFO_KIND(t->info))) { > + type_id =3D t->type; > + t =3D btf__type_by_id(btf, type_id); > + depth++; > + } > + > + if (depth =3D=3D MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t)) > + return -EINVAL; > + > + return type_id; > +} > + > int32_t btf__find_by_name(const struct btf *btf, const char *type_name) > { > uint32_t i; > @@ -280,7 +288,7 @@ int32_t btf__find_by_name(const struct b >=20 > for (i =3D 1; i <=3D btf->nr_types; i++) { > const struct btf_type *t =3D btf->types[i]; > - const char *name =3D btf_name_by_offset(btf, t->name_off); > + const char *name =3D btf__name_by_offset(btf, t->name_off); >=20 > if (name && !strcmp(type_name, name)) > return i; > @@ -371,3 +379,20 @@ int btf__fd(const struct btf *btf) > { > return btf->fd; > } > + > +const char *btf__name_by_offset(const struct btf *btf, uint32_t offset) > +{ > + if (offset < btf->hdr->str_len) > + return &btf->strings[offset]; > + else > + return NULL; > +} > + > +const struct btf_type *btf__type_by_id(const struct btf *btf, > + uint32_t type_id) > +{ > + if (type_id > btf->nr_types) > + return NULL; > + > + return btf->types[type_id]; > +} > --- a/tools/lib/bpf/btf.h > +++ b/tools/lib/bpf/btf.h > @@ -17,6 +17,9 @@ void btf__free(struct btf *btf); > struct btf *btf__new(uint8_t *data, uint32_t size, btf_print_fn_t err_log= ); > int32_t btf__find_by_name(const struct btf *btf, const char *type_name); > int64_t btf__resolve_size(const struct btf *btf, uint32_t type_id); > +int32_t btf__resolve_type(const struct btf *btf, uint32_t type_id); > int btf__fd(const struct btf *btf); > +const char *btf__name_by_offset(const struct btf *btf, uint32_t offset); > +const struct btf_type *btf__type_by_id(const struct btf *btf, uint32_t t= ype_id); >=20 > #endif >=20