unwind-libdw.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include <linux/compiler.h>
  2. #include <elfutils/libdw.h>
  3. #include <elfutils/libdwfl.h>
  4. #include <inttypes.h>
  5. #include <errno.h>
  6. #include "debug.h"
  7. #include "unwind.h"
  8. #include "unwind-libdw.h"
  9. #include "machine.h"
  10. #include "thread.h"
  11. #include <linux/types.h>
  12. #include "event.h"
  13. #include "perf_regs.h"
  14. #include "callchain.h"
  15. static char *debuginfo_path;
  16. static const Dwfl_Callbacks offline_callbacks = {
  17. .find_debuginfo = dwfl_standard_find_debuginfo,
  18. .debuginfo_path = &debuginfo_path,
  19. .section_address = dwfl_offline_section_address,
  20. };
  21. static int __report_module(struct addr_location *al, u64 ip,
  22. struct unwind_info *ui)
  23. {
  24. Dwfl_Module *mod;
  25. struct dso *dso = NULL;
  26. thread__find_addr_location(ui->thread,
  27. PERF_RECORD_MISC_USER,
  28. MAP__FUNCTION, ip, al);
  29. if (al->map)
  30. dso = al->map->dso;
  31. if (!dso)
  32. return 0;
  33. mod = dwfl_addrmodule(ui->dwfl, ip);
  34. if (!mod)
  35. mod = dwfl_report_elf(ui->dwfl, dso->short_name,
  36. dso->long_name, -1, al->map->start,
  37. false);
  38. return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
  39. }
  40. static int report_module(u64 ip, struct unwind_info *ui)
  41. {
  42. struct addr_location al;
  43. return __report_module(&al, ip, ui);
  44. }
  45. /*
  46. * Store all entries within entries array,
  47. * we will process it after we finish unwind.
  48. */
  49. static int entry(u64 ip, struct unwind_info *ui)
  50. {
  51. struct unwind_entry *e = &ui->entries[ui->idx++];
  52. struct addr_location al;
  53. if (__report_module(&al, ip, ui))
  54. return -1;
  55. e->ip = al.addr;
  56. e->map = al.map;
  57. e->sym = al.sym;
  58. pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
  59. al.sym ? al.sym->name : "''",
  60. ip,
  61. al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
  62. return 0;
  63. }
  64. static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
  65. {
  66. /* We want only single thread to be processed. */
  67. if (*thread_argp != NULL)
  68. return 0;
  69. *thread_argp = arg;
  70. return dwfl_pid(dwfl);
  71. }
  72. static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
  73. Dwarf_Word *data)
  74. {
  75. struct addr_location al;
  76. ssize_t size;
  77. thread__find_addr_map(ui->thread, PERF_RECORD_MISC_USER,
  78. MAP__FUNCTION, addr, &al);
  79. if (!al.map) {
  80. /*
  81. * We've seen cases (softice) where DWARF unwinder went
  82. * through non executable mmaps, which we need to lookup
  83. * in MAP__VARIABLE tree.
  84. */
  85. thread__find_addr_map(ui->thread, PERF_RECORD_MISC_USER,
  86. MAP__VARIABLE, addr, &al);
  87. }
  88. if (!al.map) {
  89. pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
  90. return -1;
  91. }
  92. if (!al.map->dso)
  93. return -1;
  94. size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
  95. addr, (u8 *) data, sizeof(*data));
  96. return !(size == sizeof(*data));
  97. }
  98. static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
  99. void *arg)
  100. {
  101. struct unwind_info *ui = arg;
  102. struct stack_dump *stack = &ui->sample->user_stack;
  103. u64 start, end;
  104. int offset;
  105. int ret;
  106. ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
  107. if (ret)
  108. return false;
  109. end = start + stack->size;
  110. /* Check overflow. */
  111. if (addr + sizeof(Dwarf_Word) < addr)
  112. return false;
  113. if (addr < start || addr + sizeof(Dwarf_Word) > end) {
  114. ret = access_dso_mem(ui, addr, result);
  115. if (ret) {
  116. pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
  117. " 0x%" PRIx64 "-0x%" PRIx64 "\n",
  118. addr, start, end);
  119. return false;
  120. }
  121. return true;
  122. }
  123. offset = addr - start;
  124. *result = *(Dwarf_Word *)&stack->data[offset];
  125. pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
  126. addr, (unsigned long)*result, offset);
  127. return true;
  128. }
  129. static const Dwfl_Thread_Callbacks callbacks = {
  130. .next_thread = next_thread,
  131. .memory_read = memory_read,
  132. .set_initial_registers = libdw__arch_set_initial_registers,
  133. };
  134. static int
  135. frame_callback(Dwfl_Frame *state, void *arg)
  136. {
  137. struct unwind_info *ui = arg;
  138. Dwarf_Addr pc;
  139. if (!dwfl_frame_pc(state, &pc, NULL)) {
  140. pr_err("%s", dwfl_errmsg(-1));
  141. return DWARF_CB_ABORT;
  142. }
  143. return entry(pc, ui) || !(--ui->max_stack) ?
  144. DWARF_CB_ABORT : DWARF_CB_OK;
  145. }
  146. int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
  147. struct thread *thread,
  148. struct perf_sample *data,
  149. int max_stack)
  150. {
  151. struct unwind_info *ui, ui_buf = {
  152. .sample = data,
  153. .thread = thread,
  154. .machine = thread->mg->machine,
  155. .cb = cb,
  156. .arg = arg,
  157. .max_stack = max_stack,
  158. };
  159. Dwarf_Word ip;
  160. int err = -EINVAL, i;
  161. if (!data->user_regs.regs)
  162. return -EINVAL;
  163. ui = zalloc(sizeof(ui_buf) + sizeof(ui_buf.entries[0]) * max_stack);
  164. if (!ui)
  165. return -ENOMEM;
  166. *ui = ui_buf;
  167. ui->dwfl = dwfl_begin(&offline_callbacks);
  168. if (!ui->dwfl)
  169. goto out;
  170. err = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
  171. if (err)
  172. goto out;
  173. err = report_module(ip, ui);
  174. if (err)
  175. goto out;
  176. if (!dwfl_attach_state(ui->dwfl, EM_NONE, thread->tid, &callbacks, ui))
  177. goto out;
  178. err = dwfl_getthread_frames(ui->dwfl, thread->tid, frame_callback, ui);
  179. if (err && !ui->max_stack)
  180. err = 0;
  181. /*
  182. * Display what we got based on the order setup.
  183. */
  184. for (i = 0; i < ui->idx && !err; i++) {
  185. int j = i;
  186. if (callchain_param.order == ORDER_CALLER)
  187. j = ui->idx - i - 1;
  188. err = ui->entries[j].ip ? ui->cb(&ui->entries[j], ui->arg) : 0;
  189. }
  190. out:
  191. if (err)
  192. pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
  193. dwfl_end(ui->dwfl);
  194. free(ui);
  195. return 0;
  196. }