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