Commit bb699887 authored by Quentin Monnet's avatar Quentin Monnet

python: Print BPF syscall error if DEBUG_BPF is on but log_buf is empty.

Commit 759029fe provided an option to
store the output from BPF syscall into a buffer (and not to print it
systematically to standard output) on program load in libbpf.c.

But doing so, it only stores the content of attr.log_buf, while the
error string--resulting from a failed BPF syscall--is no more displayed
when the DEBUG_BPF flag is used in the Python script responsible for
converting and injecting the code.

This commit proposes a fix for this bug by printing the error message
(associated to the return value from the syscall) from the Python
caller, when all the following conditions are met:

- the syscall fails,
- the DEBUG_BPF flag has been provided, and
- log_buf is empty (has not been filled by kernel).

Note: when DEBUG_BPF is not provided, the error string is printed in the
C wrapper in libbpf.c (bpf_prog_load) anyway.

Fixes: 759029fe ("Add option for custom log string to bpf_prog_load")
Signed-off-by: default avatarQuentin Monnet <quentin.monnet@6wind.com>
parent 5815f41a
...@@ -207,10 +207,15 @@ class BPF(object): ...@@ -207,10 +207,15 @@ class BPF(object):
lib.bpf_module_kern_version(self.module), lib.bpf_module_kern_version(self.module),
log_buf, ct.sizeof(log_buf) if log_buf else 0) log_buf, ct.sizeof(log_buf) if log_buf else 0)
if self.debug & DEBUG_BPF: if self.debug & DEBUG_BPF and log_buf.value:
print(log_buf.value.decode(), file=sys.stderr) print(log_buf.value.decode(), file=sys.stderr)
if fd < 0: if fd < 0:
if self.debug & DEBUG_BPF and not log_buf.value:
errstr = os.strerror(ct.get_errno())
raise Exception("Failed to load BPF program %s: %s" %
(func_name, errstr))
else:
raise Exception("Failed to load BPF program %s" % func_name) raise Exception("Failed to load BPF program %s" % func_name)
fn = BPF.Function(self, func_name, fd) fn = BPF.Function(self, func_name, fd)
......
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