Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | aeb00b1 | 2019-08-22 15:40:29 -0300 | [diff] [blame] | 2 | #include <stdbool.h> |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 3 | #include <linux/err.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 4 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | bafae98 | 2018-01-22 16:42:16 -0300 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <fcntl.h> |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 8 | #include "evlist.h" |
| 9 | #include "evsel.h" |
| 10 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | aeb00b1 | 2019-08-22 15:40:29 -0300 | [diff] [blame] | 11 | #include "record.h" |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 12 | #include "tests.h" |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 13 | #include "debug.h" |
Arnaldo Carvalho de Melo | e0fcfb0 | 2019-09-23 12:20:38 -0300 | [diff] [blame] | 14 | #include "util/mmap.h" |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 15 | #include <errno.h> |
Jiri Olsa | 7728fa0 | 2019-10-07 14:53:17 +0200 | [diff] [blame] | 16 | #include <perf/mmap.h> |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 17 | |
Arnaldo Carvalho de Melo | ab6526b | 2016-07-05 14:43:27 -0300 | [diff] [blame] | 18 | #ifndef O_DIRECTORY |
| 19 | #define O_DIRECTORY 00200000 |
| 20 | #endif |
| 21 | #ifndef AT_FDCWD |
| 22 | #define AT_FDCWD -100 |
| 23 | #endif |
| 24 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 25 | int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused) |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 26 | { |
Arnaldo Carvalho de Melo | b400679 | 2013-12-19 14:43:45 -0300 | [diff] [blame] | 27 | struct record_opts opts = { |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 28 | .target = { |
| 29 | .uid = UINT_MAX, |
| 30 | .uses_mmap = true, |
| 31 | }, |
Arnaldo Carvalho de Melo | 509051e | 2014-01-14 17:52:14 -0300 | [diff] [blame] | 32 | .no_buffering = true, |
| 33 | .freq = 1, |
| 34 | .mmap_pages = 256, |
| 35 | .raw_samples = true, |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 36 | }; |
| 37 | const char *filename = "/etc/passwd"; |
| 38 | int flags = O_RDONLY | O_DIRECTORY; |
Jiri Olsa | 0f98b11 | 2019-07-21 13:23:55 +0200 | [diff] [blame] | 39 | struct evlist *evlist = evlist__new(); |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 40 | struct evsel *evsel; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 41 | int err = -1, i, nr_events = 0, nr_polls = 0; |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 42 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 43 | |
| 44 | if (evlist == NULL) { |
| 45 | pr_debug("%s: perf_evlist__new\n", __func__); |
| 46 | goto out; |
| 47 | } |
| 48 | |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 49 | evsel = perf_evsel__newtp("syscalls", "sys_enter_openat"); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 50 | if (IS_ERR(evsel)) { |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 51 | pr_debug("%s: perf_evsel__newtp\n", __func__); |
| 52 | goto out_delete_evlist; |
| 53 | } |
| 54 | |
Jiri Olsa | a1cf3a7 | 2019-07-21 13:23:59 +0200 | [diff] [blame] | 55 | evlist__add(evlist, evsel); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 56 | |
| 57 | err = perf_evlist__create_maps(evlist, &opts.target); |
| 58 | if (err < 0) { |
| 59 | pr_debug("%s: perf_evlist__create_maps\n", __func__); |
| 60 | goto out_delete_evlist; |
| 61 | } |
| 62 | |
Arnaldo Carvalho de Melo | 6ec17b4 | 2020-04-29 15:57:01 -0300 | [diff] [blame] | 63 | evsel__config(evsel, &opts, NULL); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 64 | |
Jiri Olsa | 03617c2 | 2019-07-21 13:24:42 +0200 | [diff] [blame] | 65 | perf_thread_map__set_pid(evlist->core.threads, 0, getpid()); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 66 | |
Jiri Olsa | 474ddc4 | 2019-07-21 13:24:06 +0200 | [diff] [blame] | 67 | err = evlist__open(evlist); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 68 | if (err < 0) { |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 69 | pr_debug("perf_evlist__open: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 70 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 71 | goto out_delete_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Jiri Olsa | 9521b5f | 2019-07-28 12:45:35 +0200 | [diff] [blame] | 74 | err = evlist__mmap(evlist, UINT_MAX); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 75 | if (err < 0) { |
Jiri Olsa | 9521b5f | 2019-07-28 12:45:35 +0200 | [diff] [blame] | 76 | pr_debug("evlist__mmap: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 77 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 78 | goto out_delete_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Jiri Olsa | 1c87f16 | 2019-07-21 13:24:08 +0200 | [diff] [blame] | 81 | evlist__enable(evlist); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * Generate the event: |
| 85 | */ |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 86 | openat(AT_FDCWD, filename, flags); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 87 | |
| 88 | while (1) { |
| 89 | int before = nr_events; |
| 90 | |
Jiri Olsa | c976ee1 | 2019-07-30 13:04:59 +0200 | [diff] [blame] | 91 | for (i = 0; i < evlist->core.nr_mmaps; i++) { |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 92 | union perf_event *event; |
Jiri Olsa | a583053 | 2019-07-27 20:30:53 +0200 | [diff] [blame] | 93 | struct mmap *md; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 94 | |
Kan Liang | 1d1b5632 | 2018-03-01 18:09:05 -0500 | [diff] [blame] | 95 | md = &evlist->mmap[i]; |
Jiri Olsa | 7c4d418 | 2019-10-07 14:53:18 +0200 | [diff] [blame] | 96 | if (perf_mmap__read_init(&md->core) < 0) |
Kan Liang | 1d1b5632 | 2018-03-01 18:09:05 -0500 | [diff] [blame] | 97 | continue; |
| 98 | |
Jiri Olsa | 151ed5d | 2019-10-07 14:53:20 +0200 | [diff] [blame] | 99 | while ((event = perf_mmap__read_event(&md->core)) != NULL) { |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 100 | const u32 type = event->header.type; |
| 101 | int tp_flags; |
| 102 | struct perf_sample sample; |
| 103 | |
| 104 | ++nr_events; |
| 105 | |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 106 | if (type != PERF_RECORD_SAMPLE) { |
Jiri Olsa | 7728fa0 | 2019-10-07 14:53:17 +0200 | [diff] [blame] | 107 | perf_mmap__consume(&md->core); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 108 | continue; |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 109 | } |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 110 | |
Arnaldo Carvalho de Melo | 6b6017a | 2020-04-30 11:03:49 -0300 | [diff] [blame] | 111 | err = evsel__parse_sample(evsel, event, &sample); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 112 | if (err) { |
Namhyung Kim | 8719138 | 2015-10-20 00:23:48 +0900 | [diff] [blame] | 113 | pr_debug("Can't parse sample, err = %d\n", err); |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 114 | goto out_delete_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Arnaldo Carvalho de Melo | efc0cdc | 2020-04-29 16:26:57 -0300 | [diff] [blame] | 117 | tp_flags = evsel__intval(evsel, &sample, "flags"); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 118 | |
| 119 | if (flags != tp_flags) { |
| 120 | pr_debug("%s: Expected flags=%#x, got %#x\n", |
| 121 | __func__, flags, tp_flags); |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 122 | goto out_delete_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | goto out_ok; |
| 126 | } |
Jiri Olsa | 32fdc2c | 2019-10-07 14:53:19 +0200 | [diff] [blame] | 127 | perf_mmap__read_done(&md->core); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (nr_events == before) |
Jiri Olsa | 80ab298 | 2019-08-31 22:48:33 +0200 | [diff] [blame] | 131 | evlist__poll(evlist, 10); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 132 | |
| 133 | if (++nr_polls > 5) { |
| 134 | pr_debug("%s: no events!\n", __func__); |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 135 | goto out_delete_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | out_ok: |
| 139 | err = 0; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 140 | out_delete_evlist: |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 141 | evlist__delete(evlist); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 142 | out: |
| 143 | return err; |
| 144 | } |