Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 3 | #include <inttypes.h> |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 4 | #include <api/fs/tracing_path.h> |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 5 | #include <linux/err.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 6 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 9a3993d | 2017-04-18 11:33:48 -0300 | [diff] [blame] | 7 | #include <sys/types.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <fcntl.h> |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 10 | #include "thread_map.h" |
| 11 | #include "evsel.h" |
| 12 | #include "debug.h" |
| 13 | #include "tests.h" |
Arnaldo Carvalho de Melo | e4aec1b | 2019-08-21 14:01:24 -0300 | [diff] [blame] | 14 | #include "util/counts.h" |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 15 | |
Ian Rogers | 33f44bf | 2021-11-03 23:41:51 -0700 | [diff] [blame] | 16 | static int test__openat_syscall_event(struct test_suite *test __maybe_unused, |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 17 | int subtest __maybe_unused) |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 18 | { |
Ian Rogers | f9b10c8 | 2022-05-17 21:20:22 -0700 | [diff] [blame] | 19 | int err = TEST_FAIL, fd; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 20 | struct evsel *evsel; |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 21 | unsigned int nr_openat_calls = 111, i; |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 22 | struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX); |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 23 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 24 | char errbuf[BUFSIZ]; |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 25 | |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 26 | if (threads == NULL) { |
| 27 | pr_debug("thread_map__new\n"); |
Ian Rogers | f9b10c8 | 2022-05-17 21:20:22 -0700 | [diff] [blame] | 28 | return TEST_FAIL; |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Arnaldo Carvalho de Melo | 8f6725a | 2020-05-06 13:27:04 -0300 | [diff] [blame] | 31 | evsel = evsel__newtp("syscalls", "sys_enter_openat"); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 32 | if (IS_ERR(evsel)) { |
Jiri Olsa | fbf9962 | 2015-09-02 09:56:45 +0200 | [diff] [blame] | 33 | tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat"); |
Namhyung Kim | 8719138 | 2015-10-20 00:23:48 +0900 | [diff] [blame] | 34 | pr_debug("%s\n", errbuf); |
Ian Rogers | f9b10c8 | 2022-05-17 21:20:22 -0700 | [diff] [blame] | 35 | err = TEST_SKIP; |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 36 | goto out_thread_map_delete; |
| 37 | } |
| 38 | |
Arnaldo Carvalho de Melo | aa8c406 | 2020-04-29 16:21:03 -0300 | [diff] [blame] | 39 | if (evsel__open_per_thread(evsel, threads) < 0) { |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 40 | pr_debug("failed to open counter: %s, " |
| 41 | "tweak /proc/sys/kernel/perf_event_paranoid?\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 42 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Ian Rogers | f9b10c8 | 2022-05-17 21:20:22 -0700 | [diff] [blame] | 43 | err = TEST_SKIP; |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 44 | goto out_evsel_delete; |
| 45 | } |
| 46 | |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 47 | for (i = 0; i < nr_openat_calls; ++i) { |
| 48 | fd = openat(0, "/etc/passwd", O_RDONLY); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 49 | close(fd); |
| 50 | } |
| 51 | |
Arnaldo Carvalho de Melo | ea08969 | 2020-04-30 11:00:53 -0300 | [diff] [blame] | 52 | if (evsel__read_on_cpu(evsel, 0, 0) < 0) { |
| 53 | pr_debug("evsel__read_on_cpu\n"); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 54 | goto out_close_fd; |
| 55 | } |
| 56 | |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 57 | if (perf_counts(evsel->counts, 0, 0)->val != nr_openat_calls) { |
Arnaldo Carvalho de Melo | ea08969 | 2020-04-30 11:00:53 -0300 | [diff] [blame] | 58 | pr_debug("evsel__read_on_cpu: expected to intercept %d calls, got %" PRIu64 "\n", |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 59 | nr_openat_calls, perf_counts(evsel->counts, 0, 0)->val); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 60 | goto out_close_fd; |
| 61 | } |
| 62 | |
Ian Rogers | f9b10c8 | 2022-05-17 21:20:22 -0700 | [diff] [blame] | 63 | err = TEST_OK; |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 64 | out_close_fd: |
Jiri Olsa | 88761fa | 2019-07-21 13:24:50 +0200 | [diff] [blame] | 65 | perf_evsel__close_fd(&evsel->core); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 66 | out_evsel_delete: |
Jiri Olsa | 5eb2dd2 | 2019-07-21 13:23:57 +0200 | [diff] [blame] | 67 | evsel__delete(evsel); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 68 | out_thread_map_delete: |
Jiri Olsa | 7836e52 | 2019-07-21 13:24:20 +0200 | [diff] [blame] | 69 | perf_thread_map__put(threads); |
Jiri Olsa | d3b59a3 | 2012-11-10 01:46:42 +0100 | [diff] [blame] | 70 | return err; |
| 71 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame] | 72 | |
Ian Rogers | f9b10c8 | 2022-05-17 21:20:22 -0700 | [diff] [blame] | 73 | static struct test_case tests__openat_syscall_event[] = { |
| 74 | TEST_CASE_REASON("Detect openat syscall event", |
| 75 | openat_syscall_event, |
| 76 | "permissions"), |
| 77 | { .name = NULL, } |
| 78 | }; |
| 79 | |
| 80 | struct test_suite suite__openat_syscall_event = { |
| 81 | .desc = "Detect openat syscall event", |
| 82 | .test_cases = tests__openat_syscall_event, |
| 83 | }; |