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