Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2 | #include <stdbool.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3 | #include <linux/err.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4 | #include <linux/string.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <fcntl.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8 | #include "evlist.h" |
| 9 | #include "evsel.h" |
| 10 | #include "thread_map.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11 | #include "record.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12 | #include "tests.h" |
| 13 | #include "debug.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 14 | #include "util/mmap.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 15 | #include <errno.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 16 | #include <perf/mmap.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 17 | |
| 18 | #ifndef O_DIRECTORY |
| 19 | #define O_DIRECTORY 00200000 |
| 20 | #endif |
| 21 | #ifndef AT_FDCWD |
| 22 | #define AT_FDCWD -100 |
| 23 | #endif |
| 24 | |
| 25 | int test__syscall_openat_tp_fields(struct test *test __maybe_unused, int subtest __maybe_unused) |
| 26 | { |
| 27 | struct record_opts opts = { |
| 28 | .target = { |
| 29 | .uid = UINT_MAX, |
| 30 | .uses_mmap = true, |
| 31 | }, |
| 32 | .no_buffering = true, |
| 33 | .freq = 1, |
| 34 | .mmap_pages = 256, |
| 35 | .raw_samples = true, |
| 36 | }; |
| 37 | const char *filename = "/etc/passwd"; |
| 38 | int flags = O_RDONLY | O_DIRECTORY; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 39 | struct evlist *evlist = evlist__new(); |
| 40 | struct evsel *evsel; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | int err = -1, i, nr_events = 0, nr_polls = 0; |
| 42 | char sbuf[STRERR_BUFSIZE]; |
| 43 | |
| 44 | if (evlist == NULL) { |
| 45 | pr_debug("%s: perf_evlist__new\n", __func__); |
| 46 | goto out; |
| 47 | } |
| 48 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 49 | evsel = evsel__newtp("syscalls", "sys_enter_openat"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 50 | if (IS_ERR(evsel)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 51 | pr_debug("%s: evsel__newtp\n", __func__); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 52 | goto out_delete_evlist; |
| 53 | } |
| 54 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 55 | evlist__add(evlist, evsel); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [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 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 63 | evsel__config(evsel, &opts, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 65 | perf_thread_map__set_pid(evlist->core.threads, 0, getpid()); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 66 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 67 | err = evlist__open(evlist); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 68 | if (err < 0) { |
| 69 | pr_debug("perf_evlist__open: %s\n", |
| 70 | str_error_r(errno, sbuf, sizeof(sbuf))); |
| 71 | goto out_delete_evlist; |
| 72 | } |
| 73 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 74 | err = evlist__mmap(evlist, UINT_MAX); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 75 | if (err < 0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 76 | pr_debug("evlist__mmap: %s\n", |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 77 | str_error_r(errno, sbuf, sizeof(sbuf))); |
| 78 | goto out_delete_evlist; |
| 79 | } |
| 80 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 81 | evlist__enable(evlist); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * Generate the event: |
| 85 | */ |
| 86 | openat(AT_FDCWD, filename, flags); |
| 87 | |
| 88 | while (1) { |
| 89 | int before = nr_events; |
| 90 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 91 | for (i = 0; i < evlist->core.nr_mmaps; i++) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 92 | union perf_event *event; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 93 | struct mmap *md; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 94 | |
| 95 | md = &evlist->mmap[i]; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 96 | if (perf_mmap__read_init(&md->core) < 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 97 | continue; |
| 98 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 99 | while ((event = perf_mmap__read_event(&md->core)) != NULL) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 100 | const u32 type = event->header.type; |
| 101 | int tp_flags; |
| 102 | struct perf_sample sample; |
| 103 | |
| 104 | ++nr_events; |
| 105 | |
| 106 | if (type != PERF_RECORD_SAMPLE) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 107 | perf_mmap__consume(&md->core); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 108 | continue; |
| 109 | } |
| 110 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 111 | err = evsel__parse_sample(evsel, event, &sample); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 112 | if (err) { |
| 113 | pr_debug("Can't parse sample, err = %d\n", err); |
| 114 | goto out_delete_evlist; |
| 115 | } |
| 116 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 117 | tp_flags = evsel__intval(evsel, &sample, "flags"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 118 | |
| 119 | if (flags != tp_flags) { |
| 120 | pr_debug("%s: Expected flags=%#x, got %#x\n", |
| 121 | __func__, flags, tp_flags); |
| 122 | goto out_delete_evlist; |
| 123 | } |
| 124 | |
| 125 | goto out_ok; |
| 126 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 127 | perf_mmap__read_done(&md->core); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (nr_events == before) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 131 | evlist__poll(evlist, 10); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 132 | |
| 133 | if (++nr_polls > 5) { |
| 134 | pr_debug("%s: no events!\n", __func__); |
| 135 | goto out_delete_evlist; |
| 136 | } |
| 137 | } |
| 138 | out_ok: |
| 139 | err = 0; |
| 140 | out_delete_evlist: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 141 | evlist__delete(evlist); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 142 | out: |
| 143 | return err; |
| 144 | } |