xref: /openbmc/linux/tools/perf/tests/openat-syscall.c (revision f9b10c82faf5859262de10ea30261391e117b9bd)
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 
1633f44bfdSIan Rogers static int test__openat_syscall_event(struct test_suite *test __maybe_unused,
17d68f0365SIan Rogers 				      int subtest __maybe_unused)
18fbb2df23SRiku Voipio {
19*f9b10c82SIan Rogers 	int err = TEST_FAIL, 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");
28*f9b10c82SIan Rogers 		return TEST_FAIL;
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);
35*f9b10c82SIan Rogers 		err = TEST_SKIP;
36fbb2df23SRiku Voipio 		goto out_thread_map_delete;
37fbb2df23SRiku Voipio 	}
38fbb2df23SRiku Voipio 
39aa8c406bSArnaldo Carvalho de Melo 	if (evsel__open_per_thread(evsel, threads) < 0) {
40fbb2df23SRiku Voipio 		pr_debug("failed to open counter: %s, "
41fbb2df23SRiku Voipio 			 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
42c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
43*f9b10c82SIan Rogers 		err = TEST_SKIP;
44fbb2df23SRiku Voipio 		goto out_evsel_delete;
45fbb2df23SRiku Voipio 	}
46fbb2df23SRiku Voipio 
47fbb2df23SRiku Voipio 	for (i = 0; i < nr_openat_calls; ++i) {
48fbb2df23SRiku Voipio 		fd = openat(0, "/etc/passwd", O_RDONLY);
49fbb2df23SRiku Voipio 		close(fd);
50fbb2df23SRiku Voipio 	}
51fbb2df23SRiku Voipio 
52ea089692SArnaldo Carvalho de Melo 	if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
53ea089692SArnaldo Carvalho de Melo 		pr_debug("evsel__read_on_cpu\n");
54fbb2df23SRiku Voipio 		goto out_close_fd;
55fbb2df23SRiku Voipio 	}
56fbb2df23SRiku Voipio 
57a6fa0038SJiri Olsa 	if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
58ea089692SArnaldo Carvalho de Melo 		pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
59a6fa0038SJiri Olsa 			 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
60fbb2df23SRiku Voipio 		goto out_close_fd;
61fbb2df23SRiku Voipio 	}
62fbb2df23SRiku Voipio 
63*f9b10c82SIan Rogers 	err = TEST_OK;
64fbb2df23SRiku Voipio out_close_fd:
6588761fa1SJiri Olsa 	perf_evsel__close_fd(&evsel->core);
66fbb2df23SRiku Voipio out_evsel_delete:
675eb2dd2aSJiri Olsa 	evsel__delete(evsel);
68fbb2df23SRiku Voipio out_thread_map_delete:
697836e52eSJiri Olsa 	perf_thread_map__put(threads);
70fbb2df23SRiku Voipio 	return err;
71fbb2df23SRiku Voipio }
72d68f0365SIan Rogers 
73*f9b10c82SIan Rogers static struct test_case tests__openat_syscall_event[] = {
74*f9b10c82SIan Rogers 	TEST_CASE_REASON("Detect openat syscall event",
75*f9b10c82SIan Rogers 			 openat_syscall_event,
76*f9b10c82SIan Rogers 			 "permissions"),
77*f9b10c82SIan Rogers 	{	.name = NULL, }
78*f9b10c82SIan Rogers };
79*f9b10c82SIan Rogers 
80*f9b10c82SIan Rogers struct test_suite suite__openat_syscall_event = {
81*f9b10c82SIan Rogers 	.desc = "Detect openat syscall event",
82*f9b10c82SIan Rogers 	.test_cases = tests__openat_syscall_event,
83*f9b10c82SIan Rogers };
84