Commit 5ee0c7aa authored by Brenden Blanco's avatar Brenden Blanco

Merge pull request #444 from goldshtn/probefail

Fix error handling when attaching {u,k}{,ret}probes
parents 746eab44 43742ae4
...@@ -341,7 +341,7 @@ class BPF(object): ...@@ -341,7 +341,7 @@ class BPF(object):
desc.encode("ascii"), pid, cpu, group_fd, desc.encode("ascii"), pid, cpu, group_fd,
self._reader_cb_impl, ct.cast(id(self), ct.py_object)) self._reader_cb_impl, ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p) res = ct.cast(res, ct.c_void_p)
if res == None: if res.value is None:
raise Exception("Failed to attach BPF to kprobe") raise Exception("Failed to attach BPF to kprobe")
open_kprobes[ev_name] = res open_kprobes[ev_name] = res
return self return self
...@@ -389,7 +389,7 @@ class BPF(object): ...@@ -389,7 +389,7 @@ class BPF(object):
desc.encode("ascii"), pid, cpu, group_fd, desc.encode("ascii"), pid, cpu, group_fd,
self._reader_cb_impl, ct.cast(id(self), ct.py_object)) self._reader_cb_impl, ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p) res = ct.cast(res, ct.c_void_p)
if res == None: if res.value is None:
raise Exception("Failed to attach BPF to kprobe") raise Exception("Failed to attach BPF to kprobe")
open_kprobes[ev_name] = res open_kprobes[ev_name] = res
return self return self
...@@ -513,7 +513,7 @@ class BPF(object): ...@@ -513,7 +513,7 @@ class BPF(object):
desc.encode("ascii"), pid, cpu, group_fd, desc.encode("ascii"), pid, cpu, group_fd,
self._reader_cb_impl, ct.cast(id(self), ct.py_object)) self._reader_cb_impl, ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p) res = ct.cast(res, ct.c_void_p)
if res == None: if res.value is None:
raise Exception("Failed to attach BPF to uprobe") raise Exception("Failed to attach BPF to uprobe")
open_uprobes[ev_name] = res open_uprobes[ev_name] = res
return self return self
...@@ -557,7 +557,7 @@ class BPF(object): ...@@ -557,7 +557,7 @@ class BPF(object):
desc.encode("ascii"), pid, cpu, group_fd, desc.encode("ascii"), pid, cpu, group_fd,
self._reader_cb_impl, ct.cast(id(self), ct.py_object)) self._reader_cb_impl, ct.cast(id(self), ct.py_object))
res = ct.cast(res, ct.c_void_p) res = ct.cast(res, ct.c_void_p)
if res == None: if res.value is None:
raise Exception("Failed to attach BPF to uprobe") raise Exception("Failed to attach BPF to uprobe")
open_uprobes[ev_name] = res open_uprobes[ev_name] = res
return self return self
......
...@@ -361,12 +361,15 @@ int %s(struct pt_regs *ctx) ...@@ -361,12 +361,15 @@ int %s(struct pt_regs *ctx)
bpf[self.events_name].open_perf_buffer(self.print_event) bpf[self.events_name].open_perf_buffer(self.print_event)
def _attach_k(self, bpf): def _attach_k(self, bpf):
kprobes_start = len(BPF.open_kprobes())
if self.probe_type == "r": if self.probe_type == "r":
bpf.attach_kretprobe(event=self.function, bpf.attach_kretprobe(event=self.function,
fn_name=self.probe_name) fn_name=self.probe_name)
elif self.probe_type == "p" or self.probe_type == "t": elif self.probe_type == "p" or self.probe_type == "t":
bpf.attach_kprobe(event=self.function, bpf.attach_kprobe(event=self.function,
fn_name=self.probe_name) fn_name=self.probe_name)
if len(BPF.open_kprobes()) != kprobes_start + 1:
self._bail("error attaching probe")
def _attach_u(self, bpf): def _attach_u(self, bpf):
libpath = BPF.find_library(self.library) libpath = BPF.find_library(self.library)
...@@ -378,6 +381,7 @@ int %s(struct pt_regs *ctx) ...@@ -378,6 +381,7 @@ int %s(struct pt_regs *ctx)
if libpath is None or len(libpath) == 0: if libpath is None or len(libpath) == 0:
self._bail("unable to find library %s" % self.library) self._bail("unable to find library %s" % self.library)
uprobes_start = len(BPF.open_uprobes())
if self.probe_type == "r": if self.probe_type == "r":
bpf.attach_uretprobe(name=libpath, bpf.attach_uretprobe(name=libpath,
sym=self.function, sym=self.function,
...@@ -388,6 +392,8 @@ int %s(struct pt_regs *ctx) ...@@ -388,6 +392,8 @@ int %s(struct pt_regs *ctx)
sym=self.function, sym=self.function,
fn_name=self.probe_name, fn_name=self.probe_name,
pid=self.pid) pid=self.pid)
if len(BPF.open_uprobes()) != uprobes_start + 1:
self._bail("error attaching probe")
class Tool(object): class Tool(object):
examples = """ examples = """
......
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