1 // Copyright (c) 2013 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_mips.cc: MIPS-specific stackwalker.
31 //
32 // See stackwalker_mips.h for documentation.
33 //
34 // Author: Tata Elxsi
35
36 #include "common/scoped_ptr.h"
37 #include "google_breakpad/processor/call_stack.h"
38 #include "google_breakpad/processor/code_modules.h"
39 #include "google_breakpad/processor/memory_region.h"
40 #include "google_breakpad/processor/source_line_resolver_interface.h"
41 #include "google_breakpad/processor/stack_frame_cpu.h"
42 #include "processor/cfi_frame_info.h"
43 #include "processor/logging.h"
44 #include "processor/postfix_evaluator-inl.h"
45 #include "processor/stackwalker_mips.h"
46 #include "processor/windows_frame_info.h"
47 #include "google_breakpad/common/minidump_cpu_mips.h"
48
49 namespace google_breakpad {
50
StackwalkerMIPS(const SystemInfo * system_info,const MDRawContextMIPS * context,MemoryRegion * memory,const CodeModules * modules,StackFrameSymbolizer * resolver_helper)51 StackwalkerMIPS::StackwalkerMIPS(const SystemInfo* system_info,
52 const MDRawContextMIPS* context,
53 MemoryRegion* memory,
54 const CodeModules* modules,
55 StackFrameSymbolizer* resolver_helper)
56 : Stackwalker(system_info, memory, modules, resolver_helper),
57 context_(context) {
58 if (memory_) {
59 if (context_->context_flags & MD_CONTEXT_MIPS64 ) {
60 if (0xffffffffffffffff - memory_->GetBase() < memory_->GetSize() - 1) {
61 BPLOG(ERROR) << "Memory out of range for stackwalking mips64: "
62 << HexString(memory_->GetBase())
63 << "+"
64 << HexString(memory_->GetSize());
65 memory_ = NULL;
66 }
67 } else {
68 if (0xffffffff - memory_->GetBase() < memory_->GetSize() - 1) {
69 BPLOG(ERROR) << "Memory out of range for stackwalking mips32: "
70 << HexString(memory_->GetBase())
71 << "+"
72 << HexString(memory_->GetSize());
73 memory_ = NULL;
74 }
75 }
76 }
77 }
78
GetContextFrame()79 StackFrame* StackwalkerMIPS::GetContextFrame() {
80 if (!context_) {
81 BPLOG(ERROR) << "Can't get context frame without context.";
82 return NULL;
83 }
84
85 StackFrameMIPS* frame = new StackFrameMIPS();
86
87 // The instruction pointer is stored directly in a register, so pull it
88 // straight out of the CPU context structure.
89 frame->context = *context_;
90 frame->context_validity = StackFrameMIPS::CONTEXT_VALID_ALL;
91 frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
92 frame->instruction = frame->context.epc;
93
94 return frame;
95 }
96
97 // Register names for mips.
98 static const char* const kRegisterNames[] = {
99 "$zero", "$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3", "$to", "$t1",
100 "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$s0", "$s1", "$s2", "$s3",
101 "$s4", "$s5", "$s6", "$s7", "$t8", "$t9", "$k0", "$k1", "$gp", "$sp",
102 "$fp", "$ra", NULL
103 // TODO(gordanac): add float point save registers
104 };
105
GetCallerByCFIFrameInfo(const vector<StackFrame * > & frames,CFIFrameInfo * cfi_frame_info)106 StackFrameMIPS* StackwalkerMIPS::GetCallerByCFIFrameInfo(
107 const vector<StackFrame*>& frames,
108 CFIFrameInfo* cfi_frame_info) {
109 StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
110
111 if (context_->context_flags & MD_CONTEXT_MIPS) {
112 uint32_t pc = 0;
113
114 // Populate a dictionary with the valid register values in last_frame.
115 CFIFrameInfo::RegisterValueMap<uint32_t> callee_registers;
116 // Use the STACK CFI data to recover the caller's register values.
117 CFIFrameInfo::RegisterValueMap<uint32_t> caller_registers;
118
119 for (int i = 0; kRegisterNames[i]; ++i) {
120 caller_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
121 callee_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
122 }
123
124 if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
125 &caller_registers)) {
126 return NULL;
127 }
128
129 CFIFrameInfo::RegisterValueMap<uint32_t>::const_iterator entry =
130 caller_registers.find(".cfa");
131
132 if (entry != caller_registers.end()) {
133 caller_registers["$sp"] = entry->second;
134 }
135
136 entry = caller_registers.find(".ra");
137 if (entry != caller_registers.end()) {
138 caller_registers["$ra"] = entry->second;
139 pc = entry->second - 2 * sizeof(pc);
140 }
141 caller_registers["$pc"] = pc;
142 // Construct a new stack frame given the values the CFI recovered.
143 scoped_ptr<StackFrameMIPS> frame(new StackFrameMIPS());
144
145 for (int i = 0; kRegisterNames[i]; ++i) {
146 CFIFrameInfo::RegisterValueMap<uint32_t>::const_iterator caller_entry =
147 caller_registers.find(kRegisterNames[i]);
148
149 if (caller_entry != caller_registers.end()) {
150 // The value of this register is recovered; fill the context with the
151 // value from caller_registers.
152 frame->context.iregs[i] = caller_entry->second;
153 frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
154 } else if (((i >= INDEX_MIPS_REG_S0 && i <= INDEX_MIPS_REG_S7) ||
155 (i > INDEX_MIPS_REG_GP && i < INDEX_MIPS_REG_RA)) &&
156 (last_frame->context_validity &
157 StackFrameMIPS::RegisterValidFlag(i))) {
158 // If the STACK CFI data doesn't mention some callee-save register, and
159 // it is valid in the callee, assume the callee has not yet changed it.
160 // Calee-save registers according to the MIPS o32 ABI specification are:
161 // $s0 to $s7
162 // $sp, $s8
163 frame->context.iregs[i] = last_frame->context.iregs[i];
164 frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
165 }
166 }
167
168 frame->context.epc = caller_registers["$pc"];
169 frame->instruction = caller_registers["$pc"];
170 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
171
172 frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] = caller_registers["$ra"];
173 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
174
175 frame->trust = StackFrame::FRAME_TRUST_CFI;
176
177 return frame.release();
178 } else {
179 uint64_t pc = 0;
180
181 // Populate a dictionary with the valid register values in last_frame.
182 CFIFrameInfo::RegisterValueMap<uint64_t> callee_registers;
183 // Use the STACK CFI data to recover the caller's register values.
184 CFIFrameInfo::RegisterValueMap<uint64_t> caller_registers;
185
186 for (int i = 0; kRegisterNames[i]; ++i) {
187 caller_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
188 callee_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
189 }
190
191 if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
192 &caller_registers)) {
193 return NULL;
194 }
195
196 CFIFrameInfo::RegisterValueMap<uint64_t>::const_iterator entry =
197 caller_registers.find(".cfa");
198
199 if (entry != caller_registers.end()) {
200 caller_registers["$sp"] = entry->second;
201 }
202
203 entry = caller_registers.find(".ra");
204 if (entry != caller_registers.end()) {
205 caller_registers["$ra"] = entry->second;
206 pc = entry->second - 2 * sizeof(pc);
207 }
208 caller_registers["$pc"] = pc;
209 // Construct a new stack frame given the values the CFI recovered.
210 scoped_ptr<StackFrameMIPS> frame(new StackFrameMIPS());
211
212 for (int i = 0; kRegisterNames[i]; ++i) {
213 CFIFrameInfo::RegisterValueMap<uint64_t>::const_iterator caller_entry =
214 caller_registers.find(kRegisterNames[i]);
215
216 if (caller_entry != caller_registers.end()) {
217 // The value of this register is recovered; fill the context with the
218 // value from caller_registers.
219 frame->context.iregs[i] = caller_entry->second;
220 frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
221 } else if (((i >= INDEX_MIPS_REG_S0 && i <= INDEX_MIPS_REG_S7) ||
222 (i >= INDEX_MIPS_REG_GP && i < INDEX_MIPS_REG_RA)) &&
223 (last_frame->context_validity &
224 StackFrameMIPS::RegisterValidFlag(i))) {
225 // If the STACK CFI data doesn't mention some callee-save register, and
226 // it is valid in the callee, assume the callee has not yet changed it.
227 // Calee-save registers according to the MIPS o32 ABI specification are:
228 // $s0 to $s7
229 // $sp, $s8
230 frame->context.iregs[i] = last_frame->context.iregs[i];
231 frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
232 }
233 }
234
235 frame->context.epc = caller_registers["$pc"];
236 frame->instruction = caller_registers["$pc"];
237 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
238
239 frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] = caller_registers["$ra"];
240 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
241
242 frame->trust = StackFrame::FRAME_TRUST_CFI;
243
244 return frame.release();
245 }
246 }
247
GetCallerFrame(const CallStack * stack,bool stack_scan_allowed)248 StackFrame* StackwalkerMIPS::GetCallerFrame(const CallStack* stack,
249 bool stack_scan_allowed) {
250 if (!memory_ || !stack) {
251 BPLOG(ERROR) << "Can't get caller frame without memory or stack";
252 return NULL;
253 }
254
255 const vector<StackFrame*>& frames = *stack->frames();
256 StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
257 scoped_ptr<StackFrameMIPS> new_frame;
258
259 // See if there is DWARF call frame information covering this address.
260 scoped_ptr<CFIFrameInfo> cfi_frame_info(
261 frame_symbolizer_->FindCFIFrameInfo(last_frame));
262 if (cfi_frame_info.get())
263 new_frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
264
265 // If caller frame is not found in CFI try analyzing the stack.
266 if (stack_scan_allowed && !new_frame.get()) {
267 new_frame.reset(GetCallerByStackScan(frames));
268 }
269
270 // If nothing worked, tell the caller.
271 if (!new_frame.get()) {
272 return NULL;
273 }
274
275 // Should we terminate the stack walk? (end-of-stack or broken invariant)
276 if (TerminateWalk(new_frame->context.epc,
277 new_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP],
278 last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP],
279 frames.size() == 1)) {
280 return NULL;
281 }
282
283 return new_frame.release();
284 }
285
GetCallerByStackScan(const vector<StackFrame * > & frames)286 StackFrameMIPS* StackwalkerMIPS::GetCallerByStackScan(
287 const vector<StackFrame*>& frames) {
288 const uint32_t kMaxFrameStackSize = 1024;
289 const uint32_t kMinArgsOnStack = 4;
290
291 StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
292
293 if (context_->context_flags & MD_CONTEXT_MIPS) {
294 uint32_t last_sp = last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP];
295 uint32_t caller_pc, caller_sp, caller_fp;
296
297 // Return address cannot be obtained directly.
298 // Force stackwalking.
299
300 // We cannot use frame pointer to get the return address.
301 // We'll scan the stack for a
302 // return address. This can happen if last_frame is executing code
303 // for a module for which we don't have symbols.
304 int count = kMaxFrameStackSize / sizeof(caller_pc);
305
306 if (frames.size() > 1) {
307 // In case of mips32 ABI stack frame of a nonleaf function
308 // must have minimum stack frame assigned for 4 arguments (4 words).
309 // Move stack pointer for 4 words to avoid reporting non-existing frames
310 // for all frames except the topmost one.
311 // There is no way of knowing if topmost frame belongs to a leaf or
312 // a nonleaf function.
313 last_sp += kMinArgsOnStack * sizeof(caller_pc);
314 // Adjust 'count' so that return address is scanned only in limits
315 // of one stack frame.
316 count -= kMinArgsOnStack;
317 }
318
319 do {
320 // Scanning for return address from stack pointer of the last frame.
321 if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc, count)) {
322 // If we can't find an instruction pointer even with stack scanning,
323 // give up.
324 BPLOG(ERROR) << " ScanForReturnAddress failed ";
325 return NULL;
326 }
327 // Get $fp stored in the stack frame.
328 if (!memory_->GetMemoryAtAddress(caller_sp - sizeof(caller_pc),
329 &caller_fp)) {
330 BPLOG(INFO) << " GetMemoryAtAddress for fp failed " ;
331 return NULL;
332 }
333
334 count = count - (caller_sp - last_sp) / sizeof(caller_pc);
335 // Now scan the next address in the stack.
336 last_sp = caller_sp + sizeof(caller_pc);
337 } while ((caller_fp - caller_sp >= kMaxFrameStackSize) && count > 0);
338
339 if (!count) {
340 BPLOG(INFO) << " No frame found " ;
341 return NULL;
342 }
343
344 // ScanForReturnAddress found a reasonable return address. Advance
345 // $sp to the location above the one where the return address was
346 // found.
347 caller_sp += sizeof(caller_pc);
348 // caller_pc is actually containing $ra value;
349 // $pc is two instructions before $ra,
350 // so the caller_pc needs to be decremented accordingly.
351 caller_pc -= 2 * sizeof(caller_pc);
352
353 // Create a new stack frame (ownership will be transferred to the caller)
354 // and fill it in.
355 StackFrameMIPS* frame = new StackFrameMIPS();
356 frame->trust = StackFrame::FRAME_TRUST_SCAN;
357 frame->context = last_frame->context;
358 frame->context.epc = caller_pc;
359 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
360 frame->instruction = caller_pc;
361
362 frame->context.iregs[MD_CONTEXT_MIPS_REG_SP] = caller_sp;
363 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_SP;
364 frame->context.iregs[MD_CONTEXT_MIPS_REG_FP] = caller_fp;
365 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_FP;
366
367 frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] =
368 caller_pc + 2 * sizeof(caller_pc);
369 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
370
371 return frame;
372 } else {
373 uint64_t last_sp = last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP];
374 uint64_t caller_pc, caller_sp, caller_fp;
375
376 // Return address cannot be obtained directly.
377 // Force stackwalking.
378
379 // We cannot use frame pointer to get the return address.
380 // We'll scan the stack for a
381 // return address. This can happen if last_frame is executing code
382 // for a module for which we don't have symbols.
383 int count = kMaxFrameStackSize / sizeof(caller_pc);
384
385 do {
386 // Scanning for return address from stack pointer of the last frame.
387 if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc, count)) {
388 // If we can't find an instruction pointer even with stack scanning,
389 // give up.
390 BPLOG(ERROR) << " ScanForReturnAddress failed ";
391 return NULL;
392 }
393 // Get $fp stored in the stack frame.
394 if (!memory_->GetMemoryAtAddress(caller_sp - sizeof(caller_pc),
395 &caller_fp)) {
396 BPLOG(INFO) << " GetMemoryAtAddress for fp failed " ;
397 return NULL;
398 }
399
400 count = count - (caller_sp - last_sp) / sizeof(caller_pc);
401 // Now scan the next address in the stack.
402 last_sp = caller_sp + sizeof(caller_pc);
403 } while ((caller_fp - caller_sp >= kMaxFrameStackSize) && count > 0);
404
405 if (!count) {
406 BPLOG(INFO) << " No frame found " ;
407 return NULL;
408 }
409
410 // ScanForReturnAddress found a reasonable return address. Advance
411 // $sp to the location above the one where the return address was
412 // found.
413 caller_sp += sizeof(caller_pc);
414 // caller_pc is actually containing $ra value;
415 // $pc is two instructions before $ra,
416 // so the caller_pc needs to be decremented accordingly.
417 caller_pc -= 2 * sizeof(caller_pc);
418
419 // Create a new stack frame (ownership will be transferred to the caller)
420 // and fill it in.
421 StackFrameMIPS* frame = new StackFrameMIPS();
422 frame->trust = StackFrame::FRAME_TRUST_SCAN;
423 frame->context = last_frame->context;
424 frame->context.epc = caller_pc;
425 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
426 frame->instruction = caller_pc;
427
428 frame->context.iregs[MD_CONTEXT_MIPS_REG_SP] = caller_sp;
429 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_SP;
430 frame->context.iregs[MD_CONTEXT_MIPS_REG_FP] = caller_fp;
431 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_FP;
432
433 frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] =
434 caller_pc + 2 * sizeof(caller_pc);
435 frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
436
437 return frame;
438 }
439 }
440
441 } // namespace google_breakpad
442
443