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