unwind-libdw.c 5.7 KB

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