1 #include <string.h>
2 #include "perf_regs.h"
3 #include "thread.h"
4 #include "map.h"
5 #include "event.h"
6 #include "tests/tests.h"
7 
8 #define STACK_SIZE 8192
9 
10 static int sample_ustack(struct perf_sample *sample,
11 			 struct thread *thread, u64 *regs)
12 {
13 	struct stack_dump *stack = &sample->user_stack;
14 	struct map *map;
15 	unsigned long sp;
16 	u64 stack_size, *buf;
17 
18 	buf = malloc(STACK_SIZE);
19 	if (!buf) {
20 		pr_debug("failed to allocate sample uregs data\n");
21 		return -1;
22 	}
23 
24 	sp = (unsigned long) regs[PERF_REG_X86_SP];
25 
26 	map = map_groups__find(&thread->mg, MAP__FUNCTION, (u64) sp);
27 	if (!map) {
28 		pr_debug("failed to get stack map\n");
29 		return -1;
30 	}
31 
32 	stack_size = map->end - sp;
33 	stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;
34 
35 	memcpy(buf, (void *) sp, stack_size);
36 	stack->data = (char *) buf;
37 	stack->size = stack_size;
38 	return 0;
39 }
40 
41 int test__arch_unwind_sample(struct perf_sample *sample,
42 			     struct thread *thread)
43 {
44 	struct regs_dump *regs = &sample->user_regs;
45 	u64 *buf;
46 
47 	buf = malloc(sizeof(u64) * PERF_REGS_MAX);
48 	if (!buf) {
49 		pr_debug("failed to allocate sample uregs data\n");
50 		return -1;
51 	}
52 
53 	perf_regs_load(buf);
54 	regs->abi  = PERF_SAMPLE_REGS_ABI;
55 	regs->regs = buf;
56 	regs->mask = PERF_REGS_MASK;
57 
58 	return sample_ustack(sample, thread, buf);
59 }
60