/thirdparty/breakpad/processor/stackwalker_sparc.cc

http://github.com/tomahawk-player/tomahawk · C++ · 139 lines · 61 code · 24 blank · 54 comment · 9 complexity · 027c9ae2f34fe2f1689b6748cd4e6804 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_sparc.cc: sparc-specific stackwalker.
  30. //
  31. // See stackwalker_sparc.h for documentation.
  32. //
  33. // Author: Michael Shang
  34. #include "google_breakpad/processor/call_stack.h"
  35. #include "google_breakpad/processor/memory_region.h"
  36. #include "google_breakpad/processor/stack_frame_cpu.h"
  37. #include "processor/logging.h"
  38. #include "processor/stackwalker_sparc.h"
  39. namespace google_breakpad {
  40. StackwalkerSPARC::StackwalkerSPARC(const SystemInfo *system_info,
  41. const MDRawContextSPARC *context,
  42. MemoryRegion *memory,
  43. const CodeModules *modules,
  44. SymbolSupplier *supplier,
  45. SourceLineResolverInterface *resolver)
  46. : Stackwalker(system_info, memory, modules, supplier, resolver),
  47. context_(context) {
  48. }
  49. StackFrame* StackwalkerSPARC::GetContextFrame() {
  50. if (!context_ || !memory_) {
  51. BPLOG(ERROR) << "Can't get context frame without context or memory";
  52. return NULL;
  53. }
  54. StackFrameSPARC *frame = new StackFrameSPARC();
  55. // The instruction pointer is stored directly in a register, so pull it
  56. // straight out of the CPU context structure.
  57. frame->context = *context_;
  58. frame->context_validity = StackFrameSPARC::CONTEXT_VALID_ALL;
  59. frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
  60. frame->instruction = frame->context.pc;
  61. return frame;
  62. }
  63. StackFrame* StackwalkerSPARC::GetCallerFrame(const CallStack *stack) {
  64. if (!memory_ || !stack) {
  65. BPLOG(ERROR) << "Can't get caller frame without memory or stack";
  66. return NULL;
  67. }
  68. StackFrameSPARC *last_frame = static_cast<StackFrameSPARC*>(
  69. stack->frames()->back());
  70. // new: caller
  71. // old: callee
  72. // %fp, %i6 and g_r[30] is the same, see minidump_format.h
  73. // %sp, %o6 and g_r[14] is the same, see minidump_format.h
  74. // %sp_new = %fp_old
  75. // %fp_new = *(%fp_old + 32 + 32 - 8), where the callee's %i6
  76. // %pc_new = *(%fp_old + 32 + 32 - 4) + 8
  77. // which is callee's %i7 plus 8
  78. // A caller frame must reside higher in memory than its callee frames.
  79. // Anything else is an error, or an indication that we've reached the
  80. // end of the stack.
  81. u_int64_t stack_pointer = last_frame->context.g_r[30];
  82. if (stack_pointer <= last_frame->context.g_r[14]) {
  83. return NULL;
  84. }
  85. u_int32_t instruction;
  86. if (!memory_->GetMemoryAtAddress(stack_pointer + 60,
  87. &instruction) || instruction <= 1) {
  88. return NULL;
  89. }
  90. u_int32_t stack_base;
  91. if (!memory_->GetMemoryAtAddress(stack_pointer + 56,
  92. &stack_base) || stack_base <= 1) {
  93. return NULL;
  94. }
  95. StackFrameSPARC *frame = new StackFrameSPARC();
  96. frame->context = last_frame->context;
  97. frame->context.g_r[14] = stack_pointer;
  98. frame->context.g_r[30] = stack_base;
  99. // frame->context.pc is the return address, which is 2 instruction
  100. // past the branch that caused us to arrive at the callee, which are
  101. // a CALL instruction then a NOP instruction.
  102. // frame_ppc->instruction to 8 less than that. Since all sparc
  103. // instructions are 4 bytes wide, this is the address of the branch
  104. // instruction. This allows source line information to match up with the
  105. // line that contains a function call. Callers that require the exact
  106. // return address value may access the %i7/g_r[31] field of StackFrameSPARC.
  107. frame->context.pc = instruction + 8;
  108. frame->instruction = instruction;
  109. frame->context_validity = StackFrameSPARC::CONTEXT_VALID_PC |
  110. StackFrameSPARC::CONTEXT_VALID_SP |
  111. StackFrameSPARC::CONTEXT_VALID_FP;
  112. frame->trust = StackFrame::FRAME_TRUST_FP;
  113. return frame;
  114. }
  115. } // namespace google_breakpad