1 // SPDX-License-Identifier: GPL-2.0 2 #include <stdbool.h> 3 #include <inttypes.h> 4 #include <stdlib.h> 5 #include <linux/bitops.h> 6 #include <linux/kernel.h> 7 #include <linux/types.h> 8 9 #include "branch.h" 10 #include "util.h" 11 #include "event.h" 12 #include "evsel.h" 13 #include "debug.h" 14 15 #include "tests.h" 16 17 #define COMP(m) do { \ 18 if (s1->m != s2->m) { \ 19 pr_debug("Samples differ at '"#m"'\n"); \ 20 return false; \ 21 } \ 22 } while (0) 23 24 #define MCOMP(m) do { \ 25 if (memcmp(&s1->m, &s2->m, sizeof(s1->m))) { \ 26 pr_debug("Samples differ at '"#m"'\n"); \ 27 return false; \ 28 } \ 29 } while (0) 30 31 static bool samples_same(const struct perf_sample *s1, 32 const struct perf_sample *s2, 33 u64 type, u64 read_format) 34 { 35 size_t i; 36 37 if (type & PERF_SAMPLE_IDENTIFIER) 38 COMP(id); 39 40 if (type & PERF_SAMPLE_IP) 41 COMP(ip); 42 43 if (type & PERF_SAMPLE_TID) { 44 COMP(pid); 45 COMP(tid); 46 } 47 48 if (type & PERF_SAMPLE_TIME) 49 COMP(time); 50 51 if (type & PERF_SAMPLE_ADDR) 52 COMP(addr); 53 54 if (type & PERF_SAMPLE_ID) 55 COMP(id); 56 57 if (type & PERF_SAMPLE_STREAM_ID) 58 COMP(stream_id); 59 60 if (type & PERF_SAMPLE_CPU) 61 COMP(cpu); 62 63 if (type & PERF_SAMPLE_PERIOD) 64 COMP(period); 65 66 if (type & PERF_SAMPLE_READ) { 67 if (read_format & PERF_FORMAT_GROUP) 68 COMP(read.group.nr); 69 else 70 COMP(read.one.value); 71 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 72 COMP(read.time_enabled); 73 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 74 COMP(read.time_running); 75 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ 76 if (read_format & PERF_FORMAT_GROUP) { 77 for (i = 0; i < s1->read.group.nr; i++) 78 MCOMP(read.group.values[i]); 79 } else { 80 COMP(read.one.id); 81 } 82 } 83 84 if (type & PERF_SAMPLE_CALLCHAIN) { 85 COMP(callchain->nr); 86 for (i = 0; i < s1->callchain->nr; i++) 87 COMP(callchain->ips[i]); 88 } 89 90 if (type & PERF_SAMPLE_RAW) { 91 COMP(raw_size); 92 if (memcmp(s1->raw_data, s2->raw_data, s1->raw_size)) { 93 pr_debug("Samples differ at 'raw_data'\n"); 94 return false; 95 } 96 } 97 98 if (type & PERF_SAMPLE_BRANCH_STACK) { 99 COMP(branch_stack->nr); 100 for (i = 0; i < s1->branch_stack->nr; i++) 101 MCOMP(branch_stack->entries[i]); 102 } 103 104 if (type & PERF_SAMPLE_REGS_USER) { 105 size_t sz = hweight_long(s1->user_regs.mask) * sizeof(u64); 106 107 COMP(user_regs.mask); 108 COMP(user_regs.abi); 109 if (s1->user_regs.abi && 110 (!s1->user_regs.regs || !s2->user_regs.regs || 111 memcmp(s1->user_regs.regs, s2->user_regs.regs, sz))) { 112 pr_debug("Samples differ at 'user_regs'\n"); 113 return false; 114 } 115 } 116 117 if (type & PERF_SAMPLE_STACK_USER) { 118 COMP(user_stack.size); 119 if (memcmp(s1->user_stack.data, s2->user_stack.data, 120 s1->user_stack.size)) { 121 pr_debug("Samples differ at 'user_stack'\n"); 122 return false; 123 } 124 } 125 126 if (type & PERF_SAMPLE_WEIGHT) 127 COMP(weight); 128 129 if (type & PERF_SAMPLE_DATA_SRC) 130 COMP(data_src); 131 132 if (type & PERF_SAMPLE_TRANSACTION) 133 COMP(transaction); 134 135 if (type & PERF_SAMPLE_REGS_INTR) { 136 size_t sz = hweight_long(s1->intr_regs.mask) * sizeof(u64); 137 138 COMP(intr_regs.mask); 139 COMP(intr_regs.abi); 140 if (s1->intr_regs.abi && 141 (!s1->intr_regs.regs || !s2->intr_regs.regs || 142 memcmp(s1->intr_regs.regs, s2->intr_regs.regs, sz))) { 143 pr_debug("Samples differ at 'intr_regs'\n"); 144 return false; 145 } 146 } 147 148 if (type & PERF_SAMPLE_PHYS_ADDR) 149 COMP(phys_addr); 150 151 return true; 152 } 153 154 static int do_test(u64 sample_type, u64 sample_regs, u64 read_format) 155 { 156 struct evsel evsel = { 157 .needs_swap = false, 158 .core = { 159 . attr = { 160 .sample_type = sample_type, 161 .read_format = read_format, 162 }, 163 }, 164 }; 165 union perf_event *event; 166 union { 167 struct ip_callchain callchain; 168 u64 data[64]; 169 } callchain = { 170 /* 3 ips */ 171 .data = {3, 201, 202, 203}, 172 }; 173 union { 174 struct branch_stack branch_stack; 175 u64 data[64]; 176 } branch_stack = { 177 /* 1 branch_entry */ 178 .data = {1, 211, 212, 213}, 179 }; 180 u64 regs[64]; 181 const u64 raw_data[] = {0x123456780a0b0c0dULL, 0x1102030405060708ULL}; 182 const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL}; 183 struct perf_sample sample = { 184 .ip = 101, 185 .pid = 102, 186 .tid = 103, 187 .time = 104, 188 .addr = 105, 189 .id = 106, 190 .stream_id = 107, 191 .period = 108, 192 .weight = 109, 193 .cpu = 110, 194 .raw_size = sizeof(raw_data), 195 .data_src = 111, 196 .transaction = 112, 197 .raw_data = (void *)raw_data, 198 .callchain = &callchain.callchain, 199 .branch_stack = &branch_stack.branch_stack, 200 .user_regs = { 201 .abi = PERF_SAMPLE_REGS_ABI_64, 202 .mask = sample_regs, 203 .regs = regs, 204 }, 205 .user_stack = { 206 .size = sizeof(data), 207 .data = (void *)data, 208 }, 209 .read = { 210 .time_enabled = 0x030a59d664fca7deULL, 211 .time_running = 0x011b6ae553eb98edULL, 212 }, 213 .intr_regs = { 214 .abi = PERF_SAMPLE_REGS_ABI_64, 215 .mask = sample_regs, 216 .regs = regs, 217 }, 218 .phys_addr = 113, 219 }; 220 struct sample_read_value values[] = {{1, 5}, {9, 3}, {2, 7}, {6, 4},}; 221 struct perf_sample sample_out; 222 size_t i, sz, bufsz; 223 int err, ret = -1; 224 225 if (sample_type & PERF_SAMPLE_REGS_USER) 226 evsel.core.attr.sample_regs_user = sample_regs; 227 228 if (sample_type & PERF_SAMPLE_REGS_INTR) 229 evsel.core.attr.sample_regs_intr = sample_regs; 230 231 for (i = 0; i < sizeof(regs); i++) 232 *(i + (u8 *)regs) = i & 0xfe; 233 234 if (read_format & PERF_FORMAT_GROUP) { 235 sample.read.group.nr = 4; 236 sample.read.group.values = values; 237 } else { 238 sample.read.one.value = 0x08789faeb786aa87ULL; 239 sample.read.one.id = 99; 240 } 241 242 sz = perf_event__sample_event_size(&sample, sample_type, read_format); 243 bufsz = sz + 4096; /* Add a bit for overrun checking */ 244 event = malloc(bufsz); 245 if (!event) { 246 pr_debug("malloc failed\n"); 247 return -1; 248 } 249 250 memset(event, 0xff, bufsz); 251 event->header.type = PERF_RECORD_SAMPLE; 252 event->header.misc = 0; 253 event->header.size = sz; 254 255 err = perf_event__synthesize_sample(event, sample_type, read_format, 256 &sample); 257 if (err) { 258 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n", 259 "perf_event__synthesize_sample", sample_type, err); 260 goto out_free; 261 } 262 263 /* The data does not contain 0xff so we use that to check the size */ 264 for (i = bufsz; i > 0; i--) { 265 if (*(i - 1 + (u8 *)event) != 0xff) 266 break; 267 } 268 if (i != sz) { 269 pr_debug("Event size mismatch: actual %zu vs expected %zu\n", 270 i, sz); 271 goto out_free; 272 } 273 274 evsel.sample_size = __perf_evsel__sample_size(sample_type); 275 276 err = perf_evsel__parse_sample(&evsel, event, &sample_out); 277 if (err) { 278 pr_debug("%s failed for sample_type %#"PRIx64", error %d\n", 279 "perf_evsel__parse_sample", sample_type, err); 280 goto out_free; 281 } 282 283 if (!samples_same(&sample, &sample_out, sample_type, read_format)) { 284 pr_debug("parsing failed for sample_type %#"PRIx64"\n", 285 sample_type); 286 goto out_free; 287 } 288 289 ret = 0; 290 out_free: 291 free(event); 292 if (ret && read_format) 293 pr_debug("read_format %#"PRIx64"\n", read_format); 294 return ret; 295 } 296 297 /** 298 * test__sample_parsing - test sample parsing. 299 * 300 * This function implements a test that synthesizes a sample event, parses it 301 * and then checks that the parsed sample matches the original sample. The test 302 * checks sample format bits separately and together. If the test passes %0 is 303 * returned, otherwise %-1 is returned. 304 */ 305 int test__sample_parsing(struct test *test __maybe_unused, int subtest __maybe_unused) 306 { 307 const u64 rf[] = {4, 5, 6, 7, 12, 13, 14, 15}; 308 u64 sample_type; 309 u64 sample_regs; 310 size_t i; 311 int err; 312 313 /* 314 * Fail the test if it has not been updated when new sample format bits 315 * were added. Please actually update the test rather than just change 316 * the condition below. 317 */ 318 if (PERF_SAMPLE_MAX > PERF_SAMPLE_PHYS_ADDR << 1) { 319 pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n"); 320 return -1; 321 } 322 323 /* Test each sample format bit separately */ 324 for (sample_type = 1; sample_type != PERF_SAMPLE_MAX; 325 sample_type <<= 1) { 326 /* Test read_format variations */ 327 if (sample_type == PERF_SAMPLE_READ) { 328 for (i = 0; i < ARRAY_SIZE(rf); i++) { 329 err = do_test(sample_type, 0, rf[i]); 330 if (err) 331 return err; 332 } 333 continue; 334 } 335 sample_regs = 0; 336 337 if (sample_type == PERF_SAMPLE_REGS_USER) 338 sample_regs = 0x3fff; 339 340 if (sample_type == PERF_SAMPLE_REGS_INTR) 341 sample_regs = 0xff0fff; 342 343 err = do_test(sample_type, sample_regs, 0); 344 if (err) 345 return err; 346 } 347 348 /* Test all sample format bits together */ 349 sample_type = PERF_SAMPLE_MAX - 1; 350 sample_regs = 0x3fff; /* shared yb intr and user regs */ 351 for (i = 0; i < ARRAY_SIZE(rf); i++) { 352 err = do_test(sample_type, sample_regs, rf[i]); 353 if (err) 354 return err; 355 } 356 357 return 0; 358 } 359