.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* Copyright (c) 2017 Facebook |
---|
2 | | - * |
---|
3 | | - * This program is free software; you can redistribute it and/or |
---|
4 | | - * modify it under the terms of version 2 of the GNU General Public |
---|
5 | | - * License as published by the Free Software Foundation. |
---|
6 | 3 | */ |
---|
7 | 4 | #include <stdio.h> |
---|
8 | 5 | #include <unistd.h> |
---|
9 | 6 | #include <fcntl.h> |
---|
10 | 7 | #include <stdlib.h> |
---|
11 | | -#include <signal.h> |
---|
12 | | -#include <linux/bpf.h> |
---|
13 | 8 | #include <string.h> |
---|
14 | 9 | #include <linux/perf_event.h> |
---|
15 | 10 | #include <errno.h> |
---|
16 | | -#include <assert.h> |
---|
17 | | -#include <stdbool.h> |
---|
18 | 11 | #include <sys/resource.h> |
---|
| 12 | +#include <bpf/libbpf.h> |
---|
19 | 13 | #include <bpf/bpf.h> |
---|
20 | | -#include "bpf_load.h" |
---|
21 | 14 | |
---|
22 | 15 | /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*. |
---|
23 | 16 | * This requires kernel CONFIG_FTRACE_SYSCALLS to be set. |
---|
.. | .. |
---|
52 | 45 | |
---|
53 | 46 | static int test(char *filename, int num_progs) |
---|
54 | 47 | { |
---|
55 | | - int i, fd, map0_fds[num_progs], map1_fds[num_progs]; |
---|
| 48 | + int map0_fds[num_progs], map1_fds[num_progs], fd, i, j = 0; |
---|
| 49 | + struct bpf_link *links[num_progs * 4]; |
---|
| 50 | + struct bpf_object *objs[num_progs]; |
---|
| 51 | + struct bpf_program *prog; |
---|
56 | 52 | |
---|
57 | 53 | for (i = 0; i < num_progs; i++) { |
---|
58 | | - if (load_bpf_file(filename)) { |
---|
59 | | - fprintf(stderr, "%s", bpf_log_buf); |
---|
60 | | - return 1; |
---|
| 54 | + objs[i] = bpf_object__open_file(filename, NULL); |
---|
| 55 | + if (libbpf_get_error(objs[i])) { |
---|
| 56 | + fprintf(stderr, "opening BPF object file failed\n"); |
---|
| 57 | + objs[i] = NULL; |
---|
| 58 | + goto cleanup; |
---|
61 | 59 | } |
---|
62 | | - printf("prog #%d: map ids %d %d\n", i, map_fd[0], map_fd[1]); |
---|
63 | | - map0_fds[i] = map_fd[0]; |
---|
64 | | - map1_fds[i] = map_fd[1]; |
---|
| 60 | + |
---|
| 61 | + /* load BPF program */ |
---|
| 62 | + if (bpf_object__load(objs[i])) { |
---|
| 63 | + fprintf(stderr, "loading BPF object file failed\n"); |
---|
| 64 | + goto cleanup; |
---|
| 65 | + } |
---|
| 66 | + |
---|
| 67 | + map0_fds[i] = bpf_object__find_map_fd_by_name(objs[i], |
---|
| 68 | + "enter_open_map"); |
---|
| 69 | + map1_fds[i] = bpf_object__find_map_fd_by_name(objs[i], |
---|
| 70 | + "exit_open_map"); |
---|
| 71 | + if (map0_fds[i] < 0 || map1_fds[i] < 0) { |
---|
| 72 | + fprintf(stderr, "finding a map in obj file failed\n"); |
---|
| 73 | + goto cleanup; |
---|
| 74 | + } |
---|
| 75 | + |
---|
| 76 | + bpf_object__for_each_program(prog, objs[i]) { |
---|
| 77 | + links[j] = bpf_program__attach(prog); |
---|
| 78 | + if (libbpf_get_error(links[j])) { |
---|
| 79 | + fprintf(stderr, "bpf_program__attach failed\n"); |
---|
| 80 | + links[j] = NULL; |
---|
| 81 | + goto cleanup; |
---|
| 82 | + } |
---|
| 83 | + j++; |
---|
| 84 | + } |
---|
| 85 | + printf("prog #%d: map ids %d %d\n", i, map0_fds[i], map1_fds[i]); |
---|
65 | 86 | } |
---|
66 | 87 | |
---|
67 | 88 | /* current load_bpf_file has perf_event_open default pid = -1 |
---|
.. | .. |
---|
83 | 104 | verify_map(map1_fds[i]); |
---|
84 | 105 | } |
---|
85 | 106 | |
---|
| 107 | +cleanup: |
---|
| 108 | + for (j--; j >= 0; j--) |
---|
| 109 | + bpf_link__destroy(links[j]); |
---|
| 110 | + |
---|
| 111 | + for (i--; i >= 0; i--) |
---|
| 112 | + bpf_object__close(objs[i]); |
---|
86 | 113 | return 0; |
---|
87 | 114 | } |
---|
88 | 115 | |
---|