1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <errno.h>
3*4882a593Smuzhiyun #include <inttypes.h>
4*4882a593Smuzhiyun #include <api/fs/tracing_path.h>
5*4882a593Smuzhiyun #include <linux/err.h>
6*4882a593Smuzhiyun #include <linux/string.h>
7*4882a593Smuzhiyun #include <sys/types.h>
8*4882a593Smuzhiyun #include <sys/stat.h>
9*4882a593Smuzhiyun #include <fcntl.h>
10*4882a593Smuzhiyun #include "thread_map.h"
11*4882a593Smuzhiyun #include "evsel.h"
12*4882a593Smuzhiyun #include "debug.h"
13*4882a593Smuzhiyun #include "tests.h"
14*4882a593Smuzhiyun #include "util/counts.h"
15*4882a593Smuzhiyun
test__openat_syscall_event(struct test * test __maybe_unused,int subtest __maybe_unused)16*4882a593Smuzhiyun int test__openat_syscall_event(struct test *test __maybe_unused, int subtest __maybe_unused)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun int err = -1, fd;
19*4882a593Smuzhiyun struct evsel *evsel;
20*4882a593Smuzhiyun unsigned int nr_openat_calls = 111, i;
21*4882a593Smuzhiyun struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
22*4882a593Smuzhiyun char sbuf[STRERR_BUFSIZE];
23*4882a593Smuzhiyun char errbuf[BUFSIZ];
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun if (threads == NULL) {
26*4882a593Smuzhiyun pr_debug("thread_map__new\n");
27*4882a593Smuzhiyun return -1;
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun evsel = evsel__newtp("syscalls", "sys_enter_openat");
31*4882a593Smuzhiyun if (IS_ERR(evsel)) {
32*4882a593Smuzhiyun tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
33*4882a593Smuzhiyun pr_debug("%s\n", errbuf);
34*4882a593Smuzhiyun goto out_thread_map_delete;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun if (evsel__open_per_thread(evsel, threads) < 0) {
38*4882a593Smuzhiyun pr_debug("failed to open counter: %s, "
39*4882a593Smuzhiyun "tweak /proc/sys/kernel/perf_event_paranoid?\n",
40*4882a593Smuzhiyun str_error_r(errno, sbuf, sizeof(sbuf)));
41*4882a593Smuzhiyun goto out_evsel_delete;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun for (i = 0; i < nr_openat_calls; ++i) {
45*4882a593Smuzhiyun fd = openat(0, "/etc/passwd", O_RDONLY);
46*4882a593Smuzhiyun close(fd);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
50*4882a593Smuzhiyun pr_debug("evsel__read_on_cpu\n");
51*4882a593Smuzhiyun goto out_close_fd;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
55*4882a593Smuzhiyun pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
56*4882a593Smuzhiyun nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
57*4882a593Smuzhiyun goto out_close_fd;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun err = 0;
61*4882a593Smuzhiyun out_close_fd:
62*4882a593Smuzhiyun perf_evsel__close_fd(&evsel->core);
63*4882a593Smuzhiyun out_evsel_delete:
64*4882a593Smuzhiyun evsel__delete(evsel);
65*4882a593Smuzhiyun out_thread_map_delete:
66*4882a593Smuzhiyun perf_thread_map__put(threads);
67*4882a593Smuzhiyun return err;
68*4882a593Smuzhiyun }
69