1 // SPDX-License-Identifier: GPL-2.0
3 * Test dlfilter C API. A perf.data file is synthesized and then processed
4 * by perf script with a dlfilter named dlfilter-test-api-v0.so. Also a C file
5 * is compiled to provide a dso to match the synthesized perf.data file.
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>
31 #include "synthetic-events.h"
37 #define MAP_START 0x400000
40 struct perf_tool tool;
41 struct machine *machine;
48 char perf_data_file_name[PATH_MAX];
49 char c_file_name[PATH_MAX];
50 char prog_file_name[PATH_MAX];
51 char dlfilters[PATH_MAX];
54 static int test_result(const char *msg, int ret)
56 pr_debug("%s\n", msg);
60 static int process(struct perf_tool *tool, union perf_event *event,
61 struct perf_sample *sample __maybe_unused,
62 struct machine *machine __maybe_unused)
64 struct test_data *td = container_of(tool, struct test_data, tool);
67 if (writen(fd, event, event->header.size) != event->header.size)
74 #define REDIRECT_TO_DEV_NULL " >/dev/null 2>&1"
76 static __printf(1, 2) int system_cmd(const char *fmt, ...)
78 char cmd[MAXCMD + sizeof(REDIRECT_TO_DEV_NULL)];
84 ret = vsnprintf(cmd, MAXCMD, fmt, args);
87 if (ret <= 0 || ret >= MAXCMD)
91 strcat(cmd, REDIRECT_TO_DEV_NULL);
93 pr_debug("Command: %s\n", cmd);
96 pr_debug("Failed with return value %d\n", ret);
101 static bool have_gcc(void)
103 pr_debug("Checking for gcc\n");
104 return !system_cmd("gcc --version");
107 static int write_attr(struct test_data *td, u64 sample_type, u64 *id)
109 struct perf_event_attr attr = {
110 .size = sizeof(attr),
111 .type = PERF_TYPE_HARDWARE,
112 .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
113 .sample_type = sample_type,
117 return perf_event__synthesize_attr(&td->tool, &attr, 1, id, process);
120 static int write_comm(int fd, pid_t pid, pid_t tid, const char *comm_str)
122 struct perf_record_comm comm;
123 ssize_t sz = sizeof(comm);
125 comm.header.type = PERF_RECORD_COMM;
126 comm.header.misc = PERF_RECORD_MISC_USER;
127 comm.header.size = sz;
131 strncpy(comm.comm, comm_str, 16);
133 if (writen(fd, &comm, sz) != sz) {
134 pr_debug("%s failed\n", __func__);
141 static int write_mmap(int fd, pid_t pid, pid_t tid, u64 start, u64 len, u64 pgoff,
142 const char *filename)
144 char buf[PERF_SAMPLE_MAX_SIZE];
145 struct perf_record_mmap *mmap = (struct perf_record_mmap *)buf;
146 size_t fsz = roundup(strlen(filename) + 1, 8);
147 ssize_t sz = sizeof(*mmap) - sizeof(mmap->filename) + fsz;
149 mmap->header.type = PERF_RECORD_MMAP;
150 mmap->header.misc = PERF_RECORD_MISC_USER;
151 mmap->header.size = sz;
158 strncpy(mmap->filename, filename, sizeof(mmap->filename));
160 if (writen(fd, mmap, sz) != sz) {
161 pr_debug("%s failed\n", __func__);
168 static int write_sample(struct test_data *td, u64 sample_type, u64 id, pid_t pid, pid_t tid)
170 char buf[PERF_SAMPLE_MAX_SIZE];
171 union perf_event *event = (union perf_event *)buf;
172 struct perf_sample sample = {
185 event->header.type = PERF_RECORD_SAMPLE;
186 event->header.misc = PERF_RECORD_MISC_USER;
187 event->header.size = perf_event__sample_event_size(&sample, sample_type, 0);
188 err = perf_event__synthesize_sample(event, sample_type, 0, &sample);
190 return test_result("perf_event__synthesize_sample() failed", TEST_FAIL);
192 err = process(&td->tool, event, &sample, td->machine);
194 return test_result("Failed to write sample", TEST_FAIL);
199 static void close_fd(int fd)
205 static const char *prog = "int bar(){};int foo(){bar();};int main(){foo();return 0;}";
207 static int write_prog(char *file_name)
209 int fd = creat(file_name, 0644);
210 ssize_t n = strlen(prog);
211 bool err = fd < 0 || writen(fd, prog, n) != n;
217 static int get_dlfilters_path(char *buf, size_t sz)
224 perf_exe(perf, sizeof(perf));
225 perf_path = dirname(perf);
226 snprintf(path, sizeof(path), "%s/dlfilters/dlfilter-test-api-v0.so", perf_path);
227 if (access(path, R_OK)) {
228 exec_path = get_argv_exec_path();
231 snprintf(path, sizeof(path), "%s/dlfilters/dlfilter-test-api-v0.so", exec_path);
233 if (access(path, R_OK))
236 strlcpy(buf, dirname(path), sz);
240 static int check_filter_desc(struct test_data *td)
242 char *long_desc = NULL;
246 if (get_filter_desc(td->dlfilters, "dlfilter-test-api-v0.so", &desc, &long_desc) &&
247 long_desc && !strcmp(long_desc, "Filter used by the 'dlfilter C API' perf test") &&
248 desc && !strcmp(desc, "dlfilter to test v0 C API"))
258 static int get_ip_addr(struct test_data *td)
263 map = dso__new_map(td->prog_file_name);
267 sym = map__find_symbol_by_name(map, "foo");
269 td->foo = sym->start;
271 sym = map__find_symbol_by_name(map, "bar");
273 td->bar = sym->start;
277 td->ip = MAP_START + td->foo;
278 td->addr = MAP_START + td->bar;
280 return td->foo && td->bar ? 0 : -1;
283 static int do_run_perf_script(struct test_data *td, int do_early)
285 return system_cmd("%s script -i %s "
286 "--dlfilter %s/dlfilter-test-api-v0.so "
289 "--dlarg %" PRIu64 " "
290 "--dlarg %" PRIu64 " "
293 td->perf, td->perf_data_file_name, td->dlfilters,
294 verbose, td->ip, td->addr, do_early);
297 static int run_perf_script(struct test_data *td)
302 for (do_early = 0; do_early < 3; do_early++) {
303 err = do_run_perf_script(td, do_early);
310 #define TEST_SAMPLE_TYPE (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
311 PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_TIME | \
312 PERF_SAMPLE_ADDR | PERF_SAMPLE_CPU | \
313 PERF_SAMPLE_PERIOD | PERF_SAMPLE_STREAM_ID)
315 static int test__dlfilter_test(struct test_data *td)
317 u64 sample_type = TEST_SAMPLE_TYPE;
323 if (get_dlfilters_path(td->dlfilters, PATH_MAX))
324 return test_result("dlfilters not found", TEST_SKIP);
326 if (check_filter_desc(td))
327 return test_result("Failed to get expected filter description", TEST_FAIL);
330 return test_result("gcc not found", TEST_SKIP);
332 pr_debug("dlfilters path: %s\n", td->dlfilters);
334 if (write_prog(td->c_file_name))
335 return test_result("Failed to write test C file", TEST_FAIL);
338 system_cmd("cat %s ; echo", td->c_file_name);
340 if (system_cmd("gcc -g -o %s %s", td->prog_file_name, td->c_file_name))
344 system_cmd("objdump -x -dS %s", td->prog_file_name);
347 return test_result("Failed to find program symbols", TEST_FAIL);
349 pr_debug("Creating new host machine structure\n");
350 td->machine = machine__new_host();
351 td->machine->env = &perf_env;
353 td->fd = creat(td->perf_data_file_name, 0644);
355 return test_result("Failed to create test perf.data file", TEST_FAIL);
357 err = perf_header__write_pipe(td->fd);
359 return test_result("perf_header__write_pipe() failed", TEST_FAIL);
361 err = write_attr(td, sample_type, &id);
363 return test_result("perf_event__synthesize_attr() failed", TEST_FAIL);
365 if (write_comm(td->fd, pid, tid, "test-prog"))
368 if (write_mmap(td->fd, pid, tid, MAP_START, 0x10000, 0, td->prog_file_name))
371 if (write_sample(td, sample_type, id, pid, tid) != TEST_OK)
375 system_cmd("%s script -i %s -D", td->perf, td->perf_data_file_name);
377 err = run_perf_script(td);
384 static void unlink_path(const char *path)
390 static void test_data__free(struct test_data *td)
392 machine__delete(td->machine);
395 unlink_path(td->c_file_name);
396 unlink_path(td->prog_file_name);
397 unlink_path(td->perf_data_file_name);
401 int test__dlfilter(struct test *test __maybe_unused, int subtest __maybe_unused)
403 struct test_data td = {.fd = -1};
407 perf_exe(td.perf, sizeof(td.perf));
409 snprintf(td.perf_data_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-perf-data", pid);
410 snprintf(td.c_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog.c", pid);
411 snprintf(td.prog_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog", pid);
413 err = test__dlfilter_test(&td);
414 test_data__free(&td);