|
1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2 | 2 | #include <test_progs.h> |
| 3 | +#include "test_stacktrace_map.skel.h" |
3 | 4 |
|
4 | | -void test_stacktrace_map(void) |
| 5 | +static void check_stackmap(int control_map_fd, int stackid_hmap_fd, |
| 6 | + int stackmap_fd, int stack_amap_fd) |
| 7 | +{ |
| 8 | + __u32 key, val, duration = 0; |
| 9 | + int err, stack_trace_len; |
| 10 | + |
| 11 | + /* disable stack trace collection */ |
| 12 | + key = 0; |
| 13 | + val = 1; |
| 14 | + bpf_map_update_elem(control_map_fd, &key, &val, 0); |
| 15 | + |
| 16 | + /* for every element in stackid_hmap, we can find a corresponding one |
| 17 | + * in stackmap, and vice versa. |
| 18 | + */ |
| 19 | + err = compare_map_keys(stackid_hmap_fd, stackmap_fd); |
| 20 | + if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap", |
| 21 | + "err %d errno %d\n", err, errno)) |
| 22 | + return; |
| 23 | + |
| 24 | + err = compare_map_keys(stackmap_fd, stackid_hmap_fd); |
| 25 | + if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap", |
| 26 | + "err %d errno %d\n", err, errno)) |
| 27 | + return; |
| 28 | + |
| 29 | + stack_trace_len = PERF_MAX_STACK_DEPTH * sizeof(__u64); |
| 30 | + err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len); |
| 31 | + CHECK(err, "compare_stack_ips stackmap vs. stack_amap", |
| 32 | + "err %d errno %d\n", err, errno); |
| 33 | +} |
| 34 | + |
| 35 | +static void test_stacktrace_map_tp(void) |
5 | 36 | { |
6 | 37 | int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd; |
7 | 38 | const char *prog_name = "oncpu"; |
8 | | - int err, prog_fd, stack_trace_len; |
| 39 | + int err, prog_fd; |
9 | 40 | const char *file = "./test_stacktrace_map.bpf.o"; |
10 | | - __u32 key, val, duration = 0; |
| 41 | + __u32 duration = 0; |
11 | 42 | struct bpf_program *prog; |
12 | 43 | struct bpf_object *obj; |
13 | 44 | struct bpf_link *link; |
@@ -44,32 +75,56 @@ void test_stacktrace_map(void) |
44 | 75 | /* give some time for bpf program run */ |
45 | 76 | sleep(1); |
46 | 77 |
|
47 | | - /* disable stack trace collection */ |
48 | | - key = 0; |
49 | | - val = 1; |
50 | | - bpf_map_update_elem(control_map_fd, &key, &val, 0); |
51 | | - |
52 | | - /* for every element in stackid_hmap, we can find a corresponding one |
53 | | - * in stackmap, and vice versa. |
54 | | - */ |
55 | | - err = compare_map_keys(stackid_hmap_fd, stackmap_fd); |
56 | | - if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap", |
57 | | - "err %d errno %d\n", err, errno)) |
58 | | - goto disable_pmu; |
59 | | - |
60 | | - err = compare_map_keys(stackmap_fd, stackid_hmap_fd); |
61 | | - if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap", |
62 | | - "err %d errno %d\n", err, errno)) |
63 | | - goto disable_pmu; |
64 | | - |
65 | | - stack_trace_len = PERF_MAX_STACK_DEPTH * sizeof(__u64); |
66 | | - err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len); |
67 | | - if (CHECK(err, "compare_stack_ips stackmap vs. stack_amap", |
68 | | - "err %d errno %d\n", err, errno)) |
69 | | - goto disable_pmu; |
| 78 | + check_stackmap(control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd); |
70 | 79 |
|
71 | 80 | disable_pmu: |
72 | 81 | bpf_link__destroy(link); |
73 | 82 | close_prog: |
74 | 83 | bpf_object__close(obj); |
75 | 84 | } |
| 85 | + |
| 86 | +static void test_stacktrace_map_kprobe_multi(bool retprobe) |
| 87 | +{ |
| 88 | + int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd; |
| 89 | + LIBBPF_OPTS(bpf_kprobe_multi_opts, opts, |
| 90 | + .retprobe = retprobe |
| 91 | + ); |
| 92 | + LIBBPF_OPTS(bpf_test_run_opts, topts); |
| 93 | + struct test_stacktrace_map *skel; |
| 94 | + struct bpf_link *link; |
| 95 | + int prog_fd, err; |
| 96 | + |
| 97 | + skel = test_stacktrace_map__open_and_load(); |
| 98 | + if (!ASSERT_OK_PTR(skel, "test_stacktrace_map__open_and_load")) |
| 99 | + return; |
| 100 | + |
| 101 | + link = bpf_program__attach_kprobe_multi_opts(skel->progs.kprobe, |
| 102 | + "bpf_fentry_test1", &opts); |
| 103 | + if (!ASSERT_OK_PTR(link, "bpf_program__attach_kprobe_multi_opts")) |
| 104 | + goto cleanup; |
| 105 | + |
| 106 | + prog_fd = bpf_program__fd(skel->progs.trigger); |
| 107 | + err = bpf_prog_test_run_opts(prog_fd, &topts); |
| 108 | + ASSERT_OK(err, "test_run"); |
| 109 | + ASSERT_EQ(topts.retval, 0, "test_run"); |
| 110 | + |
| 111 | + control_map_fd = bpf_map__fd(skel->maps.control_map); |
| 112 | + stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap); |
| 113 | + stackmap_fd = bpf_map__fd(skel->maps.stackmap); |
| 114 | + stack_amap_fd = bpf_map__fd(skel->maps.stack_amap); |
| 115 | + |
| 116 | + check_stackmap(control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd); |
| 117 | + |
| 118 | +cleanup: |
| 119 | + test_stacktrace_map__destroy(skel); |
| 120 | +} |
| 121 | + |
| 122 | +void test_stacktrace_map(void) |
| 123 | +{ |
| 124 | + if (test__start_subtest("tp")) |
| 125 | + test_stacktrace_map_tp(); |
| 126 | + if (test__start_subtest("kprobe_multi")) |
| 127 | + test_stacktrace_map_kprobe_multi(false); |
| 128 | + if (test__start_subtest("kretprobe_multi")) |
| 129 | + test_stacktrace_map_kprobe_multi(true); |
| 130 | +} |
0 commit comments