xref: /linux/tools/perf/tests/dlfilter-test.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Test dlfilter C API. A perf.data file is synthesized and then processed
4  * by perf script with dlfilters named dlfilter-test-api-v*.so. Also a C file
5  * is compiled to provide a dso to match the synthesized perf.data file.
6  */
7 
8 #include <linux/compiler.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/perf_event.h>
12 #include <internal/lib.h>
13 #include <subcmd/exec-cmd.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <inttypes.h>
20 #include <libgen.h>
21 #include <string.h>
22 #include <errno.h>
23 #include "debug.h"
24 #include "tool.h"
25 #include "event.h"
26 #include "header.h"
27 #include "machine.h"
28 #include "dso.h"
29 #include "map.h"
30 #include "symbol.h"
31 #include "synthetic-events.h"
32 #include "util.h"
33 #include "archinsn.h"
34 #include "dlfilter.h"
35 #include "tests.h"
36 #include "util/sample.h"
37 
38 #define MAP_START 0x400000
39 
40 #define DLFILTER_TEST_NAME_MAX 128
41 
42 struct test_data {
43 	struct perf_tool tool;
44 	struct machine *machine;
45 	int fd;
46 	u64 foo;
47 	u64 bar;
48 	u64 ip;
49 	u64 addr;
50 	char name[DLFILTER_TEST_NAME_MAX];
51 	char desc[DLFILTER_TEST_NAME_MAX];
52 	char perf[PATH_MAX];
53 	char perf_data_file_name[PATH_MAX];
54 	char c_file_name[PATH_MAX];
55 	char prog_file_name[PATH_MAX];
56 	char dlfilters[PATH_MAX];
57 };
58 
test_result(const char * msg,int ret)59 static int test_result(const char *msg, int ret)
60 {
61 	pr_debug("%s\n", msg);
62 	return ret;
63 }
64 
process(const struct perf_tool * tool,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)65 static int process(const struct perf_tool *tool, union perf_event *event,
66 		   struct perf_sample *sample __maybe_unused,
67 		   struct machine *machine __maybe_unused)
68 {
69 	struct test_data *td = container_of(tool, struct test_data, tool);
70 	int fd = td->fd;
71 
72 	if (writen(fd, event, event->header.size) != event->header.size)
73 		return -1;
74 
75 	return 0;
76 }
77 
78 #define MAXCMD 4096
79 #define REDIRECT_TO_DEV_NULL " >/dev/null 2>&1"
80 
system_cmd(const char * fmt,...)81 static __printf(1, 2) int system_cmd(const char *fmt, ...)
82 {
83 	char cmd[MAXCMD + sizeof(REDIRECT_TO_DEV_NULL)];
84 	int ret;
85 
86 	va_list args;
87 
88 	va_start(args, fmt);
89 	ret = vsnprintf(cmd, MAXCMD, fmt, args);
90 	va_end(args);
91 
92 	if (ret <= 0 || ret >= MAXCMD)
93 		return -1;
94 
95 	if (verbose <= 0)
96 		strcat(cmd, REDIRECT_TO_DEV_NULL);
97 
98 	pr_debug("Command: %s\n", cmd);
99 	ret = system(cmd);
100 	if (ret)
101 		pr_debug("Failed with return value %d\n", ret);
102 
103 	return ret;
104 }
105 
have_gcc(void)106 static bool have_gcc(void)
107 {
108 	pr_debug("Checking for gcc\n");
109 	return !system_cmd("gcc --version");
110 }
111 
write_attr(struct test_data * td,u64 sample_type,u64 * id)112 static int write_attr(struct test_data *td, u64 sample_type, u64 *id)
113 {
114 	struct perf_event_attr attr = {
115 		.size = sizeof(attr),
116 		.type = PERF_TYPE_HARDWARE,
117 		.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
118 		.sample_type = sample_type,
119 		.sample_period = 1,
120 	};
121 
122 	return perf_event__synthesize_attr(&td->tool, &attr, 1, id, process);
123 }
124 
write_comm(int fd,pid_t pid,pid_t tid,const char * comm_str)125 static int write_comm(int fd, pid_t pid, pid_t tid, const char *comm_str)
126 {
127 	struct perf_record_comm comm;
128 	ssize_t sz = sizeof(comm);
129 
130 	comm.header.type = PERF_RECORD_COMM;
131 	comm.header.misc = PERF_RECORD_MISC_USER;
132 	comm.header.size = sz;
133 
134 	comm.pid = pid;
135 	comm.tid = tid;
136 	strncpy(comm.comm, comm_str, 16);
137 
138 	if (writen(fd, &comm, sz) != sz) {
139 		pr_debug("%s failed\n", __func__);
140 		return -1;
141 	}
142 
143 	return 0;
144 }
145 
write_mmap(int fd,pid_t pid,pid_t tid,u64 start,u64 len,u64 pgoff,const char * filename)146 static int write_mmap(int fd, pid_t pid, pid_t tid, u64 start, u64 len, u64 pgoff,
147 		      const char *filename)
148 {
149 	char buf[PERF_SAMPLE_MAX_SIZE];
150 	struct perf_record_mmap *mmap = (struct perf_record_mmap *)buf;
151 	size_t fsz = roundup(strlen(filename) + 1, 8);
152 	ssize_t sz = sizeof(*mmap) - sizeof(mmap->filename) + fsz;
153 
154 	mmap->header.type = PERF_RECORD_MMAP;
155 	mmap->header.misc = PERF_RECORD_MISC_USER;
156 	mmap->header.size = sz;
157 
158 	mmap->pid   = pid;
159 	mmap->tid   = tid;
160 	mmap->start = start;
161 	mmap->len   = len;
162 	mmap->pgoff = pgoff;
163 	strncpy(mmap->filename, filename, sizeof(mmap->filename));
164 
165 	if (writen(fd, mmap, sz) != sz) {
166 		pr_debug("%s failed\n", __func__);
167 		return -1;
168 	}
169 
170 	return 0;
171 }
172 
write_sample(struct test_data * td,u64 sample_type,u64 id,pid_t pid,pid_t tid)173 static int write_sample(struct test_data *td, u64 sample_type, u64 id, pid_t pid, pid_t tid)
174 {
175 	char buf[PERF_SAMPLE_MAX_SIZE];
176 	union perf_event *event = (union perf_event *)buf;
177 	struct perf_sample sample = {
178 		.ip		= td->ip,
179 		.addr		= td->addr,
180 		.id		= id,
181 		.time		= 1234567890,
182 		.cpu		= 31,
183 		.pid		= pid,
184 		.tid		= tid,
185 		.period		= 543212345,
186 		.stream_id	= 101,
187 	};
188 	int err;
189 
190 	event->header.type = PERF_RECORD_SAMPLE;
191 	event->header.misc = PERF_RECORD_MISC_USER;
192 	event->header.size = perf_event__sample_event_size(&sample, sample_type, 0);
193 	err = perf_event__synthesize_sample(event, sample_type, 0, &sample);
194 	if (err)
195 		return test_result("perf_event__synthesize_sample() failed", TEST_FAIL);
196 
197 	err = process(&td->tool, event, &sample, td->machine);
198 	if (err)
199 		return test_result("Failed to write sample", TEST_FAIL);
200 
201 	return TEST_OK;
202 }
203 
close_fd(int fd)204 static void close_fd(int fd)
205 {
206 	if (fd >= 0)
207 		close(fd);
208 }
209 
210 static const char *prog = "int bar(){};int foo(){bar();};int main(){foo();return 0;}";
211 
write_prog(char * file_name)212 static int write_prog(char *file_name)
213 {
214 	int fd = creat(file_name, 0644);
215 	ssize_t n = strlen(prog);
216 	bool err = fd < 0 || writen(fd, prog, n) != n;
217 
218 	close_fd(fd);
219 	return err ? -1 : 0;
220 }
221 
get_dlfilters_path(const char * name,char * buf,size_t sz)222 static int get_dlfilters_path(const char *name, char *buf, size_t sz)
223 {
224 	char perf[PATH_MAX];
225 	char path[PATH_MAX];
226 	char *perf_path;
227 	char *exec_path;
228 
229 	perf_exe(perf, sizeof(perf));
230 	perf_path = dirname(perf);
231 	snprintf(path, sizeof(path), "%s/dlfilters/%s", perf_path, name);
232 	if (access(path, R_OK)) {
233 		exec_path = get_argv_exec_path();
234 		if (!exec_path)
235 			return -1;
236 		snprintf(path, sizeof(path), "%s/dlfilters/%s", exec_path, name);
237 		free(exec_path);
238 		if (access(path, R_OK))
239 			return -1;
240 	}
241 	strlcpy(buf, dirname(path), sz);
242 	return 0;
243 }
244 
check_filter_desc(struct test_data * td)245 static int check_filter_desc(struct test_data *td)
246 {
247 	char *long_desc = NULL;
248 	char *desc = NULL;
249 	int ret;
250 
251 	if (get_filter_desc(td->dlfilters, td->name, &desc, &long_desc) &&
252 	    long_desc && !strcmp(long_desc, "Filter used by the 'dlfilter C API' perf test") &&
253 	    desc && !strcmp(desc, td->desc))
254 		ret = 0;
255 	else
256 		ret = -1;
257 
258 	free(desc);
259 	free(long_desc);
260 	return ret;
261 }
262 
get_ip_addr(struct test_data * td)263 static int get_ip_addr(struct test_data *td)
264 {
265 	struct map *map;
266 	struct symbol *sym;
267 
268 	map = dso__new_map(td->prog_file_name);
269 	if (!map)
270 		return -1;
271 
272 	sym = map__find_symbol_by_name(map, "foo");
273 	if (sym)
274 		td->foo = sym->start;
275 
276 	sym = map__find_symbol_by_name(map, "bar");
277 	if (sym)
278 		td->bar = sym->start;
279 
280 	map__put(map);
281 
282 	td->ip = MAP_START + td->foo;
283 	td->addr = MAP_START + td->bar;
284 
285 	return td->foo && td->bar ? 0 : -1;
286 }
287 
do_run_perf_script(struct test_data * td,int do_early)288 static int do_run_perf_script(struct test_data *td, int do_early)
289 {
290 	return system_cmd("%s script -i %s "
291 			  "--dlfilter %s/%s "
292 			  "--dlarg first "
293 			  "--dlarg %d "
294 			  "--dlarg %" PRIu64 " "
295 			  "--dlarg %" PRIu64 " "
296 			  "--dlarg %d "
297 			  "--dlarg last",
298 			  td->perf, td->perf_data_file_name, td->dlfilters,
299 			  td->name, verbose, td->ip, td->addr, do_early);
300 }
301 
run_perf_script(struct test_data * td)302 static int run_perf_script(struct test_data *td)
303 {
304 	int do_early;
305 	int err;
306 
307 	for (do_early = 0; do_early < 3; do_early++) {
308 		err = do_run_perf_script(td, do_early);
309 		if (err)
310 			return err;
311 	}
312 	return 0;
313 }
314 
315 #define TEST_SAMPLE_TYPE (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
316 			  PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_TIME | \
317 			  PERF_SAMPLE_ADDR | PERF_SAMPLE_CPU | \
318 			  PERF_SAMPLE_PERIOD | PERF_SAMPLE_STREAM_ID)
319 
test__dlfilter_test(struct test_data * td)320 static int test__dlfilter_test(struct test_data *td)
321 {
322 	u64 sample_type = TEST_SAMPLE_TYPE;
323 	pid_t pid = 12345;
324 	pid_t tid = 12346;
325 	u64 id = 99;
326 	int err;
327 
328 	if (get_dlfilters_path(td->name, td->dlfilters, PATH_MAX))
329 		return test_result("dlfilters not found", TEST_SKIP);
330 
331 	if (check_filter_desc(td))
332 		return test_result("Failed to get expected filter description", TEST_FAIL);
333 
334 	if (!have_gcc())
335 		return test_result("gcc not found", TEST_SKIP);
336 
337 	pr_debug("dlfilters path: %s\n", td->dlfilters);
338 
339 	if (write_prog(td->c_file_name))
340 		return test_result("Failed to write test C file", TEST_FAIL);
341 
342 	if (verbose > 1)
343 		system_cmd("cat %s ; echo", td->c_file_name);
344 
345 	if (system_cmd("gcc -g -o %s %s", td->prog_file_name, td->c_file_name))
346 		return TEST_FAIL;
347 
348 	if (verbose > 2)
349 		system_cmd("objdump -x -dS %s", td->prog_file_name);
350 
351 	if (get_ip_addr(td))
352 		return test_result("Failed to find program symbols", TEST_FAIL);
353 
354 	pr_debug("Creating new host machine structure\n");
355 	td->machine = machine__new_host();
356 	td->machine->env = &perf_env;
357 
358 	td->fd = creat(td->perf_data_file_name, 0644);
359 	if (td->fd < 0)
360 		return test_result("Failed to create test perf.data file", TEST_FAIL);
361 
362 	err = perf_header__write_pipe(td->fd);
363 	if (err < 0)
364 		return test_result("perf_header__write_pipe() failed", TEST_FAIL);
365 
366 	err = write_attr(td, sample_type, &id);
367 	if (err)
368 		return test_result("perf_event__synthesize_attr() failed", TEST_FAIL);
369 
370 	if (write_comm(td->fd, pid, tid, "test-prog"))
371 		return TEST_FAIL;
372 
373 	if (write_mmap(td->fd, pid, tid, MAP_START, 0x10000, 0, td->prog_file_name))
374 		return TEST_FAIL;
375 
376 	if (write_sample(td, sample_type, id, pid, tid) != TEST_OK)
377 		return TEST_FAIL;
378 
379 	if (verbose > 1)
380 		system_cmd("%s script -i %s -D", td->perf, td->perf_data_file_name);
381 
382 	err = run_perf_script(td);
383 	if (err)
384 		return TEST_FAIL;
385 
386 	return TEST_OK;
387 }
388 
unlink_path(const char * path)389 static void unlink_path(const char *path)
390 {
391 	if (*path)
392 		unlink(path);
393 }
394 
test_data__free(struct test_data * td)395 static void test_data__free(struct test_data *td)
396 {
397 	machine__delete(td->machine);
398 	close_fd(td->fd);
399 	if (verbose <= 2) {
400 		unlink_path(td->c_file_name);
401 		unlink_path(td->prog_file_name);
402 		unlink_path(td->perf_data_file_name);
403 	}
404 }
405 
test__dlfilter_ver(int ver)406 static int test__dlfilter_ver(int ver)
407 {
408 	struct test_data td = {.fd = -1};
409 	int pid = getpid();
410 	int err;
411 
412 	pr_debug("\n-- Testing version %d API --\n", ver);
413 
414 	perf_exe(td.perf, sizeof(td.perf));
415 
416 	snprintf(td.name, sizeof(td.name), "dlfilter-test-api-v%d.so", ver);
417 	snprintf(td.desc, sizeof(td.desc), "dlfilter to test v%d C API", ver);
418 	snprintf(td.perf_data_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-perf-data", pid);
419 	snprintf(td.c_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog.c", pid);
420 	snprintf(td.prog_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog", pid);
421 
422 	err = test__dlfilter_test(&td);
423 	test_data__free(&td);
424 	return err;
425 }
426 
test__dlfilter(struct test_suite * test __maybe_unused,int subtest __maybe_unused)427 static int test__dlfilter(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
428 {
429 	int err = test__dlfilter_ver(0);
430 
431 	if (err)
432 		return err;
433 	/* No test for version 1 */
434 	return test__dlfilter_ver(2);
435 }
436 
437 DEFINE_SUITE("dlfilter C API", dlfilter);
438