bpftool: Try to read btf as raw data if elf read fails
Subsystems (4)
Jiri Olsa
reviewerPATCH 1/1 bpftool: Try to read btf as raw data if elf read fails
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 libbpf: failed to get EHDR from /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
I'm also adding err init to 0 because I was getting uninitialized warnings from gcc.
--- tools/bpf/bpftool/btf.c | 47 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-)
tools/bpf/bpftool/btf.c +42 -5
@@ -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,35 @@ static int dump_btf_c(const struct btf *btf,return err; } +static struct btf *btf__parse_raw (const char *file) +{ +struct btf *btf =ERR_PTR (-EINVAL ); +__u8 *buf =NULL ; +struct stat st; +FILE *f; + +if (stat (file, &st)) +return btf; + + f =fopen (file,"rb" ); +if (!f) +return btf; + + buf =malloc (st.st_size); +if (!buf) + goto err; + +if ((size_t ) st.st_size !=fread (buf,1 , st.st_size, f)) + goto err; + + btf =btf__new (buf, st.st_size); + +err: +free (buf); +fclose (f); +return btf; +} +static int do_dump (int argc,char **argv) {struct btf *btf =NULL ;
@@ -397,7 +429,7 @@ static int do_dump(int argc, char **argv)__u32 btf_id =-1 ;const char *src;int fd =-1 ; -int err; +int err =0 ;if (!REQ_ARGS (2 )) {usage ();
@@ -468,10 +500,15 @@ static int do_dump(int argc, char **argv) btf =btf__parse_elf (*argv,NULL );if (IS_ERR (btf)) { err =PTR_ERR (btf); - btf =NULL ; -p_err ("failed to load BTF from %s: %s" , - *argv,strerror (err)); - goto done; +if (err == -LIBBPF_ERRNO__FORMAT ) + btf =btf__parse_raw (*argv); +if (IS_ERR (btf)) { + btf =NULL ; +/* Display the original error value. */ +p_err ("failed to load BTF from %s: %s" , + *argv,strerror (err)); + goto done; + } }NEXT_ARG (); }else {
Signature · 1 line
2.21.0
Quoted 2 lines · Jiri Olsa
The bpftool interface stays the same, but now it's possible to run it over BTF raw data, like:
Oh, great, I had similar patch laying around for a while, never got to cleaning it up, though, so thanks for picking this up!
Quoted 3 lines
$ bpftool btf dump file /sys/kernel/btf/vmlinux libbpf: failed to get EHDR from /sys/kernel/btf/vmlinux
We should implement this so that we don't get an extra log output with errors. I've been thinking about checking first few bytes of the file. If that matches BTF_MAGIC, then try to parse it as raw BTF, otherwise parse as ELF w/ BTF. Does it make sense?
Quoted 92 lines
[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=2I'm also adding err init to 0 because I was getting uninitialized warnings from gcc.
Signed-off-by Jiri Olsa <[email protected]>--- tools/bpf/bpftool/btf.c | 47 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-)tools/bpf/bpftool/btf.c +42
-5@@ -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,35 @@ static int dump_btf_c(const struct btf *btf,return err; } +static struct btf *btf__parse_raw (const char *file) +{ +struct btf *btf =ERR_PTR (-EINVAL ); +__u8 *buf =NULL ; +struct stat st; +FILE *f; + +if (stat (file, &st)) +return btf; + + f =fopen (file,"rb" ); +if (!f) +return btf; + + buf =malloc (st.st_size); +if (!buf) + goto err; + +if ((size_t ) st.st_size !=fread (buf,1 , st.st_size, f)) + goto err; + + btf =btf__new (buf, st.st_size); + +err: +free (buf); +fclose (f); +return btf; +} +static int do_dump (int argc,char **argv) {struct btf *btf =NULL ;@@ -397,7 +429,7 @@ static int do_dump(int argc, char **argv)__u32 btf_id =-1 ;const char *src;int fd =-1 ; -int err; +int err =0 ;if (!REQ_ARGS (2 )) {usage ();@@ -468,10 +500,15 @@ static int do_dump(int argc, char **argv) btf =btf__parse_elf (*argv,NULL );if (IS_ERR (btf)) { err =PTR_ERR (btf); - btf =NULL ; -p_err ("failed to load BTF from %s: %s" , - *argv,strerror (err)); - goto done; +if (err == -LIBBPF_ERRNO__FORMAT ) + btf =btf__parse_raw (*argv); +if (IS_ERR (btf)) { + btf =NULL ; +/* Display the original error value. */ +p_err ("failed to load BTF from %s: %s" , + *argv,strerror (err)); + goto done; + } }NEXT_ARG (); }else {
Quoted 8 lines · Andrii Nakryiko
Quoted 2 lines · Jiri Olsa
The bpftool interface stays the same, but now it's possible to run it over BTF raw data, like:
Oh, great, I had similar patch laying around for a while, never got to cleaning it up, though, so thanks for picking this up!
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
$ bpftool btf dump file /sys/kernel/btf/vmlinux libbpf: failed to get EHDR from /sys/kernel/btf/vmlinuxWe should implement this so that we don't get an extra log output with errors. I've been thinking about checking first few bytes of the file. If that matches
BTF_MAGIC, then try to parse it as raw BTF, otherwise parse as ELF w/ BTF. Does it make sense?
Agreed, this makes sense. We should not emit errors in such cases. One minor comment below.
Quoted 47 lines
Quoted 47 lines
[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=2I'm also adding err init to 0 because I was getting uninitialized warnings from gcc.
Signed-off-by Jiri Olsa <[email protected]>--- tools/bpf/bpftool/btf.c | 47 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-)tools/bpf/bpftool/btf.c +19
-0@@ -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,35 @@ static int dump_btf_c(const struct btf *btf,return err; } +static struct btf *btf__parse_raw (const char *file) +{ +struct btf *btf =ERR_PTR (-EINVAL ); +__u8 *buf =NULL ; +struct stat st; +FILE *f; + +if (stat (file, &st)) +return btf; + + f =fopen (file,"rb" ); +if (!f) +return btf; + + buf =malloc (st.st_size); +if (!buf)
Quoted 8 lines
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 417
Quoted 8 lines
tools/bpf/bpftool/btf.c +8
-0+ goto err; + +if ((size_t ) st.st_size !=fread (buf,1 , st.st_size, f)) + goto err; + + btf =btf__new (buf, st.st_size); + +err:
Non error case can also reach here. Let us change
label to a different name, e.g., "done"?
Quoted 39 lines
Quoted 38 lines
tools/bpf/bpftool/btf.c +15
-5+free (buf); +fclose (f); +return btf; +} +static int do_dump (int argc,char **argv) {struct btf *btf =NULL ;@@ -397,7 +429,7 @@ static int do_dump(int argc, char **argv)__u32 btf_id =-1 ;const char *src;int fd =-1 ; -int err; +int err =0 ;if (!REQ_ARGS (2 )) {usage ();@@ -468,10 +500,15 @@ static int do_dump(int argc, char **argv) btf =btf__parse_elf (*argv,NULL );if (IS_ERR (btf)) { err =PTR_ERR (btf); - btf =NULL ; -p_err ("failed to load BTF from %s: %s" , - *argv,strerror (err)); - goto done; +if (err == -LIBBPF_ERRNO__FORMAT ) + btf =btf__parse_raw (*argv); +if (IS_ERR (btf)) { + btf =NULL ; +/* Display the original error value. */ +p_err ("failed to load BTF from %s: %s" , + *argv,strerror (err)); + goto done; + } }NEXT_ARG (); }else {
On Fri, Oct 18, 2019 at 08:04:44PM +0000, Yonghong Song wrote:
SNIP
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
tools/bpf/bpftool/btf.c +14
-0+FILE *f; + +if (stat (file, &st)) +return btf; + + f =fopen (file,"rb" ); +if (!f) +return btf; + + buf =malloc (st.st_size); +if (!buf) + goto err; + +if ((size_t ) st.st_size !=fread (buf,1 , st.st_size, f))
Quoted 8 lines
Quoted 5 lines
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 417
Quoted 5 lines
tools/bpf/bpftool/btf.c +5
-0+ goto err; + + btf =btf__new (buf, st.st_size); + +err:Non error case can also reach here. Let us change
label to a different name, e.g., "done"?
ok, will change
thanks,
jirka
Quoted 8 lines · Andrii Nakryiko
Quoted 2 lines · Jiri Olsa
The bpftool interface stays the same, but now it's possible to run it over BTF raw data, like:
Oh, great, I had similar patch laying around for a while, never got to cleaning it up, though, so thanks for picking this up!
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
$ bpftool btf dump file /sys/kernel/btf/vmlinux libbpf: failed to get EHDR from /sys/kernel/btf/vmlinuxWe should implement this so that we don't get an extra log output with errors. I've been thinking about checking first few bytes of the file. If that matches
BTF_MAGIC, then try to parse it as raw BTF, otherwise parse as ELF w/ BTF. Does it make sense?
ok, sounds good
jirka
Quoted 32 lines · Jiri Olsa
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 libbpf: failed to get EHDR from /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=2I'm also adding err init to 0 because I was getting uninitialized warnings from gcc.
Signed-off-by Jiri Olsa <[email protected]>--- tools/bpf/bpftool/btf.c | 47 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-)tools/bpf/bpftool/btf.c +3
-0@@ -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"
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 397
Quoted 7 lines
tools/bpf/bpftool/btf.c +4
-0@@ -388,6 +391,35 @@ static int dump_btf_c(const struct btf *btf,return err; } +static struct btf *btf__parse_raw (const char *file) +{ +struct btf *btf =ERR_PTR (-EINVAL ); +__u8 *buf =NULL ;
Please drop the inits
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 402
Quoted 5 lines
tools/bpf/bpftool/btf.c +5
-0+struct stat st; +FILE *f; + +if (stat (file, &st)) +return btf;
And return constants here
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 406
Quoted 3 lines
tools/bpf/bpftool/btf.c +3
-0+ f =fopen (file,"rb" ); +if (!f) +return btf;
and here
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 410
Quoted 3 lines
tools/bpf/bpftool/btf.c +3
-0+ buf =malloc (st.st_size); +if (!buf) + goto err;
and jump to the right place here.
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 417
Quoted 6 lines
tools/bpf/bpftool/btf.c +6
-0+if ((size_t ) st.st_size !=fread (buf,1 , st.st_size, f)) + goto err; + + btf =btf__new (buf, st.st_size); + +err:
The prefix for error labels which is shared with non-error path is exit_
Quoted 9 lines
tools/bpf/bpftool/btf.c +5
-0+free (buf); +fclose (f); +return btf; +} +static int do_dump (int argc,char **argv) {struct btf *btf =NULL ;
tools/bpf/bpftool/btf.c static int do_dump(int argc, char **argv) · line 432
Quoted 5 lines
tools/bpf/bpftool/btf.c +1
-1@@ -397,7 +429,7 @@ static int do_dump(int argc, char **argv)__u32 btf_id =-1 ;const char *src;int fd =-1 ; -int err; +int err =0 ;
This change looks unnecessary.
Quoted 22 lines
tools/bpf/bpftool/btf.c +9
-4if (!REQ_ARGS (2 )) {usage ();@@ -468,10 +500,15 @@ static int do_dump(int argc, char **argv) btf =btf__parse_elf (*argv,NULL );if (IS_ERR (btf)) { err =PTR_ERR (btf); - btf =NULL ; -p_err ("failed to load BTF from %s: %s" , - *argv,strerror (err)); - goto done; +if (err == -LIBBPF_ERRNO__FORMAT ) + btf =btf__parse_raw (*argv); +if (IS_ERR (btf)) { + btf =NULL ; +/* Display the original error value. */ +p_err ("failed to load BTF from %s: %s" , + *argv,strerror (err)); + goto done; + } }NEXT_ARG (); }else {
Quoted 72 lines · Jakub Kicinski
Quoted 32 lines · Jiri Olsa
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 libbpf: failed to get EHDR from /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=2I'm also adding err init to 0 because I was getting uninitialized warnings from gcc.
Signed-off-by Jiri Olsa <[email protected]>--- tools/bpf/bpftool/btf.c | 47 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-)tools/bpf/bpftool/btf.c +3
-0@@ -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" tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 397
Quoted 7 lines
tools/bpf/bpftool/btf.c +4
-0@@ -388,6 +391,35 @@ static int dump_btf_c(const struct btf *btf,return err; } +static struct btf *btf__parse_raw (const char *file) +{ +struct btf *btf =ERR_PTR (-EINVAL ); +__u8 *buf =NULL ;Please drop the inits
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 402
Quoted 5 lines
tools/bpf/bpftool/btf.c +5
-0+struct stat st; +FILE *f; + +if (stat (file, &st)) +return btf;And return constants here
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 406
Quoted 3 lines
tools/bpf/bpftool/btf.c +3
-0+ f =fopen (file,"rb" ); +if (!f) +return btf;and here
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 410
Quoted 3 lines
tools/bpf/bpftool/btf.c +3
-0+ buf =malloc (st.st_size); +if (!buf) + goto err;and jump to the right place here.
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 417
Quoted 6 lines
tools/bpf/bpftool/btf.c +6
-0+if ((size_t ) st.st_size !=fread (buf,1 , st.st_size, f)) + goto err; + + btf =btf__new (buf, st.st_size); + +err:The prefix for error labels which is shared with non-error path is
exit_
Quoted 5 lines
tools/bpf/bpftool/btf.c static int dump_btf_c(const struct btf *btf, · line 422
Quoted 5 lines
tools/bpf/bpftool/btf.c +5
-0+free (buf); +fclose (f); +return btf; +} +
ok for all above
Quoted 11 lines
tools/bpf/bpftool/btf.c static int do_dump(int argc, char **argv) · line 432
Quoted 9 lines
tools/bpf/bpftool/btf.c +1
-1static int do_dump (int argc,char **argv) {struct btf *btf =NULL ;@@ -397,7 +429,7 @@ static int do_dump(int argc, char **argv)__u32 btf_id =-1 ;const char *src;int fd =-1 ; -int err; +int err =0 ;This change looks unnecessary.
I'm getting confusing warnings from gcc about this, but there is a code path where do_dump would return untouched err:
do_dump
int err;
} else if (is_prefix(src, "file")) {
btf = btf__parse_elf(*argv, NULL); // succeeds
}
while (argc) {
if (is_prefix(*argv, "format")) {
else { // in here
goto done;
}
done:
return err;thanks,
jirka
Quoted 17 lines · Jiri Olsa
Quoted 11 lines
tools/bpf/bpftool/btf.c static int do_dump(int argc, char **argv) · line 432
Quoted 9 lines
static int
do_dump(int argc, char **argv)
{tools/bpf/bpftool/btf.c +1
-1struct btf *btf =NULL ;@@ -397,7 +429,7 @@ static int do_dump(int argc, char **argv)__u32 btf_id =-1 ;const char *src;int fd =-1 ; -int err; +int err =0 ;This change looks unnecessary.
I'm getting confusing warnings from gcc about this, but there is a code path where
do_dumpwould return untouched err:
do_dump
Quoted 15 lines
int err;
} else if (is_prefix(src, "file")) {
btf = btf__parse_elf(*argv, NULL); // succeeds
}
while (argc) {
if (is_prefix(*argv, "format")) {
else { // in here
goto done;
}
done:
return err;ugh, right those look legit, although unrelated to you change.
err should always be set before jumping to 'done'. The error setting in this function looks super messy :( Sometimes is returns errno codes, sometimes positive values, sometimes negative, sometimes just -1. Sometimes it jumps to 'done' for no good reason, ahh :/