xref: /linux/tools/perf/tests/dlfilter-test.c (revision c7decec2f2d2ab0366567f9e30c0e1418cece43f)
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 
test_result(const char * msg,int ret)58 static int test_result(const char *msg, int ret)
59 {
60 	pr_debug("%s\n", msg);
61 	return ret;
62 }
63 
process(const struct perf_tool * tool,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)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 
system_cmd(const char * fmt,...)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 
have_gcc(void)105 static bool have_gcc(void)
106 {
107 	pr_debug("Checking for gcc\n");
108 	return !system_cmd("gcc --version");
109 }
110 
write_attr(struct test_data * td,u64 sample_type,u64 * id)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 
write_comm(int fd,pid_t pid,pid_t tid,const char * comm_str)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 
write_mmap(int fd,pid_t pid,pid_t tid,u64 start,u64 len,u64 pgoff,const char * filename)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 
write_sample(struct test_data * td,u64 sample_type,u64 id,pid_t pid,pid_t tid)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, 0);
192 	err = perf_event__synthesize_sample(event, sample_type, 0, &sample);
193 	if (err)
194 		return test_result("perf_event__synthesize_sample() failed", TEST_FAIL);
195 
196 	err = process(&td->tool, event, &sample, td->machine);
197 	if (err)
198 		return test_result("Failed to write sample", TEST_FAIL);
199 
200 	return TEST_OK;
201 }
202 
close_fd(int fd)203 static void close_fd(int fd)
204 {
205 	if (fd >= 0)
206 		close(fd);
207 }
208 
209 static const char *prog = "int bar(){};int foo(){bar();};int main(){foo();return 0;}";
210 
write_prog(char * file_name)211 static int write_prog(char *file_name)
212 {
213 	int fd = creat(file_name, 0644);
214 	ssize_t n = strlen(prog);
215 	bool err = fd < 0 || writen(fd, prog, n) != n;
216 
217 	close_fd(fd);
218 	return err ? -1 : 0;
219 }
220 
get_dlfilters_path(const char * name,char * buf,size_t sz)221 static int get_dlfilters_path(const char *name, char *buf, size_t sz)
222 {
223 	char perf[PATH_MAX];
224 	char path[PATH_MAX];
225 	char *perf_path;
226 	char *exec_path;
227 
228 	perf_exe(perf, sizeof(perf));
229 	perf_path = dirname(perf);
230 	snprintf(path, sizeof(path), "%s/dlfilters/%s", perf_path, name);
231 	if (access(path, R_OK)) {
232 		exec_path = get_argv_exec_path();
233 		if (!exec_path)
234 			return -1;
235 		snprintf(path, sizeof(path), "%s/dlfilters/%s", exec_path, name);
236 		free(exec_path);
237 		if (access(path, R_OK))
238 			return -1;
239 	}
240 	strlcpy(buf, dirname(path), sz);
241 	return 0;
242 }
243 
check_filter_desc(struct test_data * td)244 static int check_filter_desc(struct test_data *td)
245 {
246 	char *long_desc = NULL;
247 	char *desc = NULL;
248 	int ret;
249 
250 	if (get_filter_desc(td->dlfilters, td->name, &desc, &long_desc) &&
251 	    long_desc && !strcmp(long_desc, "Filter used by the 'dlfilter C API' perf test") &&
252 	    desc && !strcmp(desc, td->desc))
253 		ret = 0;
254 	else
255 		ret = -1;
256 
257 	free(desc);
258 	free(long_desc);
259 	return ret;
260 }
261 
get_ip_addr(struct test_data * td)262 static int get_ip_addr(struct test_data *td)
263 {
264 	struct map *map;
265 	struct symbol *sym;
266 
267 	map = dso__new_map(td->prog_file_name);
268 	if (!map)
269 		return -1;
270 
271 	sym = map__find_symbol_by_name(map, "foo");
272 	if (sym)
273 		td->foo = sym->start;
274 
275 	sym = map__find_symbol_by_name(map, "bar");
276 	if (sym)
277 		td->bar = sym->start;
278 
279 	map__put(map);
280 
281 	td->ip = MAP_START + td->foo;
282 	td->addr = MAP_START + td->bar;
283 
284 	return td->foo && td->bar ? 0 : -1;
285 }
286 
do_run_perf_script(struct test_data * td,int do_early)287 static int do_run_perf_script(struct test_data *td, int do_early)
288 {
289 	return system_cmd("%s script -i %s "
290 			  "--dlfilter %s/%s "
291 			  "--dlarg first "
292 			  "--dlarg %d "
293 			  "--dlarg %" PRIu64 " "
294 			  "--dlarg %" PRIu64 " "
295 			  "--dlarg %d "
296 			  "--dlarg last",
297 			  td->perf, td->perf_data_file_name, td->dlfilters,
298 			  td->name, verbose, td->ip, td->addr, do_early);
299 }
300 
run_perf_script(struct test_data * td)301 static int run_perf_script(struct test_data *td)
302 {
303 	int do_early;
304 	int err;
305 
306 	for (do_early = 0; do_early < 3; do_early++) {
307 		err = do_run_perf_script(td, do_early);
308 		if (err)
309 			return err;
310 	}
311 	return 0;
312 }
313 
314 #define TEST_SAMPLE_TYPE (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
315 			  PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_TIME | \
316 			  PERF_SAMPLE_ADDR | PERF_SAMPLE_CPU | \
317 			  PERF_SAMPLE_PERIOD | PERF_SAMPLE_STREAM_ID)
318 
test__dlfilter_test(struct test_data * td)319 static int test__dlfilter_test(struct test_data *td)
320 {
321 	struct perf_env host_env;
322 	u64 sample_type = TEST_SAMPLE_TYPE;
323 	pid_t pid = 12345;
324 	pid_t tid = 12346;
325 	u64 id = 99;
326 	int err = TEST_OK;
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 	perf_env__init(&host_env);
356 	td->machine = machine__new_host(&host_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 		err = test_result("perf_header__write_pipe() failed", TEST_FAIL);
365 		goto out;
366 	}
367 	err = write_attr(td, sample_type, &id);
368 	if (err) {
369 		err = test_result("perf_event__synthesize_attr() failed", TEST_FAIL);
370 		goto out;
371 	}
372 	if (write_comm(td->fd, pid, tid, "test-prog")) {
373 		err = TEST_FAIL;
374 		goto out;
375 	}
376 	if (write_mmap(td->fd, pid, tid, MAP_START, 0x10000, 0, td->prog_file_name)) {
377 		err = TEST_FAIL;
378 		goto out;
379 	}
380 	if (write_sample(td, sample_type, id, pid, tid) != TEST_OK) {
381 		err = TEST_FAIL;
382 		goto out;
383 	}
384 	if (verbose > 1)
385 		system_cmd("%s script -i %s -D", td->perf, td->perf_data_file_name);
386 
387 	err = run_perf_script(td) ? TEST_FAIL : TEST_OK;
388 out:
389 	perf_env__exit(&host_env);
390 	return err;
391 }
392 
unlink_path(const char * path)393 static void unlink_path(const char *path)
394 {
395 	if (*path)
396 		unlink(path);
397 }
398 
test_data__free(struct test_data * td)399 static void test_data__free(struct test_data *td)
400 {
401 	machine__delete(td->machine);
402 	close_fd(td->fd);
403 	if (verbose <= 2) {
404 		unlink_path(td->c_file_name);
405 		unlink_path(td->prog_file_name);
406 		unlink_path(td->perf_data_file_name);
407 	}
408 }
409 
test__dlfilter_ver(int ver)410 static int test__dlfilter_ver(int ver)
411 {
412 	struct test_data td = {.fd = -1};
413 	int pid = getpid();
414 	int err;
415 
416 	pr_debug("\n-- Testing version %d API --\n", ver);
417 
418 	perf_exe(td.perf, sizeof(td.perf));
419 
420 	snprintf(td.name, sizeof(td.name), "dlfilter-test-api-v%d.so", ver);
421 	snprintf(td.desc, sizeof(td.desc), "dlfilter to test v%d C API", ver);
422 	snprintf(td.perf_data_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-perf-data", pid);
423 	snprintf(td.c_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog.c", pid);
424 	snprintf(td.prog_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog", pid);
425 
426 	err = test__dlfilter_test(&td);
427 	test_data__free(&td);
428 	return err;
429 }
430 
test__dlfilter(struct test_suite * test __maybe_unused,int subtest __maybe_unused)431 static int test__dlfilter(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
432 {
433 	int err = test__dlfilter_ver(0);
434 
435 	if (err)
436 		return err;
437 	/* No test for version 1 */
438 	return test__dlfilter_ver(2);
439 }
440 
441 DEFINE_SUITE("dlfilter C API", dlfilter);
442