Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <stdio.h>
0003 #include <unistd.h>
0004 #include <string.h>
0005 #include <assert.h>
0006 #include <bpf/libbpf.h>
0007 #include <bpf/bpf.h>
0008 #include "trace_helpers.h"
0009 
0010 int main(int ac, char **argv)
0011 {
0012     char filename[256], symbol[256];
0013     struct bpf_object *obj = NULL;
0014     struct bpf_link *links[20];
0015     long key, next_key, value;
0016     struct bpf_program *prog;
0017     int map_fd, i, j = 0;
0018     const char *section;
0019     struct ksym *sym;
0020 
0021     if (load_kallsyms()) {
0022         printf("failed to process /proc/kallsyms\n");
0023         return 2;
0024     }
0025 
0026     snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0027     obj = bpf_object__open_file(filename, NULL);
0028     if (libbpf_get_error(obj)) {
0029         fprintf(stderr, "ERROR: opening BPF object file failed\n");
0030         obj = NULL;
0031         goto cleanup;
0032     }
0033 
0034     /* load BPF program */
0035     if (bpf_object__load(obj)) {
0036         fprintf(stderr, "ERROR: loading BPF object file failed\n");
0037         goto cleanup;
0038     }
0039 
0040     map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
0041     if (map_fd < 0) {
0042         fprintf(stderr, "ERROR: finding a map in obj file failed\n");
0043         goto cleanup;
0044     }
0045 
0046     bpf_object__for_each_program(prog, obj) {
0047         section = bpf_program__section_name(prog);
0048         if (sscanf(section, "kprobe/%s", symbol) != 1)
0049             continue;
0050 
0051         /* Attach prog only when symbol exists */
0052         if (ksym_get_addr(symbol)) {
0053             links[j] = bpf_program__attach(prog);
0054             if (libbpf_get_error(links[j])) {
0055                 fprintf(stderr, "bpf_program__attach failed\n");
0056                 links[j] = NULL;
0057                 goto cleanup;
0058             }
0059             j++;
0060         }
0061     }
0062 
0063     for (i = 0; i < 5; i++) {
0064         key = 0;
0065         printf("kprobing funcs:");
0066         while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
0067             bpf_map_lookup_elem(map_fd, &next_key, &value);
0068             assert(next_key == value);
0069             sym = ksym_search(value);
0070             key = next_key;
0071             if (!sym) {
0072                 printf("ksym not found. Is kallsyms loaded?\n");
0073                 continue;
0074             }
0075 
0076             printf(" %s", sym->name);
0077         }
0078         if (key)
0079             printf("\n");
0080         key = 0;
0081         while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0)
0082             bpf_map_delete_elem(map_fd, &next_key);
0083         sleep(1);
0084     }
0085 
0086 cleanup:
0087     for (j--; j >= 0; j--)
0088         bpf_link__destroy(links[j]);
0089 
0090     bpf_object__close(obj);
0091     return 0;
0092 }