dwarf-unwind.c 4.3 KB

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