Fixes for address calculation
authorBrenden Blanco <bblanco@plumgrid.com>
Thu, 28 Jan 2016 22:18:46 +0000 (14:18 -0800)
committerBrenden Blanco <bblanco@plumgrid.com>
Thu, 28 Jan 2016 22:18:46 +0000 (14:18 -0800)
The calculation of function address in non-shared libraries was
incorrect. Fix it.

Signed-off-by: Brenden Blanco <bblanco@plumgrid.com>
src/cc/libbpf.c
src/python/bcc/__init__.py
tests/cc/test_uprobes.py

index 4165911..4a5c083 100644 (file)
@@ -234,7 +234,7 @@ static int bpf_attach_tracing_event(int progfd, const char *event_path,
   attr.wakeup_events = 1;
   pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, group_fd, PERF_FLAG_FD_CLOEXEC);
   if (pfd < 0) {
-    perror("perf_event_open");
+    fprintf(stderr, "perf_event_open(%s/id): %s\n", event_path, strerror(errno));
     goto error;
   }
   perf_reader_set_fd(reader, pfd);
index a78e9e4..e4f9885 100644 (file)
@@ -765,7 +765,7 @@ class BPF(object):
         if not addr:
             raise Exception("could not determine address of symbol %s" % sym)
 
-        return (path, load_addr+addr)
+        return (path, addr-load_addr)
 
     def attach_uprobe(self, name="", sym="", addr=None,
             fn_name="", pid=-1, cpu=0, group_fd=-1):
index 00c9440..b17ecc1 100755 (executable)
@@ -5,34 +5,8 @@
 import bcc
 import ctypes
 import os
-import re
-import struct
-import time
 import unittest
 
-## code from ctypes impl
-#if struct.calcsize("l") == 4:
-#    machine = os.uname()[4] + "-32"
-#else:
-#    machine = os.uname()[4] + "-64"
-#mach_map = {
-#    "x86_64-64": "libc6,x86-64",
-#    "ppc64-64": "libc6,64bit",
-#    "sparc64-64": "libc6,64bit",
-#    "s390x-64": "libc6,64bit",
-#    "ia64-64": "libc6,IA-64",
-#}
-#abi_type = mach_map.get(machine, "libc6")
-#
-#def find_library_fullpath(name):
-#    expr = r"\s+lib%s\.[^\s]+\s+\(%s, [^)]+[^/]+([^\s]+)" % (name, abi_type)
-#    with os.popen("/sbin/ldconfig -p 2>/dev/null") as f:
-#        data = f.read()
-#    res = re.search(expr, data)
-#    if not res:
-#        return None
-#    return res.group(1)
-
 class TestUprobes(unittest.TestCase):
     def test_simple_library(self):
         text = """
@@ -58,6 +32,7 @@ int count(struct pt_regs *ctx) {
         libc.malloc_stats.argtypes = []
         libc.malloc_stats()
         self.assertEqual(b["stats"][ctypes.c_int(0)].value, 2)
+        b.detach_uretprobe(name="c", sym="malloc_stats")
         b.detach_uprobe(name="c", sym="malloc_stats")
 
     def test_simple_binary(self):
@@ -74,14 +49,14 @@ int count(struct pt_regs *ctx) {
     incr(0);
     return 0;
 }"""
-        text = text.replace("PID", "%d" % os.getpid())
         b = bcc.BPF(text=text)
-        b.attach_uprobe(name="/usr/bin/python2", sym="main", fn_name="count")
-        b.attach_uretprobe(name="/usr/bin/python2", sym="main", fn_name="count")
-        with os.popen("/usr/bin/python2 -V") as f:
+        b.attach_uprobe(name="/usr/bin/python", sym="main", fn_name="count")
+        b.attach_uretprobe(name="/usr/bin/python", sym="main", fn_name="count")
+        with os.popen("/usr/bin/python -V") as f:
             pass
         self.assertGreater(b["stats"][ctypes.c_int(0)].value, 0)
-        b.detach_uprobe(name="/usr/bin/python2", sym="main")
+        b.detach_uretprobe(name="/usr/bin/python", sym="main")
+        b.detach_uprobe(name="/usr/bin/python", sym="main")
 
 if __name__ == "__main__":
     unittest.main()