unwind-libdw.c 6.4 KB

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