dwarf-unwind.c 6.1 KB

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