dwarf-unwind.c 1.3 KB

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