File: test_uprobes2.py

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 (47 lines) | stat: -rwxr-xr-x 1,119 bytes parent folder | download
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
#!/usr/bin/env python
#
# USAGE: test_uprobe2.py
#
# Copyright 2020 Facebook, Inc
# Licensed under the Apache License, Version 2.0 (the "License")

from bcc import BPF
from unittest import main, TestCase
from subprocess import Popen, PIPE
from tempfile import NamedTemporaryFile


class TestUprobes(TestCase):
    def setUp(self):
        lib_text = b"""
__attribute__((__visibility__("default"))) void fun()
{
}
"""
        self.bpf_text = """
int trace_fun_call(void *ctx) {{
    return 1;
}}
"""
        # Compile and run the application
        self.ftemp = NamedTemporaryFile(delete=False)
        self.ftemp.close()
        comp = Popen([
            "gcc",
            "-x", "c",
            "-shared",
            "-Wl,-Ttext-segment,0x2000000",
            "-o", self.ftemp.name,
            "-"
        ], stdin=PIPE)
        comp.stdin.write(lib_text)
        comp.stdin.close()
        self.assertEqual(comp.wait(), 0)

    def test_attach1(self):
        b = BPF(text=self.bpf_text)
        b.attach_uprobe(name=self.ftemp.name, sym="fun", fn_name="trace_fun_call")


if __name__ == "__main__":
    main()