|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* Copyright (c) 2020 Google */ |
| 3 | + |
| 4 | +#include <test_progs.h> |
| 5 | +#include <bpf/libbpf.h> |
| 6 | +#include <bpf/btf.h> |
| 7 | +#include "test_ksyms_btf.skel.h" |
| 8 | + |
| 9 | +static int duration; |
| 10 | + |
| 11 | +void test_ksyms_btf(void) |
| 12 | +{ |
| 13 | + __u64 runqueues_addr, bpf_prog_active_addr; |
| 14 | + struct test_ksyms_btf *skel = NULL; |
| 15 | + struct test_ksyms_btf__data *data; |
| 16 | + struct btf *btf; |
| 17 | + int percpu_datasec; |
| 18 | + int err; |
| 19 | + |
| 20 | + err = kallsyms_find("runqueues", &runqueues_addr); |
| 21 | + if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno)) |
| 22 | + return; |
| 23 | + if (CHECK(err == -ENOENT, "ksym_find", "symbol 'runqueues' not found\n")) |
| 24 | + return; |
| 25 | + |
| 26 | + err = kallsyms_find("bpf_prog_active", &bpf_prog_active_addr); |
| 27 | + if (CHECK(err == -EINVAL, "kallsyms_fopen", "failed to open: %d\n", errno)) |
| 28 | + return; |
| 29 | + if (CHECK(err == -ENOENT, "ksym_find", "symbol 'bpf_prog_active' not found\n")) |
| 30 | + return; |
| 31 | + |
| 32 | + btf = libbpf_find_kernel_btf(); |
| 33 | + if (CHECK(IS_ERR(btf), "btf_exists", "failed to load kernel BTF: %ld\n", |
| 34 | + PTR_ERR(btf))) |
| 35 | + return; |
| 36 | + |
| 37 | + percpu_datasec = btf__find_by_name_kind(btf, ".data..percpu", |
| 38 | + BTF_KIND_DATASEC); |
| 39 | + if (percpu_datasec < 0) { |
| 40 | + printf("%s:SKIP:no PERCPU DATASEC in kernel btf\n", |
| 41 | + __func__); |
| 42 | + test__skip(); |
| 43 | + goto cleanup; |
| 44 | + } |
| 45 | + |
| 46 | + skel = test_ksyms_btf__open_and_load(); |
| 47 | + if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n")) |
| 48 | + goto cleanup; |
| 49 | + |
| 50 | + err = test_ksyms_btf__attach(skel); |
| 51 | + if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) |
| 52 | + goto cleanup; |
| 53 | + |
| 54 | + /* trigger tracepoint */ |
| 55 | + usleep(1); |
| 56 | + |
| 57 | + data = skel->data; |
| 58 | + CHECK(data->out__runqueues_addr != runqueues_addr, "runqueues_addr", |
| 59 | + "got %llu, exp %llu\n", |
| 60 | + (unsigned long long)data->out__runqueues_addr, |
| 61 | + (unsigned long long)runqueues_addr); |
| 62 | + CHECK(data->out__bpf_prog_active_addr != bpf_prog_active_addr, "bpf_prog_active_addr", |
| 63 | + "got %llu, exp %llu\n", |
| 64 | + (unsigned long long)data->out__bpf_prog_active_addr, |
| 65 | + (unsigned long long)bpf_prog_active_addr); |
| 66 | + |
| 67 | +cleanup: |
| 68 | + btf__free(btf); |
| 69 | + test_ksyms_btf__destroy(skel); |
| 70 | +} |
0 commit comments