^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "trace_helpers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) int main(int ac, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) char filename[256], symbol[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct bpf_object *obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct bpf_link *links[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) long key, next_key, value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int map_fd, i, j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) const char *section;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct ksym *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (setrlimit(RLIMIT_MEMLOCK, &r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) perror("setrlimit(RLIMIT_MEMLOCK)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (load_kallsyms()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) printf("failed to process /proc/kallsyms\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) obj = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (libbpf_get_error(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) fprintf(stderr, "ERROR: opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (bpf_object__load(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) fprintf(stderr, "ERROR: loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (map_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) fprintf(stderr, "ERROR: finding a map in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) bpf_object__for_each_program(prog, obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) section = bpf_program__section_name(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (sscanf(section, "kprobe/%s", symbol) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Attach prog only when symbol exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (ksym_get_addr(symbol)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) links[j] = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (libbpf_get_error(links[j])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) fprintf(stderr, "bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) links[j] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) j++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) for (i = 0; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) printf("kprobing funcs:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) bpf_map_lookup_elem(map_fd, &next_key, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) assert(next_key == value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) sym = ksym_search(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) key = next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (!sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) printf("ksym not found. Is kallsyms loaded?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) printf(" %s", sym->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) bpf_map_delete_elem(map_fd, &next_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) sleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) for (j--; j >= 0; j--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) bpf_link__destroy(links[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }