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__VARIABLE, (u64) sp);
27 	if (!map) {
28 		pr_debug("failed to get stack map\n");
29 		free(buf);
30 		return -1;
31 	}
32 
33 	stack_size = map->end - sp;
34 	stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;
35 
36 	memcpy(buf, (void *) sp, stack_size);
37 	stack->data = (char *) buf;
38 	stack->size = stack_size;
39 	return 0;
40 }
41 
42 int test__arch_unwind_sample(struct perf_sample *sample,
43 			     struct thread *thread)
44 {
45 	struct regs_dump *regs = &sample->user_regs;
46 	u64 *buf;
47 
48 	buf = malloc(sizeof(u64) * PERF_REGS_MAX);
49 	if (!buf) {
50 		pr_debug("failed to allocate sample uregs data\n");
51 		return -1;
52 	}
53 
54 	perf_regs_load(buf);
55 	regs->abi  = PERF_SAMPLE_REGS_ABI;
56 	regs->regs = buf;
57 	regs->mask = PERF_REGS_MASK;
58 
59 	return sample_ustack(sample, thread, buf);
60 }
61