linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benjamin Tissoires <benjamin.tissoires@redhat.com>
To: Greg KH <gregkh@linuxfoundation.org>,
	Jiri Kosina <jikos@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>
Cc: Tero Kristo <tero.kristo@linux.intel.com>,
	linux-kernel@vger.kernel.org, linux-input@vger.kernel.org,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	Benjamin Tissoires <benjamin.tissoires@redhat.com>
Subject: [RFC hid v1 01/10] bpftool: generate json output of skeletons
Date: Thu, 24 Nov 2022 16:15:54 +0100	[thread overview]
Message-ID: <20221124151603.807536-2-benjamin.tissoires@redhat.com> (raw)
In-Reply-To: <20221124151603.807536-1-benjamin.tissoires@redhat.com>

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(+)

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


  reply	other threads:[~2022-11-24 15:18 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-24 15:15 [RFC hid v1 00/10] HID-BPF: add support for in-tree BPF programs Benjamin Tissoires
2022-11-24 15:15 ` Benjamin Tissoires [this message]
2022-11-30 23:05   ` [RFC hid v1 01/10] bpftool: generate json output of skeletons Andrii Nakryiko
2022-12-01 14:22     ` Benjamin Tissoires
2022-12-01 18:21       ` Andrii Nakryiko
2022-11-24 15:15 ` [RFC hid v1 02/10] WIP: bpf: allow to pin programs from the kernel when bpffs is mounted Benjamin Tissoires
2022-11-24 15:15 ` [RFC hid v1 03/10] HID: add a tool to convert a bpf source into a generic bpf loader Benjamin Tissoires
2022-11-24 15:15 ` [RFC hid v1 04/10] HID: add the bpf loader that can attach a generic hid-bpf program Benjamin Tissoires
2022-11-24 15:15 ` [RFC hid v1 05/10] HID: add report descriptor override for the X-Keys XK24 Benjamin Tissoires
2022-11-24 15:15 ` [RFC hid v1 06/10] selftests: hid: add vmtest.sh Benjamin Tissoires
2022-11-24 15:16 ` [RFC hid v1 07/10] selftests: hid: Add a variant parameter so we can emulate specific devices Benjamin Tissoires
2022-11-24 15:16 ` [RFC hid v1 08/10] selftests: hid: add XK-24 tests Benjamin Tissoires
2022-11-24 15:16 ` [RFC hid v1 09/10] selftests: hid: ensure the program is correctly pinned Benjamin Tissoires
2022-11-24 15:16 ` [RFC hid v1 10/10] wip: vmtest aarch64 Benjamin Tissoires

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221124151603.807536-2-benjamin.tissoires@redhat.com \
    --to=benjamin.tissoires@redhat.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dmitry.torokhov@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=tero.kristo@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).