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
30 // stackwalker_arm.cc: arm-specific stackwalker.
31 //
32 // See stackwalker_arm.h for documentation.
33 //
34 // Author: Mark Mentovai, Ted Mielczarek, Jim Blandy
35
36 #include <vector>
37
38 #include "common/scoped_ptr.h"
39 #include "google_breakpad/processor/call_stack.h"
40 #include "google_breakpad/processor/memory_region.h"
41 #include "google_breakpad/processor/source_line_resolver_interface.h"
42 #include "google_breakpad/processor/stack_frame_cpu.h"
43 #include "google_breakpad/processor/system_info.h"
44 #include "processor/cfi_frame_info.h"
45 #include "processor/logging.h"
46 #include "processor/stackwalker_arm.h"
47
48 namespace google_breakpad {
49
50
StackwalkerARM(const SystemInfo * system_info,const MDRawContextARM * context,int fp_register,MemoryRegion * memory,const CodeModules * modules,StackFrameSymbolizer * resolver_helper)51 StackwalkerARM::StackwalkerARM(const SystemInfo* system_info,
52 const MDRawContextARM* context,
53 int fp_register,
54 MemoryRegion* memory,
55 const CodeModules* modules,
56 StackFrameSymbolizer* resolver_helper)
57 : Stackwalker(system_info, memory, modules, resolver_helper),
58 context_(context), fp_register_(fp_register),
59 context_frame_validity_(StackFrameARM::CONTEXT_VALID_ALL) { }
60
61
GetContextFrame()62 StackFrame* StackwalkerARM::GetContextFrame() {
63 if (!context_) {
64 BPLOG(ERROR) << "Can't get context frame without context";
65 return NULL;
66 }
67
68 StackFrameARM* frame = new StackFrameARM();
69
70 // The instruction pointer is stored directly in a register (r15), so pull it
71 // straight out of the CPU context structure.
72 frame->context = *context_;
73 frame->context_validity = context_frame_validity_;
74 frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
75 frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC];
76
77 return frame;
78 }
79
GetCallerByCFIFrameInfo(const vector<StackFrame * > & frames,CFIFrameInfo * cfi_frame_info)80 StackFrameARM* StackwalkerARM::GetCallerByCFIFrameInfo(
81 const vector<StackFrame*> &frames,
82 CFIFrameInfo* cfi_frame_info) {
83 StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
84
85 static const char* register_names[] = {
86 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
87 "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc",
88 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
89 "fps", "cpsr",
90 NULL
91 };
92
93 // Populate a dictionary with the valid register values in last_frame.
94 CFIFrameInfo::RegisterValueMap<uint32_t> callee_registers;
95 for (int i = 0; register_names[i]; i++)
96 if (last_frame->context_validity & StackFrameARM::RegisterValidFlag(i))
97 callee_registers[register_names[i]] = last_frame->context.iregs[i];
98
99 // Use the STACK CFI data to recover the caller's register values.
100 CFIFrameInfo::RegisterValueMap<uint32_t> caller_registers;
101 if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
102 &caller_registers))
103 return NULL;
104
105 // Construct a new stack frame given the values the CFI recovered.
106 scoped_ptr<StackFrameARM> frame(new StackFrameARM());
107 for (int i = 0; register_names[i]; i++) {
108 CFIFrameInfo::RegisterValueMap<uint32_t>::iterator entry =
109 caller_registers.find(register_names[i]);
110 if (entry != caller_registers.end()) {
111 // We recovered the value of this register; fill the context with the
112 // value from caller_registers.
113 frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
114 frame->context.iregs[i] = entry->second;
115 } else if (4 <= i && i <= 11 && (last_frame->context_validity &
116 StackFrameARM::RegisterValidFlag(i))) {
117 // If the STACK CFI data doesn't mention some callee-saves register, and
118 // it is valid in the callee, assume the callee has not yet changed it.
119 // Registers r4 through r11 are callee-saves, according to the Procedure
120 // Call Standard for the ARM Architecture, which the Linux ABI follows.
121 frame->context_validity |= StackFrameARM::RegisterValidFlag(i);
122 frame->context.iregs[i] = last_frame->context.iregs[i];
123 }
124 }
125 // If the CFI doesn't recover the PC explicitly, then use .ra.
126 if (!(frame->context_validity & StackFrameARM::CONTEXT_VALID_PC)) {
127 CFIFrameInfo::RegisterValueMap<uint32_t>::iterator entry =
128 caller_registers.find(".ra");
129 if (entry != caller_registers.end()) {
130 if (fp_register_ == -1) {
131 frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
132 frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = entry->second;
133 } else {
134 // The CFI updated the link register and not the program counter.
135 // Handle getting the program counter from the link register.
136 frame->context_validity |= StackFrameARM::CONTEXT_VALID_PC;
137 frame->context_validity |= StackFrameARM::CONTEXT_VALID_LR;
138 frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = entry->second;
139 frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
140 last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
141 }
142 }
143 }
144 // If the CFI doesn't recover the SP explicitly, then use .cfa.
145 if (!(frame->context_validity & StackFrameARM::CONTEXT_VALID_SP)) {
146 CFIFrameInfo::RegisterValueMap<uint32_t>::iterator entry =
147 caller_registers.find(".cfa");
148 if (entry != caller_registers.end()) {
149 frame->context_validity |= StackFrameARM::CONTEXT_VALID_SP;
150 frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = entry->second;
151 }
152 }
153
154 // If we didn't recover the PC and the SP, then the frame isn't very useful.
155 static const int essentials = (StackFrameARM::CONTEXT_VALID_SP
156 | StackFrameARM::CONTEXT_VALID_PC);
157 if ((frame->context_validity & essentials) != essentials)
158 return NULL;
159
160 frame->trust = StackFrame::FRAME_TRUST_CFI;
161 return frame.release();
162 }
163
GetCallerByStackScan(const vector<StackFrame * > & frames)164 StackFrameARM* StackwalkerARM::GetCallerByStackScan(
165 const vector<StackFrame*> &frames) {
166 StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
167 uint32_t last_sp = last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
168 uint32_t caller_sp, caller_pc;
169
170 if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc,
171 frames.size() == 1 /* is_context_frame */)) {
172 // No plausible return address was found.
173 return NULL;
174 }
175
176 // ScanForReturnAddress found a reasonable return address. Advance
177 // %sp to the location above the one where the return address was
178 // found.
179 caller_sp += 4;
180
181 // Create a new stack frame (ownership will be transferred to the caller)
182 // and fill it in.
183 StackFrameARM* frame = new StackFrameARM();
184
185 frame->trust = StackFrame::FRAME_TRUST_SCAN;
186 frame->context = last_frame->context;
187 frame->context.iregs[MD_CONTEXT_ARM_REG_PC] = caller_pc;
188 frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
189 frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
190 StackFrameARM::CONTEXT_VALID_SP;
191
192 return frame;
193 }
194
GetCallerByFramePointer(const vector<StackFrame * > & frames)195 StackFrameARM* StackwalkerARM::GetCallerByFramePointer(
196 const vector<StackFrame*> &frames) {
197 StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
198
199 if (!(last_frame->context_validity &
200 StackFrameARM::RegisterValidFlag(fp_register_))) {
201 return NULL;
202 }
203
204 uint32_t last_fp = last_frame->context.iregs[fp_register_];
205
206 uint32_t caller_fp = 0;
207 if (last_fp && !memory_->GetMemoryAtAddress(last_fp, &caller_fp)) {
208 BPLOG(ERROR) << "Unable to read caller_fp from last_fp: 0x"
209 << std::hex << last_fp;
210 return NULL;
211 }
212
213 uint32_t caller_lr = 0;
214 if (last_fp && !memory_->GetMemoryAtAddress(last_fp + 4, &caller_lr)) {
215 BPLOG(ERROR) << "Unable to read caller_lr from last_fp + 4: 0x"
216 << std::hex << (last_fp + 4);
217 return NULL;
218 }
219
220 uint32_t caller_sp = last_fp ? last_fp + 8 :
221 last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP];
222
223 // Create a new stack frame (ownership will be transferred to the caller)
224 // and fill it in.
225 StackFrameARM* frame = new StackFrameARM();
226
227 frame->trust = StackFrame::FRAME_TRUST_FP;
228 frame->context = last_frame->context;
229 frame->context.iregs[fp_register_] = caller_fp;
230 frame->context.iregs[MD_CONTEXT_ARM_REG_SP] = caller_sp;
231 frame->context.iregs[MD_CONTEXT_ARM_REG_PC] =
232 last_frame->context.iregs[MD_CONTEXT_ARM_REG_LR];
233 frame->context.iregs[MD_CONTEXT_ARM_REG_LR] = caller_lr;
234 frame->context_validity = StackFrameARM::CONTEXT_VALID_PC |
235 StackFrameARM::CONTEXT_VALID_LR |
236 StackFrameARM::RegisterValidFlag(fp_register_) |
237 StackFrameARM::CONTEXT_VALID_SP;
238 return frame;
239 }
240
GetCallerFrame(const CallStack * stack,bool stack_scan_allowed)241 StackFrame* StackwalkerARM::GetCallerFrame(const CallStack* stack,
242 bool stack_scan_allowed) {
243 if (!memory_ || !stack) {
244 BPLOG(ERROR) << "Can't get caller frame without memory or stack";
245 return NULL;
246 }
247
248 const vector<StackFrame*> &frames = *stack->frames();
249 StackFrameARM* last_frame = static_cast<StackFrameARM*>(frames.back());
250 scoped_ptr<StackFrameARM> frame;
251
252 // See if there is DWARF call frame information covering this address.
253 // TODO(jperaza): Ignore iOS CFI info until it is properly collected.
254 // https://bugs.chromium.org/p/google-breakpad/issues/detail?id=764
255 if (!system_info_ || system_info_->os != "iOS") {
256 scoped_ptr<CFIFrameInfo> cfi_frame_info(
257 frame_symbolizer_->FindCFIFrameInfo(last_frame));
258 if (cfi_frame_info.get())
259 frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
260 }
261
262 // If CFI failed, or there wasn't CFI available, fall back
263 // to frame pointer, if this is configured.
264 if (fp_register_ >= 0 && !frame.get())
265 frame.reset(GetCallerByFramePointer(frames));
266
267 // If everuthing failed, fall back to stack scanning.
268 if (stack_scan_allowed && !frame.get())
269 frame.reset(GetCallerByStackScan(frames));
270
271 // If nothing worked, tell the caller.
272 if (!frame.get())
273 return NULL;
274
275 // Should we terminate the stack walk? (end-of-stack or broken invariant)
276 if (TerminateWalk(frame->context.iregs[MD_CONTEXT_ARM_REG_PC],
277 frame->context.iregs[MD_CONTEXT_ARM_REG_SP],
278 last_frame->context.iregs[MD_CONTEXT_ARM_REG_SP],
279 frames.size() == 1)) {
280 return NULL;
281 }
282
283 // The new frame's context's PC is the return address, which is one
284 // instruction past the instruction that caused us to arrive at the
285 // callee. Set new_frame->instruction to one less than the PC. This won't
286 // reference the beginning of the call instruction, but it's at least
287 // within it, which is sufficient to get the source line information to
288 // match up with the line that contains the function call. Callers that
289 // require the exact return address value may access
290 // frame->context.iregs[MD_CONTEXT_ARM_REG_PC].
291 frame->instruction = frame->context.iregs[MD_CONTEXT_ARM_REG_PC] - 2;
292
293 return frame.release();
294 }
295
296
297 } // namespace google_breakpad
298