File: test_uprobes.lua

package info (click to toggle)
bpfcc 0.18.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,368 kB
  • sloc: ansic: 132,727; python: 36,226; cpp: 26,973; sh: 710; yacc: 525; makefile: 141; lex: 94
file content (70 lines) | stat: -rw-r--r-- 1,645 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
local suite = require("test_helper")
local ffi = require("ffi")
local TestUprobes = {}

ffi.cdef[[
  int getpid(void);
  void malloc_stats(void);
]]

function TestUprobes:test_simple_library()
  local text = [[
#include <uapi/linux/ptrace.h>
BPF_ARRAY(stats, u64, 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", pid=pid}
  b:attach_uprobe{name="c", sym="malloc_stats", fn_name="count", pid=pid, 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 <uapi/linux/ptrace.h>
BPF_ARRAY(stats, u64, 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()
end

suite("TestUprobes", TestUprobes)