blob: 85bb6729d303a828afdd7a7125fb9ccfd4f9dc8c [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Jiri Olsafbf99622015-09-02 09:56:45 +02003#include <api/fs/tracing_path.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +02004#include <linux/err.h>
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -03005#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
Jiri Olsad3b59a32012-11-10 01:46:42 +01008#include "thread_map.h"
9#include "evsel.h"
10#include "debug.h"
11#include "tests.h"
12
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -030013int test__openat_syscall_event(struct test *test __maybe_unused, int subtest __maybe_unused)
Jiri Olsad3b59a32012-11-10 01:46:42 +010014{
15 int err = -1, fd;
Jiri Olsad3b59a32012-11-10 01:46:42 +010016 struct perf_evsel *evsel;
Riku Voipio43f322b2015-04-16 16:52:53 +030017 unsigned int nr_openat_calls = 111, i;
Arnaldo Carvalho de Meloa60d7952012-12-10 15:11:43 -030018 struct thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +000019 char sbuf[STRERR_BUFSIZE];
Jiri Olsafbf99622015-09-02 09:56:45 +020020 char errbuf[BUFSIZ];
Jiri Olsad3b59a32012-11-10 01:46:42 +010021
Jiri Olsad3b59a32012-11-10 01:46:42 +010022 if (threads == NULL) {
23 pr_debug("thread_map__new\n");
24 return -1;
25 }
26
Riku Voipio43f322b2015-04-16 16:52:53 +030027 evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
Jiri Olsa8dd2a132015-09-07 10:38:06 +020028 if (IS_ERR(evsel)) {
Jiri Olsafbf99622015-09-02 09:56:45 +020029 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
Namhyung Kim87191382015-10-20 00:23:48 +090030 pr_debug("%s\n", errbuf);
Jiri Olsad3b59a32012-11-10 01:46:42 +010031 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",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030037 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsad3b59a32012-11-10 01:46:42 +010038 goto out_evsel_delete;
39 }
40
Riku Voipio43f322b2015-04-16 16:52:53 +030041 for (i = 0; i < nr_openat_calls; ++i) {
42 fd = openat(0, "/etc/passwd", O_RDONLY);
Jiri Olsad3b59a32012-11-10 01:46:42 +010043 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
Jiri Olsaa6fa0032015-06-26 11:29:11 +020051 if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) {
Jiri Olsad3b59a32012-11-10 01:46:42 +010052 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n",
Jiri Olsaa6fa0032015-06-26 11:29:11 +020053 nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val);
Jiri Olsad3b59a32012-11-10 01:46:42 +010054 goto out_close_fd;
55 }
56
57 err = 0;
58out_close_fd:
59 perf_evsel__close_fd(evsel, 1, threads->nr);
60out_evsel_delete:
61 perf_evsel__delete(evsel);
62out_thread_map_delete:
Jiri Olsa186fbb72015-06-23 00:36:05 +020063 thread_map__put(threads);
Jiri Olsad3b59a32012-11-10 01:46:42 +010064 return err;
65}