18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <string.h>
38c2ecf20Sopenharmony_ci#include "perf_regs.h"
48c2ecf20Sopenharmony_ci#include "thread.h"
58c2ecf20Sopenharmony_ci#include "map.h"
68c2ecf20Sopenharmony_ci#include "maps.h"
78c2ecf20Sopenharmony_ci#include "event.h"
88c2ecf20Sopenharmony_ci#include "debug.h"
98c2ecf20Sopenharmony_ci#include "tests/tests.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define STACK_SIZE 8192
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic int sample_ustack(struct perf_sample *sample,
148c2ecf20Sopenharmony_ci		struct thread *thread, u64 *regs)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	struct stack_dump *stack = &sample->user_stack;
178c2ecf20Sopenharmony_ci	struct map *map;
188c2ecf20Sopenharmony_ci	unsigned long sp;
198c2ecf20Sopenharmony_ci	u64 stack_size, *buf;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	buf = malloc(STACK_SIZE);
228c2ecf20Sopenharmony_ci	if (!buf) {
238c2ecf20Sopenharmony_ci		pr_debug("failed to allocate sample uregs data\n");
248c2ecf20Sopenharmony_ci		return -1;
258c2ecf20Sopenharmony_ci	}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	sp = (unsigned long) regs[PERF_REG_ARM64_SP];
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	map = maps__find(thread->maps, (u64)sp);
308c2ecf20Sopenharmony_ci	if (!map) {
318c2ecf20Sopenharmony_ci		pr_debug("failed to get stack map\n");
328c2ecf20Sopenharmony_ci		free(buf);
338c2ecf20Sopenharmony_ci		return -1;
348c2ecf20Sopenharmony_ci	}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	stack_size = map->end - sp;
378c2ecf20Sopenharmony_ci	stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	memcpy(buf, (void *) sp, stack_size);
408c2ecf20Sopenharmony_ci	stack->data = (char *) buf;
418c2ecf20Sopenharmony_ci	stack->size = stack_size;
428c2ecf20Sopenharmony_ci	return 0;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciint test__arch_unwind_sample(struct perf_sample *sample,
468c2ecf20Sopenharmony_ci		struct thread *thread)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct regs_dump *regs = &sample->user_regs;
498c2ecf20Sopenharmony_ci	u64 *buf;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	buf = calloc(1, sizeof(u64) * PERF_REGS_MAX);
528c2ecf20Sopenharmony_ci	if (!buf) {
538c2ecf20Sopenharmony_ci		pr_debug("failed to allocate sample uregs data\n");
548c2ecf20Sopenharmony_ci		return -1;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	perf_regs_load(buf);
588c2ecf20Sopenharmony_ci	regs->abi  = PERF_SAMPLE_REGS_ABI;
598c2ecf20Sopenharmony_ci	regs->regs = buf;
608c2ecf20Sopenharmony_ci	regs->mask = PERF_REGS_MASK;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	return sample_ustack(sample, thread, buf);
638c2ecf20Sopenharmony_ci}
64