aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYonghong Song <yhs@fb.com>2018-05-24 14:21:56 -0400
committerAlexei Starovoitov <ast@kernel.org>2018-05-24 21:18:20 -0400
commitecb96f7fe153c7ff2fd31db64c52a53b7e6401ab (patch)
treeb8c6e0b6ef20c9f7a3cb1ae211de6aa2e62ff2de
parent73bc4d9fc045fb709483a733683f816e64a99431 (diff)
samples/bpf: add a samples/bpf test for BPF_TASK_FD_QUERY
This is mostly to test kprobe/uprobe which needs kernel headers. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
-rw-r--r--samples/bpf/Makefile4
-rw-r--r--samples/bpf/task_fd_query_kern.c19
-rw-r--r--samples/bpf/task_fd_query_user.c382
3 files changed, 405 insertions, 0 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 62d1aa1a4cf3..7dc85ed0ce4b 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -51,6 +51,7 @@ hostprogs-y += cpustat
51hostprogs-y += xdp_adjust_tail 51hostprogs-y += xdp_adjust_tail
52hostprogs-y += xdpsock 52hostprogs-y += xdpsock
53hostprogs-y += xdp_fwd 53hostprogs-y += xdp_fwd
54hostprogs-y += task_fd_query
54 55
55# Libbpf dependencies 56# Libbpf dependencies
56LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a 57LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a
@@ -105,6 +106,7 @@ cpustat-objs := bpf_load.o cpustat_user.o
105xdp_adjust_tail-objs := xdp_adjust_tail_user.o 106xdp_adjust_tail-objs := xdp_adjust_tail_user.o
106xdpsock-objs := bpf_load.o xdpsock_user.o 107xdpsock-objs := bpf_load.o xdpsock_user.o
107xdp_fwd-objs := bpf_load.o xdp_fwd_user.o 108xdp_fwd-objs := bpf_load.o xdp_fwd_user.o
109task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS)
108 110
109# Tell kbuild to always build the programs 111# Tell kbuild to always build the programs
110always := $(hostprogs-y) 112always := $(hostprogs-y)
@@ -160,6 +162,7 @@ always += cpustat_kern.o
160always += xdp_adjust_tail_kern.o 162always += xdp_adjust_tail_kern.o
161always += xdpsock_kern.o 163always += xdpsock_kern.o
162always += xdp_fwd_kern.o 164always += xdp_fwd_kern.o
165always += task_fd_query_kern.o
163 166
164HOSTCFLAGS += -I$(objtree)/usr/include 167HOSTCFLAGS += -I$(objtree)/usr/include
165HOSTCFLAGS += -I$(srctree)/tools/lib/ 168HOSTCFLAGS += -I$(srctree)/tools/lib/
@@ -175,6 +178,7 @@ HOSTCFLAGS_offwaketime_user.o += -I$(srctree)/tools/lib/bpf/
175HOSTCFLAGS_spintest_user.o += -I$(srctree)/tools/lib/bpf/ 178HOSTCFLAGS_spintest_user.o += -I$(srctree)/tools/lib/bpf/
176HOSTCFLAGS_trace_event_user.o += -I$(srctree)/tools/lib/bpf/ 179HOSTCFLAGS_trace_event_user.o += -I$(srctree)/tools/lib/bpf/
177HOSTCFLAGS_sampleip_user.o += -I$(srctree)/tools/lib/bpf/ 180HOSTCFLAGS_sampleip_user.o += -I$(srctree)/tools/lib/bpf/
181HOSTCFLAGS_task_fd_query_user.o += -I$(srctree)/tools/lib/bpf/
178 182
179HOST_LOADLIBES += $(LIBBPF) -lelf 183HOST_LOADLIBES += $(LIBBPF) -lelf
180HOSTLOADLIBES_tracex4 += -lrt 184HOSTLOADLIBES_tracex4 += -lrt
diff --git a/samples/bpf/task_fd_query_kern.c b/samples/bpf/task_fd_query_kern.c
new file mode 100644
index 000000000000..f4b0a9ea674d
--- /dev/null
+++ b/samples/bpf/task_fd_query_kern.c
@@ -0,0 +1,19 @@
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/version.h>
3#include <linux/ptrace.h>
4#include <uapi/linux/bpf.h>
5#include "bpf_helpers.h"
6
7SEC("kprobe/blk_start_request")
8int bpf_prog1(struct pt_regs *ctx)
9{
10 return 0;
11}
12
13SEC("kretprobe/blk_account_io_completion")
14int bpf_prog2(struct pt_regs *ctx)
15{
16 return 0;
17}
18char _license[] SEC("license") = "GPL";
19u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/task_fd_query_user.c b/samples/bpf/task_fd_query_user.c
new file mode 100644
index 000000000000..8381d792f138
--- /dev/null
+++ b/samples/bpf/task_fd_query_user.c
@@ -0,0 +1,382 @@
1// SPDX-License-Identifier: GPL-2.0
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <signal.h>
6#include <unistd.h>
7#include <stdbool.h>
8#include <string.h>
9#include <stdint.h>
10#include <fcntl.h>
11#include <linux/bpf.h>
12#include <sys/ioctl.h>
13#include <sys/resource.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16
17#include "libbpf.h"
18#include "bpf_load.h"
19#include "bpf_util.h"
20#include "perf-sys.h"
21#include "trace_helpers.h"
22
23#define CHECK_PERROR_RET(condition) ({ \
24 int __ret = !!(condition); \
25 if (__ret) { \
26 printf("FAIL: %s:\n", __func__); \
27 perror(" "); \
28 return -1; \
29 } \
30})
31
32#define CHECK_AND_RET(condition) ({ \
33 int __ret = !!(condition); \
34 if (__ret) \
35 return -1; \
36})
37
38static __u64 ptr_to_u64(void *ptr)
39{
40 return (__u64) (unsigned long) ptr;
41}
42
43#define PMU_TYPE_FILE "/sys/bus/event_source/devices/%s/type"
44static int bpf_find_probe_type(const char *event_type)
45{
46 char buf[256];
47 int fd, ret;
48
49 ret = snprintf(buf, sizeof(buf), PMU_TYPE_FILE, event_type);
50 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
51
52 fd = open(buf, O_RDONLY);
53 CHECK_PERROR_RET(fd < 0);
54
55 ret = read(fd, buf, sizeof(buf));
56 close(fd);
57 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
58
59 errno = 0;
60 ret = (int)strtol(buf, NULL, 10);
61 CHECK_PERROR_RET(errno);
62 return ret;
63}
64
65#define PMU_RETPROBE_FILE "/sys/bus/event_source/devices/%s/format/retprobe"
66static int bpf_get_retprobe_bit(const char *event_type)
67{
68 char buf[256];
69 int fd, ret;
70
71 ret = snprintf(buf, sizeof(buf), PMU_RETPROBE_FILE, event_type);
72 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
73
74 fd = open(buf, O_RDONLY);
75 CHECK_PERROR_RET(fd < 0);
76
77 ret = read(fd, buf, sizeof(buf));
78 close(fd);
79 CHECK_PERROR_RET(ret < 0 || ret >= sizeof(buf));
80 CHECK_PERROR_RET(strlen(buf) < strlen("config:"));
81
82 errno = 0;
83 ret = (int)strtol(buf + strlen("config:"), NULL, 10);
84 CHECK_PERROR_RET(errno);
85 return ret;
86}
87
88static int test_debug_fs_kprobe(int prog_fd_idx, const char *fn_name,
89 __u32 expected_fd_type)
90{
91 __u64 probe_offset, probe_addr;
92 __u32 len, prog_id, fd_type;
93 char buf[256];
94 int err;
95
96 len = sizeof(buf);
97 err = bpf_task_fd_query(getpid(), event_fd[prog_fd_idx], 0, buf, &len,
98 &prog_id, &fd_type, &probe_offset,
99 &probe_addr);
100 if (err < 0) {
101 printf("FAIL: %s, for event_fd idx %d, fn_name %s\n",
102 __func__, prog_fd_idx, fn_name);
103 perror(" :");
104 return -1;
105 }
106 if (strcmp(buf, fn_name) != 0 ||
107 fd_type != expected_fd_type ||
108 probe_offset != 0x0 || probe_addr != 0x0) {
109 printf("FAIL: bpf_trace_event_query(event_fd[%d]):\n",
110 prog_fd_idx);
111 printf("buf: %s, fd_type: %u, probe_offset: 0x%llx,"
112 " probe_addr: 0x%llx\n",
113 buf, fd_type, probe_offset, probe_addr);
114 return -1;
115 }
116 return 0;
117}
118
119static int test_nondebug_fs_kuprobe_common(const char *event_type,
120 const char *name, __u64 offset, __u64 addr, bool is_return,
121 char *buf, __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
122 __u64 *probe_offset, __u64 *probe_addr)
123{
124 int is_return_bit = bpf_get_retprobe_bit(event_type);
125 int type = bpf_find_probe_type(event_type);
126 struct perf_event_attr attr = {};
127 int fd;
128
129 if (type < 0 || is_return_bit < 0) {
130 printf("FAIL: %s incorrect type (%d) or is_return_bit (%d)\n",
131 __func__, type, is_return_bit);
132 return -1;
133 }
134
135 attr.sample_period = 1;
136 attr.wakeup_events = 1;
137 if (is_return)
138 attr.config |= 1 << is_return_bit;
139
140 if (name) {
141 attr.config1 = ptr_to_u64((void *)name);
142 attr.config2 = offset;