dwarf-unwind.c 5.3 KB

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