From: Vicent Marti Date: Wed, 30 Mar 2016 15:31:20 +0000 (+0200) Subject: test.lua: Port the `test_uprobes` suite X-Git-Tag: v0.2.0~128^2~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=72d7aa3c90855b8bbc6791ba1c7d19356d6ade4c;p=platform%2Fupstream%2Fbcc.git test.lua: Port the `test_uprobes` suite --- diff --git a/tests/lua/test_uprobes.lua b/tests/lua/test_uprobes.lua new file mode 100644 index 0000000..8bcc477 --- /dev/null +++ b/tests/lua/test_uprobes.lua @@ -0,0 +1,71 @@ +require("test_helper") +local ffi = require("ffi") + +ffi.cdef[[ + int getpid(void); + void malloc_stats(void); +]] + +TestUprobes = {} + +function TestUprobes:test_simple_library() + local text = [[ +#include +BPF_TABLE("array", int, u64, stats, 1); +static void incr(int idx) { + u64 *ptr = stats.lookup(&idx); + if (ptr) + ++(*ptr); +} +int count(struct pt_regs *ctx) { + u32 pid = bpf_get_current_pid_tgid(); + if (pid == PID) + incr(0); + return 0; +}]] + + local pid = tonumber(ffi.C.getpid()) + local text = text:gsub("PID", tostring(pid)) + + local b = BPF:new{text=text} + b:attach_uprobe{name="c", sym="malloc_stats", fn_name="count"} + b:attach_uprobe{name="c", sym="malloc_stats", fn_name="count", retprobe=true} + + assert_equals(BPF.num_open_uprobes(), 2) + + ffi.C.malloc_stats() + + local stats = b:get_table("stats") + assert_equals(tonumber(stats:get(0)), 2) +end + +function TestUprobes:test_simple_binary() + local text = [[ +#include +BPF_TABLE("array", int, u64, stats, 1); +static void incr(int idx) { + u64 *ptr = stats.lookup(&idx); + if (ptr) + ++(*ptr); +} +int count(struct pt_regs *ctx) { + u32 pid = bpf_get_current_pid_tgid(); + incr(0); + return 0; +}]] + + local b = BPF:new{text=text} + b:attach_uprobe{name="/usr/bin/python", sym="main", fn_name="count"} + b:attach_uprobe{name="/usr/bin/python", sym="main", fn_name="count", retprobe=true} + + os.spawn("/usr/bin/python -V") + + local stats = b:get_table("stats") + assert_true(tonumber(stats:get(0)) >= 2) +end + +function TestUprobes:teardown() + BPF.cleanup_probes() +end + +os.exit(LuaUnit.run())