Commit d9c243e6 authored by Sasha Goldshtein's avatar Sasha Goldshtein

tests: Test new tracepoint support

parent fab68e3a
...@@ -610,10 +610,10 @@ class BPF(object): ...@@ -610,10 +610,10 @@ class BPF(object):
def _trace_autoload(self): def _trace_autoload(self):
for i in range(0, lib.bpf_num_functions(self.module)): for i in range(0, lib.bpf_num_functions(self.module)):
func_name = lib.bpf_function_name(self.module, i).decode() func_name = lib.bpf_function_name(self.module, i).decode()
if func_name.startswith("kprobe__"): if len(open_kprobes) == 0 and func_name.startswith("kprobe__"):
fn = self.load_func(func_name, BPF.KPROBE) fn = self.load_func(func_name, BPF.KPROBE)
self.attach_kprobe(event=fn.name[8:], fn_name=fn.name) self.attach_kprobe(event=fn.name[8:], fn_name=fn.name)
elif func_name.startswith("kretprobe__"): elif len(open_kprobes) == 0 and func_name.startswith("kretprobe__"):
fn = self.load_func(func_name, BPF.KPROBE) fn = self.load_func(func_name, BPF.KPROBE)
self.attach_kretprobe(event=fn.name[11:], fn_name=fn.name) self.attach_kretprobe(event=fn.name[11:], fn_name=fn.name)
elif func_name.startswith("tracepoint__"): elif func_name.startswith("tracepoint__"):
......
...@@ -22,21 +22,9 @@ def kernel_version_ge(major, minor): ...@@ -22,21 +22,9 @@ def kernel_version_ge(major, minor):
@unittest.skipUnless(kernel_version_ge(4,7), "requires kernel >= 4.7") @unittest.skipUnless(kernel_version_ge(4,7), "requires kernel >= 4.7")
class TestTracepoint(unittest.TestCase): class TestTracepoint(unittest.TestCase):
def test_tracepoint(self): def test_tracepoint(self):
text = """#include <linux/ptrace.h> text = """
struct tp_args {
unsigned long long __unused__;
char prev_comm[16];
pid_t prev_pid;
int prev_prio;
long prev_state;
char next_comm[16];
pid_t next_pid;
int next_prio;
};
BPF_HASH(switches, u32, u64); BPF_HASH(switches, u32, u64);
int probe_switch(struct tp_args *args) { TRACEPOINT_PROBE(sched, sched_switch) {
if (args == 0)
return 0;
u64 val = 0; u64 val = 0;
u32 pid = args->next_pid; u32 pid = args->next_pid;
u64 *existing = switches.lookup_or_init(&pid, &val); u64 *existing = switches.lookup_or_init(&pid, &val);
...@@ -45,13 +33,11 @@ class TestTracepoint(unittest.TestCase): ...@@ -45,13 +33,11 @@ class TestTracepoint(unittest.TestCase):
} }
""" """
b = bcc.BPF(text=text) b = bcc.BPF(text=text)
b.attach_tracepoint("sched:sched_switch", "probe_switch")
sleep(1) sleep(1)
total_switches = 0 total_switches = 0
for k, v in b["switches"].items(): for k, v in b["switches"].items():
total_switches += v.value total_switches += v.value
self.assertNotEqual(0, total_switches) self.assertNotEqual(0, total_switches)
b.detach_tracepoint("sched:sched_switch")
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
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