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