1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2045f8cd8SAdrian Hunter #include <stdbool.h>
3fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
4215a0d30SArnaldo Carvalho de Melo #include <stdlib.h>
58520a98dSArnaldo Carvalho de Melo #include <string.h>
62f2ae234SArnaldo Carvalho de Melo #include <linux/bitops.h>
7877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
8d944c4eeSBorislav Petkov #include <linux/types.h>
9045f8cd8SAdrian Hunter
10d3300a3cSArnaldo Carvalho de Melo #include "map_symbol.h"
112f2ae234SArnaldo Carvalho de Melo #include "branch.h"
12045f8cd8SAdrian Hunter #include "event.h"
13045f8cd8SAdrian Hunter #include "evsel.h"
1484f5d36fSJiri Olsa #include "debug.h"
15ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h"
165b7a29fbSIan Rogers #include "util/util.h"
17045f8cd8SAdrian Hunter
18045f8cd8SAdrian Hunter #include "tests.h"
19045f8cd8SAdrian Hunter
20045f8cd8SAdrian Hunter #define COMP(m) do { \
21045f8cd8SAdrian Hunter if (s1->m != s2->m) { \
22045f8cd8SAdrian Hunter pr_debug("Samples differ at '"#m"'\n"); \
23045f8cd8SAdrian Hunter return false; \
24045f8cd8SAdrian Hunter } \
25045f8cd8SAdrian Hunter } while (0)
26045f8cd8SAdrian Hunter
27045f8cd8SAdrian Hunter #define MCOMP(m) do { \
28045f8cd8SAdrian Hunter if (memcmp(&s1->m, &s2->m, sizeof(s1->m))) { \
29045f8cd8SAdrian Hunter pr_debug("Samples differ at '"#m"'\n"); \
30045f8cd8SAdrian Hunter return false; \
31045f8cd8SAdrian Hunter } \
32045f8cd8SAdrian Hunter } while (0)
33045f8cd8SAdrian Hunter
3410269a2cSMadhavan Srinivasan /*
3510269a2cSMadhavan Srinivasan * Hardcode the expected values for branch_entry flags.
3610269a2cSMadhavan Srinivasan * These are based on the input value (213) specified
3710269a2cSMadhavan Srinivasan * in branch_stack variable.
3810269a2cSMadhavan Srinivasan */
39cb5a63feSThomas Richter #define BS_EXPECTED_BE 0xa000d00000000000
40*6ade6c64SSandipan Das #define BS_EXPECTED_LE 0x1aa00000000
4110269a2cSMadhavan Srinivasan #define FLAG(s) s->branch_stack->entries[i].flags
4210269a2cSMadhavan Srinivasan
samples_same(const struct perf_sample * s1,const struct perf_sample * s2,u64 type,u64 read_format,bool needs_swap)43045f8cd8SAdrian Hunter static bool samples_same(const struct perf_sample *s1,
44352ea45aSJiri Olsa const struct perf_sample *s2,
4510269a2cSMadhavan Srinivasan u64 type, u64 read_format, bool needs_swap)
46045f8cd8SAdrian Hunter {
47045f8cd8SAdrian Hunter size_t i;
48045f8cd8SAdrian Hunter
49045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_IDENTIFIER)
50045f8cd8SAdrian Hunter COMP(id);
51045f8cd8SAdrian Hunter
52045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_IP)
53045f8cd8SAdrian Hunter COMP(ip);
54045f8cd8SAdrian Hunter
55045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_TID) {
56045f8cd8SAdrian Hunter COMP(pid);
57045f8cd8SAdrian Hunter COMP(tid);
58045f8cd8SAdrian Hunter }
59045f8cd8SAdrian Hunter
60045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_TIME)
61045f8cd8SAdrian Hunter COMP(time);
62045f8cd8SAdrian Hunter
63045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_ADDR)
64045f8cd8SAdrian Hunter COMP(addr);
65045f8cd8SAdrian Hunter
66045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_ID)
67045f8cd8SAdrian Hunter COMP(id);
68045f8cd8SAdrian Hunter
69045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_STREAM_ID)
70045f8cd8SAdrian Hunter COMP(stream_id);
71045f8cd8SAdrian Hunter
72045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_CPU)
73045f8cd8SAdrian Hunter COMP(cpu);
74045f8cd8SAdrian Hunter
75045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_PERIOD)
76045f8cd8SAdrian Hunter COMP(period);
77045f8cd8SAdrian Hunter
78045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_READ) {
79045f8cd8SAdrian Hunter if (read_format & PERF_FORMAT_GROUP)
80045f8cd8SAdrian Hunter COMP(read.group.nr);
81045f8cd8SAdrian Hunter else
82045f8cd8SAdrian Hunter COMP(read.one.value);
83045f8cd8SAdrian Hunter if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
84045f8cd8SAdrian Hunter COMP(read.time_enabled);
85045f8cd8SAdrian Hunter if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
86045f8cd8SAdrian Hunter COMP(read.time_running);
87045f8cd8SAdrian Hunter /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
88045f8cd8SAdrian Hunter if (read_format & PERF_FORMAT_GROUP) {
89f52679b7SNamhyung Kim for (i = 0; i < s1->read.group.nr; i++) {
90f52679b7SNamhyung Kim /* FIXME: check values without LOST */
91f52679b7SNamhyung Kim if (read_format & PERF_FORMAT_LOST)
92045f8cd8SAdrian Hunter MCOMP(read.group.values[i]);
93f52679b7SNamhyung Kim }
94045f8cd8SAdrian Hunter } else {
95045f8cd8SAdrian Hunter COMP(read.one.id);
96f52679b7SNamhyung Kim if (read_format & PERF_FORMAT_LOST)
97f52679b7SNamhyung Kim COMP(read.one.lost);
98045f8cd8SAdrian Hunter }
99045f8cd8SAdrian Hunter }
100045f8cd8SAdrian Hunter
101045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_CALLCHAIN) {
102045f8cd8SAdrian Hunter COMP(callchain->nr);
103045f8cd8SAdrian Hunter for (i = 0; i < s1->callchain->nr; i++)
104045f8cd8SAdrian Hunter COMP(callchain->ips[i]);
105045f8cd8SAdrian Hunter }
106045f8cd8SAdrian Hunter
107045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_RAW) {
108045f8cd8SAdrian Hunter COMP(raw_size);
109045f8cd8SAdrian Hunter if (memcmp(s1->raw_data, s2->raw_data, s1->raw_size)) {
110045f8cd8SAdrian Hunter pr_debug("Samples differ at 'raw_data'\n");
111045f8cd8SAdrian Hunter return false;
112045f8cd8SAdrian Hunter }
113045f8cd8SAdrian Hunter }
114045f8cd8SAdrian Hunter
115045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_BRANCH_STACK) {
116045f8cd8SAdrian Hunter COMP(branch_stack->nr);
11742bbabedSKan Liang COMP(branch_stack->hw_idx);
11810269a2cSMadhavan Srinivasan for (i = 0; i < s1->branch_stack->nr; i++) {
11910269a2cSMadhavan Srinivasan if (needs_swap)
1205b7a29fbSIan Rogers return ((host_is_bigendian()) ?
12110269a2cSMadhavan Srinivasan (FLAG(s2).value == BS_EXPECTED_BE) :
12210269a2cSMadhavan Srinivasan (FLAG(s2).value == BS_EXPECTED_LE));
12310269a2cSMadhavan Srinivasan else
124045f8cd8SAdrian Hunter MCOMP(branch_stack->entries[i]);
125045f8cd8SAdrian Hunter }
12610269a2cSMadhavan Srinivasan }
127045f8cd8SAdrian Hunter
128045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_REGS_USER) {
129352ea45aSJiri Olsa size_t sz = hweight_long(s1->user_regs.mask) * sizeof(u64);
130045f8cd8SAdrian Hunter
131352ea45aSJiri Olsa COMP(user_regs.mask);
132045f8cd8SAdrian Hunter COMP(user_regs.abi);
133045f8cd8SAdrian Hunter if (s1->user_regs.abi &&
134045f8cd8SAdrian Hunter (!s1->user_regs.regs || !s2->user_regs.regs ||
135045f8cd8SAdrian Hunter memcmp(s1->user_regs.regs, s2->user_regs.regs, sz))) {
136045f8cd8SAdrian Hunter pr_debug("Samples differ at 'user_regs'\n");
137045f8cd8SAdrian Hunter return false;
138045f8cd8SAdrian Hunter }
139045f8cd8SAdrian Hunter }
140045f8cd8SAdrian Hunter
141045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_STACK_USER) {
142045f8cd8SAdrian Hunter COMP(user_stack.size);
143605a3069SRasmus Villemoes if (memcmp(s1->user_stack.data, s2->user_stack.data,
144045f8cd8SAdrian Hunter s1->user_stack.size)) {
145045f8cd8SAdrian Hunter pr_debug("Samples differ at 'user_stack'\n");
146045f8cd8SAdrian Hunter return false;
147045f8cd8SAdrian Hunter }
148045f8cd8SAdrian Hunter }
149045f8cd8SAdrian Hunter
150045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_WEIGHT)
151045f8cd8SAdrian Hunter COMP(weight);
152045f8cd8SAdrian Hunter
153045f8cd8SAdrian Hunter if (type & PERF_SAMPLE_DATA_SRC)
154045f8cd8SAdrian Hunter COMP(data_src);
155045f8cd8SAdrian Hunter
156091a4ef5SAdrian Hunter if (type & PERF_SAMPLE_TRANSACTION)
157091a4ef5SAdrian Hunter COMP(transaction);
158091a4ef5SAdrian Hunter
15926ff0f0aSStephane Eranian if (type & PERF_SAMPLE_REGS_INTR) {
16026ff0f0aSStephane Eranian size_t sz = hweight_long(s1->intr_regs.mask) * sizeof(u64);
16126ff0f0aSStephane Eranian
16226ff0f0aSStephane Eranian COMP(intr_regs.mask);
16326ff0f0aSStephane Eranian COMP(intr_regs.abi);
16426ff0f0aSStephane Eranian if (s1->intr_regs.abi &&
16526ff0f0aSStephane Eranian (!s1->intr_regs.regs || !s2->intr_regs.regs ||
16626ff0f0aSStephane Eranian memcmp(s1->intr_regs.regs, s2->intr_regs.regs, sz))) {
16726ff0f0aSStephane Eranian pr_debug("Samples differ at 'intr_regs'\n");
16826ff0f0aSStephane Eranian return false;
16926ff0f0aSStephane Eranian }
17026ff0f0aSStephane Eranian }
17126ff0f0aSStephane Eranian
172fc33dccbSKan Liang if (type & PERF_SAMPLE_PHYS_ADDR)
173fc33dccbSKan Liang COMP(phys_addr);
174fc33dccbSKan Liang
175ba78c1c5SNamhyung Kim if (type & PERF_SAMPLE_CGROUP)
176ba78c1c5SNamhyung Kim COMP(cgroup);
177ba78c1c5SNamhyung Kim
178dc67d192SArnaldo Carvalho de Melo if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
179dc67d192SArnaldo Carvalho de Melo COMP(data_page_size);
180dc67d192SArnaldo Carvalho de Melo
181d8eda898SStephane Eranian if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
182d8eda898SStephane Eranian COMP(code_page_size);
183d8eda898SStephane Eranian
18498dcf14dSAdrian Hunter if (type & PERF_SAMPLE_AUX) {
18598dcf14dSAdrian Hunter COMP(aux_sample.size);
18698dcf14dSAdrian Hunter if (memcmp(s1->aux_sample.data, s2->aux_sample.data,
18798dcf14dSAdrian Hunter s1->aux_sample.size)) {
18898dcf14dSAdrian Hunter pr_debug("Samples differ at 'aux_sample'\n");
18998dcf14dSAdrian Hunter return false;
19098dcf14dSAdrian Hunter }
19198dcf14dSAdrian Hunter }
19298dcf14dSAdrian Hunter
193045f8cd8SAdrian Hunter return true;
194045f8cd8SAdrian Hunter }
195045f8cd8SAdrian Hunter
do_test(u64 sample_type,u64 sample_regs,u64 read_format)19626ff0f0aSStephane Eranian static int do_test(u64 sample_type, u64 sample_regs, u64 read_format)
197045f8cd8SAdrian Hunter {
19832dcd021SJiri Olsa struct evsel evsel = {
199045f8cd8SAdrian Hunter .needs_swap = false,
2001fc632ceSJiri Olsa .core = {
201045f8cd8SAdrian Hunter . attr = {
202045f8cd8SAdrian Hunter .sample_type = sample_type,
203045f8cd8SAdrian Hunter .read_format = read_format,
204045f8cd8SAdrian Hunter },
2051fc632ceSJiri Olsa },
206045f8cd8SAdrian Hunter };
207045f8cd8SAdrian Hunter union perf_event *event;
208045f8cd8SAdrian Hunter union {
209045f8cd8SAdrian Hunter struct ip_callchain callchain;
210045f8cd8SAdrian Hunter u64 data[64];
211045f8cd8SAdrian Hunter } callchain = {
212045f8cd8SAdrian Hunter /* 3 ips */
213045f8cd8SAdrian Hunter .data = {3, 201, 202, 203},
214045f8cd8SAdrian Hunter };
215045f8cd8SAdrian Hunter union {
216045f8cd8SAdrian Hunter struct branch_stack branch_stack;
217045f8cd8SAdrian Hunter u64 data[64];
218045f8cd8SAdrian Hunter } branch_stack = {
219045f8cd8SAdrian Hunter /* 1 branch_entry */
22042bbabedSKan Liang .data = {1, -1ULL, 211, 212, 213},
221045f8cd8SAdrian Hunter };
22226ff0f0aSStephane Eranian u64 regs[64];
223c5c97cadSNamhyung Kim const u32 raw_data[] = {0x12345678, 0x0a0b0c0d, 0x11020304, 0x05060708, 0 };
224045f8cd8SAdrian Hunter const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
22598dcf14dSAdrian Hunter const u64 aux_data[] = {0xa55a, 0, 0xeeddee, 0x0282028202820282};
226045f8cd8SAdrian Hunter struct perf_sample sample = {
227045f8cd8SAdrian Hunter .ip = 101,
228045f8cd8SAdrian Hunter .pid = 102,
229045f8cd8SAdrian Hunter .tid = 103,
230045f8cd8SAdrian Hunter .time = 104,
231045f8cd8SAdrian Hunter .addr = 105,
232045f8cd8SAdrian Hunter .id = 106,
233045f8cd8SAdrian Hunter .stream_id = 107,
234045f8cd8SAdrian Hunter .period = 108,
235045f8cd8SAdrian Hunter .weight = 109,
236045f8cd8SAdrian Hunter .cpu = 110,
237045f8cd8SAdrian Hunter .raw_size = sizeof(raw_data),
238045f8cd8SAdrian Hunter .data_src = 111,
239091a4ef5SAdrian Hunter .transaction = 112,
240045f8cd8SAdrian Hunter .raw_data = (void *)raw_data,
241045f8cd8SAdrian Hunter .callchain = &callchain.callchain,
24242bbabedSKan Liang .no_hw_idx = false,
243045f8cd8SAdrian Hunter .branch_stack = &branch_stack.branch_stack,
244045f8cd8SAdrian Hunter .user_regs = {
245045f8cd8SAdrian Hunter .abi = PERF_SAMPLE_REGS_ABI_64,
24626ff0f0aSStephane Eranian .mask = sample_regs,
24726ff0f0aSStephane Eranian .regs = regs,
248045f8cd8SAdrian Hunter },
249045f8cd8SAdrian Hunter .user_stack = {
250045f8cd8SAdrian Hunter .size = sizeof(data),
251045f8cd8SAdrian Hunter .data = (void *)data,
252045f8cd8SAdrian Hunter },
253045f8cd8SAdrian Hunter .read = {
254045f8cd8SAdrian Hunter .time_enabled = 0x030a59d664fca7deULL,
255045f8cd8SAdrian Hunter .time_running = 0x011b6ae553eb98edULL,
256045f8cd8SAdrian Hunter },
25726ff0f0aSStephane Eranian .intr_regs = {
25826ff0f0aSStephane Eranian .abi = PERF_SAMPLE_REGS_ABI_64,
25926ff0f0aSStephane Eranian .mask = sample_regs,
26026ff0f0aSStephane Eranian .regs = regs,
26126ff0f0aSStephane Eranian },
262fc33dccbSKan Liang .phys_addr = 113,
263ba78c1c5SNamhyung Kim .cgroup = 114,
264dc67d192SArnaldo Carvalho de Melo .data_page_size = 115,
265d8eda898SStephane Eranian .code_page_size = 116,
26698dcf14dSAdrian Hunter .aux_sample = {
26798dcf14dSAdrian Hunter .size = sizeof(aux_data),
26898dcf14dSAdrian Hunter .data = (void *)aux_data,
26998dcf14dSAdrian Hunter },
270045f8cd8SAdrian Hunter };
271f52679b7SNamhyung Kim struct sample_read_value values[] = {{1, 5, 0}, {9, 3, 0}, {2, 7, 0}, {6, 4, 1},};
27210269a2cSMadhavan Srinivasan struct perf_sample sample_out, sample_out_endian;
273045f8cd8SAdrian Hunter size_t i, sz, bufsz;
274045f8cd8SAdrian Hunter int err, ret = -1;
275045f8cd8SAdrian Hunter
27626ff0f0aSStephane Eranian if (sample_type & PERF_SAMPLE_REGS_USER)
2771fc632ceSJiri Olsa evsel.core.attr.sample_regs_user = sample_regs;
27826ff0f0aSStephane Eranian
27926ff0f0aSStephane Eranian if (sample_type & PERF_SAMPLE_REGS_INTR)
2801fc632ceSJiri Olsa evsel.core.attr.sample_regs_intr = sample_regs;
28126ff0f0aSStephane Eranian
28242bbabedSKan Liang if (sample_type & PERF_SAMPLE_BRANCH_STACK)
28342bbabedSKan Liang evsel.core.attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
28442bbabedSKan Liang
28526ff0f0aSStephane Eranian for (i = 0; i < sizeof(regs); i++)
28626ff0f0aSStephane Eranian *(i + (u8 *)regs) = i & 0xfe;
287045f8cd8SAdrian Hunter
288045f8cd8SAdrian Hunter if (read_format & PERF_FORMAT_GROUP) {
289045f8cd8SAdrian Hunter sample.read.group.nr = 4;
290045f8cd8SAdrian Hunter sample.read.group.values = values;
291045f8cd8SAdrian Hunter } else {
292045f8cd8SAdrian Hunter sample.read.one.value = 0x08789faeb786aa87ULL;
293045f8cd8SAdrian Hunter sample.read.one.id = 99;
294f52679b7SNamhyung Kim sample.read.one.lost = 1;
295045f8cd8SAdrian Hunter }
296045f8cd8SAdrian Hunter
297352ea45aSJiri Olsa sz = perf_event__sample_event_size(&sample, sample_type, read_format);
298045f8cd8SAdrian Hunter bufsz = sz + 4096; /* Add a bit for overrun checking */
299045f8cd8SAdrian Hunter event = malloc(bufsz);
300045f8cd8SAdrian Hunter if (!event) {
301045f8cd8SAdrian Hunter pr_debug("malloc failed\n");
302045f8cd8SAdrian Hunter return -1;
303045f8cd8SAdrian Hunter }
304045f8cd8SAdrian Hunter
305045f8cd8SAdrian Hunter memset(event, 0xff, bufsz);
306045f8cd8SAdrian Hunter event->header.type = PERF_RECORD_SAMPLE;
307045f8cd8SAdrian Hunter event->header.misc = 0;
308045f8cd8SAdrian Hunter event->header.size = sz;
309045f8cd8SAdrian Hunter
310352ea45aSJiri Olsa err = perf_event__synthesize_sample(event, sample_type, read_format,
311936f1f30SAdrian Hunter &sample);
312045f8cd8SAdrian Hunter if (err) {
313045f8cd8SAdrian Hunter pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
314045f8cd8SAdrian Hunter "perf_event__synthesize_sample", sample_type, err);
315045f8cd8SAdrian Hunter goto out_free;
316045f8cd8SAdrian Hunter }
317045f8cd8SAdrian Hunter
318045f8cd8SAdrian Hunter /* The data does not contain 0xff so we use that to check the size */
319045f8cd8SAdrian Hunter for (i = bufsz; i > 0; i--) {
320045f8cd8SAdrian Hunter if (*(i - 1 + (u8 *)event) != 0xff)
321045f8cd8SAdrian Hunter break;
322045f8cd8SAdrian Hunter }
323045f8cd8SAdrian Hunter if (i != sz) {
324045f8cd8SAdrian Hunter pr_debug("Event size mismatch: actual %zu vs expected %zu\n",
325045f8cd8SAdrian Hunter i, sz);
326045f8cd8SAdrian Hunter goto out_free;
327045f8cd8SAdrian Hunter }
328045f8cd8SAdrian Hunter
3292aaefde4SArnaldo Carvalho de Melo evsel.sample_size = __evsel__sample_size(sample_type);
330045f8cd8SAdrian Hunter
3316b6017a2SArnaldo Carvalho de Melo err = evsel__parse_sample(&evsel, event, &sample_out);
332045f8cd8SAdrian Hunter if (err) {
333045f8cd8SAdrian Hunter pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
3346b6017a2SArnaldo Carvalho de Melo "evsel__parse_sample", sample_type, err);
335045f8cd8SAdrian Hunter goto out_free;
336045f8cd8SAdrian Hunter }
337045f8cd8SAdrian Hunter
33810269a2cSMadhavan Srinivasan if (!samples_same(&sample, &sample_out, sample_type, read_format, evsel.needs_swap)) {
339045f8cd8SAdrian Hunter pr_debug("parsing failed for sample_type %#"PRIx64"\n",
340045f8cd8SAdrian Hunter sample_type);
341045f8cd8SAdrian Hunter goto out_free;
342045f8cd8SAdrian Hunter }
343045f8cd8SAdrian Hunter
34410269a2cSMadhavan Srinivasan if (sample_type == PERF_SAMPLE_BRANCH_STACK) {
34510269a2cSMadhavan Srinivasan evsel.needs_swap = true;
34610269a2cSMadhavan Srinivasan evsel.sample_size = __evsel__sample_size(sample_type);
34710269a2cSMadhavan Srinivasan err = evsel__parse_sample(&evsel, event, &sample_out_endian);
34810269a2cSMadhavan Srinivasan if (err) {
34910269a2cSMadhavan Srinivasan pr_debug("%s failed for sample_type %#"PRIx64", error %d\n",
35010269a2cSMadhavan Srinivasan "evsel__parse_sample", sample_type, err);
35110269a2cSMadhavan Srinivasan goto out_free;
35210269a2cSMadhavan Srinivasan }
35310269a2cSMadhavan Srinivasan
35410269a2cSMadhavan Srinivasan if (!samples_same(&sample, &sample_out_endian, sample_type, read_format, evsel.needs_swap)) {
35510269a2cSMadhavan Srinivasan pr_debug("parsing failed for sample_type %#"PRIx64"\n",
35610269a2cSMadhavan Srinivasan sample_type);
35710269a2cSMadhavan Srinivasan goto out_free;
35810269a2cSMadhavan Srinivasan }
35910269a2cSMadhavan Srinivasan }
36010269a2cSMadhavan Srinivasan
361045f8cd8SAdrian Hunter ret = 0;
362045f8cd8SAdrian Hunter out_free:
363045f8cd8SAdrian Hunter free(event);
364045f8cd8SAdrian Hunter if (ret && read_format)
365045f8cd8SAdrian Hunter pr_debug("read_format %#"PRIx64"\n", read_format);
366045f8cd8SAdrian Hunter return ret;
367045f8cd8SAdrian Hunter }
368045f8cd8SAdrian Hunter
369045f8cd8SAdrian Hunter /**
370045f8cd8SAdrian Hunter * test__sample_parsing - test sample parsing.
371045f8cd8SAdrian Hunter *
372045f8cd8SAdrian Hunter * This function implements a test that synthesizes a sample event, parses it
373045f8cd8SAdrian Hunter * and then checks that the parsed sample matches the original sample. The test
374045f8cd8SAdrian Hunter * checks sample format bits separately and together. If the test passes %0 is
375045f8cd8SAdrian Hunter * returned, otherwise %-1 is returned.
376045f8cd8SAdrian Hunter */
test__sample_parsing(struct test_suite * test __maybe_unused,int subtest __maybe_unused)37733f44bfdSIan Rogers static int test__sample_parsing(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
378045f8cd8SAdrian Hunter {
379f52679b7SNamhyung Kim const u64 rf[] = {4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 28, 29, 30, 31};
380045f8cd8SAdrian Hunter u64 sample_type;
38126ff0f0aSStephane Eranian u64 sample_regs;
382045f8cd8SAdrian Hunter size_t i;
383045f8cd8SAdrian Hunter int err;
384045f8cd8SAdrian Hunter
385045f8cd8SAdrian Hunter /*
386045f8cd8SAdrian Hunter * Fail the test if it has not been updated when new sample format bits
387091a4ef5SAdrian Hunter * were added. Please actually update the test rather than just change
388091a4ef5SAdrian Hunter * the condition below.
389045f8cd8SAdrian Hunter */
390c7444297SKan Liang if (PERF_SAMPLE_MAX > PERF_SAMPLE_WEIGHT_STRUCT << 1) {
39111a4d435SArnaldo Carvalho de Melo pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n");
392045f8cd8SAdrian Hunter return -1;
393045f8cd8SAdrian Hunter }
394045f8cd8SAdrian Hunter
395045f8cd8SAdrian Hunter /* Test each sample format bit separately */
396045f8cd8SAdrian Hunter for (sample_type = 1; sample_type != PERF_SAMPLE_MAX;
397045f8cd8SAdrian Hunter sample_type <<= 1) {
398045f8cd8SAdrian Hunter /* Test read_format variations */
399045f8cd8SAdrian Hunter if (sample_type == PERF_SAMPLE_READ) {
400045f8cd8SAdrian Hunter for (i = 0; i < ARRAY_SIZE(rf); i++) {
401045f8cd8SAdrian Hunter err = do_test(sample_type, 0, rf[i]);
402045f8cd8SAdrian Hunter if (err)
403045f8cd8SAdrian Hunter return err;
404045f8cd8SAdrian Hunter }
405045f8cd8SAdrian Hunter continue;
406045f8cd8SAdrian Hunter }
40726ff0f0aSStephane Eranian sample_regs = 0;
408045f8cd8SAdrian Hunter
409045f8cd8SAdrian Hunter if (sample_type == PERF_SAMPLE_REGS_USER)
41026ff0f0aSStephane Eranian sample_regs = 0x3fff;
411045f8cd8SAdrian Hunter
41226ff0f0aSStephane Eranian if (sample_type == PERF_SAMPLE_REGS_INTR)
41326ff0f0aSStephane Eranian sample_regs = 0xff0fff;
41426ff0f0aSStephane Eranian
41526ff0f0aSStephane Eranian err = do_test(sample_type, sample_regs, 0);
416045f8cd8SAdrian Hunter if (err)
417045f8cd8SAdrian Hunter return err;
418045f8cd8SAdrian Hunter }
419045f8cd8SAdrian Hunter
420c7444297SKan Liang /*
421c7444297SKan Liang * Test all sample format bits together
422c7444297SKan Liang * Note: PERF_SAMPLE_WEIGHT and PERF_SAMPLE_WEIGHT_STRUCT cannot
423c7444297SKan Liang * be set simultaneously.
424c7444297SKan Liang */
425c7444297SKan Liang sample_type = (PERF_SAMPLE_MAX - 1) & ~PERF_SAMPLE_WEIGHT;
42626ff0f0aSStephane Eranian sample_regs = 0x3fff; /* shared yb intr and user regs */
427045f8cd8SAdrian Hunter for (i = 0; i < ARRAY_SIZE(rf); i++) {
42826ff0f0aSStephane Eranian err = do_test(sample_type, sample_regs, rf[i]);
429045f8cd8SAdrian Hunter if (err)
430045f8cd8SAdrian Hunter return err;
431045f8cd8SAdrian Hunter }
432045f8cd8SAdrian Hunter
433045f8cd8SAdrian Hunter return 0;
434045f8cd8SAdrian Hunter }
435d68f0365SIan Rogers
436d68f0365SIan Rogers DEFINE_SUITE("Sample parsing", sample_parsing);
437