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