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