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

from bcc import BPF
import os
import sys
from unittest import main, TestCase

class TestKprobeRgx(TestCase):
    def setUp(self):
        self.b = BPF(text=b"""
        typedef struct { int idx; } Key;
        typedef struct { u64 val; } Val;
        BPF_HASH(stats, Key, Val, 3);
        int hello(void *ctx) {
          Val *val = stats.lookup_or_try_init(&(Key){1}, &(Val){0});
          if (val) {
            val->val++;
          }
          return 0;
        }
        int goodbye(void *ctx) {
          Val *val = stats.lookup_or_try_init(&(Key){2}, &(Val){0});
          if (val) {
            val->val++;
          }
          return 0;
        }
        """)
        self.b.attach_kprobe(event_re=b"^" + self.b.get_syscall_prefix() + b"bp.*",
                             fn_name=b"hello")
        self.b.attach_kretprobe(event_re=b"^" + self.b.get_syscall_prefix() + b"bp.*",
                                fn_name=b"goodbye")

    def test_send1(self):
        k1 = self.b[b"stats"].Key(1)
        k2 = self.b[b"stats"].Key(2)
        self.assertTrue(self.b[b"stats"][k1].val >= 2)
        self.assertTrue(self.b[b"stats"][k2].val == 1)

class TestKprobeReplace(TestCase):
    def setUp(self):
        self.b = BPF(text=b"int empty(void *ctx) { return 0; }")

    def test_periods(self):
        self.b.attach_kprobe(event_re=b"^tcp_enter_cwr.*", fn_name=b"empty")

if __name__ == "__main__":
    main()