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