dwarf-unwind.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #if defined (__x86_64__) || defined (__i386__)
  14. #include "arch-tests.h"
  15. #endif
  16. /* For bsearch. We try to unwind functions in shared object. */
  17. #include <stdlib.h>
  18. static int mmap_handler(struct perf_tool *tool __maybe_unused,
  19. union perf_event *event,
  20. struct perf_sample *sample __maybe_unused,
  21. struct machine *machine)
  22. {
  23. return machine__process_mmap2_event(machine, event, NULL);
  24. }
  25. static int init_live_machine(struct machine *machine)
  26. {
  27. union perf_event event;
  28. pid_t pid = getpid();
  29. return perf_event__synthesize_mmap_events(NULL, &event, pid, pid,
  30. mmap_handler, machine, true, 500);
  31. }
  32. #define MAX_STACK 8
  33. static int unwind_entry(struct unwind_entry *entry, void *arg)
  34. {
  35. unsigned long *cnt = (unsigned long *) arg;
  36. char *symbol = entry->sym ? entry->sym->name : NULL;
  37. static const char *funcs[MAX_STACK] = {
  38. "test__arch_unwind_sample",
  39. "unwind_thread",
  40. "compare",
  41. "bsearch",
  42. "krava_3",
  43. "krava_2",
  44. "krava_1",
  45. "test__dwarf_unwind"
  46. };
  47. if (*cnt >= MAX_STACK) {
  48. pr_debug("failed: crossed the max stack value %d\n", MAX_STACK);
  49. return -1;
  50. }
  51. if (!symbol) {
  52. pr_debug("failed: got unresolved address 0x%" PRIx64 "\n",
  53. entry->ip);
  54. return -1;
  55. }
  56. pr_debug("got: %s 0x%" PRIx64 "\n", symbol, entry->ip);
  57. return strcmp((const char *) symbol, funcs[(*cnt)++]);
  58. }
  59. __attribute__ ((noinline))
  60. static int unwind_thread(struct thread *thread)
  61. {
  62. struct perf_sample sample;
  63. unsigned long cnt = 0;
  64. int err = -1;
  65. memset(&sample, 0, sizeof(sample));
  66. if (test__arch_unwind_sample(&sample, thread)) {
  67. pr_debug("failed to get unwind sample\n");
  68. goto out;
  69. }
  70. err = unwind__get_entries(unwind_entry, &cnt, thread,
  71. &sample, MAX_STACK);
  72. if (err)
  73. pr_debug("unwind failed\n");
  74. else if (cnt != MAX_STACK) {
  75. pr_debug("got wrong number of stack entries %lu != %d\n",
  76. cnt, MAX_STACK);
  77. err = -1;
  78. }
  79. out:
  80. free(sample.user_stack.data);
  81. free(sample.user_regs.regs);
  82. return err;
  83. }
  84. static int global_unwind_retval = -INT_MAX;
  85. __attribute__ ((noinline))
  86. static int compare(void *p1, void *p2)
  87. {
  88. /* Any possible value should be 'thread' */
  89. struct thread *thread = *(struct thread **)p1;
  90. if (global_unwind_retval == -INT_MAX)
  91. global_unwind_retval = unwind_thread(thread);
  92. return p1 - p2;
  93. }
  94. __attribute__ ((noinline))
  95. static int krava_3(struct thread *thread)
  96. {
  97. struct thread *array[2] = {thread, thread};
  98. void *fp = &bsearch;
  99. /*
  100. * make _bsearch a volatile function pointer to
  101. * prevent potential optimization, which may expand
  102. * bsearch and call compare directly from this function,
  103. * instead of libc shared object.
  104. */
  105. void *(*volatile _bsearch)(void *, void *, size_t,
  106. size_t, int (*)(void *, void *));
  107. _bsearch = fp;
  108. _bsearch(array, &thread, 2, sizeof(struct thread **), compare);
  109. return global_unwind_retval;
  110. }
  111. __attribute__ ((noinline))
  112. static int krava_2(struct thread *thread)
  113. {
  114. return krava_3(thread);
  115. }
  116. __attribute__ ((noinline))
  117. static int krava_1(struct thread *thread)
  118. {
  119. return krava_2(thread);
  120. }
  121. int test__dwarf_unwind(void)
  122. {
  123. struct machines machines;
  124. struct machine *machine;
  125. struct thread *thread;
  126. int err = -1;
  127. machines__init(&machines);
  128. machine = machines__find(&machines, HOST_KERNEL_ID);
  129. if (!machine) {
  130. pr_err("Could not get machine\n");
  131. return -1;
  132. }
  133. callchain_param.record_mode = CALLCHAIN_DWARF;
  134. if (init_live_machine(machine)) {
  135. pr_err("Could not init machine\n");
  136. goto out;
  137. }
  138. if (verbose > 1)
  139. machine__fprintf(machine, stderr);
  140. thread = machine__find_thread(machine, getpid(), getpid());
  141. if (!thread) {
  142. pr_err("Could not get thread\n");
  143. goto out;
  144. }
  145. err = krava_1(thread);
  146. thread__put(thread);
  147. out:
  148. machine__delete_threads(machine);
  149. machine__exit(machine);
  150. machines__exit(&machines);
  151. return err;
  152. }