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"
36 #include "util/sample.h"
38 #define MAP_START 0x400000
41 struct perf_tool tool;
42 struct machine *machine;
49 char perf_data_file_name[PATH_MAX];
50 char c_file_name[PATH_MAX];
51 char prog_file_name[PATH_MAX];
52 char dlfilters[PATH_MAX];
55 static int test_result(const char *msg, int ret)
57 pr_debug("%s\n", msg);
61 static int process(struct perf_tool *tool, union perf_event *event,
62 struct perf_sample *sample __maybe_unused,
63 struct machine *machine __maybe_unused)
65 struct test_data *td = container_of(tool, struct test_data, tool);
68 if (writen(fd, event, event->header.size) != event->header.size)
75 #define REDIRECT_TO_DEV_NULL " >/dev/null 2>&1"
77 static __printf(1, 2) int system_cmd(const char *fmt, ...)
79 char cmd[MAXCMD + sizeof(REDIRECT_TO_DEV_NULL)];
85 ret = vsnprintf(cmd, MAXCMD, fmt, args);
88 if (ret <= 0 || ret >= MAXCMD)
92 strcat(cmd, REDIRECT_TO_DEV_NULL);
94 pr_debug("Command: %s\n", cmd);
97 pr_debug("Failed with return value %d\n", ret);
102 static bool have_gcc(void)
104 pr_debug("Checking for gcc\n");
105 return !system_cmd("gcc --version");
108 static int write_attr(struct test_data *td, u64 sample_type, u64 *id)
110 struct perf_event_attr attr = {
111 .size = sizeof(attr),
112 .type = PERF_TYPE_HARDWARE,
113 .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
114 .sample_type = sample_type,
118 return perf_event__synthesize_attr(&td->tool, &attr, 1, id, process);
121 static int write_comm(int fd, pid_t pid, pid_t tid, const char *comm_str)
123 struct perf_record_comm comm;
124 ssize_t sz = sizeof(comm);
126 comm.header.type = PERF_RECORD_COMM;
127 comm.header.misc = PERF_RECORD_MISC_USER;
128 comm.header.size = sz;
132 strncpy(comm.comm, comm_str, 16);
134 if (writen(fd, &comm, sz) != sz) {
135 pr_debug("%s failed\n", __func__);
142 static int write_mmap(int fd, pid_t pid, pid_t tid, u64 start, u64 len, u64 pgoff,
143 const char *filename)
145 char buf[PERF_SAMPLE_MAX_SIZE];
146 struct perf_record_mmap *mmap = (struct perf_record_mmap *)buf;
147 size_t fsz = roundup(strlen(filename) + 1, 8);
148 ssize_t sz = sizeof(*mmap) - sizeof(mmap->filename) + fsz;
150 mmap->header.type = PERF_RECORD_MMAP;
151 mmap->header.misc = PERF_RECORD_MISC_USER;
152 mmap->header.size = sz;
159 strncpy(mmap->filename, filename, sizeof(mmap->filename));
161 if (writen(fd, mmap, sz) != sz) {
162 pr_debug("%s failed\n", __func__);
169 static int write_sample(struct test_data *td, u64 sample_type, u64 id, pid_t pid, pid_t tid)
171 char buf[PERF_SAMPLE_MAX_SIZE];
172 union perf_event *event = (union perf_event *)buf;
173 struct perf_sample sample = {
186 event->header.type = PERF_RECORD_SAMPLE;
187 event->header.misc = PERF_RECORD_MISC_USER;
188 event->header.size = perf_event__sample_event_size(&sample, sample_type, 0);
189 err = perf_event__synthesize_sample(event, sample_type, 0, &sample);
191 return test_result("perf_event__synthesize_sample() failed", TEST_FAIL);
193 err = process(&td->tool, event, &sample, td->machine);
195 return test_result("Failed to write sample", TEST_FAIL);
200 static void close_fd(int fd)
206 static const char *prog = "int bar(){};int foo(){bar();};int main(){foo();return 0;}";
208 static int write_prog(char *file_name)
210 int fd = creat(file_name, 0644);
211 ssize_t n = strlen(prog);
212 bool err = fd < 0 || writen(fd, prog, n) != n;
218 static int get_dlfilters_path(char *buf, size_t sz)
225 perf_exe(perf, sizeof(perf));
226 perf_path = dirname(perf);
227 snprintf(path, sizeof(path), "%s/dlfilters/dlfilter-test-api-v0.so", perf_path);
228 if (access(path, R_OK)) {
229 exec_path = get_argv_exec_path();
232 snprintf(path, sizeof(path), "%s/dlfilters/dlfilter-test-api-v0.so", exec_path);
234 if (access(path, R_OK))
237 strlcpy(buf, dirname(path), sz);
241 static int check_filter_desc(struct test_data *td)
243 char *long_desc = NULL;
247 if (get_filter_desc(td->dlfilters, "dlfilter-test-api-v0.so", &desc, &long_desc) &&
248 long_desc && !strcmp(long_desc, "Filter used by the 'dlfilter C API' perf test") &&
249 desc && !strcmp(desc, "dlfilter to test v0 C API"))
259 static int get_ip_addr(struct test_data *td)
264 map = dso__new_map(td->prog_file_name);
268 sym = map__find_symbol_by_name(map, "foo");
270 td->foo = sym->start;
272 sym = map__find_symbol_by_name(map, "bar");
274 td->bar = sym->start;
278 td->ip = MAP_START + td->foo;
279 td->addr = MAP_START + td->bar;
281 return td->foo && td->bar ? 0 : -1;
284 static int do_run_perf_script(struct test_data *td, int do_early)
286 return system_cmd("%s script -i %s "
287 "--dlfilter %s/dlfilter-test-api-v0.so "
290 "--dlarg %" PRIu64 " "
291 "--dlarg %" PRIu64 " "
294 td->perf, td->perf_data_file_name, td->dlfilters,
295 verbose, td->ip, td->addr, do_early);
298 static int run_perf_script(struct test_data *td)
303 for (do_early = 0; do_early < 3; do_early++) {
304 err = do_run_perf_script(td, do_early);
311 #define TEST_SAMPLE_TYPE (PERF_SAMPLE_IP | PERF_SAMPLE_TID | \
312 PERF_SAMPLE_IDENTIFIER | PERF_SAMPLE_TIME | \
313 PERF_SAMPLE_ADDR | PERF_SAMPLE_CPU | \
314 PERF_SAMPLE_PERIOD | PERF_SAMPLE_STREAM_ID)
316 static int test__dlfilter_test(struct test_data *td)
318 u64 sample_type = TEST_SAMPLE_TYPE;
324 if (get_dlfilters_path(td->dlfilters, PATH_MAX))
325 return test_result("dlfilters not found", TEST_SKIP);
327 if (check_filter_desc(td))
328 return test_result("Failed to get expected filter description", TEST_FAIL);
331 return test_result("gcc not found", TEST_SKIP);
333 pr_debug("dlfilters path: %s\n", td->dlfilters);
335 if (write_prog(td->c_file_name))
336 return test_result("Failed to write test C file", TEST_FAIL);
339 system_cmd("cat %s ; echo", td->c_file_name);
341 if (system_cmd("gcc -g -o %s %s", td->prog_file_name, td->c_file_name))
345 system_cmd("objdump -x -dS %s", td->prog_file_name);
348 return test_result("Failed to find program symbols", TEST_FAIL);
350 pr_debug("Creating new host machine structure\n");
351 td->machine = machine__new_host();
352 td->machine->env = &perf_env;
354 td->fd = creat(td->perf_data_file_name, 0644);
356 return test_result("Failed to create test perf.data file", TEST_FAIL);
358 err = perf_header__write_pipe(td->fd);
360 return test_result("perf_header__write_pipe() failed", TEST_FAIL);
362 err = write_attr(td, sample_type, &id);
364 return test_result("perf_event__synthesize_attr() failed", TEST_FAIL);
366 if (write_comm(td->fd, pid, tid, "test-prog"))
369 if (write_mmap(td->fd, pid, tid, MAP_START, 0x10000, 0, td->prog_file_name))
372 if (write_sample(td, sample_type, id, pid, tid) != TEST_OK)
376 system_cmd("%s script -i %s -D", td->perf, td->perf_data_file_name);
378 err = run_perf_script(td);
385 static void unlink_path(const char *path)
391 static void test_data__free(struct test_data *td)
393 machine__delete(td->machine);
396 unlink_path(td->c_file_name);
397 unlink_path(td->prog_file_name);
398 unlink_path(td->perf_data_file_name);
402 static int test__dlfilter(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
404 struct test_data td = {.fd = -1};
408 perf_exe(td.perf, sizeof(td.perf));
410 snprintf(td.perf_data_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-perf-data", pid);
411 snprintf(td.c_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog.c", pid);
412 snprintf(td.prog_file_name, PATH_MAX, "/tmp/dlfilter-test-%u-prog", pid);
414 err = test__dlfilter_test(&td);
415 test_data__free(&td);
419 DEFINE_SUITE("dlfilter C API", dlfilter);