1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23237f281SWang Nan #include <string.h>
33237f281SWang Nan #include "perf_regs.h"
43237f281SWang Nan #include "thread.h"
53237f281SWang Nan #include "map.h"
6c54d241bSArnaldo Carvalho de Melo #include "maps.h"
73237f281SWang Nan #include "event.h"
83237f281SWang Nan #include "debug.h"
93237f281SWang Nan #include "tests/tests.h"
103237f281SWang Nan
113237f281SWang Nan #define STACK_SIZE 8192
123237f281SWang Nan
sample_ustack(struct perf_sample * sample,struct thread * thread,u64 * regs)133237f281SWang Nan static int sample_ustack(struct perf_sample *sample,
143237f281SWang Nan struct thread *thread, u64 *regs)
153237f281SWang Nan {
163237f281SWang Nan struct stack_dump *stack = &sample->user_stack;
173237f281SWang Nan struct map *map;
183237f281SWang Nan unsigned long sp;
193237f281SWang Nan u64 stack_size, *buf;
203237f281SWang Nan
213237f281SWang Nan buf = malloc(STACK_SIZE);
223237f281SWang Nan if (!buf) {
233237f281SWang Nan pr_debug("failed to allocate sample uregs data\n");
243237f281SWang Nan return -1;
253237f281SWang Nan }
263237f281SWang Nan
273237f281SWang Nan sp = (unsigned long) regs[PERF_REG_ARM64_SP];
283237f281SWang Nan
29fe87797dSArnaldo Carvalho de Melo map = maps__find(thread__maps(thread), (u64)sp);
303237f281SWang Nan if (!map) {
313237f281SWang Nan pr_debug("failed to get stack map\n");
323237f281SWang Nan free(buf);
333237f281SWang Nan return -1;
343237f281SWang Nan }
353237f281SWang Nan
36*e5116f46SIan Rogers stack_size = map__end(map) - sp;
373237f281SWang Nan stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;
383237f281SWang Nan
393237f281SWang Nan memcpy(buf, (void *) sp, stack_size);
403237f281SWang Nan stack->data = (char *) buf;
413237f281SWang Nan stack->size = stack_size;
423237f281SWang Nan return 0;
433237f281SWang Nan }
443237f281SWang Nan
test__arch_unwind_sample(struct perf_sample * sample,struct thread * thread)453237f281SWang Nan int test__arch_unwind_sample(struct perf_sample *sample,
463237f281SWang Nan struct thread *thread)
473237f281SWang Nan {
483237f281SWang Nan struct regs_dump *regs = &sample->user_regs;
493237f281SWang Nan u64 *buf;
503237f281SWang Nan
513237f281SWang Nan buf = calloc(1, sizeof(u64) * PERF_REGS_MAX);
523237f281SWang Nan if (!buf) {
533237f281SWang Nan pr_debug("failed to allocate sample uregs data\n");
543237f281SWang Nan return -1;
553237f281SWang Nan }
563237f281SWang Nan
573237f281SWang Nan perf_regs_load(buf);
583237f281SWang Nan regs->abi = PERF_SAMPLE_REGS_ABI;
593237f281SWang Nan regs->regs = buf;
603237f281SWang Nan regs->mask = PERF_REGS_MASK;
613237f281SWang Nan
623237f281SWang Nan return sample_ustack(sample, thread, buf);
633237f281SWang Nan }
64