dwarf-unwind.c 5.2 KB

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