xref: /openbmc/linux/tools/perf/tests/openat-syscall.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
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>
10*fd3f518fSIan Rogers #include <unistd.h>
11fbb2df23SRiku Voipio #include "thread_map.h"
12fbb2df23SRiku Voipio #include "evsel.h"
13fbb2df23SRiku Voipio #include "debug.h"
14fbb2df23SRiku Voipio #include "tests.h"
15e4aec1b1SArnaldo Carvalho de Melo #include "util/counts.h"
16fbb2df23SRiku Voipio 
test__openat_syscall_event(struct test_suite * test __maybe_unused,int subtest __maybe_unused)1733f44bfdSIan Rogers static int test__openat_syscall_event(struct test_suite *test __maybe_unused,
18d68f0365SIan Rogers 				      int subtest __maybe_unused)
19fbb2df23SRiku Voipio {
20f9b10c82SIan Rogers 	int err = TEST_FAIL, fd;
2132dcd021SJiri Olsa 	struct evsel *evsel;
22fbb2df23SRiku Voipio 	unsigned int nr_openat_calls = 111, i;
239749b90eSJiri Olsa 	struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
24fbb2df23SRiku Voipio 	char sbuf[STRERR_BUFSIZE];
25fbf99625SJiri Olsa 	char errbuf[BUFSIZ];
26fbb2df23SRiku Voipio 
27fbb2df23SRiku Voipio 	if (threads == NULL) {
28fbb2df23SRiku Voipio 		pr_debug("thread_map__new\n");
29f9b10c82SIan Rogers 		return TEST_FAIL;
30fbb2df23SRiku Voipio 	}
31fbb2df23SRiku Voipio 
328f6725a2SArnaldo Carvalho de Melo 	evsel = evsel__newtp("syscalls", "sys_enter_openat");
338dd2a131SJiri Olsa 	if (IS_ERR(evsel)) {
34fbf99625SJiri Olsa 		tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
3587191383SNamhyung Kim 		pr_debug("%s\n", errbuf);
36f9b10c82SIan Rogers 		err = TEST_SKIP;
37fbb2df23SRiku Voipio 		goto out_thread_map_delete;
38fbb2df23SRiku Voipio 	}
39fbb2df23SRiku Voipio 
40aa8c406bSArnaldo Carvalho de Melo 	if (evsel__open_per_thread(evsel, threads) < 0) {
41fbb2df23SRiku Voipio 		pr_debug("failed to open counter: %s, "
42fbb2df23SRiku Voipio 			 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
43c8b5f2c9SArnaldo Carvalho de Melo 			 str_error_r(errno, sbuf, sizeof(sbuf)));
44f9b10c82SIan Rogers 		err = TEST_SKIP;
45fbb2df23SRiku Voipio 		goto out_evsel_delete;
46fbb2df23SRiku Voipio 	}
47fbb2df23SRiku Voipio 
48fbb2df23SRiku Voipio 	for (i = 0; i < nr_openat_calls; ++i) {
49fbb2df23SRiku Voipio 		fd = openat(0, "/etc/passwd", O_RDONLY);
50fbb2df23SRiku Voipio 		close(fd);
51fbb2df23SRiku Voipio 	}
52fbb2df23SRiku Voipio 
53ea089692SArnaldo Carvalho de Melo 	if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
54ea089692SArnaldo Carvalho de Melo 		pr_debug("evsel__read_on_cpu\n");
55fbb2df23SRiku Voipio 		goto out_close_fd;
56fbb2df23SRiku Voipio 	}
57fbb2df23SRiku Voipio 
58a6fa0038SJiri Olsa 	if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
59ea089692SArnaldo Carvalho de Melo 		pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
60a6fa0038SJiri Olsa 			 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
61fbb2df23SRiku Voipio 		goto out_close_fd;
62fbb2df23SRiku Voipio 	}
63fbb2df23SRiku Voipio 
64f9b10c82SIan Rogers 	err = TEST_OK;
65fbb2df23SRiku Voipio out_close_fd:
6688761fa1SJiri Olsa 	perf_evsel__close_fd(&evsel->core);
67fbb2df23SRiku Voipio out_evsel_delete:
685eb2dd2aSJiri Olsa 	evsel__delete(evsel);
69fbb2df23SRiku Voipio out_thread_map_delete:
707836e52eSJiri Olsa 	perf_thread_map__put(threads);
71fbb2df23SRiku Voipio 	return err;
72fbb2df23SRiku Voipio }
73d68f0365SIan Rogers 
74f9b10c82SIan Rogers static struct test_case tests__openat_syscall_event[] = {
75f9b10c82SIan Rogers 	TEST_CASE_REASON("Detect openat syscall event",
76f9b10c82SIan Rogers 			 openat_syscall_event,
77f9b10c82SIan Rogers 			 "permissions"),
78f9b10c82SIan Rogers 	{	.name = NULL, }
79f9b10c82SIan Rogers };
80f9b10c82SIan Rogers 
81f9b10c82SIan Rogers struct test_suite suite__openat_syscall_event = {
82f9b10c82SIan Rogers 	.desc = "Detect openat syscall event",
83f9b10c82SIan Rogers 	.test_cases = tests__openat_syscall_event,
84f9b10c82SIan Rogers };
85