Commit a9436460 authored by Jiri Olsa's avatar Jiri Olsa Committed by Daniel Borkmann

bpftool: Allow to read btf as raw data

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: default avatarJiri Olsa <jolsa@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Acked-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Link: https://lore.kernel.org/bpf/20191024133025.10691-1-jolsa@kernel.org
parent 38207291
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
#include <libbpf.h> #include <libbpf.h>
#include <linux/btf.h> #include <linux/btf.h>
#include <linux/hashtable.h> #include <linux/hashtable.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "btf.h" #include "btf.h"
#include "json_writer.h" #include "json_writer.h"
...@@ -388,6 +391,54 @@ static int dump_btf_c(const struct btf *btf, ...@@ -388,6 +391,54 @@ static int dump_btf_c(const struct btf *btf,
return err; 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) static int do_dump(int argc, char **argv)
{ {
struct btf *btf = NULL; struct btf *btf = NULL;
...@@ -465,7 +516,11 @@ static int do_dump(int argc, char **argv) ...@@ -465,7 +516,11 @@ static int do_dump(int argc, char **argv)
} }
NEXT_ARG(); NEXT_ARG();
} else if (is_prefix(src, "file")) { } 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)) { if (IS_ERR(btf)) {
err = PTR_ERR(btf); err = PTR_ERR(btf);
btf = NULL; btf = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment