unwind-libdw.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. static char *debuginfo_path;
  15. static const Dwfl_Callbacks offline_callbacks = {
  16. .find_debuginfo = dwfl_standard_find_debuginfo,
  17. .debuginfo_path = &debuginfo_path,
  18. .section_address = dwfl_offline_section_address,
  19. };
  20. static int __report_module(struct addr_location *al, u64 ip,
  21. struct unwind_info *ui)
  22. {
  23. Dwfl_Module *mod;
  24. struct dso *dso = NULL;
  25. thread__find_addr_location(ui->thread,
  26. PERF_RECORD_MISC_USER,
  27. MAP__FUNCTION, ip, al);
  28. if (al->map)
  29. dso = al->map->dso;
  30. if (!dso)
  31. return 0;
  32. mod = dwfl_addrmodule(ui->dwfl, ip);
  33. if (mod) {
  34. Dwarf_Addr s;
  35. dwfl_module_info(mod, NULL, &s, NULL, NULL, NULL, NULL, NULL);
  36. if (s != al->map->start - al->map->pgoff)
  37. mod = 0;
  38. }
  39. if (!mod)
  40. mod = dwfl_report_elf(ui->dwfl, dso->short_name,
  41. (dso->symsrc_filename ? dso->symsrc_filename : dso->long_name), -1, al->map->start - al->map->pgoff,
  42. false);
  43. return mod && dwfl_addrmodule(ui->dwfl, ip) == mod ? 0 : -1;
  44. }
  45. static int report_module(u64 ip, struct unwind_info *ui)
  46. {
  47. struct addr_location al;
  48. return __report_module(&al, ip, ui);
  49. }
  50. static int entry(u64 ip, struct unwind_info *ui)
  51. {
  52. struct unwind_entry e;
  53. struct addr_location al;
  54. if (__report_module(&al, ip, ui))
  55. return -1;
  56. e.ip = ip;
  57. e.map = al.map;
  58. e.sym = al.sym;
  59. pr_debug("unwind: %s:ip = 0x%" PRIx64 " (0x%" PRIx64 ")\n",
  60. al.sym ? al.sym->name : "''",
  61. ip,
  62. al.map ? al.map->map_ip(al.map, ip) : (u64) 0);
  63. return ui->cb(&e, ui->arg);
  64. }
  65. static pid_t next_thread(Dwfl *dwfl, void *arg, void **thread_argp)
  66. {
  67. /* We want only single thread to be processed. */
  68. if (*thread_argp != NULL)
  69. return 0;
  70. *thread_argp = arg;
  71. return dwfl_pid(dwfl);
  72. }
  73. static int access_dso_mem(struct unwind_info *ui, Dwarf_Addr addr,
  74. Dwarf_Word *data)
  75. {
  76. struct addr_location al;
  77. ssize_t size;
  78. thread__find_addr_map(ui->thread, PERF_RECORD_MISC_USER,
  79. MAP__FUNCTION, addr, &al);
  80. if (!al.map) {
  81. pr_debug("unwind: no map for %lx\n", (unsigned long)addr);
  82. return -1;
  83. }
  84. if (!al.map->dso)
  85. return -1;
  86. size = dso__data_read_addr(al.map->dso, al.map, ui->machine,
  87. addr, (u8 *) data, sizeof(*data));
  88. return !(size == sizeof(*data));
  89. }
  90. static bool memory_read(Dwfl *dwfl __maybe_unused, Dwarf_Addr addr, Dwarf_Word *result,
  91. void *arg)
  92. {
  93. struct unwind_info *ui = arg;
  94. struct stack_dump *stack = &ui->sample->user_stack;
  95. u64 start, end;
  96. int offset;
  97. int ret;
  98. ret = perf_reg_value(&start, &ui->sample->user_regs, PERF_REG_SP);
  99. if (ret)
  100. return false;
  101. end = start + stack->size;
  102. /* Check overflow. */
  103. if (addr + sizeof(Dwarf_Word) < addr)
  104. return false;
  105. if (addr < start || addr + sizeof(Dwarf_Word) > end) {
  106. ret = access_dso_mem(ui, addr, result);
  107. if (ret) {
  108. pr_debug("unwind: access_mem 0x%" PRIx64 " not inside range"
  109. " 0x%" PRIx64 "-0x%" PRIx64 "\n",
  110. addr, start, end);
  111. return false;
  112. }
  113. return true;
  114. }
  115. offset = addr - start;
  116. *result = *(Dwarf_Word *)&stack->data[offset];
  117. pr_debug("unwind: access_mem addr 0x%" PRIx64 ", val %lx, offset %d\n",
  118. addr, (unsigned long)*result, offset);
  119. return true;
  120. }
  121. static const Dwfl_Thread_Callbacks callbacks = {
  122. .next_thread = next_thread,
  123. .memory_read = memory_read,
  124. .set_initial_registers = libdw__arch_set_initial_registers,
  125. };
  126. static int
  127. frame_callback(Dwfl_Frame *state, void *arg)
  128. {
  129. struct unwind_info *ui = arg;
  130. Dwarf_Addr pc;
  131. if (!dwfl_frame_pc(state, &pc, NULL)) {
  132. pr_err("%s", dwfl_errmsg(-1));
  133. return DWARF_CB_ABORT;
  134. }
  135. return entry(pc, ui) || !(--ui->max_stack) ?
  136. DWARF_CB_ABORT : DWARF_CB_OK;
  137. }
  138. int unwind__get_entries(unwind_entry_cb_t cb, void *arg,
  139. struct thread *thread,
  140. struct perf_sample *data,
  141. int max_stack)
  142. {
  143. struct unwind_info ui = {
  144. .sample = data,
  145. .thread = thread,
  146. .machine = thread->mg->machine,
  147. .cb = cb,
  148. .arg = arg,
  149. .max_stack = max_stack,
  150. };
  151. Dwarf_Word ip;
  152. int err = -EINVAL;
  153. if (!data->user_regs.regs)
  154. return -EINVAL;
  155. ui.dwfl = dwfl_begin(&offline_callbacks);
  156. if (!ui.dwfl)
  157. goto out;
  158. err = perf_reg_value(&ip, &data->user_regs, PERF_REG_IP);
  159. if (err)
  160. goto out;
  161. err = report_module(ip, &ui);
  162. if (err)
  163. goto out;
  164. if (!dwfl_attach_state(ui.dwfl, EM_NONE, thread->tid, &callbacks, &ui))
  165. goto out;
  166. err = dwfl_getthread_frames(ui.dwfl, thread->tid, frame_callback, &ui);
  167. if (err && !ui.max_stack)
  168. err = 0;
  169. out:
  170. if (err)
  171. pr_debug("unwind: failed with '%s'\n", dwfl_errmsg(-1));
  172. dwfl_end(ui.dwfl);
  173. return 0;
  174. }