dwarf-unwind.c 3.8 KB

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