Path: blob/master/tools/perf/arch/x86/tests/dwarf-unwind.c
26295 views
// SPDX-License-Identifier: GPL-2.01#include <string.h>2#include "perf_regs.h"3#include "thread.h"4#include "map.h"5#include "maps.h"6#include "event.h"7#include "debug.h"8#include "tests/tests.h"910#define STACK_SIZE 81921112static int sample_ustack(struct perf_sample *sample,13struct thread *thread, u64 *regs)14{15struct stack_dump *stack = &sample->user_stack;16struct map *map;17unsigned long sp;18u64 stack_size, *buf;1920buf = malloc(STACK_SIZE);21if (!buf) {22pr_debug("failed to allocate sample uregs data\n");23return -1;24}2526sp = (unsigned long) regs[PERF_REG_X86_SP];2728map = maps__find(thread__maps(thread), (u64)sp);29if (!map) {30pr_debug("failed to get stack map\n");31free(buf);32return -1;33}3435stack_size = map__end(map) - sp;36map__put(map);37stack_size = stack_size > STACK_SIZE ? STACK_SIZE : stack_size;3839memcpy(buf, (void *) sp, stack_size);40#ifdef MEMORY_SANITIZER41/*42* Copying the stack may copy msan poison, avoid false positives in the43* unwinder by removing the poison here.44*/45__msan_unpoison(buf, stack_size);46#endif47stack->data = (char *) buf;48stack->size = stack_size;49return 0;50}5152int test__arch_unwind_sample(struct perf_sample *sample,53struct thread *thread)54{55struct regs_dump *regs = perf_sample__user_regs(sample);56u64 *buf;5758buf = malloc(sizeof(u64) * PERF_REGS_MAX);59if (!buf) {60pr_debug("failed to allocate sample uregs data\n");61return -1;62}6364#ifdef MEMORY_SANITIZER65/*66* Assignments to buf in the assembly function perf_regs_load aren't67* seen by memory sanitizer. Zero the memory to convince memory68* sanitizer the memory is initialized.69*/70memset(buf, 0, sizeof(u64) * PERF_REGS_MAX);71#endif72perf_regs_load(buf);73regs->abi = PERF_SAMPLE_REGS_ABI;74regs->regs = buf;75regs->mask = PERF_REGS_MASK;7677return sample_ustack(sample, thread, buf);78}798081