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> |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 3 | #include "perf.h" |
| 4 | #include "evlist.h" |
| 5 | #include "evsel.h" |
| 6 | #include "thread_map.h" |
| 7 | #include "tests.h" |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 8 | #include "debug.h" |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 9 | #include <errno.h> |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 10 | |
Arnaldo Carvalho de Melo | ab6526b | 2016-07-05 14:43:27 -0300 | [diff] [blame] | 11 | #ifndef O_DIRECTORY |
| 12 | #define O_DIRECTORY 00200000 |
| 13 | #endif |
| 14 | #ifndef AT_FDCWD |
| 15 | #define AT_FDCWD -100 |
| 16 | #endif |
| 17 | |
Arnaldo Carvalho de Melo | 81f17c9 | 2017-08-03 15:16:31 -0300 | [diff] [blame] | 18 | 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] | 19 | { |
Arnaldo Carvalho de Melo | b400679 | 2013-12-19 14:43:45 -0300 | [diff] [blame] | 20 | struct record_opts opts = { |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 21 | .target = { |
| 22 | .uid = UINT_MAX, |
| 23 | .uses_mmap = true, |
| 24 | }, |
Arnaldo Carvalho de Melo | 509051e | 2014-01-14 17:52:14 -0300 | [diff] [blame] | 25 | .no_buffering = true, |
| 26 | .freq = 1, |
| 27 | .mmap_pages = 256, |
| 28 | .raw_samples = true, |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 29 | }; |
| 30 | const char *filename = "/etc/passwd"; |
| 31 | int flags = O_RDONLY | O_DIRECTORY; |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 32 | struct perf_evlist *evlist = perf_evlist__new(); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 33 | struct perf_evsel *evsel; |
| 34 | int err = -1, i, nr_events = 0, nr_polls = 0; |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 35 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 36 | |
| 37 | if (evlist == NULL) { |
| 38 | pr_debug("%s: perf_evlist__new\n", __func__); |
| 39 | goto out; |
| 40 | } |
| 41 | |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 42 | evsel = perf_evsel__newtp("syscalls", "sys_enter_openat"); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 43 | if (IS_ERR(evsel)) { |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 44 | pr_debug("%s: perf_evsel__newtp\n", __func__); |
| 45 | goto out_delete_evlist; |
| 46 | } |
| 47 | |
| 48 | perf_evlist__add(evlist, evsel); |
| 49 | |
| 50 | err = perf_evlist__create_maps(evlist, &opts.target); |
| 51 | if (err < 0) { |
| 52 | pr_debug("%s: perf_evlist__create_maps\n", __func__); |
| 53 | goto out_delete_evlist; |
| 54 | } |
| 55 | |
Arnaldo Carvalho de Melo | e68ae9c | 2016-04-11 18:15:29 -0300 | [diff] [blame] | 56 | perf_evsel__config(evsel, &opts, NULL); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 57 | |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 58 | thread_map__set_pid(evlist->threads, 0, getpid()); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 59 | |
| 60 | err = perf_evlist__open(evlist); |
| 61 | if (err < 0) { |
Masami Hiramatsu | ba3dfff | 2014-08-14 02:22:45 +0000 | [diff] [blame] | 62 | pr_debug("perf_evlist__open: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 63 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 64 | goto out_delete_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Wang Nan | f74b9d3a | 2017-12-03 02:00:37 +0000 | [diff] [blame^] | 67 | err = perf_evlist__mmap(evlist, UINT_MAX); |
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__mmap: %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 | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 71 | goto out_delete_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | perf_evlist__enable(evlist); |
| 75 | |
| 76 | /* |
| 77 | * Generate the event: |
| 78 | */ |
Riku Voipio | 43f322b | 2015-04-16 16:52:53 +0300 | [diff] [blame] | 79 | openat(AT_FDCWD, filename, flags); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 80 | |
| 81 | while (1) { |
| 82 | int before = nr_events; |
| 83 | |
| 84 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 85 | union perf_event *event; |
| 86 | |
| 87 | while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) { |
| 88 | const u32 type = event->header.type; |
| 89 | int tp_flags; |
| 90 | struct perf_sample sample; |
| 91 | |
| 92 | ++nr_events; |
| 93 | |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 94 | if (type != PERF_RECORD_SAMPLE) { |
| 95 | perf_evlist__mmap_consume(evlist, i); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 96 | continue; |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 97 | } |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 98 | |
| 99 | err = perf_evsel__parse_sample(evsel, event, &sample); |
| 100 | if (err) { |
Namhyung Kim | 8719138 | 2015-10-20 00:23:48 +0900 | [diff] [blame] | 101 | pr_debug("Can't parse sample, err = %d\n", err); |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 102 | goto out_delete_evlist; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | tp_flags = perf_evsel__intval(evsel, &sample, "flags"); |
| 106 | |
| 107 | if (flags != tp_flags) { |
| 108 | pr_debug("%s: Expected flags=%#x, got %#x\n", |
| 109 | __func__, flags, tp_flags); |
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 | goto out_ok; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (nr_events == before) |
Arnaldo Carvalho de Melo | f66a889d | 2014-08-18 17:25:59 -0300 | [diff] [blame] | 118 | perf_evlist__poll(evlist, 10); |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 119 | |
| 120 | if (++nr_polls > 5) { |
| 121 | pr_debug("%s: no events!\n", __func__); |
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 | out_ok: |
| 126 | err = 0; |
Jiri Olsa | dc447ee | 2012-11-10 01:46:49 +0100 | [diff] [blame] | 127 | out_delete_evlist: |
| 128 | perf_evlist__delete(evlist); |
| 129 | out: |
| 130 | return err; |
| 131 | } |