xref: /OK3568_Linux_fs/kernel/tools/perf/arch/x86/tests/dwarf-unwind.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <string.h>
3*4882a593Smuzhiyun #include "perf_regs.h"
4*4882a593Smuzhiyun #include "thread.h"
5*4882a593Smuzhiyun #include "map.h"
6*4882a593Smuzhiyun #include "maps.h"
7*4882a593Smuzhiyun #include "event.h"
8*4882a593Smuzhiyun #include "debug.h"
9*4882a593Smuzhiyun #include "tests/tests.h"
10*4882a593Smuzhiyun #include "arch-tests.h"
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define STACK_SIZE 8192
13*4882a593Smuzhiyun 
sample_ustack(struct perf_sample * sample,struct thread * thread,u64 * regs)14*4882a593Smuzhiyun static int sample_ustack(struct perf_sample *sample,
15*4882a593Smuzhiyun 			 struct thread *thread, u64 *regs)
16*4882a593Smuzhiyun {
17*4882a593Smuzhiyun 	struct stack_dump *stack = &sample->user_stack;
18*4882a593Smuzhiyun 	struct map *map;
19*4882a593Smuzhiyun 	unsigned long sp;
20*4882a593Smuzhiyun 	u64 stack_size, *buf;
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun 	buf = malloc(STACK_SIZE);
23*4882a593Smuzhiyun 	if (!buf) {
24*4882a593Smuzhiyun 		pr_debug("failed to allocate sample uregs data\n");
25*4882a593Smuzhiyun 		return -1;
26*4882a593Smuzhiyun 	}
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	sp = (unsigned long) regs[PERF_REG_X86_SP];
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun 	map = maps__find(thread->maps, (u64)sp);
31*4882a593Smuzhiyun 	if (!map) {
32*4882a593Smuzhiyun 		pr_debug("failed to get stack map\n");
33*4882a593Smuzhiyun 		free(buf);
34*4882a593Smuzhiyun 		return -1;
35*4882a593Smuzhiyun 	}
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	stack_size = map->end - sp;
38*4882a593Smuzhiyun 	stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	memcpy(buf, (void *) sp, stack_size);
41*4882a593Smuzhiyun #ifdef MEMORY_SANITIZER
42*4882a593Smuzhiyun 	/*
43*4882a593Smuzhiyun 	 * Copying the stack may copy msan poison, avoid false positives in the
44*4882a593Smuzhiyun 	 * unwinder by removing the poison here.
45*4882a593Smuzhiyun 	 */
46*4882a593Smuzhiyun 	__msan_unpoison(buf, stack_size);
47*4882a593Smuzhiyun #endif
48*4882a593Smuzhiyun 	stack->data = (char *) buf;
49*4882a593Smuzhiyun 	stack->size = stack_size;
50*4882a593Smuzhiyun 	return 0;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
test__arch_unwind_sample(struct perf_sample * sample,struct thread * thread)53*4882a593Smuzhiyun int test__arch_unwind_sample(struct perf_sample *sample,
54*4882a593Smuzhiyun 			     struct thread *thread)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	struct regs_dump *regs = &sample->user_regs;
57*4882a593Smuzhiyun 	u64 *buf;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	buf = malloc(sizeof(u64) * PERF_REGS_MAX);
60*4882a593Smuzhiyun 	if (!buf) {
61*4882a593Smuzhiyun 		pr_debug("failed to allocate sample uregs data\n");
62*4882a593Smuzhiyun 		return -1;
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #ifdef MEMORY_SANITIZER
66*4882a593Smuzhiyun 	/*
67*4882a593Smuzhiyun 	 * Assignments to buf in the assembly function perf_regs_load aren't
68*4882a593Smuzhiyun 	 * seen by memory sanitizer. Zero the memory to convince memory
69*4882a593Smuzhiyun 	 * sanitizer the memory is initialized.
70*4882a593Smuzhiyun 	 */
71*4882a593Smuzhiyun 	memset(buf, 0, sizeof(u64) * PERF_REGS_MAX);
72*4882a593Smuzhiyun #endif
73*4882a593Smuzhiyun 	perf_regs_load(buf);
74*4882a593Smuzhiyun 	regs->abi  = PERF_SAMPLE_REGS_ABI;
75*4882a593Smuzhiyun 	regs->regs = buf;
76*4882a593Smuzhiyun 	regs->mask = PERF_REGS_MASK;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	return sample_ustack(sample, thread, buf);
79*4882a593Smuzhiyun }
80