dwarf-unwind.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <linux/compiler.h>
  2. #include <linux/types.h>
  3. #include <unistd.h>
  4. #include "tests.h"
  5. #include "debug.h"
  6. #include "machine.h"
  7. #include "event.h"
  8. #include "unwind.h"
  9. #include "perf_regs.h"
  10. #include "map.h"
  11. #include "thread.h"
  12. #include "callchain.h"
  13. static int mmap_handler(struct perf_tool *tool __maybe_unused,
  14. union perf_event *event,
  15. struct perf_sample *sample __maybe_unused,
  16. struct machine *machine)
  17. {
  18. return machine__process_mmap2_event(machine, event, NULL);
  19. }
  20. static int init_live_machine(struct machine *machine)
  21. {
  22. union perf_event event;
  23. pid_t pid = getpid();
  24. return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
  25. mmap_handler, machine, true);
  26. }
  27. #define MAX_STACK 6
  28. static int unwind_entry(struct unwind_entry *entry, void *arg)
  29. {
  30. unsigned long *cnt = (unsigned long *) arg;
  31. char *symbol = entry->sym ? entry->sym->name : NULL;
  32. static const char *funcs[MAX_STACK] = {
  33. "test__arch_unwind_sample",
  34. "unwind_thread",
  35. "krava_3",
  36. "krava_2",
  37. "krava_1",
  38. "test__dwarf_unwind"
  39. };
  40. if (*cnt >= MAX_STACK) {
  41. pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
  42. return -1;
  43. }
  44. if (!symbol) {
  45. pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
  46. entry->ip);
  47. return -1;
  48. }
  49. pr_debug("got: %s 0x%" PRIx64 "\n", symbol, entry->ip);
  50. return strcmp((const char *) symbol, funcs[(*cnt)++]);
  51. }
  52. __attribute__ ((noinline))
  53. static int unwind_thread(struct thread *thread, struct machine *machine)
  54. {
  55. struct perf_sample sample;
  56. unsigned long cnt = 0;
  57. int err = -1;
  58. memset(&sample, 0, sizeof(sample));
  59. if (test__arch_unwind_sample(&sample, thread)) {
  60. pr_debug("failed to get unwind sample\n");
  61. goto out;
  62. }
  63. err = unwind__get_entries(unwind_entry, &cnt, machine, thread,
  64. &sample, MAX_STACK);
  65. if (err)
  66. pr_debug("unwind failed\n");
  67. else if (cnt != MAX_STACK) {
  68. pr_debug("got wrong number of stack entries %lu != %d\n",
  69. cnt, MAX_STACK);
  70. err = -1;
  71. }
  72. out:
  73. free(sample.user_stack.data);
  74. free(sample.user_regs.regs);
  75. return err;
  76. }
  77. __attribute__ ((noinline))
  78. static int krava_3(struct thread *thread, struct machine *machine)
  79. {
  80. return unwind_thread(thread, machine);
  81. }
  82. __attribute__ ((noinline))
  83. static int krava_2(struct thread *thread, struct machine *machine)
  84. {
  85. return krava_3(thread, machine);
  86. }
  87. __attribute__ ((noinline))
  88. static int krava_1(struct thread *thread, struct machine *machine)
  89. {
  90. return krava_2(thread, machine);
  91. }
  92. int test__dwarf_unwind(void)
  93. {
  94. struct machines machines;
  95. struct machine *machine;
  96. struct thread *thread;
  97. int err = -1;
  98. machines__init(&machines);
  99. machine = machines__find(&machines, HOST_KERNEL_ID);
  100. if (!machine) {
  101. pr_err("Could not get machine\n");
  102. return -1;
  103. }
  104. callchain_param.record_mode = CALLCHAIN_DWARF;
  105. if (init_live_machine(machine)) {
  106. pr_err("Could not init machine\n");
  107. goto out;
  108. }
  109. if (verbose > 1)
  110. machine__fprintf(machine, stderr);
  111. thread = machine__find_thread(machine, getpid(), getpid());
  112. if (!thread) {
  113. pr_err("Could not get thread\n");
  114. goto out;
  115. }
  116. err = krava_1(thread, machine);
  117. out:
  118. machine__delete_threads(machine);
  119. machine__exit(machine);
  120. machines__exit(&machines);
  121. return err;
  122. }