unwind-libdw.c 4.4 KB

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