OfflineUnwinder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "OfflineUnwinder.h"
  17. #include <sys/mman.h>
  18. #include <android-base/logging.h>
  19. #include <unwindstack/MachineArm.h>
  20. #include <unwindstack/MachineArm64.h>
  21. #include <unwindstack/MachineX86.h>
  22. #include <unwindstack/MachineX86_64.h>
  23. #include <unwindstack/Maps.h>
  24. #include <unwindstack/Regs.h>
  25. #include <unwindstack/RegsArm.h>
  26. #include <unwindstack/RegsArm64.h>
  27. #include <unwindstack/RegsX86.h>
  28. #include <unwindstack/RegsX86_64.h>
  29. #include <unwindstack/Unwinder.h>
  30. #include <unwindstack/UserArm.h>
  31. #include <unwindstack/UserArm64.h>
  32. #include <unwindstack/UserX86.h>
  33. #include <unwindstack/UserX86_64.h>
  34. #include "environment.h"
  35. #include "perf_regs.h"
  36. #include "read_apk.h"
  37. #include "thread_tree.h"
  38. static_assert(simpleperf::map_flags::PROT_JIT_SYMFILE_MAP ==
  39. unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP, "");
  40. namespace simpleperf {
  41. // Max frames seen so far is 463, in http://b/110923759.
  42. static constexpr size_t MAX_UNWINDING_FRAMES = 512;
  43. static unwindstack::Regs* GetBacktraceRegs(const RegSet& regs) {
  44. switch (regs.arch) {
  45. case ARCH_ARM: {
  46. unwindstack::arm_user_regs arm_user_regs;
  47. memset(&arm_user_regs, 0, sizeof(arm_user_regs));
  48. static_assert(
  49. static_cast<int>(unwindstack::ARM_REG_R0) == static_cast<int>(PERF_REG_ARM_R0), "");
  50. static_assert(
  51. static_cast<int>(unwindstack::ARM_REG_LAST) == static_cast<int>(PERF_REG_ARM_MAX), "");
  52. for (size_t i = unwindstack::ARM_REG_R0; i < unwindstack::ARM_REG_LAST; ++i) {
  53. arm_user_regs.regs[i] = static_cast<uint32_t>(regs.data[i]);
  54. }
  55. return unwindstack::RegsArm::Read(&arm_user_regs);
  56. }
  57. case ARCH_ARM64: {
  58. unwindstack::arm64_user_regs arm64_user_regs;
  59. memset(&arm64_user_regs, 0, sizeof(arm64_user_regs));
  60. static_assert(
  61. static_cast<int>(unwindstack::ARM64_REG_R0) == static_cast<int>(PERF_REG_ARM64_X0), "");
  62. static_assert(
  63. static_cast<int>(unwindstack::ARM64_REG_R30) == static_cast<int>(PERF_REG_ARM64_LR), "");
  64. memcpy(&arm64_user_regs.regs[unwindstack::ARM64_REG_R0], &regs.data[PERF_REG_ARM64_X0],
  65. sizeof(uint64_t) * (PERF_REG_ARM64_LR - PERF_REG_ARM64_X0 + 1));
  66. arm64_user_regs.sp = regs.data[PERF_REG_ARM64_SP];
  67. arm64_user_regs.pc = regs.data[PERF_REG_ARM64_PC];
  68. return unwindstack::RegsArm64::Read(&arm64_user_regs);
  69. }
  70. case ARCH_X86_32: {
  71. unwindstack::x86_user_regs x86_user_regs;
  72. memset(&x86_user_regs, 0, sizeof(x86_user_regs));
  73. x86_user_regs.eax = static_cast<uint32_t>(regs.data[PERF_REG_X86_AX]);
  74. x86_user_regs.ebx = static_cast<uint32_t>(regs.data[PERF_REG_X86_BX]);
  75. x86_user_regs.ecx = static_cast<uint32_t>(regs.data[PERF_REG_X86_CX]);
  76. x86_user_regs.edx = static_cast<uint32_t>(regs.data[PERF_REG_X86_DX]);
  77. x86_user_regs.ebp = static_cast<uint32_t>(regs.data[PERF_REG_X86_BP]);
  78. x86_user_regs.edi = static_cast<uint32_t>(regs.data[PERF_REG_X86_DI]);
  79. x86_user_regs.esi = static_cast<uint32_t>(regs.data[PERF_REG_X86_SI]);
  80. x86_user_regs.esp = static_cast<uint32_t>(regs.data[PERF_REG_X86_SP]);
  81. x86_user_regs.eip = static_cast<uint32_t>(regs.data[PERF_REG_X86_IP]);
  82. return unwindstack::RegsX86::Read(&x86_user_regs);
  83. }
  84. case ARCH_X86_64: {
  85. unwindstack::x86_64_user_regs x86_64_user_regs;
  86. memset(&x86_64_user_regs, 0, sizeof(x86_64_user_regs));
  87. x86_64_user_regs.rax = regs.data[PERF_REG_X86_AX];
  88. x86_64_user_regs.rbx = regs.data[PERF_REG_X86_BX];
  89. x86_64_user_regs.rcx = regs.data[PERF_REG_X86_CX];
  90. x86_64_user_regs.rdx = regs.data[PERF_REG_X86_DX];
  91. x86_64_user_regs.r8 = regs.data[PERF_REG_X86_R8];
  92. x86_64_user_regs.r9 = regs.data[PERF_REG_X86_R9];
  93. x86_64_user_regs.r10 = regs.data[PERF_REG_X86_R10];
  94. x86_64_user_regs.r11 = regs.data[PERF_REG_X86_R11];
  95. x86_64_user_regs.r12 = regs.data[PERF_REG_X86_R12];
  96. x86_64_user_regs.r13 = regs.data[PERF_REG_X86_R13];
  97. x86_64_user_regs.r14 = regs.data[PERF_REG_X86_R14];
  98. x86_64_user_regs.r15 = regs.data[PERF_REG_X86_R15];
  99. x86_64_user_regs.rdi = regs.data[PERF_REG_X86_DI];
  100. x86_64_user_regs.rsi = regs.data[PERF_REG_X86_SI];
  101. x86_64_user_regs.rbp = regs.data[PERF_REG_X86_BP];
  102. x86_64_user_regs.rsp = regs.data[PERF_REG_X86_SP];
  103. x86_64_user_regs.rip = regs.data[PERF_REG_X86_IP];
  104. return unwindstack::RegsX86_64::Read(&x86_64_user_regs);
  105. }
  106. default:
  107. return nullptr;
  108. }
  109. }
  110. static unwindstack::MapInfo* CreateMapInfo(const MapEntry* entry) {
  111. const char* name = entry->dso->GetDebugFilePath().c_str();
  112. uint64_t pgoff = entry->pgoff;
  113. if (entry->pgoff == 0) {
  114. auto tuple = SplitUrlInApk(entry->dso->GetDebugFilePath());
  115. if (std::get<0>(tuple)) {
  116. // The unwinder does not understand the ! format, so change back to
  117. // the previous format (apk, offset).
  118. EmbeddedElf* elf = ApkInspector::FindElfInApkByName(std::get<1>(tuple), std::get<2>(tuple));
  119. if (elf != nullptr) {
  120. name = elf->filepath().c_str();
  121. pgoff = elf->entry_offset();
  122. }
  123. }
  124. }
  125. return new unwindstack::MapInfo(nullptr, entry->start_addr, entry->get_end_addr(), pgoff,
  126. PROT_READ | PROT_EXEC | entry->flags, name);
  127. }
  128. void UnwindMaps::UpdateMaps(const MapSet& map_set) {
  129. if (version_ == map_set.version) {
  130. return;
  131. }
  132. version_ = map_set.version;
  133. size_t i = 0;
  134. size_t old_size = entries_.size();
  135. for (auto it = map_set.maps.begin(); it != map_set.maps.end();) {
  136. const MapEntry* entry = it->second;
  137. if (i < old_size && entry == entries_[i]) {
  138. i++;
  139. ++it;
  140. } else if (i == old_size || entry->start_addr <= entries_[i]->start_addr) {
  141. // Add an entry.
  142. entries_.push_back(entry);
  143. maps_.emplace_back(CreateMapInfo(entry));
  144. ++it;
  145. } else {
  146. // Remove an entry.
  147. entries_[i] = nullptr;
  148. maps_[i++] = nullptr;
  149. }
  150. }
  151. while (i < old_size) {
  152. entries_[i] = nullptr;
  153. maps_[i++] = nullptr;
  154. }
  155. std::sort(entries_.begin(), entries_.end(), [](const auto& e1, const auto& e2) {
  156. if (e1 == nullptr || e2 == nullptr) {
  157. return e1 != nullptr;
  158. }
  159. return e1->start_addr < e2->start_addr;
  160. });
  161. std::sort(maps_.begin(), maps_.end(),
  162. [](const auto& m1, const auto& m2) {
  163. if (m1 == nullptr || m2 == nullptr) {
  164. return m1 != nullptr;
  165. }
  166. return m1->start < m2->start;
  167. });
  168. entries_.resize(map_set.maps.size());
  169. maps_.resize(map_set.maps.size());
  170. }
  171. OfflineUnwinder::OfflineUnwinder(bool collect_stat) : collect_stat_(collect_stat) {
  172. unwindstack::Elf::SetCachingEnabled(true);
  173. }
  174. bool OfflineUnwinder::UnwindCallChain(const ThreadEntry& thread, const RegSet& regs,
  175. const char* stack, size_t stack_size,
  176. std::vector<uint64_t>* ips, std::vector<uint64_t>* sps) {
  177. uint64_t start_time;
  178. if (collect_stat_) {
  179. start_time = GetSystemClock();
  180. }
  181. is_callchain_broken_for_incomplete_jit_debug_info_ = false;
  182. ips->clear();
  183. sps->clear();
  184. std::vector<uint64_t> result;
  185. uint64_t sp_reg_value;
  186. if (!regs.GetSpRegValue(&sp_reg_value)) {
  187. LOG(ERROR) << "can't get sp reg value";
  188. return false;
  189. }
  190. uint64_t stack_addr = sp_reg_value;
  191. UnwindMaps& cached_map = cached_maps_[thread.pid];
  192. cached_map.UpdateMaps(*thread.maps);
  193. std::shared_ptr<unwindstack::MemoryOfflineBuffer> stack_memory(
  194. new unwindstack::MemoryOfflineBuffer(reinterpret_cast<const uint8_t*>(stack),
  195. stack_addr, stack_addr + stack_size));
  196. std::unique_ptr<unwindstack::Regs> unwind_regs(GetBacktraceRegs(regs));
  197. if (!unwind_regs) {
  198. return false;
  199. }
  200. unwindstack::Unwinder unwinder(MAX_UNWINDING_FRAMES, &cached_map, unwind_regs.get(),
  201. stack_memory);
  202. unwinder.SetResolveNames(false);
  203. unwinder.Unwind();
  204. size_t last_jit_method_frame = UINT_MAX;
  205. for (auto& frame : unwinder.frames()) {
  206. // Unwinding in arm architecture can return 0 pc address.
  207. // If frame.map.start == 0, this frame doesn't hit any map, it could be:
  208. // 1. In an executable map not backed by a file. Note that RecordCommand::ShouldOmitRecord()
  209. // may omit maps only exist memory.
  210. // 2. An incorrectly unwound frame. Like caused by invalid stack data, as in
  211. // SampleRecord::GetValidStackSize(). Or caused by incomplete JIT debug info.
  212. // We want to remove this frame and callchains following it in either case.
  213. if (frame.pc == 0 || frame.map_start == 0) {
  214. is_callchain_broken_for_incomplete_jit_debug_info_ = true;
  215. break;
  216. }
  217. if (frame.map_flags & unwindstack::MAPS_FLAGS_JIT_SYMFILE_MAP) {
  218. last_jit_method_frame = ips->size();
  219. }
  220. ips->push_back(frame.pc);
  221. sps->push_back(frame.sp);
  222. }
  223. // If the unwound frames stop near to a JITed method, it may be caused by incomplete JIT debug
  224. // info.
  225. if (last_jit_method_frame != UINT_MAX && last_jit_method_frame + 3 > ips->size()) {
  226. is_callchain_broken_for_incomplete_jit_debug_info_ = true;
  227. }
  228. uint64_t ip_reg_value;
  229. if (!regs.GetIpRegValue(&ip_reg_value)) {
  230. LOG(ERROR) << "can't get ip reg value";
  231. return false;
  232. }
  233. if (ips->empty()) {
  234. ips->push_back(ip_reg_value);
  235. sps->push_back(sp_reg_value);
  236. } else {
  237. // Check if the unwinder returns ip reg value as the first ip address in callstack.
  238. CHECK_EQ((*ips)[0], ip_reg_value);
  239. }
  240. if (collect_stat_) {
  241. unwinding_result_.used_time = GetSystemClock() - start_time;
  242. switch (unwinder.LastErrorCode()) {
  243. case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
  244. unwinding_result_.stop_reason = UnwindingResult::EXCEED_MAX_FRAMES_LIMIT;
  245. break;
  246. case unwindstack::ERROR_MEMORY_INVALID: {
  247. uint64_t addr = unwinder.LastErrorAddress();
  248. // Because we don't have precise stack range here, just guess an addr is in stack
  249. // if sp - 128K <= addr <= sp.
  250. if (addr <= stack_addr && addr >= stack_addr - 128 * 1024) {
  251. unwinding_result_.stop_reason = UnwindingResult::ACCESS_STACK_FAILED;
  252. } else {
  253. unwinding_result_.stop_reason = UnwindingResult::ACCESS_MEM_FAILED;
  254. }
  255. unwinding_result_.stop_info.addr = addr;
  256. break;
  257. }
  258. case unwindstack::ERROR_INVALID_MAP:
  259. unwinding_result_.stop_reason = UnwindingResult::MAP_MISSING;
  260. break;
  261. default:
  262. unwinding_result_.stop_reason = UnwindingResult::UNKNOWN_REASON;
  263. break;
  264. }
  265. unwinding_result_.stack_start = stack_addr;
  266. unwinding_result_.stack_end = stack_addr + stack_size;
  267. }
  268. return true;
  269. }
  270. } // namespace simpleperf