Merge pull request #444 from goldshtn/probefail

Fix error handling when attaching {u,k}{,ret}probes
diff --git a/src/python/bcc/__init__.py b/src/python/bcc/__init__.py
index 0da1a67..cc54705 100644
--- a/src/python/bcc/__init__.py
+++ b/src/python/bcc/__init__.py
@@ -341,7 +341,7 @@
                 desc.encode("ascii"), pid, cpu, group_fd,
                 self._reader_cb_impl, ct.cast(id(self), ct.py_object))
         res = ct.cast(res, ct.c_void_p)
-        if res == None:
+        if res.value is None:
             raise Exception("Failed to attach BPF to kprobe")
         open_kprobes[ev_name] = res
         return self
@@ -389,7 +389,7 @@
                 desc.encode("ascii"), pid, cpu, group_fd,
                 self._reader_cb_impl, ct.cast(id(self), ct.py_object))
         res = ct.cast(res, ct.c_void_p)
-        if res == None:
+        if res.value is None:
             raise Exception("Failed to attach BPF to kprobe")
         open_kprobes[ev_name] = res
         return self
@@ -513,7 +513,7 @@
                 desc.encode("ascii"), pid, cpu, group_fd,
                 self._reader_cb_impl, ct.cast(id(self), ct.py_object))
         res = ct.cast(res, ct.c_void_p)
-        if res == None:
+        if res.value is None:
             raise Exception("Failed to attach BPF to uprobe")
         open_uprobes[ev_name] = res
         return self
@@ -557,7 +557,7 @@
                 desc.encode("ascii"), pid, cpu, group_fd,
                 self._reader_cb_impl, ct.cast(id(self), ct.py_object))
         res = ct.cast(res, ct.c_void_p)
-        if res == None:
+        if res.value is None:
             raise Exception("Failed to attach BPF to uprobe")
         open_uprobes[ev_name] = res
         return self
diff --git a/tools/trace.py b/tools/trace.py
index 33d0afa..4aac067 100755
--- a/tools/trace.py
+++ b/tools/trace.py
@@ -361,12 +361,15 @@
                 bpf[self.events_name].open_perf_buffer(self.print_event)
 
         def _attach_k(self, bpf):
+                kprobes_start = len(BPF.open_kprobes())
                 if self.probe_type == "r":
                         bpf.attach_kretprobe(event=self.function,
                                              fn_name=self.probe_name)
                 elif self.probe_type == "p" or self.probe_type == "t":
                         bpf.attach_kprobe(event=self.function,
                                           fn_name=self.probe_name)
+                if len(BPF.open_kprobes()) != kprobes_start + 1:
+                        self._bail("error attaching probe")
 
         def _attach_u(self, bpf):
                 libpath = BPF.find_library(self.library)
@@ -378,6 +381,7 @@
                 if libpath is None or len(libpath) == 0:
                         self._bail("unable to find library %s" % self.library)
 
+                uprobes_start = len(BPF.open_uprobes())
                 if self.probe_type == "r":
                         bpf.attach_uretprobe(name=libpath,
                                              sym=self.function,
@@ -388,6 +392,8 @@
                                           sym=self.function,
                                           fn_name=self.probe_name,
                                           pid=self.pid)
+                if len(BPF.open_uprobes()) != uprobes_start + 1:
+                        self._bail("error attaching probe")
 
 class Tool(object):
         examples = """