diff mbox series

[RFC,hid,v1,01/10] bpftool: generate json output of skeletons

Message ID 20221124151603.807536-2-benjamin.tissoires@redhat.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series HID-BPF: add support for in-tree BPF programs | expand

Checks

Context Check Description
netdev/tree_selection success Guessing tree name failed - patch did not apply, async
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-9 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-10 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-11 pending Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-13 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-14 fail Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-15 fail Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-16 pending Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-18 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-19 fail Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-20 fail Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-21 fail Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-23 fail Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-26 pending Logs for test_progs_no_alu32_parallel on s390x with gcc
bpf/vmtest-bpf-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-28 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-29 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-30 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-31 pending Logs for test_progs_parallel on s390x with gcc
bpf/vmtest-bpf-VM_Test-32 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-33 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-34 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-35 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-VM_Test-36 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-37 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-38 success Logs for test_verifier on x86_64 with llvm-16

Commit Message

Benjamin Tissoires Nov. 24, 2022, 3:15 p.m. UTC
So we can then build light skeletons with loaders in any language.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
---
 tools/bpf/bpftool/gen.c | 95 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

Comments

Andrii Nakryiko Nov. 30, 2022, 11:05 p.m. UTC | #1
On Thu, Nov 24, 2022 at 7:16 AM Benjamin Tissoires
<benjamin.tissoires@redhat.com> wrote:
>
> So we can then build light skeletons with loaders in any language.
>

It would be useful to include an example generated JSON. Other than
that the overall idea makes sense. Kind of machine-friendly "BPF
object schema" to allow automation.

> Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> ---
>  tools/bpf/bpftool/gen.c | 95 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 95 insertions(+)
>
> diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
> index cf8b4e525c88..818a5209b3ac 100644
> --- a/tools/bpf/bpftool/gen.c
> +++ b/tools/bpf/bpftool/gen.c
> @@ -904,6 +904,96 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li
>         }
>  }
>
> +static int gen_json(struct bpf_object *obj, const char *obj_name, size_t file_sz, uint8_t *obj_data)
> +{
> +       struct bpf_program *prog;
> +       struct bpf_map *map;
> +       char ident[256];
> +
> +       jsonw_start_object(json_wtr);   /* root object */
> +
> +       jsonw_string_field(json_wtr, "name", obj_name);
> +
> +       jsonw_bool_field(json_wtr, "use_loader", use_loader);
> +
> +       /* print all maps */
> +       jsonw_name(json_wtr, "maps");
> +       jsonw_start_array(json_wtr);
> +       bpf_object__for_each_map(map, obj) {
> +               if (!get_map_ident(map, ident, sizeof(ident))) {
> +                       p_err("ignoring unrecognized internal map '%s'...",
> +                             bpf_map__name(map));
> +                       continue;
> +               }
> +
> +               jsonw_start_object(json_wtr);   /* map object */
> +               jsonw_string_field(json_wtr, "ident", ident);
> +               jsonw_string_field(json_wtr, "name", bpf_map__name(map));
> +
> +               /* print mmap data value */
> +               if (is_internal_mmapable_map(map, ident, sizeof(ident))) {
> +                       const void *mmap_data = NULL;
> +                       size_t mmap_size = 0;
> +
> +                       mmap_data = bpf_map__initial_value(map, &mmap_size);
> +
> +                       jsonw_uint_field(json_wtr, "size", mmap_size);
> +                       jsonw_uint_field(json_wtr, "mmap_sz", bpf_map_mmap_sz(map));
> +                       jsonw_name(json_wtr, "data");
> +                       print_hex_data_json((uint8_t *)mmap_data, mmap_size);
> +
> +               }
> +               jsonw_end_object(json_wtr);     /* map object */
> +       }
> +       jsonw_end_array(json_wtr);
> +
> +       /* print all progs */
> +       jsonw_name(json_wtr, "progs");
> +       jsonw_start_array(json_wtr);
> +       bpf_object__for_each_program(prog, obj) {
> +               jsonw_start_object(json_wtr);   /* prog object */
> +               jsonw_string_field(json_wtr, "name", bpf_program__name(prog));
> +               jsonw_string_field(json_wtr, "sec", bpf_program__section_name(prog));
> +               jsonw_end_object(json_wtr);     /* prog object */
> +       }
> +       jsonw_end_array(json_wtr);
> +
> +       /* print object data */
> +       if (use_loader) {
> +               DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
> +               int err = 0;
> +
> +               err = bpf_object__gen_loader(obj, &opts);
> +               if (err)
> +                       return err;
> +
> +               err = bpf_object__load(obj);
> +               if (err) {
> +                       p_err("failed to load object file");
> +                       return err;
> +               }
> +               /* If there was no error during load then gen_loader_opts
> +                * are populated with the loader program.
> +                */
> +
> +               jsonw_uint_field(json_wtr, "data_sz", opts.data_sz);
> +               jsonw_name(json_wtr, "data");
> +               print_hex_data_json((uint8_t *)opts.data, opts.data_sz);
> +
> +               jsonw_uint_field(json_wtr, "insns_sz", opts.insns_sz);
> +               jsonw_name(json_wtr, "insns");
> +               print_hex_data_json((uint8_t *)opts.insns, opts.insns_sz);
> +
> +       } else {
> +               jsonw_name(json_wtr, "data");
> +               print_hex_data_json(obj_data, file_sz);
> +       }
> +
> +       jsonw_end_object(json_wtr);     /* root object */
> +
> +       return 0;
> +}
> +
>  static int do_skeleton(int argc, char **argv)
>  {
>         char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
> @@ -986,6 +1076,11 @@ static int do_skeleton(int argc, char **argv)
>                 goto out;
>         }
>
> +       if (json_output) {
> +               err = gen_json(obj, obj_name, file_sz, (uint8_t *)obj_data);
> +               goto out;
> +       }
> +
>         bpf_object__for_each_map(map, obj) {
>                 if (!get_map_ident(map, ident, sizeof(ident))) {
>                         p_err("ignoring unrecognized internal map '%s'...",
> --
> 2.38.1
>
Benjamin Tissoires Dec. 1, 2022, 2:22 p.m. UTC | #2
On Thu, Dec 1, 2022 at 12:06 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Nov 24, 2022 at 7:16 AM Benjamin Tissoires
> <benjamin.tissoires@redhat.com> wrote:
> >
> > So we can then build light skeletons with loaders in any language.
> >
>
> It would be useful to include an example generated JSON. Other than
> that the overall idea makes sense. Kind of machine-friendly "BPF
> object schema" to allow automation.
>

Great :)
I'll add the generated example in v2 then.

However, I have problems figuring out how I can hit the
"codegen_datasecs()" path. I think that's the only code path I am not
duplicating from the do_skeleton() function, but I don't know what
kind of .bpf.c program will trigger that part.

Also, should I add some tests for it in tools/testing/selftests/bpf?

Cheers,
Benjamin

> > Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> > ---
> >  tools/bpf/bpftool/gen.c | 95 +++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 95 insertions(+)
> >
> > diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
> > index cf8b4e525c88..818a5209b3ac 100644
> > --- a/tools/bpf/bpftool/gen.c
> > +++ b/tools/bpf/bpftool/gen.c
> > @@ -904,6 +904,96 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li
> >         }
> >  }
> >
> > +static int gen_json(struct bpf_object *obj, const char *obj_name, size_t file_sz, uint8_t *obj_data)
> > +{
> > +       struct bpf_program *prog;
> > +       struct bpf_map *map;
> > +       char ident[256];
> > +
> > +       jsonw_start_object(json_wtr);   /* root object */
> > +
> > +       jsonw_string_field(json_wtr, "name", obj_name);
> > +
> > +       jsonw_bool_field(json_wtr, "use_loader", use_loader);
> > +
> > +       /* print all maps */
> > +       jsonw_name(json_wtr, "maps");
> > +       jsonw_start_array(json_wtr);
> > +       bpf_object__for_each_map(map, obj) {
> > +               if (!get_map_ident(map, ident, sizeof(ident))) {
> > +                       p_err("ignoring unrecognized internal map '%s'...",
> > +                             bpf_map__name(map));
> > +                       continue;
> > +               }
> > +
> > +               jsonw_start_object(json_wtr);   /* map object */
> > +               jsonw_string_field(json_wtr, "ident", ident);
> > +               jsonw_string_field(json_wtr, "name", bpf_map__name(map));
> > +
> > +               /* print mmap data value */
> > +               if (is_internal_mmapable_map(map, ident, sizeof(ident))) {
> > +                       const void *mmap_data = NULL;
> > +                       size_t mmap_size = 0;
> > +
> > +                       mmap_data = bpf_map__initial_value(map, &mmap_size);
> > +
> > +                       jsonw_uint_field(json_wtr, "size", mmap_size);
> > +                       jsonw_uint_field(json_wtr, "mmap_sz", bpf_map_mmap_sz(map));
> > +                       jsonw_name(json_wtr, "data");
> > +                       print_hex_data_json((uint8_t *)mmap_data, mmap_size);
> > +
> > +               }
> > +               jsonw_end_object(json_wtr);     /* map object */
> > +       }
> > +       jsonw_end_array(json_wtr);
> > +
> > +       /* print all progs */
> > +       jsonw_name(json_wtr, "progs");
> > +       jsonw_start_array(json_wtr);
> > +       bpf_object__for_each_program(prog, obj) {
> > +               jsonw_start_object(json_wtr);   /* prog object */
> > +               jsonw_string_field(json_wtr, "name", bpf_program__name(prog));
> > +               jsonw_string_field(json_wtr, "sec", bpf_program__section_name(prog));
> > +               jsonw_end_object(json_wtr);     /* prog object */
> > +       }
> > +       jsonw_end_array(json_wtr);
> > +
> > +       /* print object data */
> > +       if (use_loader) {
> > +               DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
> > +               int err = 0;
> > +
> > +               err = bpf_object__gen_loader(obj, &opts);
> > +               if (err)
> > +                       return err;
> > +
> > +               err = bpf_object__load(obj);
> > +               if (err) {
> > +                       p_err("failed to load object file");
> > +                       return err;
> > +               }
> > +               /* If there was no error during load then gen_loader_opts
> > +                * are populated with the loader program.
> > +                */
> > +
> > +               jsonw_uint_field(json_wtr, "data_sz", opts.data_sz);
> > +               jsonw_name(json_wtr, "data");
> > +               print_hex_data_json((uint8_t *)opts.data, opts.data_sz);
> > +
> > +               jsonw_uint_field(json_wtr, "insns_sz", opts.insns_sz);
> > +               jsonw_name(json_wtr, "insns");
> > +               print_hex_data_json((uint8_t *)opts.insns, opts.insns_sz);
> > +
> > +       } else {
> > +               jsonw_name(json_wtr, "data");
> > +               print_hex_data_json(obj_data, file_sz);
> > +       }
> > +
> > +       jsonw_end_object(json_wtr);     /* root object */
> > +
> > +       return 0;
> > +}
> > +
> >  static int do_skeleton(int argc, char **argv)
> >  {
> >         char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
> > @@ -986,6 +1076,11 @@ static int do_skeleton(int argc, char **argv)
> >                 goto out;
> >         }
> >
> > +       if (json_output) {
> > +               err = gen_json(obj, obj_name, file_sz, (uint8_t *)obj_data);
> > +               goto out;
> > +       }
> > +
> >         bpf_object__for_each_map(map, obj) {
> >                 if (!get_map_ident(map, ident, sizeof(ident))) {
> >                         p_err("ignoring unrecognized internal map '%s'...",
> > --
> > 2.38.1
> >
>
Andrii Nakryiko Dec. 1, 2022, 6:21 p.m. UTC | #3
On Thu, Dec 1, 2022 at 6:23 AM Benjamin Tissoires
<benjamin.tissoires@redhat.com> wrote:
>
> On Thu, Dec 1, 2022 at 12:06 AM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Thu, Nov 24, 2022 at 7:16 AM Benjamin Tissoires
> > <benjamin.tissoires@redhat.com> wrote:
> > >
> > > So we can then build light skeletons with loaders in any language.
> > >
> >
> > It would be useful to include an example generated JSON. Other than
> > that the overall idea makes sense. Kind of machine-friendly "BPF
> > object schema" to allow automation.
> >
>
> Great :)
> I'll add the generated example in v2 then.
>
> However, I have problems figuring out how I can hit the
> "codegen_datasecs()" path. I think that's the only code path I am not
> duplicating from the do_skeleton() function, but I don't know what
> kind of .bpf.c program will trigger that part.
>

You'll hit this whenever you have global variables in your .bpf.c
code. It's very important part, so let's make sure it is covered. Try
using both `const volatile` read-only variables (they will end up in
.rodata datasec) and global non-read-only variables (they will go into
.data, if initialized to non-zero defaults, or .bss if
zero-initialized).

> Also, should I add some tests for it in tools/testing/selftests/bpf?

Yes, it probably makes sense to check expected JSON output is
generated for a few example BPF object files.

>
> Cheers,
> Benjamin
>
> > > Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> > > ---
> > >  tools/bpf/bpftool/gen.c | 95 +++++++++++++++++++++++++++++++++++++++++
> > >  1 file changed, 95 insertions(+)
> > >
> > > diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
> > > index cf8b4e525c88..818a5209b3ac 100644
> > > --- a/tools/bpf/bpftool/gen.c
> > > +++ b/tools/bpf/bpftool/gen.c
> > > @@ -904,6 +904,96 @@ codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li
> > >         }
> > >  }
> > >
> > > +static int gen_json(struct bpf_object *obj, const char *obj_name, size_t file_sz, uint8_t *obj_data)
> > > +{
> > > +       struct bpf_program *prog;
> > > +       struct bpf_map *map;
> > > +       char ident[256];
> > > +
> > > +       jsonw_start_object(json_wtr);   /* root object */
> > > +
> > > +       jsonw_string_field(json_wtr, "name", obj_name);
> > > +
> > > +       jsonw_bool_field(json_wtr, "use_loader", use_loader);
> > > +
> > > +       /* print all maps */
> > > +       jsonw_name(json_wtr, "maps");
> > > +       jsonw_start_array(json_wtr);
> > > +       bpf_object__for_each_map(map, obj) {
> > > +               if (!get_map_ident(map, ident, sizeof(ident))) {
> > > +                       p_err("ignoring unrecognized internal map '%s'...",
> > > +                             bpf_map__name(map));
> > > +                       continue;
> > > +               }
> > > +
> > > +               jsonw_start_object(json_wtr);   /* map object */
> > > +               jsonw_string_field(json_wtr, "ident", ident);
> > > +               jsonw_string_field(json_wtr, "name", bpf_map__name(map));
> > > +
> > > +               /* print mmap data value */
> > > +               if (is_internal_mmapable_map(map, ident, sizeof(ident))) {
> > > +                       const void *mmap_data = NULL;
> > > +                       size_t mmap_size = 0;
> > > +
> > > +                       mmap_data = bpf_map__initial_value(map, &mmap_size);
> > > +
> > > +                       jsonw_uint_field(json_wtr, "size", mmap_size);
> > > +                       jsonw_uint_field(json_wtr, "mmap_sz", bpf_map_mmap_sz(map));
> > > +                       jsonw_name(json_wtr, "data");
> > > +                       print_hex_data_json((uint8_t *)mmap_data, mmap_size);
> > > +
> > > +               }
> > > +               jsonw_end_object(json_wtr);     /* map object */
> > > +       }
> > > +       jsonw_end_array(json_wtr);
> > > +
> > > +       /* print all progs */
> > > +       jsonw_name(json_wtr, "progs");
> > > +       jsonw_start_array(json_wtr);
> > > +       bpf_object__for_each_program(prog, obj) {
> > > +               jsonw_start_object(json_wtr);   /* prog object */
> > > +               jsonw_string_field(json_wtr, "name", bpf_program__name(prog));
> > > +               jsonw_string_field(json_wtr, "sec", bpf_program__section_name(prog));
> > > +               jsonw_end_object(json_wtr);     /* prog object */
> > > +       }
> > > +       jsonw_end_array(json_wtr);
> > > +
> > > +       /* print object data */
> > > +       if (use_loader) {
> > > +               DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
> > > +               int err = 0;
> > > +
> > > +               err = bpf_object__gen_loader(obj, &opts);
> > > +               if (err)
> > > +                       return err;
> > > +
> > > +               err = bpf_object__load(obj);
> > > +               if (err) {
> > > +                       p_err("failed to load object file");
> > > +                       return err;
> > > +               }
> > > +               /* If there was no error during load then gen_loader_opts
> > > +                * are populated with the loader program.
> > > +                */
> > > +
> > > +               jsonw_uint_field(json_wtr, "data_sz", opts.data_sz);
> > > +               jsonw_name(json_wtr, "data");
> > > +               print_hex_data_json((uint8_t *)opts.data, opts.data_sz);
> > > +
> > > +               jsonw_uint_field(json_wtr, "insns_sz", opts.insns_sz);
> > > +               jsonw_name(json_wtr, "insns");
> > > +               print_hex_data_json((uint8_t *)opts.insns, opts.insns_sz);
> > > +
> > > +       } else {
> > > +               jsonw_name(json_wtr, "data");
> > > +               print_hex_data_json(obj_data, file_sz);
> > > +       }
> > > +
> > > +       jsonw_end_object(json_wtr);     /* root object */
> > > +
> > > +       return 0;
> > > +}
> > > +
> > >  static int do_skeleton(int argc, char **argv)
> > >  {
> > >         char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
> > > @@ -986,6 +1076,11 @@ static int do_skeleton(int argc, char **argv)
> > >                 goto out;
> > >         }
> > >
> > > +       if (json_output) {
> > > +               err = gen_json(obj, obj_name, file_sz, (uint8_t *)obj_data);
> > > +               goto out;
> > > +       }
> > > +
> > >         bpf_object__for_each_map(map, obj) {
> > >                 if (!get_map_ident(map, ident, sizeof(ident))) {
> > >                         p_err("ignoring unrecognized internal map '%s'...",
> > > --
> > > 2.38.1
> > >
> >
>
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index cf8b4e525c88..818a5209b3ac 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -904,6 +904,96 @@  codegen_progs_skeleton(struct bpf_object *obj, size_t prog_cnt, bool populate_li
 	}
 }
 
+static int gen_json(struct bpf_object *obj, const char *obj_name, size_t file_sz, uint8_t *obj_data)
+{
+	struct bpf_program *prog;
+	struct bpf_map *map;
+	char ident[256];
+
+	jsonw_start_object(json_wtr);	/* root object */
+
+	jsonw_string_field(json_wtr, "name", obj_name);
+
+	jsonw_bool_field(json_wtr, "use_loader", use_loader);
+
+	/* print all maps */
+	jsonw_name(json_wtr, "maps");
+	jsonw_start_array(json_wtr);
+	bpf_object__for_each_map(map, obj) {
+		if (!get_map_ident(map, ident, sizeof(ident))) {
+			p_err("ignoring unrecognized internal map '%s'...",
+			      bpf_map__name(map));
+			continue;
+		}
+
+		jsonw_start_object(json_wtr);	/* map object */
+		jsonw_string_field(json_wtr, "ident", ident);
+		jsonw_string_field(json_wtr, "name", bpf_map__name(map));
+
+		/* print mmap data value */
+		if (is_internal_mmapable_map(map, ident, sizeof(ident))) {
+			const void *mmap_data = NULL;
+			size_t mmap_size = 0;
+
+			mmap_data = bpf_map__initial_value(map, &mmap_size);
+
+			jsonw_uint_field(json_wtr, "size", mmap_size);
+			jsonw_uint_field(json_wtr, "mmap_sz", bpf_map_mmap_sz(map));
+			jsonw_name(json_wtr, "data");
+			print_hex_data_json((uint8_t *)mmap_data, mmap_size);
+
+		}
+		jsonw_end_object(json_wtr);	/* map object */
+	}
+	jsonw_end_array(json_wtr);
+
+	/* print all progs */
+	jsonw_name(json_wtr, "progs");
+	jsonw_start_array(json_wtr);
+	bpf_object__for_each_program(prog, obj) {
+		jsonw_start_object(json_wtr);	/* prog object */
+		jsonw_string_field(json_wtr, "name", bpf_program__name(prog));
+		jsonw_string_field(json_wtr, "sec", bpf_program__section_name(prog));
+		jsonw_end_object(json_wtr);	/* prog object */
+	}
+	jsonw_end_array(json_wtr);
+
+	/* print object data */
+	if (use_loader) {
+		DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
+		int err = 0;
+
+		err = bpf_object__gen_loader(obj, &opts);
+		if (err)
+			return err;
+
+		err = bpf_object__load(obj);
+		if (err) {
+			p_err("failed to load object file");
+			return err;
+		}
+		/* If there was no error during load then gen_loader_opts
+		 * are populated with the loader program.
+		 */
+
+		jsonw_uint_field(json_wtr, "data_sz", opts.data_sz);
+		jsonw_name(json_wtr, "data");
+		print_hex_data_json((uint8_t *)opts.data, opts.data_sz);
+
+		jsonw_uint_field(json_wtr, "insns_sz", opts.insns_sz);
+		jsonw_name(json_wtr, "insns");
+		print_hex_data_json((uint8_t *)opts.insns, opts.insns_sz);
+
+	} else {
+		jsonw_name(json_wtr, "data");
+		print_hex_data_json(obj_data, file_sz);
+	}
+
+	jsonw_end_object(json_wtr);	/* root object */
+
+	return 0;
+}
+
 static int do_skeleton(int argc, char **argv)
 {
 	char header_guard[MAX_OBJ_NAME_LEN + sizeof("__SKEL_H__")];
@@ -986,6 +1076,11 @@  static int do_skeleton(int argc, char **argv)
 		goto out;
 	}
 
+	if (json_output) {
+		err = gen_json(obj, obj_name, file_sz, (uint8_t *)obj_data);
+		goto out;
+	}
+
 	bpf_object__for_each_map(map, obj) {
 		if (!get_map_ident(map, ident, sizeof(ident))) {
 			p_err("ignoring unrecognized internal map '%s'...",