unwind-libdw.c 5.4 KB

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