Backtrace.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2013 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 <inttypes.h>
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <sys/types.h>
  20. #include <ucontext.h>
  21. #include <string>
  22. #include <android-base/stringprintf.h>
  23. #include <android-base/threads.h>
  24. #include <backtrace/Backtrace.h>
  25. #include <backtrace/BacktraceMap.h>
  26. #include <demangle.h>
  27. #include "BacktraceLog.h"
  28. #include "UnwindStack.h"
  29. using android::base::StringPrintf;
  30. //-------------------------------------------------------------------------
  31. // Backtrace functions.
  32. //-------------------------------------------------------------------------
  33. Backtrace::Backtrace(pid_t pid, pid_t tid, BacktraceMap* map)
  34. : pid_(pid), tid_(tid), map_(map), map_shared_(true) {
  35. if (map_ == nullptr) {
  36. map_ = BacktraceMap::Create(pid);
  37. map_shared_ = false;
  38. }
  39. }
  40. Backtrace::~Backtrace() {
  41. if (map_ && !map_shared_) {
  42. delete map_;
  43. map_ = nullptr;
  44. }
  45. }
  46. std::string Backtrace::GetFunctionName(uint64_t pc, uint64_t* offset, const backtrace_map_t* map) {
  47. backtrace_map_t map_value;
  48. if (map == nullptr) {
  49. FillInMap(pc, &map_value);
  50. map = &map_value;
  51. }
  52. // If no map is found, or this map is backed by a device, then return nothing.
  53. if (map->start == 0 || (map->flags & PROT_DEVICE_MAP)) {
  54. return "";
  55. }
  56. return demangle(GetFunctionNameRaw(pc, offset).c_str());
  57. }
  58. bool Backtrace::VerifyReadWordArgs(uint64_t ptr, word_t* out_value) {
  59. if (ptr & (sizeof(word_t)-1)) {
  60. BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr));
  61. *out_value = static_cast<word_t>(-1);
  62. return false;
  63. }
  64. return true;
  65. }
  66. std::string Backtrace::FormatFrameData(size_t frame_num) {
  67. if (frame_num >= frames_.size()) {
  68. return "";
  69. }
  70. return FormatFrameData(&frames_[frame_num]);
  71. }
  72. std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
  73. std::string map_name;
  74. if (BacktraceMap::IsValid(frame->map)) {
  75. map_name = frame->map.Name();
  76. if (!frame->map.name.empty()) {
  77. if (map_name[0] == '[' && map_name[map_name.size() - 1] == ']') {
  78. map_name.resize(map_name.size() - 1);
  79. map_name += StringPrintf(":%" PRIPTR "]", frame->map.start);
  80. }
  81. }
  82. } else {
  83. map_name = "<unknown>";
  84. }
  85. std::string line(StringPrintf("#%02zu pc %" PRIPTR " ", frame->num, frame->rel_pc));
  86. line += map_name;
  87. // Special handling for non-zero offset maps, we need to print that
  88. // information.
  89. if (frame->map.offset != 0) {
  90. line += " (offset " + StringPrintf("0x%" PRIx64, frame->map.offset) + ")";
  91. }
  92. if (!frame->func_name.empty()) {
  93. line += " (" + frame->func_name;
  94. if (frame->func_offset) {
  95. line += StringPrintf("+%" PRIu64, frame->func_offset);
  96. }
  97. line += ')';
  98. }
  99. return line;
  100. }
  101. void Backtrace::FillInMap(uint64_t pc, backtrace_map_t* map) {
  102. if (map_ != nullptr) {
  103. map_->FillIn(pc, map);
  104. }
  105. }
  106. Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
  107. if (pid == BACKTRACE_CURRENT_PROCESS) {
  108. pid = getpid();
  109. if (tid == BACKTRACE_CURRENT_THREAD) {
  110. tid = android::base::GetThreadId();
  111. }
  112. } else if (tid == BACKTRACE_CURRENT_THREAD) {
  113. tid = pid;
  114. }
  115. if (pid == getpid()) {
  116. return new UnwindStackCurrent(pid, tid, map);
  117. } else {
  118. return new UnwindStackPtrace(pid, tid, map);
  119. }
  120. }
  121. std::string Backtrace::GetErrorString(BacktraceUnwindError error) {
  122. switch (error.error_code) {
  123. case BACKTRACE_UNWIND_NO_ERROR:
  124. return "No error";
  125. case BACKTRACE_UNWIND_ERROR_SETUP_FAILED:
  126. return "Setup failed";
  127. case BACKTRACE_UNWIND_ERROR_MAP_MISSING:
  128. return "No map found";
  129. case BACKTRACE_UNWIND_ERROR_INTERNAL:
  130. return "Internal libbacktrace error, please submit a bugreport";
  131. case BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST:
  132. return "Thread doesn't exist";
  133. case BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT:
  134. return "Thread has not responded to signal in time";
  135. case BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION:
  136. return "Attempt to use an unsupported feature";
  137. case BACKTRACE_UNWIND_ERROR_NO_CONTEXT:
  138. return "Attempt to do an offline unwind without a context";
  139. case BACKTRACE_UNWIND_ERROR_EXCEED_MAX_FRAMES_LIMIT:
  140. return "Exceed MAX_BACKTRACE_FRAMES limit";
  141. case BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED:
  142. return android::base::StringPrintf("Failed to read memory at addr 0x%" PRIx64,
  143. error.error_info.addr);
  144. case BACKTRACE_UNWIND_ERROR_ACCESS_REG_FAILED:
  145. return android::base::StringPrintf("Failed to read register %" PRIu64, error.error_info.regno);
  146. case BACKTRACE_UNWIND_ERROR_FIND_PROC_INFO_FAILED:
  147. return "Failed to find a function in debug sections";
  148. case BACKTRACE_UNWIND_ERROR_EXECUTE_DWARF_INSTRUCTION_FAILED:
  149. return "Failed to execute dwarf instructions in debug sections";
  150. case BACKTRACE_UNWIND_ERROR_UNWIND_INFO:
  151. return "Failed to unwind due to invalid unwind information";
  152. case BACKTRACE_UNWIND_ERROR_REPEATED_FRAME:
  153. return "Failed to unwind due to same sp/pc repeating";
  154. case BACKTRACE_UNWIND_ERROR_INVALID_ELF:
  155. return "Failed to unwind due to invalid elf";
  156. }
  157. }