unwind-libdw.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 - al->map->pgoff)
  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 - al->map->pgoff,
  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 = ip;
  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. if (!thread__find_map(ui->thread, PERF_RECORD_MISC_USER, addr, &al)) {
  88. pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
  89. return -1;
  90. }
  91. if (!al.map->dso)
  92. return -1;
  93. size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
  94. addr, (u8 *) data, sizeof(*data));
  95. return !(size == sizeof(*data));
  96. }
  97. static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
  98. void *arg)
  99. {
  100. struct unwind_info *ui = arg;
  101. struct stack_dump *stack = &ui->sample->user_stack;
  102. u64 start, end;
  103. int offset;
  104. int ret;
  105. ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
  106. if (ret)
  107. return false;
  108. end = start + stack->size;
  109. /* Check overflow. */
  110. if (addr + sizeof(Dwarf_Word) < addr)
  111. return false;
  112. if (addr < start || addr + sizeof(Dwarf_Word) > end) {
  113. ret = access_dso_mem(ui, addr, result);
  114. if (ret) {
  115. pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
  116. " 0x%" PRIx64 "-0x%" PRIx64 "\n",
  117. addr, start, end);
  118. return false;
  119. }
  120. return true;
  121. }
  122. offset = addr - start;
  123. *result = *(Dwarf_Word *)&stack->data[offset];
  124. pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
  125. addr, (unsigned long)*result, offset);
  126. return true;
  127. }
  128. static const Dwfl_Thread_Callbacks callbacks = {
  129. .next_thread = next_thread,
  130. .memory_read = memory_read,
  131. .set_initial_registers = libdw__arch_set_initial_registers,
  132. };
  133. static int
  134. frame_callback(Dwfl_Frame *state, void *arg)
  135. {
  136. struct unwind_info *ui = arg;
  137. Dwarf_Addr pc;
  138. bool isactivation;
  139. if (!dwfl_frame_pc(state, &pc, NULL)) {
  140. pr_err("%s", dwfl_errmsg(-1));
  141. return DWARF_CB_ABORT;
  142. }
  143. // report the module before we query for isactivation
  144. report_module(pc, ui);
  145. if (!dwfl_frame_pc(state, &pc, &isactivation)) {
  146. pr_err("%s", dwfl_errmsg(-1));
  147. return DWARF_CB_ABORT;
  148. }
  149. if (!isactivation)
  150. --pc;
  151. return entry(pc, ui) || !(--ui->max_stack) ?
  152. DWARF_CB_ABORT : DWARF_CB_OK;
  153. }
  154. int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
  155. struct thread *thread,
  156. struct perf_sample *data,
  157. int max_stack)
  158. {
  159. struct unwind_info *ui, ui_buf = {
  160. .sample = data,
  161. .thread = thread,
  162. .machine = thread->mg->machine,
  163. .cb = cb,
  164. .arg = arg,
  165. .max_stack = max_stack,
  166. };
  167. Dwarf_Word ip;
  168. int err = -EINVAL, i;
  169. if (!data->user_regs.regs)
  170. return -EINVAL;
  171. ui = zalloc(sizeof(ui_buf) + sizeof(ui_buf.entries[0]) * max_stack);
  172. if (!ui)
  173. return -ENOMEM;
  174. *ui = ui_buf;
  175. ui->dwfl = dwfl_begin(&offline_callbacks);
  176. if (!ui->dwfl)
  177. goto out;
  178. err = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
  179. if (err)
  180. goto out;
  181. err = report_module(ip, ui);
  182. if (err)
  183. goto out;
  184. err = !dwfl_attach_state(ui->dwfl, EM_NONE, thread->tid, &callbacks, ui);
  185. if (err)
  186. goto out;
  187. err = dwfl_getthread_frames(ui->dwfl, thread->tid, frame_callback, ui);
  188. if (err && ui->max_stack != max_stack)
  189. err = 0;
  190. /*
  191. * Display what we got based on the order setup.
  192. */
  193. for (i = 0; i < ui->idx && !err; i++) {
  194. int j = i;
  195. if (callchain_param.order == ORDER_CALLER)
  196. j = ui->idx - i - 1;
  197. err = ui->entries[j].ip ? ui->cb(&ui->entries[j], ui->arg) : 0;
  198. }
  199. out:
  200. if (err)
  201. pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
  202. dwfl_end(ui->dwfl);
  203. free(ui);
  204. return 0;
  205. }