UnwindStack.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2017 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. #define _GNU_SOURCE 1
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <memory>
  21. #include <set>
  22. #include <string>
  23. #include <backtrace/Backtrace.h>
  24. #include <demangle.h>
  25. #include <unwindstack/Elf.h>
  26. #include <unwindstack/MapInfo.h>
  27. #include <unwindstack/Maps.h>
  28. #include <unwindstack/Memory.h>
  29. #include <unwindstack/Regs.h>
  30. #include <unwindstack/RegsGetLocal.h>
  31. #if !defined(NO_LIBDEXFILE_SUPPORT)
  32. #include <unwindstack/DexFiles.h>
  33. #endif
  34. #include <unwindstack/Unwinder.h>
  35. #include "BacktraceLog.h"
  36. #include "UnwindStack.h"
  37. #include "UnwindStackMap.h"
  38. bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map,
  39. std::vector<backtrace_frame_data_t>* frames, size_t num_ignore_frames,
  40. std::vector<std::string>* skip_names, BacktraceUnwindError* error) {
  41. UnwindStackMap* stack_map = reinterpret_cast<UnwindStackMap*>(back_map);
  42. auto process_memory = stack_map->process_memory();
  43. unwindstack::Unwinder unwinder(MAX_BACKTRACE_FRAMES + num_ignore_frames, stack_map->stack_maps(),
  44. regs, stack_map->process_memory());
  45. unwinder.SetResolveNames(stack_map->ResolveNames());
  46. stack_map->SetArch(regs->Arch());
  47. if (stack_map->GetJitDebug() != nullptr) {
  48. unwinder.SetJitDebug(stack_map->GetJitDebug(), regs->Arch());
  49. }
  50. #if !defined(NO_LIBDEXFILE_SUPPORT)
  51. if (stack_map->GetDexFiles() != nullptr) {
  52. unwinder.SetDexFiles(stack_map->GetDexFiles(), regs->Arch());
  53. }
  54. #endif
  55. unwinder.Unwind(skip_names, &stack_map->GetSuffixesToIgnore());
  56. if (error != nullptr) {
  57. switch (unwinder.LastErrorCode()) {
  58. case unwindstack::ERROR_NONE:
  59. error->error_code = BACKTRACE_UNWIND_NO_ERROR;
  60. break;
  61. case unwindstack::ERROR_MEMORY_INVALID:
  62. error->error_code = BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED;
  63. error->error_info.addr = unwinder.LastErrorAddress();
  64. break;
  65. case unwindstack::ERROR_UNWIND_INFO:
  66. error->error_code = BACKTRACE_UNWIND_ERROR_UNWIND_INFO;
  67. break;
  68. case unwindstack::ERROR_UNSUPPORTED:
  69. error->error_code = BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION;
  70. break;
  71. case unwindstack::ERROR_INVALID_MAP:
  72. error->error_code = BACKTRACE_UNWIND_ERROR_MAP_MISSING;
  73. break;
  74. case unwindstack::ERROR_MAX_FRAMES_EXCEEDED:
  75. error->error_code = BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT;
  76. break;
  77. case unwindstack::ERROR_REPEATED_FRAME:
  78. error->error_code = BACKTRACE_UNWIND_ERROR_REPEATED_FRAME;
  79. break;
  80. case unwindstack::ERROR_INVALID_ELF:
  81. error->error_code = BACKTRACE_UNWIND_ERROR_INVALID_ELF;
  82. break;
  83. }
  84. }
  85. if (num_ignore_frames >= unwinder.NumFrames()) {
  86. frames->resize(0);
  87. return true;
  88. }
  89. auto unwinder_frames = unwinder.frames();
  90. frames->resize(unwinder.NumFrames() - num_ignore_frames);
  91. size_t cur_frame = 0;
  92. for (size_t i = num_ignore_frames; i < unwinder.NumFrames(); i++) {
  93. auto frame = &unwinder_frames[i];
  94. backtrace_frame_data_t* back_frame = &frames->at(cur_frame);
  95. back_frame->num = cur_frame++;
  96. back_frame->rel_pc = frame->rel_pc;
  97. back_frame->pc = frame->pc;
  98. back_frame->sp = frame->sp;
  99. back_frame->func_name = demangle(frame->function_name.c_str());
  100. back_frame->func_offset = frame->function_offset;
  101. back_frame->map.name = frame->map_name;
  102. back_frame->map.start = frame->map_start;
  103. back_frame->map.end = frame->map_end;
  104. back_frame->map.offset = frame->map_elf_start_offset;
  105. back_frame->map.load_bias = frame->map_load_bias;
  106. back_frame->map.flags = frame->map_flags;
  107. }
  108. return true;
  109. }
  110. bool Backtrace::UnwindOffline(unwindstack::Regs* regs, BacktraceMap* back_map,
  111. const backtrace_stackinfo_t& stack,
  112. std::vector<backtrace_frame_data_t>* frames,
  113. BacktraceUnwindError* error) {
  114. UnwindStackOfflineMap* offline_map = reinterpret_cast<UnwindStackOfflineMap*>(back_map);
  115. // Create the process memory from the stack data since this will almost
  116. // always be different each unwind.
  117. if (!offline_map->CreateProcessMemory(stack)) {
  118. if (error != nullptr) {
  119. error->error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED;
  120. }
  121. return false;
  122. }
  123. return Backtrace::Unwind(regs, back_map, frames, 0U, nullptr, error);
  124. }
  125. UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map)
  126. : BacktraceCurrent(pid, tid, map) {}
  127. std::string UnwindStackCurrent::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
  128. return GetMap()->GetFunctionName(pc, offset);
  129. }
  130. bool UnwindStackCurrent::UnwindFromContext(size_t num_ignore_frames, void* ucontext) {
  131. std::unique_ptr<unwindstack::Regs> regs;
  132. if (ucontext == nullptr) {
  133. regs.reset(unwindstack::Regs::CreateFromLocal());
  134. // Fill in the registers from this function. Do it here to avoid
  135. // one extra function call appearing in the unwind.
  136. unwindstack::RegsGetLocal(regs.get());
  137. } else {
  138. regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), ucontext));
  139. }
  140. std::vector<std::string> skip_names{"libunwindstack.so", "libbacktrace.so"};
  141. if (!skip_frames_) {
  142. skip_names.clear();
  143. }
  144. return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, &skip_names, &error_);
  145. }
  146. UnwindStackPtrace::UnwindStackPtrace(pid_t pid, pid_t tid, BacktraceMap* map)
  147. : BacktracePtrace(pid, tid, map), memory_(pid) {}
  148. std::string UnwindStackPtrace::GetFunctionNameRaw(uint64_t pc, uint64_t* offset) {
  149. return GetMap()->GetFunctionName(pc, offset);
  150. }
  151. bool UnwindStackPtrace::Unwind(size_t num_ignore_frames, void* context) {
  152. std::unique_ptr<unwindstack::Regs> regs;
  153. if (context == nullptr) {
  154. regs.reset(unwindstack::Regs::RemoteGet(Tid()));
  155. } else {
  156. regs.reset(unwindstack::Regs::CreateFromUcontext(unwindstack::Regs::CurrentArch(), context));
  157. }
  158. return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
  159. }
  160. size_t UnwindStackPtrace::Read(uint64_t addr, uint8_t* buffer, size_t bytes) {
  161. return memory_.Read(addr, buffer, bytes);
  162. }
  163. UnwindStackOffline::UnwindStackOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map,
  164. bool map_shared)
  165. : Backtrace(pid, tid, map), arch_(arch) {
  166. map_shared_ = map_shared;
  167. }
  168. bool UnwindStackOffline::Unwind(size_t num_ignore_frames, void* ucontext) {
  169. if (ucontext == nullptr) {
  170. return false;
  171. }
  172. unwindstack::ArchEnum arch;
  173. switch (arch_) {
  174. case ARCH_ARM:
  175. arch = unwindstack::ARCH_ARM;
  176. break;
  177. case ARCH_ARM64:
  178. arch = unwindstack::ARCH_ARM64;
  179. break;
  180. case ARCH_X86:
  181. arch = unwindstack::ARCH_X86;
  182. break;
  183. case ARCH_X86_64:
  184. arch = unwindstack::ARCH_X86_64;
  185. break;
  186. default:
  187. return false;
  188. }
  189. std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromUcontext(arch, ucontext));
  190. return Backtrace::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames, nullptr, &error_);
  191. }
  192. std::string UnwindStackOffline::GetFunctionNameRaw(uint64_t, uint64_t*) {
  193. return "";
  194. }
  195. size_t UnwindStackOffline::Read(uint64_t, uint8_t*, size_t) {
  196. return 0;
  197. }
  198. bool UnwindStackOffline::ReadWord(uint64_t, word_t*) {
  199. return false;
  200. }
  201. Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid,
  202. const std::vector<backtrace_map_t>& maps,
  203. const backtrace_stackinfo_t& stack) {
  204. std::unique_ptr<UnwindStackOfflineMap> map(
  205. reinterpret_cast<UnwindStackOfflineMap*>(BacktraceMap::CreateOffline(pid, maps)));
  206. if (map.get() == nullptr || !map->CreateProcessMemory(stack)) {
  207. return nullptr;
  208. }
  209. return new UnwindStackOffline(arch, pid, tid, map.release(), false);
  210. }
  211. Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) {
  212. if (map == nullptr) {
  213. return nullptr;
  214. }
  215. return new UnwindStackOffline(arch, pid, tid, map, true);
  216. }
  217. void Backtrace::SetGlobalElfCache(bool enable) {
  218. unwindstack::Elf::SetCachingEnabled(enable);
  219. }