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// stackwalk_common.cc: Module shared by the {micro,mini}dump_stackwalck
31// executables to print the content of dumps (w/ stack traces) on the console.
32//
33// Author: Mark Mentovai
34
35#include "processor/stackwalk_common.h"
36
37#include <assert.h>
38#include <stdlib.h>
39#include <string.h>
40
41#include <string>
42#include <vector>
43
44#include "common/stdio_wrapper.h"
45#include "common/using_std_string.h"
46#include "google_breakpad/processor/call_stack.h"
47#include "google_breakpad/processor/code_module.h"
48#include "google_breakpad/processor/code_modules.h"
49#include "google_breakpad/processor/process_state.h"
50#include "google_breakpad/processor/source_line_resolver_interface.h"
51#include "google_breakpad/processor/stack_frame_cpu.h"
52#include "processor/logging.h"
53#include "processor/pathname_stripper.h"
54
55namespace google_breakpad {
56
57namespace {
58
59using std::vector;
60using std::unique_ptr;
61
62// Separator character for machine readable output.
63static const char kOutputSeparator = '|';
64
65// PrintRegister prints a register's name and value to stdout. It will
66// print four registers on a line. For the first register in a set,
67// pass 0 for |start_col|. For registers in a set, pass the most recent
68// return value of PrintRegister.
69// The caller is responsible for printing the final newline after a set
70// of registers is completely printed, regardless of the number of calls
71// to PrintRegister.
72static const int kMaxWidth = 80; // optimize for an 80-column terminal
73static int PrintRegister(const char* name, uint32_t value, int start_col) {
74 char buffer[64];
75 snprintf(buffer, sizeof(buffer), " %5s = 0x%08x", name, value);
76
77 if (start_col + static_cast<ssize_t>(strlen(buffer)) > kMaxWidth) {
78 start_col = 0;
79 printf("\n ");
80 }
81 fputs(buffer, stdout);
82
83 return start_col + strlen(buffer);
84}
85
86// PrintRegister64 does the same thing, but for 64-bit registers.
87static int PrintRegister64(const char* name, uint64_t value, int start_col) {
88 char buffer[64];
89 snprintf(buffer, sizeof(buffer), " %5s = 0x%016" PRIx64 , name, value);
90
91 if (start_col + static_cast<ssize_t>(strlen(buffer)) > kMaxWidth) {
92 start_col = 0;
93 printf("\n ");
94 }
95 fputs(buffer, stdout);
96
97 return start_col + strlen(buffer);
98}
99
100// StripSeparator takes a string |original| and returns a copy
101// of the string with all occurences of |kOutputSeparator| removed.
102static string StripSeparator(const string& original) {
103 string result = original;
104 string::size_type position = 0;
105 while ((position = result.find(kOutputSeparator, position)) != string::npos) {
106 result.erase(position, 1);
107 }
108 position = 0;
109 while ((position = result.find('\n', position)) != string::npos) {
110 result.erase(position, 1);
111 }
112 return result;
113}
114
115// PrintStackContents prints the stack contents of the current frame to stdout.
116static void PrintStackContents(const string& indent,
117 const StackFrame* frame,
118 const StackFrame* prev_frame,
119 const string& cpu,
120 const MemoryRegion* memory,
121 const CodeModules* modules,
122 SourceLineResolverInterface* resolver) {
123 // Find stack range.
124 int word_length = 0;
125 uint64_t stack_begin = 0, stack_end = 0;
126 if (cpu == "x86") {
127 word_length = 4;
128 const StackFrameX86* frame_x86 = static_cast<const StackFrameX86*>(frame);
129 const StackFrameX86* prev_frame_x86 =
130 static_cast<const StackFrameX86*>(prev_frame);
131 if ((frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESP) &&
132 (prev_frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESP)) {
133 stack_begin = frame_x86->context.esp;
134 stack_end = prev_frame_x86->context.esp;
135 }
136 } else if (cpu == "amd64") {
137 word_length = 8;
138 const StackFrameAMD64* frame_amd64 =
139 static_cast<const StackFrameAMD64*>(frame);
140 const StackFrameAMD64* prev_frame_amd64 =
141 static_cast<const StackFrameAMD64*>(prev_frame);
142 if ((frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RSP) &&
143 (prev_frame_amd64->context_validity &
144 StackFrameAMD64::CONTEXT_VALID_RSP)) {
145 stack_begin = frame_amd64->context.rsp;
146 stack_end = prev_frame_amd64->context.rsp;
147 }
148 } else if (cpu == "arm") {
149 word_length = 4;
150 const StackFrameARM* frame_arm = static_cast<const StackFrameARM*>(frame);
151 const StackFrameARM* prev_frame_arm =
152 static_cast<const StackFrameARM*>(prev_frame);
153 if ((frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_SP) &&
154 (prev_frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_SP)) {
155 stack_begin = frame_arm->context.iregs[13];
156 stack_end = prev_frame_arm->context.iregs[13];
157 }
158 } else if (cpu == "arm64") {
159 word_length = 8;
160 const StackFrameARM64* frame_arm64 =
161 static_cast<const StackFrameARM64*>(frame);
162 const StackFrameARM64* prev_frame_arm64 =
163 static_cast<const StackFrameARM64*>(prev_frame);
164 if ((frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_SP) &&
165 (prev_frame_arm64->context_validity &
166 StackFrameARM64::CONTEXT_VALID_SP)) {
167 stack_begin = frame_arm64->context.iregs[31];
168 stack_end = prev_frame_arm64->context.iregs[31];
169 }
170 }
171 if (!word_length || !stack_begin || !stack_end)
172 return;
173
174 // Print stack contents.
175 printf("\n%sStack contents:", indent.c_str());
176 for(uint64_t address = stack_begin; address < stack_end; ) {
177 // Print the start address of this row.
178 if (word_length == 4)
179 printf("\n%s %08x", indent.c_str(), static_cast<uint32_t>(address));
180 else
181 printf("\n%s %016" PRIx64, indent.c_str(), address);
182
183 // Print data in hex.
184 const int kBytesPerRow = 16;
185 string data_as_string;
186 for (int i = 0; i < kBytesPerRow; ++i, ++address) {
187 uint8_t value = 0;
188 if (address < stack_end &&
189 memory->GetMemoryAtAddress(address, &value)) {
190 printf(" %02x", value);
191 data_as_string.push_back(isprint(value) ? value : '.');
192 } else {
193 printf(" ");
194 data_as_string.push_back(' ');
195 }
196 }
197 // Print data as string.
198 printf(" %s", data_as_string.c_str());
199 }
200
201 // Try to find instruction pointers from stack.
202 printf("\n%sPossible instruction pointers:\n", indent.c_str());
203 for (uint64_t address = stack_begin; address < stack_end;
204 address += word_length) {
205 StackFrame pointee_frame;
206
207 // Read a word (possible instruction pointer) from stack.
208 if (word_length == 4) {
209 uint32_t data32 = 0;
210 memory->GetMemoryAtAddress(address, &data32);
211 pointee_frame.instruction = data32;
212 } else {
213 uint64_t data64 = 0;
214 memory->GetMemoryAtAddress(address, &data64);
215 pointee_frame.instruction = data64;
216 }
217 pointee_frame.module =
218 modules->GetModuleForAddress(pointee_frame.instruction);
219
220 // Try to look up the function name.
221 std::deque<unique_ptr<StackFrame>> inlined_frames;
222 if (pointee_frame.module)
223 resolver->FillSourceLineInfo(&pointee_frame, &inlined_frames);
224
225 // Print function name.
226 auto print_function_name = [&](StackFrame* frame) {
227 if (!frame->function_name.empty()) {
228 if (word_length == 4) {
229 printf("%s *(0x%08x) = 0x%08x", indent.c_str(),
230 static_cast<uint32_t>(address),
231 static_cast<uint32_t>(frame->instruction));
232 } else {
233 printf("%s *(0x%016" PRIx64 ") = 0x%016" PRIx64, indent.c_str(),
234 address, frame->instruction);
235 }
236 printf(
237 " <%s> [%s : %d + 0x%" PRIx64 "]\n", frame->function_name.c_str(),
238 PathnameStripper::File(frame->source_file_name).c_str(),
239 frame->source_line, frame->instruction - frame->source_line_base);
240 }
241 };
242 print_function_name(&pointee_frame);
243 for (unique_ptr<StackFrame> &frame : inlined_frames)
244 print_function_name(frame.get());
245 }
246 printf("\n");
247}
248
249// PrintStack prints the call stack in |stack| to stdout, in a reasonably
250// useful form. Module, function, and source file names are displayed if
251// they are available. The code offset to the base code address of the
252// source line, function, or module is printed, preferring them in that
253// order. If no source line, function, or module information is available,
254// an absolute code offset is printed.
255//
256// If |cpu| is a recognized CPU name, relevant register state for each stack
257// frame printed is also output, if available.
258static void PrintStack(const CallStack* stack,
259 const string& cpu,
260 bool output_stack_contents,
261 const MemoryRegion* memory,
262 const CodeModules* modules,
263 SourceLineResolverInterface* resolver) {
264 int frame_count = stack->frames()->size();
265 if (frame_count == 0) {
266 printf(" <no frames>\n");
267 }
268 for (int frame_index = 0; frame_index < frame_count; ++frame_index) {
269 const StackFrame* frame = stack->frames()->at(frame_index);
270 printf("%2d ", frame_index);
271
272 uint64_t instruction_address = frame->ReturnAddress();
273
274 if (frame->module) {
275 printf("%s", PathnameStripper::File(frame->module->code_file()).c_str());
276 if (!frame->function_name.empty()) {
277 printf("!%s", frame->function_name.c_str());
278 if (!frame->source_file_name.empty()) {
279 string source_file = PathnameStripper::File(frame->source_file_name);
280 printf(" [%s : %d + 0x%" PRIx64 "]",
281 source_file.c_str(),
282 frame->source_line,
283 instruction_address - frame->source_line_base);
284 } else {
285 printf(" + 0x%" PRIx64, instruction_address - frame->function_base);
286 }
287 } else {
288 printf(" + 0x%" PRIx64,
289 instruction_address - frame->module->base_address());
290 }
291 } else {
292 printf("0x%" PRIx64, instruction_address);
293 }
294 printf("\n ");
295
296 // Inlined frames don't have registers info.
297 if (frame->trust != StackFrameAMD64::FRAME_TRUST_INLINE) {
298 int sequence = 0;
299 if (cpu == "x86") {
300 const StackFrameX86* frame_x86 =
301 reinterpret_cast<const StackFrameX86*>(frame);
302
303 if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EIP)
304 sequence = PrintRegister("eip", frame_x86->context.eip, sequence);
305 if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESP)
306 sequence = PrintRegister("esp", frame_x86->context.esp, sequence);
307 if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EBP)
308 sequence = PrintRegister("ebp", frame_x86->context.ebp, sequence);
309 if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EBX)
310 sequence = PrintRegister("ebx", frame_x86->context.ebx, sequence);
311 if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESI)
312 sequence = PrintRegister("esi", frame_x86->context.esi, sequence);
313 if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EDI)
314 sequence = PrintRegister("edi", frame_x86->context.edi, sequence);
315 if (frame_x86->context_validity == StackFrameX86::CONTEXT_VALID_ALL) {
316 sequence = PrintRegister("eax", frame_x86->context.eax, sequence);
317 sequence = PrintRegister("ecx", frame_x86->context.ecx, sequence);
318 sequence = PrintRegister("edx", frame_x86->context.edx, sequence);
319 sequence = PrintRegister("efl", frame_x86->context.eflags, sequence);
320 }
321 } else if (cpu == "ppc") {
322 const StackFramePPC* frame_ppc =
323 reinterpret_cast<const StackFramePPC*>(frame);
324
325 if (frame_ppc->context_validity & StackFramePPC::CONTEXT_VALID_SRR0)
326 sequence = PrintRegister("srr0", frame_ppc->context.srr0, sequence);
327 if (frame_ppc->context_validity & StackFramePPC::CONTEXT_VALID_GPR1)
328 sequence = PrintRegister("r1", frame_ppc->context.gpr[1], sequence);
329 } else if (cpu == "amd64") {
330 const StackFrameAMD64* frame_amd64 =
331 reinterpret_cast<const StackFrameAMD64*>(frame);
332
333 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RAX)
334 sequence = PrintRegister64("rax", frame_amd64->context.rax, sequence);
335 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RDX)
336 sequence = PrintRegister64("rdx", frame_amd64->context.rdx, sequence);
337 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RCX)
338 sequence = PrintRegister64("rcx", frame_amd64->context.rcx, sequence);
339 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RBX)
340 sequence = PrintRegister64("rbx", frame_amd64->context.rbx, sequence);
341 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RSI)
342 sequence = PrintRegister64("rsi", frame_amd64->context.rsi, sequence);
343 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RDI)
344 sequence = PrintRegister64("rdi", frame_amd64->context.rdi, sequence);
345 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RBP)
346 sequence = PrintRegister64("rbp", frame_amd64->context.rbp, sequence);
347 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RSP)
348 sequence = PrintRegister64("rsp", frame_amd64->context.rsp, sequence);
349 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R8)
350 sequence = PrintRegister64("r8", frame_amd64->context.r8, sequence);
351 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R9)
352 sequence = PrintRegister64("r9", frame_amd64->context.r9, sequence);
353 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R10)
354 sequence = PrintRegister64("r10", frame_amd64->context.r10, sequence);
355 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R11)
356 sequence = PrintRegister64("r11", frame_amd64->context.r11, sequence);
357 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R12)
358 sequence = PrintRegister64("r12", frame_amd64->context.r12, sequence);
359 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R13)
360 sequence = PrintRegister64("r13", frame_amd64->context.r13, sequence);
361 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R14)
362 sequence = PrintRegister64("r14", frame_amd64->context.r14, sequence);
363 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R15)
364 sequence = PrintRegister64("r15", frame_amd64->context.r15, sequence);
365 if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RIP)
366 sequence = PrintRegister64("rip", frame_amd64->context.rip, sequence);
367 } else if (cpu == "sparc") {
368 const StackFrameSPARC* frame_sparc =
369 reinterpret_cast<const StackFrameSPARC*>(frame);
370
371 if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_SP)
372 sequence =
373 PrintRegister("sp", frame_sparc->context.g_r[14], sequence);
374 if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_FP)
375 sequence =
376 PrintRegister("fp", frame_sparc->context.g_r[30], sequence);
377 if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_PC)
378 sequence = PrintRegister("pc", frame_sparc->context.pc, sequence);
379 } else if (cpu == "arm") {
380 const StackFrameARM* frame_arm =
381 reinterpret_cast<const StackFrameARM*>(frame);
382
383 // Argument registers (caller-saves), which will likely only be valid
384 // for the youngest frame.
385 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R0)
386 sequence = PrintRegister("r0", frame_arm->context.iregs[0], sequence);
387 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R1)
388 sequence = PrintRegister("r1", frame_arm->context.iregs[1], sequence);
389 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R2)
390 sequence = PrintRegister("r2", frame_arm->context.iregs[2], sequence);
391 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R3)
392 sequence = PrintRegister("r3", frame_arm->context.iregs[3], sequence);
393
394 // General-purpose callee-saves registers.
395 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R4)
396 sequence = PrintRegister("r4", frame_arm->context.iregs[4], sequence);
397 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R5)
398 sequence = PrintRegister("r5", frame_arm->context.iregs[5], sequence);
399 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R6)
400 sequence = PrintRegister("r6", frame_arm->context.iregs[6], sequence);
401 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R7)
402 sequence = PrintRegister("r7", frame_arm->context.iregs[7], sequence);
403 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R8)
404 sequence = PrintRegister("r8", frame_arm->context.iregs[8], sequence);
405 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R9)
406 sequence = PrintRegister("r9", frame_arm->context.iregs[9], sequence);
407 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R10)
408 sequence =
409 PrintRegister("r10", frame_arm->context.iregs[10], sequence);
410 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R12)
411 sequence =
412 PrintRegister("r12", frame_arm->context.iregs[12], sequence);
413
414 // Registers with a dedicated or conventional purpose.
415 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_FP)
416 sequence =
417 PrintRegister("fp", frame_arm->context.iregs[11], sequence);
418 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_SP)
419 sequence =
420 PrintRegister("sp", frame_arm->context.iregs[13], sequence);
421 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_LR)
422 sequence =
423 PrintRegister("lr", frame_arm->context.iregs[14], sequence);
424 if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_PC)
425 sequence =
426 PrintRegister("pc", frame_arm->context.iregs[15], sequence);
427 } else if (cpu == "arm64") {
428 const StackFrameARM64* frame_arm64 =
429 reinterpret_cast<const StackFrameARM64*>(frame);
430
431 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X0) {
432 sequence =
433 PrintRegister64("x0", frame_arm64->context.iregs[0], sequence);
434 }
435 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X1) {
436 sequence =
437 PrintRegister64("x1", frame_arm64->context.iregs[1], sequence);
438 }
439 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X2) {
440 sequence =
441 PrintRegister64("x2", frame_arm64->context.iregs[2], sequence);
442 }
443 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X3) {
444 sequence =
445 PrintRegister64("x3", frame_arm64->context.iregs[3], sequence);
446 }
447 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X4) {
448 sequence =
449 PrintRegister64("x4", frame_arm64->context.iregs[4], sequence);
450 }
451 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X5) {
452 sequence =
453 PrintRegister64("x5", frame_arm64->context.iregs[5], sequence);
454 }
455 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X6) {
456 sequence =
457 PrintRegister64("x6", frame_arm64->context.iregs[6], sequence);
458 }
459 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X7) {
460 sequence =
461 PrintRegister64("x7", frame_arm64->context.iregs[7], sequence);
462 }
463 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X8) {
464 sequence =
465 PrintRegister64("x8", frame_arm64->context.iregs[8], sequence);
466 }
467 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_X9) {
468 sequence =
469 PrintRegister64("x9", frame_arm64->context.iregs[9], sequence);
470 }
471 if (frame_arm64->context_validity &
472 StackFrameARM64::CONTEXT_VALID_X10) {
473 sequence =
474 PrintRegister64("x10", frame_arm64->context.iregs[10], sequence);
475 }
476 if (frame_arm64->context_validity &
477 StackFrameARM64::CONTEXT_VALID_X11) {
478 sequence =
479 PrintRegister64("x11", frame_arm64->context.iregs[11], sequence);
480 }
481 if (frame_arm64->context_validity &
482 StackFrameARM64::CONTEXT_VALID_X12) {
483 sequence =
484 PrintRegister64("x12", frame_arm64->context.iregs[12], sequence);
485 }
486 if (frame_arm64->context_validity &
487 StackFrameARM64::CONTEXT_VALID_X13) {
488 sequence =
489 PrintRegister64("x13", frame_arm64->context.iregs[13], sequence);
490 }
491 if (frame_arm64->context_validity &
492 StackFrameARM64::CONTEXT_VALID_X14) {
493 sequence =
494 PrintRegister64("x14", frame_arm64->context.iregs[14], sequence);
495 }
496 if (frame_arm64->context_validity &
497 StackFrameARM64::CONTEXT_VALID_X15) {
498 sequence =
499 PrintRegister64("x15", frame_arm64->context.iregs[15], sequence);
500 }
501 if (frame_arm64->context_validity &
502 StackFrameARM64::CONTEXT_VALID_X16) {
503 sequence =
504 PrintRegister64("x16", frame_arm64->context.iregs[16], sequence);
505 }
506 if (frame_arm64->context_validity &
507 StackFrameARM64::CONTEXT_VALID_X17) {
508 sequence =
509 PrintRegister64("x17", frame_arm64->context.iregs[17], sequence);
510 }
511 if (frame_arm64->context_validity &
512 StackFrameARM64::CONTEXT_VALID_X18) {
513 sequence =
514 PrintRegister64("x18", frame_arm64->context.iregs[18], sequence);
515 }
516 if (frame_arm64->context_validity &
517 StackFrameARM64::CONTEXT_VALID_X19) {
518 sequence =
519 PrintRegister64("x19", frame_arm64->context.iregs[19], sequence);
520 }
521 if (frame_arm64->context_validity &
522 StackFrameARM64::CONTEXT_VALID_X20) {
523 sequence =
524 PrintRegister64("x20", frame_arm64->context.iregs[20], sequence);
525 }
526 if (frame_arm64->context_validity &
527 StackFrameARM64::CONTEXT_VALID_X21) {
528 sequence =
529 PrintRegister64("x21", frame_arm64->context.iregs[21], sequence);
530 }
531 if (frame_arm64->context_validity &
532 StackFrameARM64::CONTEXT_VALID_X22) {
533 sequence =
534 PrintRegister64("x22", frame_arm64->context.iregs[22], sequence);
535 }
536 if (frame_arm64->context_validity &
537 StackFrameARM64::CONTEXT_VALID_X23) {
538 sequence =
539 PrintRegister64("x23", frame_arm64->context.iregs[23], sequence);
540 }
541 if (frame_arm64->context_validity &
542 StackFrameARM64::CONTEXT_VALID_X24) {
543 sequence =
544 PrintRegister64("x24", frame_arm64->context.iregs[24], sequence);
545 }
546 if (frame_arm64->context_validity &
547 StackFrameARM64::CONTEXT_VALID_X25) {
548 sequence =
549 PrintRegister64("x25", frame_arm64->context.iregs[25], sequence);
550 }
551 if (frame_arm64->context_validity &
552 StackFrameARM64::CONTEXT_VALID_X26) {
553 sequence =
554 PrintRegister64("x26", frame_arm64->context.iregs[26], sequence);
555 }
556 if (frame_arm64->context_validity &
557 StackFrameARM64::CONTEXT_VALID_X27) {
558 sequence =
559 PrintRegister64("x27", frame_arm64->context.iregs[27], sequence);
560 }
561 if (frame_arm64->context_validity &
562 StackFrameARM64::CONTEXT_VALID_X28) {
563 sequence =
564 PrintRegister64("x28", frame_arm64->context.iregs[28], sequence);
565 }
566
567 // Registers with a dedicated or conventional purpose.
568 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_FP) {
569 sequence =
570 PrintRegister64("fp", frame_arm64->context.iregs[29], sequence);
571 }
572 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_LR) {
573 sequence =
574 PrintRegister64("lr", frame_arm64->context.iregs[30], sequence);
575 }
576 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_SP) {
577 sequence =
578 PrintRegister64("sp", frame_arm64->context.iregs[31], sequence);
579 }
580 if (frame_arm64->context_validity & StackFrameARM64::CONTEXT_VALID_PC) {
581 sequence =
582 PrintRegister64("pc", frame_arm64->context.iregs[32], sequence);
583 }
584 } else if ((cpu == "mips") || (cpu == "mips64")) {
585 const StackFrameMIPS* frame_mips =
586 reinterpret_cast<const StackFrameMIPS*>(frame);
587
588 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_GP)
589 sequence = PrintRegister64(
590 "gp", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_GP],
591 sequence);
592 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_SP)
593 sequence = PrintRegister64(
594 "sp", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_SP],
595 sequence);
596 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_FP)
597 sequence = PrintRegister64(
598 "fp", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_FP],
599 sequence);
600 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_RA)
601 sequence = PrintRegister64(
602 "ra", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_RA],
603 sequence);
604 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_PC)
605 sequence = PrintRegister64("pc", frame_mips->context.epc, sequence);
606
607 // Save registers s0-s7
608 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_S0)
609 sequence = PrintRegister64(
610 "s0", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_S0],
611 sequence);
612 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_S1)
613 sequence = PrintRegister64(
614 "s1", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_S1],
615 sequence);
616 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_S2)
617 sequence = PrintRegister64(
618 "s2", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_S2],
619 sequence);
620 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_S3)
621 sequence = PrintRegister64(
622 "s3", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_S3],
623 sequence);
624 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_S4)
625 sequence = PrintRegister64(
626 "s4", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_S4],
627 sequence);
628 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_S5)
629 sequence = PrintRegister64(
630 "s5", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_S5],
631 sequence);
632 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_S6)
633 sequence = PrintRegister64(
634 "s6", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_S6],
635 sequence);
636 if (frame_mips->context_validity & StackFrameMIPS::CONTEXT_VALID_S7)
637 sequence = PrintRegister64(
638 "s7", frame_mips->context.iregs[MD_CONTEXT_MIPS_REG_S7],
639 sequence);
640 }
641 }
642 printf("\n Found by: %s\n", frame->trust_description().c_str());
643
644 // Print stack contents.
645 if (output_stack_contents && frame_index + 1 < frame_count) {
646 const string indent(" ");
647 PrintStackContents(indent, frame, stack->frames()->at(frame_index + 1),
648 cpu, memory, modules, resolver);
649 }
650 }
651}
652
653// PrintStackMachineReadable prints the call stack in |stack| to stdout,
654// in the following machine readable pipe-delimited text format:
655// thread number|frame number|module|function|source file|line|offset
656//
657// Module, function, source file, and source line may all be empty
658// depending on availability. The code offset follows the same rules as
659// PrintStack above.
660static void PrintStackMachineReadable(int thread_num, const CallStack* stack) {
661 int frame_count = stack->frames()->size();
662 for (int frame_index = 0; frame_index < frame_count; ++frame_index) {
663 const StackFrame* frame = stack->frames()->at(frame_index);
664 printf("%d%c%d%c", thread_num, kOutputSeparator, frame_index,
665 kOutputSeparator);
666
667 uint64_t instruction_address = frame->ReturnAddress();
668
669 if (frame->module) {
670 assert(!frame->module->code_file().empty());
671 printf("%s", StripSeparator(PathnameStripper::File(
672 frame->module->code_file())).c_str());
673 if (!frame->function_name.empty()) {
674 printf("%c%s", kOutputSeparator,
675 StripSeparator(frame->function_name).c_str());
676 if (!frame->source_file_name.empty()) {
677 printf("%c%s%c%d%c0x%" PRIx64,
678 kOutputSeparator,
679 StripSeparator(frame->source_file_name).c_str(),
680 kOutputSeparator,
681 frame->source_line,
682 kOutputSeparator,
683 instruction_address - frame->source_line_base);
684 } else {
685 printf("%c%c%c0x%" PRIx64,
686 kOutputSeparator, // empty source file
687 kOutputSeparator, // empty source line
688 kOutputSeparator,
689 instruction_address - frame->function_base);
690 }
691 } else {
692 printf("%c%c%c%c0x%" PRIx64,
693 kOutputSeparator, // empty function name
694 kOutputSeparator, // empty source file
695 kOutputSeparator, // empty source line
696 kOutputSeparator,
697 instruction_address - frame->module->base_address());
698 }
699 } else {
700 // the printf before this prints a trailing separator for module name
701 printf("%c%c%c%c0x%" PRIx64,
702 kOutputSeparator, // empty function name
703 kOutputSeparator, // empty source file
704 kOutputSeparator, // empty source line
705 kOutputSeparator,
706 instruction_address);
707 }
708 printf("\n");
709 }
710}
711
712// ContainsModule checks whether a given |module| is in the vector
713// |modules_without_symbols|.
714static bool ContainsModule(
715 const vector<const CodeModule*>* modules,
716 const CodeModule* module) {
717 assert(modules);
718 assert(module);
719 vector<const CodeModule*>::const_iterator iter;
720 for (iter = modules->begin(); iter != modules->end(); ++iter) {
721 if (module->debug_file().compare((*iter)->debug_file()) == 0 &&
722 module->debug_identifier().compare((*iter)->debug_identifier()) == 0) {
723 return true;
724 }
725 }
726 return false;
727}
728
729// PrintModule prints a single |module| to stdout.
730// |modules_without_symbols| should contain the list of modules that were
731// confirmed to be missing their symbols during the stack walk.
732static void PrintModule(
733 const CodeModule* module,
734 const vector<const CodeModule*>* modules_without_symbols,
735 const vector<const CodeModule*>* modules_with_corrupt_symbols,
736 uint64_t main_address) {
737 string symbol_issues;
738 if (ContainsModule(modules_without_symbols, module)) {
739 symbol_issues = " (WARNING: No symbols, " +
740 PathnameStripper::File(module->debug_file()) + ", " +
741 module->debug_identifier() + ")";
742 } else if (ContainsModule(modules_with_corrupt_symbols, module)) {
743 symbol_issues = " (WARNING: Corrupt symbols, " +
744 PathnameStripper::File(module->debug_file()) + ", " +
745 module->debug_identifier() + ")";
746 }
747 uint64_t base_address = module->base_address();
748 printf("0x%08" PRIx64 " - 0x%08" PRIx64 " %s %s%s%s\n",
749 base_address, base_address + module->size() - 1,
750 PathnameStripper::File(module->code_file()).c_str(),
751 module->version().empty() ? "???" : module->version().c_str(),
752 main_address != 0 && base_address == main_address ? " (main)" : "",
753 symbol_issues.c_str());
754}
755
756// PrintModules prints the list of all loaded |modules| to stdout.
757// |modules_without_symbols| should contain the list of modules that were
758// confirmed to be missing their symbols during the stack walk.
759static void PrintModules(
760 const CodeModules* modules,
761 const vector<const CodeModule*>* modules_without_symbols,
762 const vector<const CodeModule*>* modules_with_corrupt_symbols) {
763 if (!modules)
764 return;
765
766 printf("\n");
767 printf("Loaded modules:\n");
768
769 uint64_t main_address = 0;
770 const CodeModule* main_module = modules->GetMainModule();
771 if (main_module) {
772 main_address = main_module->base_address();
773 }
774
775 unsigned int module_count = modules->module_count();
776 for (unsigned int module_sequence = 0;
777 module_sequence < module_count;
778 ++module_sequence) {
779 const CodeModule* module = modules->GetModuleAtSequence(module_sequence);
780 PrintModule(module, modules_without_symbols, modules_with_corrupt_symbols,
781 main_address);
782 }
783}
784
785// PrintModulesMachineReadable outputs a list of loaded modules,
786// one per line, in the following machine-readable pipe-delimited
787// text format:
788// Module|{Module Filename}|{Version}|{Debug Filename}|{Debug Identifier}|
789// {Base Address}|{Max Address}|{Main}
790static void PrintModulesMachineReadable(const CodeModules* modules) {
791 if (!modules)
792 return;
793
794 uint64_t main_address = 0;
795 const CodeModule* main_module = modules->GetMainModule();
796 if (main_module) {
797 main_address = main_module->base_address();
798 }
799
800 unsigned int module_count = modules->module_count();
801 for (unsigned int module_sequence = 0;
802 module_sequence < module_count;
803 ++module_sequence) {
804 const CodeModule* module = modules->GetModuleAtSequence(module_sequence);
805 uint64_t base_address = module->base_address();
806 printf("Module%c%s%c%s%c%s%c%s%c0x%08" PRIx64 "%c0x%08" PRIx64 "%c%d\n",
807 kOutputSeparator,
808 StripSeparator(PathnameStripper::File(module->code_file())).c_str(),
809 kOutputSeparator, StripSeparator(module->version()).c_str(),
810 kOutputSeparator,
811 StripSeparator(PathnameStripper::File(module->debug_file())).c_str(),
812 kOutputSeparator,
813 StripSeparator(module->debug_identifier()).c_str(),
814 kOutputSeparator, base_address,
815 kOutputSeparator, base_address + module->size() - 1,
816 kOutputSeparator,
817 main_module != NULL && base_address == main_address ? 1 : 0);
818 }
819}
820
821} // namespace
822
823void PrintProcessState(const ProcessState& process_state,
824 bool output_stack_contents,
825 bool output_requesting_thread_only,
826 SourceLineResolverInterface* resolver) {
827 // Print OS and CPU information.
828 string cpu = process_state.system_info()->cpu;
829 string cpu_info = process_state.system_info()->cpu_info;
830 printf("Operating system: %s\n", process_state.system_info()->os.c_str());
831 printf(" %s\n",
832 process_state.system_info()->os_version.c_str());
833 printf("CPU: %s\n", cpu.c_str());
834 if (!cpu_info.empty()) {
835 // This field is optional.
836 printf(" %s\n", cpu_info.c_str());
837 }
838 printf(" %d CPU%s\n",
839 process_state.system_info()->cpu_count,
840 process_state.system_info()->cpu_count != 1 ? "s" : "");
841 printf("\n");
842
843 // Print GPU information
844 string gl_version = process_state.system_info()->gl_version;
845 string gl_vendor = process_state.system_info()->gl_vendor;
846 string gl_renderer = process_state.system_info()->gl_renderer;
847 printf("GPU:");
848 if (!gl_version.empty() || !gl_vendor.empty() || !gl_renderer.empty()) {
849 printf(" %s\n", gl_version.c_str());
850 printf(" %s\n", gl_vendor.c_str());
851 printf(" %s\n", gl_renderer.c_str());
852 } else {
853 printf(" UNKNOWN\n");
854 }
855 printf("\n");
856
857 // Print crash information.
858 if (process_state.crashed()) {
859 printf("Crash reason: %s\n", process_state.crash_reason().c_str());
860 printf("Crash address: 0x%" PRIx64 "\n", process_state.crash_address());
861 } else {
862 printf("No crash\n");
863 }
864
865 string assertion = process_state.assertion();
866 if (!assertion.empty()) {
867 printf("Assertion: %s\n", assertion.c_str());
868 }
869
870 // Compute process uptime if the process creation and crash times are
871 // available in the dump.
872 if (process_state.time_date_stamp() != 0 &&
873 process_state.process_create_time() != 0 &&
874 process_state.time_date_stamp() >= process_state.process_create_time()) {
875 printf("Process uptime: %d seconds\n",
876 process_state.time_date_stamp() -
877 process_state.process_create_time());
878 } else {
879 printf("Process uptime: not available\n");
880 }
881
882 // If the thread that requested the dump is known, print it first.
883 int requesting_thread = process_state.requesting_thread();
884 if (requesting_thread != -1) {
885 printf("\n");
886 printf("Thread %d (%s)\n",
887 requesting_thread,
888 process_state.crashed() ? "crashed" :
889 "requested dump, did not crash");
890 PrintStack(process_state.threads()->at(requesting_thread), cpu,
891 output_stack_contents,
892 process_state.thread_memory_regions()->at(requesting_thread),
893 process_state.modules(), resolver);
894 }
895
896 if (!output_requesting_thread_only) {
897 // Print all of the threads in the dump.
898 int thread_count = process_state.threads()->size();
899 for (int thread_index = 0; thread_index < thread_count; ++thread_index) {
900 if (thread_index != requesting_thread) {
901 // Don't print the crash thread again, it was already printed.
902 printf("\n");
903 printf("Thread %d\n", thread_index);
904 PrintStack(process_state.threads()->at(thread_index), cpu,
905 output_stack_contents,
906 process_state.thread_memory_regions()->at(thread_index),
907 process_state.modules(), resolver);
908 }
909 }
910 }
911
912 PrintModules(process_state.modules(),
913 process_state.modules_without_symbols(),
914 process_state.modules_with_corrupt_symbols());
915}
916
917void PrintProcessStateMachineReadable(const ProcessState& process_state) {
918 // Print OS and CPU information.
919 // OS|{OS Name}|{OS Version}
920 // CPU|{CPU Name}|{CPU Info}|{Number of CPUs}
921 // GPU|{GPU version}|{GPU vendor}|{GPU renderer}
922 printf("OS%c%s%c%s\n", kOutputSeparator,
923 StripSeparator(process_state.system_info()->os).c_str(),
924 kOutputSeparator,
925 StripSeparator(process_state.system_info()->os_version).c_str());
926 printf("CPU%c%s%c%s%c%d\n", kOutputSeparator,
927 StripSeparator(process_state.system_info()->cpu).c_str(),
928 kOutputSeparator,
929 // this may be empty
930 StripSeparator(process_state.system_info()->cpu_info).c_str(),
931 kOutputSeparator,
932 process_state.system_info()->cpu_count);
933 printf("GPU%c%s%c%s%c%s\n", kOutputSeparator,
934 StripSeparator(process_state.system_info()->gl_version).c_str(),
935 kOutputSeparator,
936 StripSeparator(process_state.system_info()->gl_vendor).c_str(),
937 kOutputSeparator,
938 StripSeparator(process_state.system_info()->gl_renderer).c_str());
939
940 int requesting_thread = process_state.requesting_thread();
941
942 // Print crash information.
943 // Crash|{Crash Reason}|{Crash Address}|{Crashed Thread}
944 printf("Crash%c", kOutputSeparator);
945 if (process_state.crashed()) {
946 printf("%s%c0x%" PRIx64 "%c",
947 StripSeparator(process_state.crash_reason()).c_str(),
948 kOutputSeparator, process_state.crash_address(), kOutputSeparator);
949 } else {
950 // print assertion info, if available, in place of crash reason,
951 // instead of the unhelpful "No crash"
952 string assertion = process_state.assertion();
953 if (!assertion.empty()) {
954 printf("%s%c%c", StripSeparator(assertion).c_str(),
955 kOutputSeparator, kOutputSeparator);
956 } else {
957 printf("No crash%c%c", kOutputSeparator, kOutputSeparator);
958 }
959 }
960
961 if (requesting_thread != -1) {
962 printf("%d\n", requesting_thread);
963 } else {
964 printf("\n");
965 }
966
967 PrintModulesMachineReadable(process_state.modules());
968
969 // blank line to indicate start of threads
970 printf("\n");
971
972 // If the thread that requested the dump is known, print it first.
973 if (requesting_thread != -1) {
974 PrintStackMachineReadable(requesting_thread,
975 process_state.threads()->at(requesting_thread));
976 }
977
978 // Print all of the threads in the dump.
979 int thread_count = process_state.threads()->size();
980 for (int thread_index = 0; thread_index < thread_count; ++thread_index) {
981 if (thread_index != requesting_thread) {
982 // Don't print the crash thread again, it was already printed.
983 PrintStackMachineReadable(thread_index,
984 process_state.threads()->at(thread_index));
985 }
986 }
987}
988
989} // namespace google_breakpad
990