From mboxrd@z Thu Jan 1 00:00:00 1970 From: Song Liu Subject: Re: [PATCH bpf-next 3/3] bpf: btf: json print map dump with btf info Date: Wed, 20 Jun 2018 23:22:52 +0000 Message-ID: <60109415-AE62-4E4A-BF7D-401F66245523@fb.com> References: <20180620203051.223156973@fb.com> <20180620203703.208599277@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" , "netdev@vger.kernel.org" , Kernel Team , "linux-kernel@vger.kernel.org" To: Okash Khawaja Return-path: In-Reply-To: <20180620203703.208599277@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 modifies `bpftool map dump [-j|-p] id ` to json- > print and pretty-json-print map dump. It calls btf_dumper introduced in > previous patch to accomplish this. >=20 > The patch only prints debug info when -j or -p flags are supplied. Then > too, if the map has associated btf data loaded. Otherwise the usual > debug-less output is printed. >=20 > Signed-off-by: Okash Khawaja > Acked-by: Martin KaFai Lau >=20 > --- > tools/bpf/bpftool/map.c | 94 ++++++++++++++++++++++++++++++++++++++++++= ++++-- > 1 file changed, 91 insertions(+), 3 deletions(-) >=20 > --- a/tools/bpf/bpftool/map.c > +++ b/tools/bpf/bpftool/map.c > @@ -43,9 +43,13 @@ > #include > #include > #include > +#include >=20 > #include >=20 > +#include "json_writer.h" > +#include "btf.h" > +#include "btf_dumper.h" > #include "main.h" >=20 > static const char * const map_type_name[] =3D { > @@ -508,6 +512,83 @@ static int do_show(int argc, char **argv > return errno =3D=3D ENOENT ? 0 : -1; > } >=20 > + > +static int do_dump_btf(struct btf *btf, struct bpf_map_info *map_info, > + void *key, void *value) > +{ > + int ret; > + > + jsonw_start_object(json_wtr); > + jsonw_name(json_wtr, "key"); > + > + ret =3D btf_dumper_type(btf, json_wtr, map_info->btf_key_type_id, key); > + if (ret) > + goto out; > + > + jsonw_end_object(json_wtr); > + > + jsonw_start_object(json_wtr); > + jsonw_name(json_wtr, "value"); > + > + ret =3D btf_dumper_type(btf, json_wtr, map_info->btf_value_type_id, > + value); > + > +out: > + /* end of root object */ > + jsonw_end_object(json_wtr); > + > + return ret; > +} Please add some tests for do_dump_btf(), including some invalid data type/k= ind.=20 > + > +static struct btf *get_btf(struct bpf_map_info *map_info) > +{ > + int btf_fd =3D bpf_btf_get_fd_by_id(map_info->btf_id); > + struct bpf_btf_info btf_info =3D { 0 }; > + __u32 len =3D sizeof(btf_info); > + uint32_t last_size; > + int err; > + struct btf *btf =3D NULL; > + void *ptr =3D NULL, *temp_ptr; > + > + if (btf_fd < 0) > + return NULL; > + > + btf_info.btf_size =3D 4096; Is btf_size always a constant of 4096?=20 > + do { > + last_size =3D btf_info.btf_size; > + temp_ptr =3D realloc(ptr, last_size); > + if (!temp_ptr) { > + p_err("unable allocate memory for debug info."); > + goto exit_free; > + } > + > + ptr =3D temp_ptr; > + bzero(ptr, last_size); > + btf_info.btf =3D ptr_to_u64(ptr); > + err =3D bpf_obj_get_info_by_fd(btf_fd, &btf_info, &len); > + } while (!err && btf_info.btf_size > last_size && last_size =3D=3D 4096= ); > + > + if (err || btf_info.btf_size > last_size) { > + p_info("can't get btf info. debug info won't be displayed. error: %s", > + err ? strerror(errno) : "exceeds size retry"); > + goto exit_free; > + } > + > + btf =3D btf__new((uint8_t *) btf_info.btf, > + btf_info.btf_size, NULL); > + if (IS_ERR(btf)) { > + printf("error when initialising btf: %s\n", > + strerror(PTR_ERR(btf))); > + btf =3D NULL; > + } > + > +exit_free: > + close(btf_fd); > + free(ptr); > + > + return btf; > +} > + > static int do_dump(int argc, char **argv) > { > void *key, *value, *prev_key; > @@ -516,6 +597,7 @@ static int do_dump(int argc, char **argv > __u32 len =3D sizeof(info); > int err; > int fd; > + struct btf *btf =3D NULL; >=20 > if (argc !=3D 2) > usage(); > @@ -538,6 +620,8 @@ static int do_dump(int argc, char **argv > goto exit_free; > } >=20 > + btf =3D get_btf(&info); > + > prev_key =3D NULL; > if (json_output) > jsonw_start_array(json_wtr); > @@ -550,9 +634,12 @@ static int do_dump(int argc, char **argv > } >=20 > if (!bpf_map_lookup_elem(fd, key, value)) { > - if (json_output) > - print_entry_json(&info, key, value); > - else > + if (json_output) { > + if (btf) > + do_dump_btf(btf, &info, key, value); > + else > + print_entry_json(&info, key, value); > + } else > print_entry_plain(&info, key, value); > } else { > if (json_output) { > @@ -584,6 +671,7 @@ exit_free: > free(key); > free(value); > close(fd); > + btf__free(btf); >=20 > return err; > } >=20