unwind-libdw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 <dwarf-regs.h>
  10. #include "unwind.h"
  11. #include "unwind-libdw.h"
  12. #include "machine.h"
  13. #include "map.h"
  14. #include "symbol.h"
  15. #include "thread.h"
  16. #include <linux/types.h>
  17. #include <linux/zalloc.h>
  18. #include "event.h"
  19. #include "perf_regs.h"
  20. #include "callchain.h"
  21. #include "util/env.h"
  22. /*
  23. * The dwfl thread argument passed to functions like memory_read. Memory has to
  24. * be allocated to persist of multiple uses of the dwfl.
  25. */
  26. struct dwfl_ui_thread_info {
  27. /* Back link to the dwfl. */
  28. Dwfl *dwfl;
  29. /* The current unwind info, only 1 is supported. */
  30. struct unwind_info *ui;
  31. };
  32. static char *debuginfo_path;
  33. static int __find_debuginfo(Dwfl_Module *mod __maybe_unused, void **userdata,
  34. const char *modname __maybe_unused, Dwarf_Addr base __maybe_unused,
  35. const char *file_name, const char *debuglink_file __maybe_unused,
  36. GElf_Word debuglink_crc __maybe_unused, char **debuginfo_file_name)
  37. {
  38. const struct dso *dso = *userdata;
  39. assert(dso);
  40. if (dso__symsrc_filename(dso) && strcmp(file_name, dso__symsrc_filename(dso)))
  41. *debuginfo_file_name = strdup(dso__symsrc_filename(dso));
  42. return -1;
  43. }
  44. void libdw__invalidate_dwfl(struct maps *maps, void *arg)
  45. {
  46. struct dwfl_ui_thread_info *dwfl_ui_ti = arg;
  47. if (!dwfl_ui_ti)
  48. return;
  49. assert(dwfl_ui_ti->ui == NULL);
  50. maps__set_libdw_addr_space_dwfl(maps, NULL);
  51. dwfl_end(dwfl_ui_ti->dwfl);
  52. free(dwfl_ui_ti);
  53. }
  54. static const Dwfl_Callbacks offline_callbacks = {
  55. .find_debuginfo = __find_debuginfo,
  56. .debuginfo_path = &debuginfo_path,
  57. .section_address = dwfl_offline_section_address,
  58. // .find_elf is not set as we use dwfl_report_elf() instead.
  59. };
  60. static int __report_module(struct addr_location *al, u64 ip,
  61. struct unwind_info *ui)
  62. {
  63. Dwfl_Module *mod;
  64. struct dso *dso = NULL;
  65. Dwarf_Addr base;
  66. /*
  67. * Some callers will use al->sym, so we can't just use the
  68. * cheaper thread__find_map() here.
  69. */
  70. thread__find_symbol(ui->thread, PERF_RECORD_MISC_USER, ip, al);
  71. if (al->map)
  72. dso = map__dso(al->map);
  73. if (!dso)
  74. return 0;
  75. /*
  76. * The generated JIT DSO files only map the code segment without
  77. * ELF headers. Since JIT codes used to be packed in a memory
  78. * segment, calculating the base address using pgoff falls into
  79. * a different code in another DSO. So just use the map->start
  80. * directly to pick the correct one.
  81. */
  82. if (!strncmp(dso__long_name(dso), "/tmp/jitted-", 12))
  83. base = map__start(al->map);
  84. else
  85. base = map__start(al->map) - map__pgoff(al->map);
  86. mod = dwfl_addrmodule(ui->dwfl, ip);
  87. if (mod) {
  88. Dwarf_Addr s;
  89. dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL);
  90. if (s != base)
  91. mod = NULL;
  92. }
  93. if (!mod) {
  94. char filename[PATH_MAX];
  95. __symbol__join_symfs(filename, sizeof(filename), dso__long_name(dso));
  96. /* Don't hang up on device files like /dev/dri/renderD128. */
  97. if (is_regular_file(filename)) {
  98. mod = dwfl_report_elf(ui->dwfl, dso__short_name(dso), filename, -1,
  99. base, false);
  100. }
  101. }
  102. if (!mod) {
  103. char filename[PATH_MAX];
  104. if (dso__build_id_filename(dso, filename, sizeof(filename), false))
  105. mod = dwfl_report_elf(ui->dwfl, dso__short_name(dso), filename, -1,
  106. base, false);
  107. }
  108. if (mod) {
  109. void **userdatap;
  110. dwfl_module_info(mod, &userdatap, NULL, NULL, NULL, NULL, NULL, NULL);
  111. *userdatap = dso;
  112. }
  113. return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
  114. }
  115. static int report_module(u64 ip, struct unwind_info *ui)
  116. {
  117. struct addr_location al;
  118. int res;
  119. addr_location__init(&al);
  120. res = __report_module(&al, ip, ui);
  121. addr_location__exit(&al);
  122. return res;
  123. }
  124. /*
  125. * Store all entries within entries array,
  126. * we will process it after we finish unwind.
  127. */
  128. static int entry(u64 ip, struct unwind_info *ui)
  129. {
  130. struct unwind_entry *e = &ui->entries[ui->idx++];
  131. struct addr_location al;
  132. addr_location__init(&al);
  133. if (__report_module(&al, ip, ui)) {
  134. addr_location__exit(&al);
  135. return -1;
  136. }
  137. e->ip = ip;
  138. e->ms.thread = thread__get(al.thread);
  139. e->ms.map = map__get(al.map);
  140. e->ms.sym = al.sym;
  141. pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
  142. al.sym ? al.sym->name : "''",
  143. ip,
  144. al.map ? map__map_ip(al.map, ip) : (u64) 0);
  145. addr_location__exit(&al);
  146. return 0;
  147. }
  148. static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
  149. {
  150. /* We want only single thread to be processed. */
  151. if (*thread_argp != NULL)
  152. return 0;
  153. *thread_argp = arg;
  154. return dwfl_pid(dwfl);
  155. }
  156. static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
  157. Dwarf_Word *data)
  158. {
  159. struct addr_location al;
  160. ssize_t size;
  161. struct dso *dso;
  162. addr_location__init(&al);
  163. if (!thread__find_map(ui->thread, PERF_RECORD_MISC_USER, addr, &al)) {
  164. pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
  165. goto out_fail;
  166. }
  167. dso = map__dso(al.map);
  168. if (!dso)
  169. goto out_fail;
  170. size = dso__data_read_addr(dso, al.map, ui->machine, addr, (u8 *) data, sizeof(*data));
  171. addr_location__exit(&al);
  172. return !(size == sizeof(*data));
  173. out_fail:
  174. addr_location__exit(&al);
  175. return -1;
  176. }
  177. static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
  178. void *arg)
  179. {
  180. struct dwfl_ui_thread_info *dwfl_ui_ti = arg;
  181. struct unwind_info *ui = dwfl_ui_ti->ui;
  182. struct stack_dump *stack = &ui->sample->user_stack;
  183. u64 start, end;
  184. int offset;
  185. int ret;
  186. if (!ui->sample->user_regs)
  187. return false;
  188. ret = perf_reg_value(&start, ui->sample->user_regs,
  189. perf_arch_reg_sp(ui->e_machine));
  190. if (ret)
  191. return false;
  192. end = start + stack->size;
  193. /* Check overflow. */
  194. if (addr + sizeof(Dwarf_Word) < addr)
  195. return false;
  196. if (addr < start || addr + sizeof(Dwarf_Word) > end) {
  197. ret = access_dso_mem(ui, addr, result);
  198. if (ret) {
  199. pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
  200. " 0x%" PRIx64 "-0x%" PRIx64 "\n",
  201. addr, start, end);
  202. return false;
  203. }
  204. return true;
  205. }
  206. offset = addr - start;
  207. *result = *(Dwarf_Word *)&stack->data[offset];
  208. pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
  209. addr, (unsigned long)*result, offset);
  210. return true;
  211. }
  212. static bool libdw_set_initial_registers(Dwfl_Thread *thread, void *arg)
  213. {
  214. struct dwfl_ui_thread_info *dwfl_ui_ti = arg;
  215. struct unwind_info *ui = dwfl_ui_ti->ui;
  216. struct regs_dump *user_regs = perf_sample__user_regs(ui->sample);
  217. Dwarf_Word *dwarf_regs;
  218. int max_dwarf_reg = 0;
  219. bool ret;
  220. uint16_t e_machine = ui->e_machine;
  221. int e_flags = ui->e_flags;
  222. uint64_t ip_perf_reg = perf_arch_reg_ip(e_machine);
  223. Dwarf_Word val = 0;
  224. /*
  225. * For every possible perf register in the bitmap determine the dwarf
  226. * register and use to compute the max.
  227. */
  228. for (int perf_reg = 0; perf_reg < 64; perf_reg++) {
  229. if (user_regs->mask & (1ULL << perf_reg)) {
  230. int dwarf_reg =
  231. get_dwarf_regnum_for_perf_regnum(perf_reg, e_machine,
  232. e_flags,
  233. /*only_libdw_supported=*/true);
  234. if (dwarf_reg > max_dwarf_reg)
  235. max_dwarf_reg = dwarf_reg;
  236. }
  237. }
  238. dwarf_regs = calloc(max_dwarf_reg + 1, sizeof(*dwarf_regs));
  239. if (!dwarf_regs)
  240. return false;
  241. for (int perf_reg = 0; perf_reg < 64; perf_reg++) {
  242. if (user_regs->mask & (1ULL << perf_reg)) {
  243. int dwarf_reg =
  244. get_dwarf_regnum_for_perf_regnum(perf_reg, e_machine,
  245. e_flags,
  246. /*only_libdw_supported=*/true);
  247. if (dwarf_reg >= 0) {
  248. val = 0;
  249. if (perf_reg_value(&val, user_regs, perf_reg) == 0)
  250. dwarf_regs[dwarf_reg] = val;
  251. }
  252. }
  253. }
  254. if (perf_reg_value(&val, user_regs, ip_perf_reg) == 0)
  255. dwfl_thread_state_register_pc(thread, val);
  256. ret = dwfl_thread_state_registers(thread, 0, max_dwarf_reg + 1, dwarf_regs);
  257. free(dwarf_regs);
  258. return ret;
  259. }
  260. static const Dwfl_Thread_Callbacks callbacks = {
  261. .next_thread = next_thread,
  262. .memory_read = memory_read,
  263. .set_initial_registers = libdw_set_initial_registers,
  264. };
  265. static int
  266. frame_callback(Dwfl_Frame *state, void *arg)
  267. {
  268. struct unwind_info *ui = arg;
  269. Dwarf_Addr pc;
  270. bool isactivation;
  271. if (!dwfl_frame_pc(state, &pc, NULL)) {
  272. if (!ui->best_effort)
  273. pr_err("%s", dwfl_errmsg(-1));
  274. return DWARF_CB_ABORT;
  275. }
  276. // report the module before we query for isactivation
  277. report_module(pc, ui);
  278. if (!dwfl_frame_pc(state, &pc, &isactivation)) {
  279. if (!ui->best_effort)
  280. pr_err("%s", dwfl_errmsg(-1));
  281. return DWARF_CB_ABORT;
  282. }
  283. if (!isactivation)
  284. --pc;
  285. return entry(pc, ui) || !(--ui->max_stack) ?
  286. DWARF_CB_ABORT : DWARF_CB_OK;
  287. }
  288. int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
  289. struct thread *thread,
  290. struct perf_sample *data,
  291. int max_stack,
  292. bool best_effort)
  293. {
  294. struct maps *maps = thread__maps(thread);
  295. struct machine *machine = maps__machine(maps);
  296. uint32_t e_flags = 0;
  297. uint16_t e_machine = thread__e_machine(thread, machine, &e_flags);
  298. struct dwfl_ui_thread_info *dwfl_ui_ti;
  299. static struct unwind_info *ui;
  300. Dwfl *dwfl;
  301. Dwarf_Word ip;
  302. int err = -EINVAL, i;
  303. if (!data->user_regs || !data->user_regs->regs)
  304. return -EINVAL;
  305. ui = zalloc(sizeof(*ui) + sizeof(ui->entries[0]) * max_stack);
  306. if (!ui)
  307. return -ENOMEM;
  308. *ui = (struct unwind_info){
  309. .sample = data,
  310. .thread = thread,
  311. .machine = machine,
  312. .cb = cb,
  313. .arg = arg,
  314. .max_stack = max_stack,
  315. .e_machine = e_machine,
  316. .e_flags = e_flags,
  317. .best_effort = best_effort
  318. };
  319. dwfl_ui_ti = maps__libdw_addr_space_dwfl(maps);
  320. if (dwfl_ui_ti) {
  321. dwfl = dwfl_ui_ti->dwfl;
  322. } else {
  323. dwfl_ui_ti = zalloc(sizeof(*dwfl_ui_ti));
  324. dwfl = dwfl_begin(&offline_callbacks);
  325. if (!dwfl)
  326. goto out;
  327. dwfl_ui_ti->dwfl = dwfl;
  328. maps__set_libdw_addr_space_dwfl(maps, dwfl_ui_ti);
  329. }
  330. assert(dwfl_ui_ti->ui == NULL);
  331. assert(dwfl_ui_ti->dwfl == dwfl);
  332. assert(dwfl_ui_ti == maps__libdw_addr_space_dwfl(maps));
  333. dwfl_ui_ti->ui = ui;
  334. ui->dwfl = dwfl;
  335. err = perf_reg_value(&ip, data->user_regs, perf_arch_reg_ip(e_machine));
  336. if (err)
  337. goto out;
  338. err = report_module(ip, ui);
  339. if (err)
  340. goto out;
  341. dwfl_attach_state(dwfl, /*elf=*/NULL, thread__tid(thread), &callbacks,
  342. /* Dwfl thread function argument*/dwfl_ui_ti);
  343. // Ignore thread already attached error.
  344. err = dwfl_getthread_frames(dwfl, thread__tid(thread), frame_callback,
  345. /* Dwfl frame function argument*/ui);
  346. if (err && ui->max_stack != max_stack)
  347. err = 0;
  348. /*
  349. * Display what we got based on the order setup.
  350. */
  351. for (i = 0; i < ui->idx && !err; i++) {
  352. int j = i;
  353. if (callchain_param.order == ORDER_CALLER)
  354. j = ui->idx - i - 1;
  355. err = ui->entries[j].ip ? ui->cb(&ui->entries[j], ui->arg) : 0;
  356. }
  357. out:
  358. if (err)
  359. pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
  360. for (i = 0; i < ui->idx; i++)
  361. map_symbol__exit(&ui->entries[i].ms);
  362. dwfl_ui_ti->ui = NULL;
  363. free(ui);
  364. return 0;
  365. }