xref: /openbmc/linux/tools/perf/tests/openat-syscall.c (revision d68f0365087395fe232e39ac9c8ee53627522c3c)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a43783aeSArnaldo Carvalho de Melo #include <errno.h>
3fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
4fbf99625SJiri Olsa #include <api/fs/tracing_path.h>
58dd2a131SJiri Olsa #include <linux/err.h>
68520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
79a3993d4SArnaldo Carvalho de Melo #include <sys/types.h>
89a3993d4SArnaldo Carvalho de Melo #include <sys/stat.h>
99a3993d4SArnaldo Carvalho de Melo #include <fcntl.h>
10fbb2df23SRiku Voipio #include "thread_map.h"
11fbb2df23SRiku Voipio #include "evsel.h"
12fbb2df23SRiku Voipio #include "debug.h"
13fbb2df23SRiku Voipio #include "tests.h"
14e4aec1b1SArnaldo Carvalho de Melo #include "util/counts.h"
15fbb2df23SRiku Voipio 
16*d68f0365SIan Rogers static int test__openat_syscall_event(struct test *test __maybe_unused,
17*d68f0365SIan Rogers 				      int subtest __maybe_unused)
18fbb2df23SRiku Voipio {
19fbb2df23SRiku Voipio 	int err = -1, fd;
2032dcd021SJiri Olsa 	struct evsel *evsel;
21fbb2df23SRiku Voipio 	unsigned int nr_openat_calls = 111, i;
229749b90eSJiri Olsa 	struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
23fbb2df23SRiku Voipio 	char sbuf[STRERR_BUFSIZE];
24fbf99625SJiri Olsa 	char errbuf[BUFSIZ];
25fbb2df23SRiku Voipio 
26fbb2df23SRiku Voipio 	if (threads == NULL) {
27fbb2df23SRiku Voipio 		pr_debug("thread_map__new\n");
28fbb2df23SRiku Voipio 		return -1;
29fbb2df23SRiku Voipio 	}
30fbb2df23SRiku Voipio 
318f6725a2SArnaldo Carvalho de Melo 	evsel = evsel__newtp("syscalls", "sys_enter_openat");
328dd2a131SJiri Olsa 	if (IS_ERR(evsel)) {
33fbf99625SJiri Olsa 		tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
3487191383SNamhyung Kim 		pr_debug("%s\n", errbuf);
35fbb2df23SRiku Voipio 		goto out_thread_map_delete;
36fbb2df23SRiku Voipio 	}
37fbb2df23SRiku Voipio 
38aa8c406bSArnaldo Carvalho de Melo 	if (evsel__open_per_thread(evsel, threads) < 0) {
39fbb2df23SRiku Voipio 		pr_debug("failed to open counter: %s, "
40fbb2df23SRiku Voipio 			 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
41c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
42fbb2df23SRiku Voipio 		goto out_evsel_delete;
43fbb2df23SRiku Voipio 	}
44fbb2df23SRiku Voipio 
45fbb2df23SRiku Voipio 	for (i = 0; i < nr_openat_calls; ++i) {
46fbb2df23SRiku Voipio 		fd = openat(0, "/etc/passwd", O_RDONLY);
47fbb2df23SRiku Voipio 		close(fd);
48fbb2df23SRiku Voipio 	}
49fbb2df23SRiku Voipio 
50ea089692SArnaldo Carvalho de Melo 	if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
51ea089692SArnaldo Carvalho de Melo 		pr_debug("evsel__read_on_cpu\n");
52fbb2df23SRiku Voipio 		goto out_close_fd;
53fbb2df23SRiku Voipio 	}
54fbb2df23SRiku Voipio 
55a6fa0038SJiri Olsa 	if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
56ea089692SArnaldo Carvalho de Melo 		pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
57a6fa0038SJiri Olsa 			 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
58fbb2df23SRiku Voipio 		goto out_close_fd;
59fbb2df23SRiku Voipio 	}
60fbb2df23SRiku Voipio 
61fbb2df23SRiku Voipio 	err = 0;
62fbb2df23SRiku Voipio out_close_fd:
6388761fa1SJiri Olsa 	perf_evsel__close_fd(&evsel->core);
64fbb2df23SRiku Voipio out_evsel_delete:
655eb2dd2aSJiri Olsa 	evsel__delete(evsel);
66fbb2df23SRiku Voipio out_thread_map_delete:
677836e52eSJiri Olsa 	perf_thread_map__put(threads);
68fbb2df23SRiku Voipio 	return err;
69fbb2df23SRiku Voipio }
70*d68f0365SIan Rogers 
71*d68f0365SIan Rogers DEFINE_SUITE("Detect openat syscall event", openat_syscall_event);
72