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