Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 2 | #include <stdbool.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 3 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 215a0d3 | 2019-07-04 11:21:24 -0300 | [diff] [blame] | 4 | #include <stdlib.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 5 | #include <string.h> |
Arnaldo Carvalho de Melo | 2f2ae23 | 2019-01-27 14:08:22 +0100 | [diff] [blame] | 6 | #include <linux/bitops.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Borislav Petkov | d944c4e | 2014-04-25 21:31:02 +0200 | [diff] [blame] | 8 | #include <linux/types.h> |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 9 | |
Arnaldo Carvalho de Melo | d3300a3 | 2019-08-30 15:09:54 -0300 | [diff] [blame] | 10 | #include "map_symbol.h" |
Arnaldo Carvalho de Melo | 2f2ae23 | 2019-01-27 14:08:22 +0100 | [diff] [blame] | 11 | #include "branch.h" |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 12 | #include "event.h" |
| 13 | #include "evsel.h" |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 14 | #include "debug.h" |
Arnaldo Carvalho de Melo | ea49e01 | 2019-09-18 11:36:13 -0300 | [diff] [blame] | 15 | #include "util/synthetic-events.h" |
Madhavan Srinivasan | 10269a2 | 2021-10-28 17:07:14 +0530 | [diff] [blame] | 16 | #include "util/trace-event.h" |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 17 | |
| 18 | #include "tests.h" |
| 19 | |
| 20 | #define COMP(m) do { \ |
| 21 | if (s1->m != s2->m) { \ |
| 22 | pr_debug("Samples differ at '"#m"'\n"); \ |
| 23 | return false; \ |
| 24 | } \ |
| 25 | } while (0) |
| 26 | |
| 27 | #define MCOMP(m) do { \ |
| 28 | if (memcmp(&s1->m, &s2->m, sizeof(s1->m))) { \ |
| 29 | pr_debug("Samples differ at '"#m"'\n"); \ |
| 30 | return false; \ |
| 31 | } \ |
| 32 | } while (0) |
| 33 | |
Madhavan Srinivasan | 10269a2 | 2021-10-28 17:07:14 +0530 | [diff] [blame] | 34 | /* |
| 35 | * Hardcode the expected values for branch_entry flags. |
| 36 | * These are based on the input value (213) specified |
| 37 | * in branch_stack variable. |
| 38 | */ |
| 39 | #define BS_EXPECTED_BE 0xa00d000000000000 |
| 40 | #define BS_EXPECTED_LE 0xd5000000 |
| 41 | #define FLAG(s) s->branch_stack->entries[i].flags |
| 42 | |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 43 | static bool samples_same(const struct perf_sample *s1, |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 44 | const struct perf_sample *s2, |
Madhavan Srinivasan | 10269a2 | 2021-10-28 17:07:14 +0530 | [diff] [blame] | 45 | u64 type, u64 read_format, bool needs_swap) |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 46 | { |
| 47 | size_t i; |
| 48 | |
| 49 | if (type & PERF_SAMPLE_IDENTIFIER) |
| 50 | COMP(id); |
| 51 | |
| 52 | if (type & PERF_SAMPLE_IP) |
| 53 | COMP(ip); |
| 54 | |
| 55 | if (type & PERF_SAMPLE_TID) { |
| 56 | COMP(pid); |
| 57 | COMP(tid); |
| 58 | } |
| 59 | |
| 60 | if (type & PERF_SAMPLE_TIME) |
| 61 | COMP(time); |
| 62 | |
| 63 | if (type & PERF_SAMPLE_ADDR) |
| 64 | COMP(addr); |
| 65 | |
| 66 | if (type & PERF_SAMPLE_ID) |
| 67 | COMP(id); |
| 68 | |
| 69 | if (type & PERF_SAMPLE_STREAM_ID) |
| 70 | COMP(stream_id); |
| 71 | |
| 72 | if (type & PERF_SAMPLE_CPU) |
| 73 | COMP(cpu); |
| 74 | |
| 75 | if (type & PERF_SAMPLE_PERIOD) |
| 76 | COMP(period); |
| 77 | |
| 78 | if (type & PERF_SAMPLE_READ) { |
| 79 | if (read_format & PERF_FORMAT_GROUP) |
| 80 | COMP(read.group.nr); |
| 81 | else |
| 82 | COMP(read.one.value); |
| 83 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 84 | COMP(read.time_enabled); |
| 85 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 86 | COMP(read.time_running); |
| 87 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ |
| 88 | if (read_format & PERF_FORMAT_GROUP) { |
| 89 | for (i = 0; i < s1->read.group.nr; i++) |
| 90 | MCOMP(read.group.values[i]); |
| 91 | } else { |
| 92 | COMP(read.one.id); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (type & PERF_SAMPLE_CALLCHAIN) { |
| 97 | COMP(callchain->nr); |
| 98 | for (i = 0; i < s1->callchain->nr; i++) |
| 99 | COMP(callchain->ips[i]); |
| 100 | } |
| 101 | |
| 102 | if (type & PERF_SAMPLE_RAW) { |
| 103 | COMP(raw_size); |
| 104 | if (memcmp(s1->raw_data, s2->raw_data, s1->raw_size)) { |
| 105 | pr_debug("Samples differ at 'raw_data'\n"); |
| 106 | return false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (type & PERF_SAMPLE_BRANCH_STACK) { |
| 111 | COMP(branch_stack->nr); |
Kan Liang | 42bbabe | 2020-02-28 08:30:00 -0800 | [diff] [blame] | 112 | COMP(branch_stack->hw_idx); |
Madhavan Srinivasan | 10269a2 | 2021-10-28 17:07:14 +0530 | [diff] [blame] | 113 | for (i = 0; i < s1->branch_stack->nr; i++) { |
| 114 | if (needs_swap) |
| 115 | return ((tep_is_bigendian()) ? |
| 116 | (FLAG(s2).value == BS_EXPECTED_BE) : |
| 117 | (FLAG(s2).value == BS_EXPECTED_LE)); |
| 118 | else |
| 119 | MCOMP(branch_stack->entries[i]); |
| 120 | } |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if (type & PERF_SAMPLE_REGS_USER) { |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 124 | size_t sz = hweight_long(s1->user_regs.mask) * sizeof(u64); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 125 | |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 126 | COMP(user_regs.mask); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 127 | COMP(user_regs.abi); |
| 128 | if (s1->user_regs.abi && |
| 129 | (!s1->user_regs.regs || !s2->user_regs.regs || |
| 130 | memcmp(s1->user_regs.regs, s2->user_regs.regs, sz))) { |
| 131 | pr_debug("Samples differ at 'user_regs'\n"); |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (type & PERF_SAMPLE_STACK_USER) { |
| 137 | COMP(user_stack.size); |
Rasmus Villemoes | 605a306 | 2015-01-22 18:01:23 +0100 | [diff] [blame] | 138 | if (memcmp(s1->user_stack.data, s2->user_stack.data, |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 139 | s1->user_stack.size)) { |
| 140 | pr_debug("Samples differ at 'user_stack'\n"); |
| 141 | return false; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (type & PERF_SAMPLE_WEIGHT) |
| 146 | COMP(weight); |
| 147 | |
| 148 | if (type & PERF_SAMPLE_DATA_SRC) |
| 149 | COMP(data_src); |
| 150 | |
Adrian Hunter | 091a4ef | 2013-11-01 15:51:37 +0200 | [diff] [blame] | 151 | if (type & PERF_SAMPLE_TRANSACTION) |
| 152 | COMP(transaction); |
| 153 | |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 154 | if (type & PERF_SAMPLE_REGS_INTR) { |
| 155 | size_t sz = hweight_long(s1->intr_regs.mask) * sizeof(u64); |
| 156 | |
| 157 | COMP(intr_regs.mask); |
| 158 | COMP(intr_regs.abi); |
| 159 | if (s1->intr_regs.abi && |
| 160 | (!s1->intr_regs.regs || !s2->intr_regs.regs || |
| 161 | memcmp(s1->intr_regs.regs, s2->intr_regs.regs, sz))) { |
| 162 | pr_debug("Samples differ at 'intr_regs'\n"); |
| 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | |
Kan Liang | fc33dcc | 2017-08-29 13:11:12 -0400 | [diff] [blame] | 167 | if (type & PERF_SAMPLE_PHYS_ADDR) |
| 168 | COMP(phys_addr); |
| 169 | |
Namhyung Kim | ba78c1c | 2020-03-25 21:45:30 +0900 | [diff] [blame] | 170 | if (type & PERF_SAMPLE_CGROUP) |
| 171 | COMP(cgroup); |
| 172 | |
Arnaldo Carvalho de Melo | dc67d19 | 2020-12-16 12:45:10 -0300 | [diff] [blame] | 173 | if (type & PERF_SAMPLE_DATA_PAGE_SIZE) |
| 174 | COMP(data_page_size); |
| 175 | |
Stephane Eranian | d8eda89 | 2021-01-05 11:57:52 -0800 | [diff] [blame] | 176 | if (type & PERF_SAMPLE_CODE_PAGE_SIZE) |
| 177 | COMP(code_page_size); |
| 178 | |
Adrian Hunter | 98dcf14 | 2019-11-15 14:42:11 +0200 | [diff] [blame] | 179 | if (type & PERF_SAMPLE_AUX) { |
| 180 | COMP(aux_sample.size); |
| 181 | if (memcmp(s1->aux_sample.data, s2->aux_sample.data, |
| 182 | s1->aux_sample.size)) { |
| 183 | pr_debug("Samples differ at 'aux_sample'\n"); |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 191 | static int do_test(u64 sample_type, u64 sample_regs, u64 read_format) |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 192 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 193 | struct evsel evsel = { |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 194 | .needs_swap = false, |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 195 | .core = { |
| 196 | . attr = { |
| 197 | .sample_type = sample_type, |
| 198 | .read_format = read_format, |
| 199 | }, |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 200 | }, |
| 201 | }; |
| 202 | union perf_event *event; |
| 203 | union { |
| 204 | struct ip_callchain callchain; |
| 205 | u64 data[64]; |
| 206 | } callchain = { |
| 207 | /* 3 ips */ |
| 208 | .data = {3, 201, 202, 203}, |
| 209 | }; |
| 210 | union { |
| 211 | struct branch_stack branch_stack; |
| 212 | u64 data[64]; |
| 213 | } branch_stack = { |
| 214 | /* 1 branch_entry */ |
Kan Liang | 42bbabe | 2020-02-28 08:30:00 -0800 | [diff] [blame] | 215 | .data = {1, -1ULL, 211, 212, 213}, |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 216 | }; |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 217 | u64 regs[64]; |
Namhyung Kim | c5c97ca | 2021-02-14 18:16:38 +0900 | [diff] [blame] | 218 | const u32 raw_data[] = {0x12345678, 0x0a0b0c0d, 0x11020304, 0x05060708, 0 }; |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 219 | const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL}; |
Adrian Hunter | 98dcf14 | 2019-11-15 14:42:11 +0200 | [diff] [blame] | 220 | const u64 aux_data[] = {0xa55a, 0, 0xeeddee, 0x0282028202820282}; |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 221 | struct perf_sample sample = { |
| 222 | .ip = 101, |
| 223 | .pid = 102, |
| 224 | .tid = 103, |
| 225 | .time = 104, |
| 226 | .addr = 105, |
| 227 | .id = 106, |
| 228 | .stream_id = 107, |
| 229 | .period = 108, |
| 230 | .weight = 109, |
| 231 | .cpu = 110, |
| 232 | .raw_size = sizeof(raw_data), |
| 233 | .data_src = 111, |
Adrian Hunter | 091a4ef | 2013-11-01 15:51:37 +0200 | [diff] [blame] | 234 | .transaction = 112, |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 235 | .raw_data = (void *)raw_data, |
| 236 | .callchain = &callchain.callchain, |
Kan Liang | 42bbabe | 2020-02-28 08:30:00 -0800 | [diff] [blame] | 237 | .no_hw_idx = false, |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 238 | .branch_stack = &branch_stack.branch_stack, |
| 239 | .user_regs = { |
| 240 | .abi = PERF_SAMPLE_REGS_ABI_64, |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 241 | .mask = sample_regs, |
| 242 | .regs = regs, |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 243 | }, |
| 244 | .user_stack = { |
| 245 | .size = sizeof(data), |
| 246 | .data = (void *)data, |
| 247 | }, |
| 248 | .read = { |
| 249 | .time_enabled = 0x030a59d664fca7deULL, |
| 250 | .time_running = 0x011b6ae553eb98edULL, |
| 251 | }, |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 252 | .intr_regs = { |
| 253 | .abi = PERF_SAMPLE_REGS_ABI_64, |
| 254 | .mask = sample_regs, |
| 255 | .regs = regs, |
| 256 | }, |
Kan Liang | fc33dcc | 2017-08-29 13:11:12 -0400 | [diff] [blame] | 257 | .phys_addr = 113, |
Namhyung Kim | ba78c1c | 2020-03-25 21:45:30 +0900 | [diff] [blame] | 258 | .cgroup = 114, |
Arnaldo Carvalho de Melo | dc67d19 | 2020-12-16 12:45:10 -0300 | [diff] [blame] | 259 | .data_page_size = 115, |
Stephane Eranian | d8eda89 | 2021-01-05 11:57:52 -0800 | [diff] [blame] | 260 | .code_page_size = 116, |
Adrian Hunter | 98dcf14 | 2019-11-15 14:42:11 +0200 | [diff] [blame] | 261 | .aux_sample = { |
| 262 | .size = sizeof(aux_data), |
| 263 | .data = (void *)aux_data, |
| 264 | }, |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 265 | }; |
| 266 | struct sample_read_value values[] = {{1, 5}, {9, 3}, {2, 7}, {6, 4},}; |
Madhavan Srinivasan | 10269a2 | 2021-10-28 17:07:14 +0530 | [diff] [blame] | 267 | struct perf_sample sample_out, sample_out_endian; |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 268 | size_t i, sz, bufsz; |
| 269 | int err, ret = -1; |
| 270 | |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 271 | if (sample_type & PERF_SAMPLE_REGS_USER) |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 272 | evsel.core.attr.sample_regs_user = sample_regs; |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 273 | |
| 274 | if (sample_type & PERF_SAMPLE_REGS_INTR) |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 275 | evsel.core.attr.sample_regs_intr = sample_regs; |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 276 | |
Kan Liang | 42bbabe | 2020-02-28 08:30:00 -0800 | [diff] [blame] | 277 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) |
| 278 | evsel.core.attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX; |
| 279 | |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 280 | for (i = 0; i < sizeof(regs); i++) |
| 281 | *(i + (u8 *)regs) = i & 0xfe; |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 282 | |
| 283 | if (read_format & PERF_FORMAT_GROUP) { |
| 284 | sample.read.group.nr = 4; |
| 285 | sample.read.group.values = values; |
| 286 | } else { |
| 287 | sample.read.one.value = 0x08789faeb786aa87ULL; |
| 288 | sample.read.one.id = 99; |
| 289 | } |
| 290 | |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 291 | sz = perf_event__sample_event_size(&sample, sample_type, read_format); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 292 | bufsz = sz + 4096; /* Add a bit for overrun checking */ |
| 293 | event = malloc(bufsz); |
| 294 | if (!event) { |
| 295 | pr_debug("malloc failed\n"); |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | memset(event, 0xff, bufsz); |
| 300 | event->header.type = PERF_RECORD_SAMPLE; |
| 301 | event->header.misc = 0; |
| 302 | event->header.size = sz; |
| 303 | |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 304 | err = perf_event__synthesize_sample(event, sample_type, read_format, |
Adrian Hunter | 936f1f3 | 2018-01-16 15:14:52 +0200 | [diff] [blame] | 305 | &sample); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 306 | if (err) { |
| 307 | pr_debug("%s failed for sample_type %#"PRIx64", error %d\n", |
| 308 | "perf_event__synthesize_sample", sample_type, err); |
| 309 | goto out_free; |
| 310 | } |
| 311 | |
| 312 | /* The data does not contain 0xff so we use that to check the size */ |
| 313 | for (i = bufsz; i > 0; i--) { |
| 314 | if (*(i - 1 + (u8 *)event) != 0xff) |
| 315 | break; |
| 316 | } |
| 317 | if (i != sz) { |
| 318 | pr_debug("Event size mismatch: actual %zu vs expected %zu\n", |
| 319 | i, sz); |
| 320 | goto out_free; |
| 321 | } |
| 322 | |
Arnaldo Carvalho de Melo | 2aaefde | 2020-04-29 16:00:27 -0300 | [diff] [blame] | 323 | evsel.sample_size = __evsel__sample_size(sample_type); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 324 | |
Arnaldo Carvalho de Melo | 6b6017a | 2020-04-30 11:03:49 -0300 | [diff] [blame] | 325 | err = evsel__parse_sample(&evsel, event, &sample_out); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 326 | if (err) { |
| 327 | pr_debug("%s failed for sample_type %#"PRIx64", error %d\n", |
Arnaldo Carvalho de Melo | 6b6017a | 2020-04-30 11:03:49 -0300 | [diff] [blame] | 328 | "evsel__parse_sample", sample_type, err); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 329 | goto out_free; |
| 330 | } |
| 331 | |
Madhavan Srinivasan | 10269a2 | 2021-10-28 17:07:14 +0530 | [diff] [blame] | 332 | if (!samples_same(&sample, &sample_out, sample_type, read_format, evsel.needs_swap)) { |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 333 | pr_debug("parsing failed for sample_type %#"PRIx64"\n", |
| 334 | sample_type); |
| 335 | goto out_free; |
| 336 | } |
| 337 | |
Madhavan Srinivasan | 10269a2 | 2021-10-28 17:07:14 +0530 | [diff] [blame] | 338 | if (sample_type == PERF_SAMPLE_BRANCH_STACK) { |
| 339 | evsel.needs_swap = true; |
| 340 | evsel.sample_size = __evsel__sample_size(sample_type); |
| 341 | err = evsel__parse_sample(&evsel, event, &sample_out_endian); |
| 342 | if (err) { |
| 343 | pr_debug("%s failed for sample_type %#"PRIx64", error %d\n", |
| 344 | "evsel__parse_sample", sample_type, err); |
| 345 | goto out_free; |
| 346 | } |
| 347 | |
| 348 | if (!samples_same(&sample, &sample_out_endian, sample_type, read_format, evsel.needs_swap)) { |
| 349 | pr_debug("parsing failed for sample_type %#"PRIx64"\n", |
| 350 | sample_type); |
| 351 | goto out_free; |
| 352 | } |
| 353 | } |
| 354 | |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 355 | ret = 0; |
| 356 | out_free: |
| 357 | free(event); |
| 358 | if (ret && read_format) |
| 359 | pr_debug("read_format %#"PRIx64"\n", read_format); |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * test__sample_parsing - test sample parsing. |
| 365 | * |
| 366 | * This function implements a test that synthesizes a sample event, parses it |
| 367 | * and then checks that the parsed sample matches the original sample. The test |
| 368 | * checks sample format bits separately and together. If the test passes %0 is |
| 369 | * returned, otherwise %-1 is returned. |
| 370 | */ |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame^] | 371 | static int test__sample_parsing(struct test *test __maybe_unused, int subtest __maybe_unused) |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 372 | { |
| 373 | const u64 rf[] = {4, 5, 6, 7, 12, 13, 14, 15}; |
| 374 | u64 sample_type; |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 375 | u64 sample_regs; |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 376 | size_t i; |
| 377 | int err; |
| 378 | |
| 379 | /* |
| 380 | * Fail the test if it has not been updated when new sample format bits |
Adrian Hunter | 091a4ef | 2013-11-01 15:51:37 +0200 | [diff] [blame] | 381 | * were added. Please actually update the test rather than just change |
| 382 | * the condition below. |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 383 | */ |
Kan Liang | c744429 | 2021-02-02 12:09:11 -0800 | [diff] [blame] | 384 | if (PERF_SAMPLE_MAX > PERF_SAMPLE_WEIGHT_STRUCT << 1) { |
Arnaldo Carvalho de Melo | 11a4d43 | 2013-10-22 15:24:58 -0300 | [diff] [blame] | 385 | pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n"); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | /* Test each sample format bit separately */ |
| 390 | for (sample_type = 1; sample_type != PERF_SAMPLE_MAX; |
| 391 | sample_type <<= 1) { |
| 392 | /* Test read_format variations */ |
| 393 | if (sample_type == PERF_SAMPLE_READ) { |
| 394 | for (i = 0; i < ARRAY_SIZE(rf); i++) { |
| 395 | err = do_test(sample_type, 0, rf[i]); |
| 396 | if (err) |
| 397 | return err; |
| 398 | } |
| 399 | continue; |
| 400 | } |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 401 | sample_regs = 0; |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 402 | |
| 403 | if (sample_type == PERF_SAMPLE_REGS_USER) |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 404 | sample_regs = 0x3fff; |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 405 | |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 406 | if (sample_type == PERF_SAMPLE_REGS_INTR) |
| 407 | sample_regs = 0xff0fff; |
| 408 | |
| 409 | err = do_test(sample_type, sample_regs, 0); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 410 | if (err) |
| 411 | return err; |
| 412 | } |
| 413 | |
Kan Liang | c744429 | 2021-02-02 12:09:11 -0800 | [diff] [blame] | 414 | /* |
| 415 | * Test all sample format bits together |
| 416 | * Note: PERF_SAMPLE_WEIGHT and PERF_SAMPLE_WEIGHT_STRUCT cannot |
| 417 | * be set simultaneously. |
| 418 | */ |
| 419 | sample_type = (PERF_SAMPLE_MAX - 1) & ~PERF_SAMPLE_WEIGHT; |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 420 | sample_regs = 0x3fff; /* shared yb intr and user regs */ |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 421 | for (i = 0; i < ARRAY_SIZE(rf); i++) { |
Stephane Eranian | 26ff0f0 | 2014-09-24 13:48:40 +0200 | [diff] [blame] | 422 | err = do_test(sample_type, sample_regs, rf[i]); |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 423 | if (err) |
| 424 | return err; |
| 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | } |
Ian Rogers | d68f036 | 2021-11-03 23:41:50 -0700 | [diff] [blame^] | 429 | |
| 430 | DEFINE_SUITE("Sample parsing", sample_parsing); |