/thirdparty/breakpad/processor/stackwalker_arm.cc

http://github.com/tomahawk-player/tomahawk · C++ · 293 lines · 175 code · 44 blank · 74 comment · 39 complexity · 644497e8121f84c1a642372549bb5dca MD5 · raw file

  1. // Copyright (c) 2010 Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // stackwalker_arm.cc: arm-specific stackwalker.
  30. //
  31. // See stackwalker_arm.h for documentation.
  32. //
  33. // Author: Mark Mentovai, Ted Mielczarek, Jim Blandy
  34. #include "google_breakpad/processor/call_stack.h"
  35. #include "google_breakpad/processor/memory_region.h"
  36. #include "google_breakpad/processor/source_line_resolver_interface.h"
  37. #include "google_breakpad/processor/stack_frame_cpu.h"
  38. #include "processor/cfi_frame_info.h"
  39. #include "processor/logging.h"
  40. #include "processor/scoped_ptr.h"
  41. #include "processor/stackwalker_arm.h"
  42. namespace google_breakpad {
  43. StackwalkerARM::StackwalkerARM(const SystemInfo *system_info,
  44. const MDRawContextARM *context,
  45. int fp_register,
  46. MemoryRegion *memory,
  47. const CodeModules *modules,
  48. SymbolSupplier *supplier,
  49. SourceLineResolverInterface *resolver)
  50. : Stackwalker(system_info, memory, modules, supplier, resolver),
  51. context_(context), fp_register_(fp_register),
  52. context_frame_validity_(StackFrameARM::CONTEXT_VALID_ALL) { }
  53. StackFrame* StackwalkerARM::GetContextFrame() {
  54. if (!context_ || !memory_) {
  55. BPLOG(ERROR) << "Can't get context frame without context or memory";
  56. return NULL;
  57. }
  58. StackFrameARM *frame = new StackFrameARM();
  59. // The instruction pointer is stored directly in a register (r15), so pull it
  60. // straight out of the CPU context structure.
  61. frame->context = *context_;
  62. frame->context_validity = context_frame_validity_;
  63. frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
  64. frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC];
  65. return frame;
  66. }
  67. StackFrameARM *StackwalkerARM::GetCallerByCFIFrameInfo(
  68. const vector<StackFrame *> &frames,
  69. CFIFrameInfo *cfi_frame_info) {
  70. StackFrameARM *last_frame = static_cast<StackFrameARM *>(frames.back());
  71. static const char *register_names[] = {
  72. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  73. "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
  74. "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
  75. "fps", "cpsr",
  76. NULL
  77. };
  78. // Populate a dictionary with the valid register values in last_frame.
  79. CFIFrameInfo::RegisterValueMap<u_int32_t> callee_registers;
  80. for (int i = 0; register_names[i]; i++)
  81. if (last_frame->context_validity & StackFrameARM::RegisterValidFlag(i))
  82. callee_registers[register_names[i]] = last_frame->context.iregs[i];
  83. // Use the STACK CFI data to recover the caller's register values.
  84. CFIFrameInfo::RegisterValueMap<u_int32_t> caller_registers;
  85. if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
  86. &caller_registers))
  87. return NULL;
  88. // Construct a new stack frame given the values the CFI recovered.
  89. scoped_ptr<StackFrameARM> frame(new StackFrameARM());
  90. for (int i = 0; register_names[i]; i++) {
  91. CFIFrameInfo::RegisterValueMap<u_int32_t>::iterator entry =
  92. caller_registers.find(register_names[i]);
  93. if (entry != caller_registers.end()) {
  94. // We recovered the value of this register; fill the context with the
  95. // value from caller_registers.
  96. frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
  97. frame->context.iregs[i] = entry->second;
  98. } else if (4 <= i && i <= 11 && (last_frame->context_validity &
  99. StackFrameARM::RegisterValidFlag(i))) {
  100. // If the STACK CFI data doesn't mention some callee-saves register, and
  101. // it is valid in the callee, assume the callee has not yet changed it.
  102. // Registers r4 through r11 are callee-saves, according to the Procedure
  103. // Call Standard for the ARM Architecture, which the Linux ABI follows.
  104. frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
  105. frame->context.iregs[i] = last_frame->context.iregs[i];
  106. }
  107. }
  108. // If the CFI doesn't recover the PC explicitly, then use .ra.
  109. if (! (frame->context_validity & StackFrameARM::CONTEXT_VALID_PC)) {
  110. CFIFrameInfo::RegisterValueMap<u_int32_t>::iterator entry =
  111. caller_registers.find(".ra");
  112. if (entry != caller_registers.end()) {
  113. if (fp_register_ == -1) {
  114. frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
  115. frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = entry->second;
  116. } else {
  117. // The CFI updated the link register and not the program counter.
  118. // Handle getting the program counter from the link register.
  119. frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
  120. frame->context_validity |= StackFrameARM::CONTEXT_VALID_LR;
  121. frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = entry->second;
  122. frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
  123. last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
  124. }
  125. }
  126. }
  127. // If the CFI doesn't recover the SP explicitly, then use .cfa.
  128. if (! (frame->context_validity & StackFrameARM::CONTEXT_VALID_SP)) {
  129. CFIFrameInfo::RegisterValueMap<u_int32_t>::iterator entry =
  130. caller_registers.find(".cfa");
  131. if (entry != caller_registers.end()) {
  132. frame->context_validity |= StackFrameARM::CONTEXT_VALID_SP;
  133. frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = entry->second;
  134. }
  135. }
  136. // If we didn't recover the PC and the SP, then the frame isn't very useful.
  137. static const int essentials = (StackFrameARM::CONTEXT_VALID_SP
  138. | StackFrameARM::CONTEXT_VALID_PC);
  139. if ((frame->context_validity & essentials) != essentials)
  140. return NULL;
  141. frame->trust = StackFrame::FRAME_TRUST_CFI;
  142. return frame.release();
  143. }
  144. StackFrameARM *StackwalkerARM::GetCallerByStackScan(
  145. const vector<StackFrame *> &frames) {
  146. StackFrameARM *last_frame = static_cast<StackFrameARM *>(frames.back());
  147. u_int32_t last_sp = last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
  148. u_int32_t caller_sp, caller_pc;
  149. if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc)) {
  150. // No plausible return address was found.
  151. return NULL;
  152. }
  153. // ScanForReturnAddress found a reasonable return address. Advance
  154. // %sp to the location above the one where the return address was
  155. // found.
  156. caller_sp += 4;
  157. // Create a new stack frame (ownership will be transferred to the caller)
  158. // and fill it in.
  159. StackFrameARM *frame = new StackFrameARM();
  160. frame->trust = StackFrame::FRAME_TRUST_SCAN;
  161. frame->context = last_frame->context;
  162. frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = caller_pc;
  163. frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
  164. frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
  165. StackFrameARM::CONTEXT_VALID_SP;
  166. return frame;
  167. }
  168. StackFrameARM *StackwalkerARM::GetCallerByFramePointer(
  169. const vector<StackFrame *> &frames) {
  170. StackFrameARM *last_frame = static_cast<StackFrameARM *>(frames.back());
  171. if (!(last_frame->context_validity &
  172. StackFrameARM::RegisterValidFlag(fp_register_))) {
  173. return NULL;
  174. }
  175. u_int32_t last_fp = last_frame->context.iregs[fp_register_];
  176. u_int32_t caller_fp = 0;
  177. if (last_fp && !memory_->GetMemoryAtAddress(last_fp, &caller_fp)) {
  178. BPLOG(ERROR) << "Unable to read caller_fp from last_fp: 0x"
  179. << std::hex << last_fp;
  180. return NULL;
  181. }
  182. u_int32_t caller_lr = 0;
  183. if (last_fp && !memory_->GetMemoryAtAddress(last_fp + 4, &caller_lr)) {
  184. BPLOG(ERROR) << "Unable to read caller_lr from last_fp + 4: 0x"
  185. << std::hex << (last_fp + 4);
  186. return NULL;
  187. }
  188. u_int32_t caller_sp = last_fp ? last_fp + 8 :
  189. last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
  190. // Create a new stack frame (ownership will be transferred to the caller)
  191. // and fill it in.
  192. StackFrameARM *frame = new StackFrameARM();
  193. frame->trust = StackFrame::FRAME_TRUST_FP;
  194. frame->context = last_frame->context;
  195. frame->context.iregs[fp_register_] = caller_fp;
  196. frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
  197. frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
  198. last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
  199. frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = caller_lr;
  200. frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
  201. StackFrameARM::CONTEXT_VALID_LR |
  202. StackFrameARM::RegisterValidFlag(fp_register_) |
  203. StackFrameARM::CONTEXT_VALID_SP;
  204. return frame;
  205. }
  206. StackFrame* StackwalkerARM::GetCallerFrame(const CallStack *stack) {
  207. if (!memory_ || !stack) {
  208. BPLOG(ERROR) << "Can't get caller frame without memory or stack";
  209. return NULL;
  210. }
  211. const vector<StackFrame *> &frames = *stack->frames();
  212. StackFrameARM *last_frame = static_cast<StackFrameARM *>(frames.back());
  213. scoped_ptr<StackFrameARM> frame;
  214. // See if there is DWARF call frame information covering this address.
  215. scoped_ptr<CFIFrameInfo> cfi_frame_info(
  216. resolver_ ? resolver_->FindCFIFrameInfo(last_frame) : NULL);
  217. if (cfi_frame_info.get())
  218. frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
  219. // If CFI failed, or there wasn't CFI available, fall back
  220. // to frame pointer, if this is configured.
  221. if (fp_register_ >= 0 && !frame.get())
  222. frame.reset(GetCallerByFramePointer(frames));
  223. // If everuthing failed, fall back to stack scanning.
  224. if (!frame.get())
  225. frame.reset(GetCallerByStackScan(frames));
  226. // If nothing worked, tell the caller.
  227. if (!frame.get())
  228. return NULL;
  229. // An instruction address of zero marks the end of the stack.
  230. if (frame->context.iregs[MD_CONTEXT_ARM_REG_PC] == 0)
  231. return NULL;
  232. // If the new stack pointer is at a lower address than the old, then
  233. // that's clearly incorrect. Treat this as end-of-stack to enforce
  234. // progress and avoid infinite loops.
  235. if (frame->context.iregs[MD_CONTEXT_ARM_REG_SP]
  236. < last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP])
  237. return NULL;
  238. // The new frame's context's PC is the return address, which is one
  239. // instruction past the instruction that caused us to arrive at the
  240. // callee. Set new_frame->instruction to one less than the PC. This won't
  241. // reference the beginning of the call instruction, but it's at least
  242. // within it, which is sufficient to get the source line information to
  243. // match up with the line that contains the function call. Callers that
  244. // require the exact return address value may access
  245. // frame->context.iregs[MD_CONTEXT_ARM_REG_PC].
  246. frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC] - 2;
  247. return frame.release();
  248. }
  249. } // namespace google_breakpad