File: test_trace1.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 (45 lines) | stat: -rwxr-xr-x 1,374 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
#!/usr/bin/env python
# Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")

from ctypes import c_uint, c_ulong, Structure
from bcc import BPF
import os
from time import sleep
import sys
from unittest import main, TestCase

arg1 = sys.argv.pop(1)
arg2 = ""
if len(sys.argv) > 1:
  arg2 = sys.argv.pop(1)

Key = None
Leaf = None
if arg1.endswith(".b"):
    class Key(Structure):
        _fields_ = [("fd", c_ulong)]
    class Leaf(Structure):
        _fields_ = [("stat1", c_ulong),
                    ("stat2", c_ulong)]

class TestKprobe(TestCase):
    def setUp(self):
        b = BPF(arg1, arg2, debug=0)
        self.stats = b.get_table("stats", Key, Leaf)
        b.attach_kprobe(event=b.get_syscall_fnname("write"), fn_name="sys_wr")
        b.attach_kprobe(event=b.get_syscall_fnname("read"), fn_name="sys_rd")
        b.attach_kprobe(event="htab_map_get_next_key", fn_name="sys_rd")

    def test_trace1(self):
        with open("/dev/null", "a") as f:
            for i in range(0, 100):
                os.write(f.fileno(), b"")
        with open("/etc/services", "r") as f:
            for i in range(0, 200):
                os.read(f.fileno(), 1)
        for key, leaf in self.stats.items():
            print("fd %x:" % key.fd, "stat1 %d" % leaf.stat1, "stat2 %d" % leaf.stat2)

if __name__ == "__main__":
    main()