unwind-libdw.c 4.5 KB

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