blob: 85a8f0fe7aea00da1dcf1484fc1458e1786d0d0a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Jiri Olsafbf99622015-09-02 09:56:45 +02004#include <api/fs/tracing_path.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +02005#include <linux/err.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -03006#include <linux/string.h>
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -03007#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
Jiri Olsad3b59a32012-11-10 01:46:42 +010010#include "thread_map.h"
11#include "evsel.h"
12#include "debug.h"
13#include "tests.h"
Arnaldo Carvalho de Meloe4aec1b2019-08-21 14:01:24 -030014#include "util/counts.h"
Jiri Olsad3b59a32012-11-10 01:46:42 +010015
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030016int test__openat_syscall_event(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsad3b59a32012-11-10 01:46:42 +010017{
18 int err = -1, fd;
Jiri Olsa32dcd022019-07-21 13:23:51 +020019 struct evsel *evsel;
Riku Voipio43f322b2015-04-16 16:52:53 +030020 unsigned int nr_openat_calls = 111, i;
Jiri Olsa9749b902019-07-21 13:23:50 +020021 struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000022 char sbuf[STRERR_BUFSIZE];
Jiri Olsafbf99622015-09-02 09:56:45 +020023 char errbuf[BUFSIZ];
Jiri Olsad3b59a32012-11-10 01:46:42 +010024
Jiri Olsad3b59a32012-11-10 01:46:42 +010025 if (threads == NULL) {
26 pr_debug("thread_map__new\n");
27 return -1;
28 }
29
Arnaldo Carvalho de Melo8f6725a2020-05-06 13:27:04 -030030 evsel = evsel__newtp("syscalls", "sys_enter_openat");
Jiri Olsa8dd2a132015-09-07 10:38:06 +020031 if (IS_ERR(evsel)) {
Jiri Olsafbf99622015-09-02 09:56:45 +020032 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
Namhyung Kim87191382015-10-20 00:23:48 +090033 pr_debug("%s\n", errbuf);
Jiri Olsad3b59a32012-11-10 01:46:42 +010034 goto out_thread_map_delete;
35 }
36
Arnaldo Carvalho de Meloaa8c4062020-04-29 16:21:03 -030037 if (evsel__open_per_thread(evsel, threads) < 0) {
Jiri Olsad3b59a32012-11-10 01:46:42 +010038 pr_debug("failed to open counter: %s, "
39 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030040 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsad3b59a32012-11-10 01:46:42 +010041 goto out_evsel_delete;
42 }
43
Riku Voipio43f322b2015-04-16 16:52:53 +030044 for (i = 0; i < nr_openat_calls; ++i) {
45 fd = openat(0, "/etc/passwd", O_RDONLY);
Jiri Olsad3b59a32012-11-10 01:46:42 +010046 close(fd);
47 }
48
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -030049 if (evsel__read_on_cpu(evsel, 0, 0) < 0) {
50 pr_debug("evsel__read_on_cpu\n");
Jiri Olsad3b59a32012-11-10 01:46:42 +010051 goto out_close_fd;
52 }
53
Jiri Olsaa6fa0032015-06-26 11:29:11 +020054 if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
Arnaldo Carvalho de Meloea089692020-04-30 11:00:53 -030055 pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
Jiri Olsaa6fa0032015-06-26 11:29:11 +020056 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
Jiri Olsad3b59a32012-11-10 01:46:42 +010057 goto out_close_fd;
58 }
59
60 err = 0;
61out_close_fd:
Jiri Olsa88761fa2019-07-21 13:24:50 +020062 perf_evsel__close_fd(&evsel->core);
Jiri Olsad3b59a32012-11-10 01:46:42 +010063out_evsel_delete:
Jiri Olsa5eb2dd22019-07-21 13:23:57 +020064 evsel__delete(evsel);
Jiri Olsad3b59a32012-11-10 01:46:42 +010065out_thread_map_delete:
Jiri Olsa7836e522019-07-21 13:24:20 +020066 perf_thread_map__put(threads);
Jiri Olsad3b59a32012-11-10 01:46:42 +010067 return err;
68}