From bb699887b84309ecce043d6cf41b55cfb55285d3 Mon Sep 17 00:00:00 2001 From: Quentin Monnet Date: Thu, 9 Jun 2016 14:55:29 +0200 Subject: [PATCH] python: Print BPF syscall error if DEBUG_BPF is on but log_buf is empty. Commit 759029fea8066b41b54be5447137db95cb1313c4 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: Quentin Monnet --- src/python/bcc/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/python/bcc/__init__.py b/src/python/bcc/__init__.py index 804c481..b9259df 100644 --- a/src/python/bcc/__init__.py +++ b/src/python/bcc/__init__.py @@ -207,11 +207,16 @@ class BPF(object): lib.bpf_module_kern_version(self.module), 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) if fd < 0: - raise Exception("Failed to load BPF program %s" % func_name) + 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) fn = BPF.Function(self, func_name, fd) self.funcs[func_name] = fn -- 2.7.4