1 // Copyright (c) 2007, 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 
30 // stackwalker_amd64.cc: amd64-specific stackwalker.
31 //
32 // See stackwalker_amd64.h for documentation.
33 //
34 // Author: Mark Mentovai, Ted Mielczarek
35 
36 
37 #include "processor/stackwalker_amd64.h"
38 #include "google_breakpad/processor/call_stack.h"
39 #include "google_breakpad/processor/memory_region.h"
40 #include "google_breakpad/processor/stack_frame_cpu.h"
41 #include "processor/logging.h"
42 
43 namespace google_breakpad {
44 
45 
StackwalkerAMD64(const SystemInfo * system_info,const MDRawContextAMD64 * context,MemoryRegion * memory,const CodeModules * modules,SymbolSupplier * supplier,SourceLineResolverInterface * resolver)46 StackwalkerAMD64::StackwalkerAMD64(const SystemInfo *system_info,
47                                    const MDRawContextAMD64 *context,
48                                    MemoryRegion *memory,
49                                    const CodeModules *modules,
50                                    SymbolSupplier *supplier,
51                                    SourceLineResolverInterface *resolver)
52     : Stackwalker(system_info, memory, modules, supplier, resolver),
53       context_(context) {
54 }
55 
56 
GetContextFrame()57 StackFrame* StackwalkerAMD64::GetContextFrame() {
58   if (!context_ || !memory_) {
59     BPLOG(ERROR) << "Can't get context frame without context or memory";
60     return NULL;
61   }
62 
63   StackFrameAMD64 *frame = new StackFrameAMD64();
64 
65   // The instruction pointer is stored directly in a register, so pull it
66   // straight out of the CPU context structure.
67   frame->context = *context_;
68   frame->context_validity = StackFrameAMD64::CONTEXT_VALID_ALL;
69   frame->instruction = frame->context.rip;
70 
71   return frame;
72 }
73 
74 
GetCallerFrame(const CallStack * stack,const vector<linked_ptr<StackFrameInfo>> & stack_frame_info)75 StackFrame* StackwalkerAMD64::GetCallerFrame(
76     const CallStack *stack,
77     const vector< linked_ptr<StackFrameInfo> > &stack_frame_info) {
78   if (!memory_ || !stack) {
79     BPLOG(ERROR) << "Can't get caller frame without memory or stack";
80     return NULL;
81   }
82 
83   StackFrameAMD64 *last_frame = static_cast<StackFrameAMD64*>(
84       stack->frames()->back());
85 
86   //FIXME: this pretty much doesn't work at all due to FPO
87   // being enabled by default.
88   // Brain-dead stackwalking:
89   // %rip_new = *(%rbp_old + 8)
90   // %rsp_new = %rbp_old + 16
91   // %rbp_new = *(%rbp_old)
92 
93   // A caller frame must reside higher in memory than its callee frames.
94   // Anything else is an error, or an indication that we've reached the
95   // end of the stack.
96   u_int64_t stack_pointer = last_frame->context.rbp + 16;
97   if (stack_pointer <= last_frame->context.rsp) {
98     return NULL;
99   }
100 
101   u_int64_t instruction;
102   if (!memory_->GetMemoryAtAddress(last_frame->context.rbp + 8,
103                                    &instruction) ||
104       instruction <= 1) {
105     return NULL;
106   }
107 
108   u_int64_t stack_base;
109   if (!memory_->GetMemoryAtAddress(last_frame->context.rbp,
110                                    &stack_base) ||
111       stack_base <= 1) {
112     return NULL;
113   }
114 
115   StackFrameAMD64 *frame = new StackFrameAMD64();
116 
117   frame->context = last_frame->context;
118   frame->context.rip = instruction;
119   frame->context.rsp = stack_pointer;
120   frame->context.rbp = stack_base;
121   frame->context_validity = StackFrameAMD64::CONTEXT_VALID_RIP |
122                             StackFrameAMD64::CONTEXT_VALID_RSP |
123                             StackFrameAMD64::CONTEXT_VALID_RBP;
124 
125   frame->instruction = frame->context.rip - 1;
126 
127   return frame;
128 }
129 
130 
131 }  // namespace google_breakpad
132