xref: /openbmc/linux/tools/perf/arch/x86/tests/dwarf-unwind.c (revision 568beb27959b0515d325ea1c6cf211eed2d66740)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2aa16b81fSJiri Olsa #include <string.h>
3aa16b81fSJiri Olsa #include "perf_regs.h"
4aa16b81fSJiri Olsa #include "thread.h"
5aa16b81fSJiri Olsa #include "map.h"
6c54d241bSArnaldo Carvalho de Melo #include "maps.h"
7aa16b81fSJiri Olsa #include "event.h"
884f5d36fSJiri Olsa #include "debug.h"
9aa16b81fSJiri Olsa #include "tests/tests.h"
10d8b167f9SMatt Fleming #include "arch-tests.h"
11aa16b81fSJiri Olsa 
12aa16b81fSJiri Olsa #define STACK_SIZE 8192
13aa16b81fSJiri Olsa 
14aa16b81fSJiri Olsa static int sample_ustack(struct perf_sample *sample,
15aa16b81fSJiri Olsa 			 struct thread *thread, u64 *regs)
16aa16b81fSJiri Olsa {
17aa16b81fSJiri Olsa 	struct stack_dump *stack = &sample->user_stack;
18aa16b81fSJiri Olsa 	struct map *map;
19aa16b81fSJiri Olsa 	unsigned long sp;
20aa16b81fSJiri Olsa 	u64 stack_size, *buf;
21aa16b81fSJiri Olsa 
22aa16b81fSJiri Olsa 	buf = malloc(STACK_SIZE);
23aa16b81fSJiri Olsa 	if (!buf) {
24aa16b81fSJiri Olsa 		pr_debug("failed to allocate sample uregs data\n");
25aa16b81fSJiri Olsa 		return -1;
26aa16b81fSJiri Olsa 	}
27aa16b81fSJiri Olsa 
28aa16b81fSJiri Olsa 	sp = (unsigned long) regs[PERF_REG_X86_SP];
29aa16b81fSJiri Olsa 
30fe87797dSArnaldo Carvalho de Melo 	map = maps__find(thread->maps, (u64)sp);
31aa16b81fSJiri Olsa 	if (!map) {
32aa16b81fSJiri Olsa 		pr_debug("failed to get stack map\n");
33763d7f5fSMasanari Iida 		free(buf);
34aa16b81fSJiri Olsa 		return -1;
35aa16b81fSJiri Olsa 	}
36aa16b81fSJiri Olsa 
37aa16b81fSJiri Olsa 	stack_size = map->end - sp;
38aa16b81fSJiri Olsa 	stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;
39aa16b81fSJiri Olsa 
40aa16b81fSJiri Olsa 	memcpy(buf, (void *) sp, stack_size);
41*568beb27SIan Rogers #ifdef MEMORY_SANITIZER
42*568beb27SIan Rogers 	/*
43*568beb27SIan Rogers 	 * Copying the stack may copy msan poison, avoid false positives in the
44*568beb27SIan Rogers 	 * unwinder by removing the poison here.
45*568beb27SIan Rogers 	 */
46*568beb27SIan Rogers 	__msan_unpoison(buf, stack_size);
47*568beb27SIan Rogers #endif
48aa16b81fSJiri Olsa 	stack->data = (char *) buf;
49aa16b81fSJiri Olsa 	stack->size = stack_size;
50aa16b81fSJiri Olsa 	return 0;
51aa16b81fSJiri Olsa }
52aa16b81fSJiri Olsa 
53aa16b81fSJiri Olsa int test__arch_unwind_sample(struct perf_sample *sample,
54aa16b81fSJiri Olsa 			     struct thread *thread)
55aa16b81fSJiri Olsa {
56aa16b81fSJiri Olsa 	struct regs_dump *regs = &sample->user_regs;
57aa16b81fSJiri Olsa 	u64 *buf;
58aa16b81fSJiri Olsa 
59aa16b81fSJiri Olsa 	buf = malloc(sizeof(u64) * PERF_REGS_MAX);
60aa16b81fSJiri Olsa 	if (!buf) {
61aa16b81fSJiri Olsa 		pr_debug("failed to allocate sample uregs data\n");
62aa16b81fSJiri Olsa 		return -1;
63aa16b81fSJiri Olsa 	}
64aa16b81fSJiri Olsa 
650fb0d615SIan Rogers #ifdef MEMORY_SANITIZER
660fb0d615SIan Rogers 	/*
670fb0d615SIan Rogers 	 * Assignments to buf in the assembly function perf_regs_load aren't
680fb0d615SIan Rogers 	 * seen by memory sanitizer. Zero the memory to convince memory
690fb0d615SIan Rogers 	 * sanitizer the memory is initialized.
700fb0d615SIan Rogers 	 */
710fb0d615SIan Rogers 	memset(buf, 0, sizeof(u64) * PERF_REGS_MAX);
720fb0d615SIan Rogers #endif
73aa16b81fSJiri Olsa 	perf_regs_load(buf);
74aa16b81fSJiri Olsa 	regs->abi  = PERF_SAMPLE_REGS_ABI;
75aa16b81fSJiri Olsa 	regs->regs = buf;
76352ea45aSJiri Olsa 	regs->mask = PERF_REGS_MASK;
77aa16b81fSJiri Olsa 
78aa16b81fSJiri Olsa 	return sample_ustack(sample, thread, buf);
79aa16b81fSJiri Olsa }
80