/ src / processor / stackwalker_sparc.cc
stackwalker_sparc.cc
  1  // Copyright 2010 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  // stackwalker_sparc.cc: sparc-specific stackwalker.
 30  //
 31  // See stackwalker_sparc.h for documentation.
 32  //
 33  // Author: Michael Shang
 34  
 35  
 36  #ifdef HAVE_CONFIG_H
 37  #include <config.h>  // Must come first
 38  #endif
 39  
 40  #include "google_breakpad/processor/call_stack.h"
 41  #include "google_breakpad/processor/memory_region.h"
 42  #include "google_breakpad/processor/stack_frame_cpu.h"
 43  #include "processor/logging.h"
 44  #include "processor/stackwalker_sparc.h"
 45  
 46  namespace google_breakpad {
 47  
 48  
 49  StackwalkerSPARC::StackwalkerSPARC(const SystemInfo* system_info,
 50                                     const MDRawContextSPARC* context,
 51                                     MemoryRegion* memory,
 52                                     const CodeModules* modules,
 53                                     StackFrameSymbolizer* resolver_helper)
 54      : Stackwalker(system_info, memory, modules, resolver_helper),
 55        context_(context) {
 56  }
 57  
 58  
 59  StackFrame* StackwalkerSPARC::GetContextFrame() {
 60    if (!context_) {
 61      BPLOG(ERROR) << "Can't get context frame without context";
 62      return NULL;
 63    }
 64  
 65    StackFrameSPARC* frame = new StackFrameSPARC();
 66  
 67    // The instruction pointer is stored directly in a register, so pull it
 68    // straight out of the CPU context structure.
 69    frame->context = *context_;
 70    frame->context_validity = StackFrameSPARC::CONTEXT_VALID_ALL;
 71    frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
 72    frame->instruction = frame->context.pc;
 73  
 74    return frame;
 75  }
 76  
 77  
 78  StackFrame* StackwalkerSPARC::GetCallerFrame(const CallStack* stack,
 79                                               bool stack_scan_allowed) {
 80    if (!memory_ || !stack) {
 81      BPLOG(ERROR) << "Can't get caller frame without memory or stack";
 82      return NULL;
 83    }
 84  
 85    StackFrameSPARC* last_frame = static_cast<StackFrameSPARC*>(
 86        stack->frames()->back());
 87  
 88    // new: caller
 89    // old: callee
 90    // %fp, %i6 and g_r[30] is the same, see minidump_format.h
 91    // %sp, %o6 and g_r[14] is the same, see minidump_format.h
 92    // %sp_new = %fp_old
 93    // %fp_new = *(%fp_old + 32 + 32 - 8), where the callee's %i6
 94    // %pc_new = *(%fp_old + 32 + 32 - 4) + 8
 95    // which is callee's %i7 plus 8
 96  
 97    // A caller frame must reside higher in memory than its callee frames.
 98    // Anything else is an error, or an indication that we've reached the
 99    // end of the stack.
100    uint64_t stack_pointer = last_frame->context.g_r[30];
101    if (stack_pointer <= last_frame->context.g_r[14]) {
102      return NULL;
103    }
104  
105    uint32_t instruction;
106    if (!memory_->GetMemoryAtAddress(stack_pointer + 60,
107                       &instruction) || instruction <= 1) {
108      return NULL;
109    }
110  
111    uint32_t stack_base;
112    if (!memory_->GetMemoryAtAddress(stack_pointer + 56,
113                       &stack_base) || stack_base <= 1) {
114      return NULL;
115    }
116  
117    // Should we terminate the stack walk? (end-of-stack or broken invariant)
118    if (TerminateWalk(instruction, stack_pointer, last_frame->context.g_r[14],
119                      /*is_context_frame=*/last_frame->trust ==
120                          StackFrame::FRAME_TRUST_CONTEXT)) {
121      return NULL;
122    }
123  
124    StackFrameSPARC* frame = new StackFrameSPARC();
125  
126    frame->context = last_frame->context;
127    frame->context.g_r[14] = stack_pointer;
128    frame->context.g_r[30] = stack_base;
129  
130    // frame->context.pc is the return address, which is 2 instruction
131    // past the branch that caused us to arrive at the callee, which are
132    // a CALL instruction then a NOP instruction.
133    // frame_ppc->instruction to 8 less than that.  Since all sparc
134    // instructions are 4 bytes wide, this is the address of the branch
135    // instruction.  This allows source line information to match up with the
136    // line that contains a function call.  Callers that require the exact
137    // return address value may access the %i7/g_r[31] field of StackFrameSPARC.
138    frame->context.pc = instruction + 8;
139    frame->instruction = instruction;
140    frame->context_validity = StackFrameSPARC::CONTEXT_VALID_PC |
141                              StackFrameSPARC::CONTEXT_VALID_SP |
142                              StackFrameSPARC::CONTEXT_VALID_FP;
143    frame->trust = StackFrame::FRAME_TRUST_FP;
144  
145    return frame;
146  }
147  
148  
149  }  // namespace google_breakpad