diff mbox series

bpftool: Allow to read btf as raw data

Message ID 20191024133025.10691-1-jolsa@kernel.org
State Accepted
Delegated to: BPF Maintainers
Headers show
Series bpftool: Allow to read btf as raw data | expand

Commit Message

Jiri Olsa Oct. 24, 2019, 1:30 p.m. UTC
The bpftool interface stays the same, but now it's possible
to run it over BTF raw data, like:

  $ bpftool btf dump file /sys/kernel/btf/vmlinux
  [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
  [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
  [3] CONST '(anon)' type_id=2

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
v3 changes:
 - fix title

v2 changes:
 - added is_btf_raw to find out which btf__parse_* function to call
 - changed labels and error propagation in btf__parse_raw
 - drop the err initialization, which is not needed under this change

 tools/bpf/bpftool/btf.c | 57 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)

Comments

Jakub Kicinski Oct. 24, 2019, 5:55 p.m. UTC | #1
On Thu, 24 Oct 2019 15:30:25 +0200, Jiri Olsa wrote:
> The bpftool interface stays the same, but now it's possible
> to run it over BTF raw data, like:
> 
>   $ bpftool btf dump file /sys/kernel/btf/vmlinux
>   [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
>   [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
>   [3] CONST '(anon)' type_id=2
> 
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
> v3 changes:
>  - fix title
> 
> v2 changes:
>  - added is_btf_raw to find out which btf__parse_* function to call
>  - changed labels and error propagation in btf__parse_raw
>  - drop the err initialization, which is not needed under this change


Aw, this is v3? Looks like I replied to the older now, such confusion :)
Andrii Nakryiko Oct. 25, 2019, 5:10 a.m. UTC | #2
On 10/24/19 6:30 AM, Jiri Olsa wrote:
> The bpftool interface stays the same, but now it's possible
> to run it over BTF raw data, like:
> 
>    $ bpftool btf dump file /sys/kernel/btf/vmlinux
>    [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
>    [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
>    [3] CONST '(anon)' type_id=2
> 
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
> v3 changes:
>   - fix title

The subject (by now) should be "[PATCH v3 bpf-next] ...". Include both 
version and intended tree (bpf-next usually).

> 
> v2 changes:
>   - added is_btf_raw to find out which btf__parse_* function to call
>   - changed labels and error propagation in btf__parse_raw
>   - drop the err initialization, which is not needed under this change
> 
>   tools/bpf/bpftool/btf.c | 57 ++++++++++++++++++++++++++++++++++++++++-
>   1 file changed, 56 insertions(+), 1 deletion(-)
> 


Overall looks good. It's kind of sad that we re-open that file twice, 
but we can optimize that later, if it ever is a problem. We should 
probably eventually implement btf__parse_raw as libbpf API with mmap2().
But perfect is the enemy of the good, so:

Acked-by: Andrii Nakryiko <andriin@fb.com>

> diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
> index 9a9376d1d3df..a7b8bf233cf5 100644
> --- a/tools/bpf/bpftool/btf.c
> +++ b/tools/bpf/bpftool/btf.c
> @@ -12,6 +12,9 @@

[...]
Jiri Olsa Oct. 25, 2019, 7:15 a.m. UTC | #3
On Thu, Oct 24, 2019 at 10:55:37AM -0700, Jakub Kicinski wrote:
> On Thu, 24 Oct 2019 15:30:25 +0200, Jiri Olsa wrote:
> > The bpftool interface stays the same, but now it's possible
> > to run it over BTF raw data, like:
> > 
> >   $ bpftool btf dump file /sys/kernel/btf/vmlinux
> >   [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
> >   [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
> >   [3] CONST '(anon)' type_id=2
> > 
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> > v3 changes:
> >  - fix title
> > 
> > v2 changes:
> >  - added is_btf_raw to find out which btf__parse_* function to call
> >  - changed labels and error propagation in btf__parse_raw
> >  - drop the err initialization, which is not needed under this change
> 
> 
> Aw, this is v3? Looks like I replied to the older now, such confusion :)

yea, that went well.. sry, missed the v3 in title

jirka
Jakub Kicinski Oct. 25, 2019, 6:39 p.m. UTC | #4
On Thu, 24 Oct 2019 15:30:25 +0200, Jiri Olsa wrote:
> The bpftool interface stays the same, but now it's possible
> to run it over BTF raw data, like:
> 
>   $ bpftool btf dump file /sys/kernel/btf/vmlinux
>   [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
>   [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
>   [3] CONST '(anon)' type_id=2
> 
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>

Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Jiri Olsa Oct. 25, 2019, 8:03 p.m. UTC | #5
On Fri, Oct 25, 2019 at 11:39:01AM -0700, Jakub Kicinski wrote:
> On Thu, 24 Oct 2019 15:30:25 +0200, Jiri Olsa wrote:
> > The bpftool interface stays the same, but now it's possible
> > to run it over BTF raw data, like:
> > 
> >   $ bpftool btf dump file /sys/kernel/btf/vmlinux
> >   [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
> >   [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
> >   [3] CONST '(anon)' type_id=2
> > 
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> 
> Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>

[root@ibm-z-107 bpftool]# ./bpftool btf dump file /sys/kernel/btf/vmlinux  | head -3
[1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
[2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
[3] CONST '(anon)' type_id=2
[root@ibm-z-107 bpftool]# lscpu | grep Endian
Byte Order:          Big Endian

thanks,
jirka
Daniel Borkmann Oct. 25, 2019, 9:40 p.m. UTC | #6
On Thu, Oct 24, 2019 at 03:30:25PM +0200, Jiri Olsa wrote:
> The bpftool interface stays the same, but now it's possible
> to run it over BTF raw data, like:
> 
>   $ bpftool btf dump file /sys/kernel/btf/vmlinux
>   [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
>   [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
>   [3] CONST '(anon)' type_id=2
> 
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>

Applied, thanks!
Jakub Kicinski Oct. 26, 2019, 6:42 p.m. UTC | #7
On Fri, 25 Oct 2019 22:03:51 +0200, Jiri Olsa wrote:
> On Fri, Oct 25, 2019 at 11:39:01AM -0700, Jakub Kicinski wrote:
> > On Thu, 24 Oct 2019 15:30:25 +0200, Jiri Olsa wrote:  
> > > The bpftool interface stays the same, but now it's possible
> > > to run it over BTF raw data, like:
> > > 
> > >   $ bpftool btf dump file /sys/kernel/btf/vmlinux
> > >   [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
> > >   [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
> > >   [3] CONST '(anon)' type_id=2
> > > 
> > > Signed-off-by: Jiri Olsa <jolsa@kernel.org>  
> > 
> > Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>  
> 
> [root@ibm-z-107 bpftool]# ./bpftool btf dump file /sys/kernel/btf/vmlinux  | head -3
> [1] INT '(anon)' size=4 bits_offset=0 nr_bits=32 encoding=(none)
> [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64 encoding=(none)
> [3] CONST '(anon)' type_id=2
> [root@ibm-z-107 bpftool]# lscpu | grep Endian
> Byte Order:          Big Endian

Thanks for checking! I thought the on-disk format is fixed like the
ELF magic number. But if two bads make a right then all is good ☺️
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
index 9a9376d1d3df..a7b8bf233cf5 100644
--- a/tools/bpf/bpftool/btf.c
+++ b/tools/bpf/bpftool/btf.c
@@ -12,6 +12,9 @@ 
 #include <libbpf.h>
 #include <linux/btf.h>
 #include <linux/hashtable.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #include "btf.h"
 #include "json_writer.h"
@@ -388,6 +391,54 @@  static int dump_btf_c(const struct btf *btf,
 	return err;
 }
 
+static struct btf *btf__parse_raw(const char *file)
+{
+	struct btf *btf;
+	struct stat st;
+	__u8 *buf;
+	FILE *f;
+
+	if (stat(file, &st))
+		return NULL;
+
+	f = fopen(file, "rb");
+	if (!f)
+		return NULL;
+
+	buf = malloc(st.st_size);
+	if (!buf) {
+		btf = ERR_PTR(-ENOMEM);
+		goto exit_close;
+	}
+
+	if ((size_t) st.st_size != fread(buf, 1, st.st_size, f)) {
+		btf = ERR_PTR(-EINVAL);
+		goto exit_free;
+	}
+
+	btf = btf__new(buf, st.st_size);
+
+exit_free:
+	free(buf);
+exit_close:
+	fclose(f);
+	return btf;
+}
+
+static bool is_btf_raw(const char *file)
+{
+	__u16 magic = 0;
+	int fd;
+
+	fd = open(file, O_RDONLY);
+	if (fd < 0)
+		return false;
+
+	read(fd, &magic, sizeof(magic));
+	close(fd);
+	return magic == BTF_MAGIC;
+}
+
 static int do_dump(int argc, char **argv)
 {
 	struct btf *btf = NULL;
@@ -465,7 +516,11 @@  static int do_dump(int argc, char **argv)
 		}
 		NEXT_ARG();
 	} else if (is_prefix(src, "file")) {
-		btf = btf__parse_elf(*argv, NULL);
+		if (is_btf_raw(*argv))
+			btf = btf__parse_raw(*argv);
+		else
+			btf = btf__parse_elf(*argv, NULL);
+
 		if (IS_ERR(btf)) {
 			err = PTR_ERR(btf);
 			btf = NULL;