• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# USAGE: test_uprobe2.py
4#
5# Copyright 2020 Facebook, Inc
6# Licensed under the Apache License, Version 2.0 (the "License")
7
8from bcc import BPF
9from unittest import main, TestCase
10from subprocess import Popen, PIPE
11from tempfile import NamedTemporaryFile
12
13
14class TestUprobes(TestCase):
15    def setUp(self):
16        lib_text = b"""
17__attribute__((__visibility__("default"))) void fun()
18{
19}
20"""
21        self.bpf_text = """
22int trace_fun_call(void *ctx) {{
23    return 1;
24}}
25"""
26        # Compile and run the application
27        self.ftemp = NamedTemporaryFile(delete=False)
28        self.ftemp.close()
29        comp = Popen([
30            "gcc",
31            "-x", "c",
32            "-shared",
33            "-Wl,-Ttext-segment,0x2000000",
34            "-o", self.ftemp.name,
35            "-"
36        ], stdin=PIPE)
37        comp.stdin.write(lib_text)
38        comp.stdin.close()
39        self.assertEqual(comp.wait(), 0)
40
41    def test_attach1(self):
42        b = BPF(text=self.bpf_text)
43        b.attach_uprobe(name=self.ftemp.name, sym="fun", fn_name="trace_fun_call")
44
45
46if __name__ == "__main__":
47    main()
48