michael@0: // Copyright (c) 2010 Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // minidump_stackwalk.cc: Process a minidump with MinidumpProcessor, printing michael@0: // the results, including stack traces. michael@0: // michael@0: // Author: Mark Mentovai michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "common/scoped_ptr.h" michael@0: #include "common/using_std_string.h" michael@0: #include "google_breakpad/processor/basic_source_line_resolver.h" michael@0: #include "google_breakpad/processor/call_stack.h" michael@0: #include "google_breakpad/processor/code_module.h" michael@0: #include "google_breakpad/processor/code_modules.h" michael@0: #include "google_breakpad/processor/minidump.h" michael@0: #include "google_breakpad/processor/minidump_processor.h" michael@0: #include "google_breakpad/processor/process_state.h" michael@0: #include "google_breakpad/processor/stack_frame_cpu.h" michael@0: #include "processor/logging.h" michael@0: #include "processor/pathname_stripper.h" michael@0: #include "processor/simple_symbol_supplier.h" michael@0: michael@0: namespace { michael@0: michael@0: using std::vector; michael@0: using google_breakpad::BasicSourceLineResolver; michael@0: using google_breakpad::CallStack; michael@0: using google_breakpad::CodeModule; michael@0: using google_breakpad::CodeModules; michael@0: using google_breakpad::MinidumpModule; michael@0: using google_breakpad::MinidumpProcessor; michael@0: using google_breakpad::PathnameStripper; michael@0: using google_breakpad::ProcessState; michael@0: using google_breakpad::scoped_ptr; michael@0: using google_breakpad::SimpleSymbolSupplier; michael@0: using google_breakpad::StackFrame; michael@0: using google_breakpad::StackFramePPC; michael@0: using google_breakpad::StackFrameSPARC; michael@0: using google_breakpad::StackFrameX86; michael@0: using google_breakpad::StackFrameAMD64; michael@0: using google_breakpad::StackFrameARM; michael@0: michael@0: // Separator character for machine readable output. michael@0: static const char kOutputSeparator = '|'; michael@0: michael@0: // PrintRegister prints a register's name and value to stdout. It will michael@0: // print four registers on a line. For the first register in a set, michael@0: // pass 0 for |start_col|. For registers in a set, pass the most recent michael@0: // return value of PrintRegister. michael@0: // The caller is responsible for printing the final newline after a set michael@0: // of registers is completely printed, regardless of the number of calls michael@0: // to PrintRegister. michael@0: static const int kMaxWidth = 80; // optimize for an 80-column terminal michael@0: static int PrintRegister(const char *name, uint32_t value, int start_col) { michael@0: char buffer[64]; michael@0: snprintf(buffer, sizeof(buffer), " %5s = 0x%08x", name, value); michael@0: michael@0: if (start_col + static_cast(strlen(buffer)) > kMaxWidth) { michael@0: start_col = 0; michael@0: printf("\n "); michael@0: } michael@0: fputs(buffer, stdout); michael@0: michael@0: return start_col + strlen(buffer); michael@0: } michael@0: michael@0: // PrintRegister64 does the same thing, but for 64-bit registers. michael@0: static int PrintRegister64(const char *name, uint64_t value, int start_col) { michael@0: char buffer[64]; michael@0: snprintf(buffer, sizeof(buffer), " %5s = 0x%016" PRIx64 , name, value); michael@0: michael@0: if (start_col + static_cast(strlen(buffer)) > kMaxWidth) { michael@0: start_col = 0; michael@0: printf("\n "); michael@0: } michael@0: fputs(buffer, stdout); michael@0: michael@0: return start_col + strlen(buffer); michael@0: } michael@0: michael@0: // StripSeparator takes a string |original| and returns a copy michael@0: // of the string with all occurences of |kOutputSeparator| removed. michael@0: static string StripSeparator(const string &original) { michael@0: string result = original; michael@0: string::size_type position = 0; michael@0: while ((position = result.find(kOutputSeparator, position)) != string::npos) { michael@0: result.erase(position, 1); michael@0: } michael@0: position = 0; michael@0: while ((position = result.find('\n', position)) != string::npos) { michael@0: result.erase(position, 1); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: // PrintStack prints the call stack in |stack| to stdout, in a reasonably michael@0: // useful form. Module, function, and source file names are displayed if michael@0: // they are available. The code offset to the base code address of the michael@0: // source line, function, or module is printed, preferring them in that michael@0: // order. If no source line, function, or module information is available, michael@0: // an absolute code offset is printed. michael@0: // michael@0: // If |cpu| is a recognized CPU name, relevant register state for each stack michael@0: // frame printed is also output, if available. michael@0: static void PrintStack(const CallStack *stack, const string &cpu) { michael@0: int frame_count = stack->frames()->size(); michael@0: if (frame_count == 0) { michael@0: printf(" \n"); michael@0: } michael@0: for (int frame_index = 0; frame_index < frame_count; ++frame_index) { michael@0: const StackFrame *frame = stack->frames()->at(frame_index); michael@0: printf("%2d ", frame_index); michael@0: michael@0: uint64_t instruction_address = frame->ReturnAddress(); michael@0: michael@0: if (frame->module) { michael@0: printf("%s", PathnameStripper::File(frame->module->code_file()).c_str()); michael@0: if (!frame->function_name.empty()) { michael@0: printf("!%s", frame->function_name.c_str()); michael@0: if (!frame->source_file_name.empty()) { michael@0: string source_file = PathnameStripper::File(frame->source_file_name); michael@0: printf(" [%s : %d + 0x%" PRIx64 "]", michael@0: source_file.c_str(), michael@0: frame->source_line, michael@0: instruction_address - frame->source_line_base); michael@0: } else { michael@0: printf(" + 0x%" PRIx64, instruction_address - frame->function_base); michael@0: } michael@0: } else { michael@0: printf(" + 0x%" PRIx64, michael@0: instruction_address - frame->module->base_address()); michael@0: } michael@0: } else { michael@0: printf("0x%" PRIx64, instruction_address); michael@0: } michael@0: printf("\n "); michael@0: michael@0: int sequence = 0; michael@0: if (cpu == "x86") { michael@0: const StackFrameX86 *frame_x86 = michael@0: reinterpret_cast(frame); michael@0: michael@0: if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EIP) michael@0: sequence = PrintRegister("eip", frame_x86->context.eip, sequence); michael@0: if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESP) michael@0: sequence = PrintRegister("esp", frame_x86->context.esp, sequence); michael@0: if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EBP) michael@0: sequence = PrintRegister("ebp", frame_x86->context.ebp, sequence); michael@0: if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EBX) michael@0: sequence = PrintRegister("ebx", frame_x86->context.ebx, sequence); michael@0: if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESI) michael@0: sequence = PrintRegister("esi", frame_x86->context.esi, sequence); michael@0: if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EDI) michael@0: sequence = PrintRegister("edi", frame_x86->context.edi, sequence); michael@0: if (frame_x86->context_validity == StackFrameX86::CONTEXT_VALID_ALL) { michael@0: sequence = PrintRegister("eax", frame_x86->context.eax, sequence); michael@0: sequence = PrintRegister("ecx", frame_x86->context.ecx, sequence); michael@0: sequence = PrintRegister("edx", frame_x86->context.edx, sequence); michael@0: sequence = PrintRegister("efl", frame_x86->context.eflags, sequence); michael@0: } michael@0: } else if (cpu == "ppc") { michael@0: const StackFramePPC *frame_ppc = michael@0: reinterpret_cast(frame); michael@0: michael@0: if (frame_ppc->context_validity & StackFramePPC::CONTEXT_VALID_SRR0) michael@0: sequence = PrintRegister("srr0", frame_ppc->context.srr0, sequence); michael@0: if (frame_ppc->context_validity & StackFramePPC::CONTEXT_VALID_GPR1) michael@0: sequence = PrintRegister("r1", frame_ppc->context.gpr[1], sequence); michael@0: } else if (cpu == "amd64") { michael@0: const StackFrameAMD64 *frame_amd64 = michael@0: reinterpret_cast(frame); michael@0: michael@0: if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RBX) michael@0: sequence = PrintRegister64("rbx", frame_amd64->context.rbx, sequence); michael@0: if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R12) michael@0: sequence = PrintRegister64("r12", frame_amd64->context.r12, sequence); michael@0: if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R13) michael@0: sequence = PrintRegister64("r13", frame_amd64->context.r13, sequence); michael@0: if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R14) michael@0: sequence = PrintRegister64("r14", frame_amd64->context.r14, sequence); michael@0: if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R15) michael@0: sequence = PrintRegister64("r15", frame_amd64->context.r15, sequence); michael@0: if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RIP) michael@0: sequence = PrintRegister64("rip", frame_amd64->context.rip, sequence); michael@0: if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RSP) michael@0: sequence = PrintRegister64("rsp", frame_amd64->context.rsp, sequence); michael@0: if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RBP) michael@0: sequence = PrintRegister64("rbp", frame_amd64->context.rbp, sequence); michael@0: } else if (cpu == "sparc") { michael@0: const StackFrameSPARC *frame_sparc = michael@0: reinterpret_cast(frame); michael@0: michael@0: if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_SP) michael@0: sequence = PrintRegister("sp", frame_sparc->context.g_r[14], sequence); michael@0: if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_FP) michael@0: sequence = PrintRegister("fp", frame_sparc->context.g_r[30], sequence); michael@0: if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_PC) michael@0: sequence = PrintRegister("pc", frame_sparc->context.pc, sequence); michael@0: } else if (cpu == "arm") { michael@0: const StackFrameARM *frame_arm = michael@0: reinterpret_cast(frame); michael@0: michael@0: // Argument registers (caller-saves), which will likely only be valid michael@0: // for the youngest frame. michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R0) michael@0: sequence = PrintRegister("r0", frame_arm->context.iregs[0], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R1) michael@0: sequence = PrintRegister("r1", frame_arm->context.iregs[1], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R2) michael@0: sequence = PrintRegister("r2", frame_arm->context.iregs[2], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R3) michael@0: sequence = PrintRegister("r3", frame_arm->context.iregs[3], sequence); michael@0: michael@0: // General-purpose callee-saves registers. michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R4) michael@0: sequence = PrintRegister("r4", frame_arm->context.iregs[4], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R5) michael@0: sequence = PrintRegister("r5", frame_arm->context.iregs[5], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R6) michael@0: sequence = PrintRegister("r6", frame_arm->context.iregs[6], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R7) michael@0: sequence = PrintRegister("r7", frame_arm->context.iregs[7], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R8) michael@0: sequence = PrintRegister("r8", frame_arm->context.iregs[8], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R9) michael@0: sequence = PrintRegister("r9", frame_arm->context.iregs[9], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R10) michael@0: sequence = PrintRegister("r10", frame_arm->context.iregs[10], sequence); michael@0: michael@0: // Registers with a dedicated or conventional purpose. michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_FP) michael@0: sequence = PrintRegister("fp", frame_arm->context.iregs[11], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_SP) michael@0: sequence = PrintRegister("sp", frame_arm->context.iregs[13], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_LR) michael@0: sequence = PrintRegister("lr", frame_arm->context.iregs[14], sequence); michael@0: if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_PC) michael@0: sequence = PrintRegister("pc", frame_arm->context.iregs[15], sequence); michael@0: } michael@0: printf("\n Found by: %s\n", frame->trust_description().c_str()); michael@0: } michael@0: } michael@0: michael@0: // PrintStackMachineReadable prints the call stack in |stack| to stdout, michael@0: // in the following machine readable pipe-delimited text format: michael@0: // thread number|frame number|module|function|source file|line|offset michael@0: // michael@0: // Module, function, source file, and source line may all be empty michael@0: // depending on availability. The code offset follows the same rules as michael@0: // PrintStack above. michael@0: static void PrintStackMachineReadable(int thread_num, const CallStack *stack) { michael@0: int frame_count = stack->frames()->size(); michael@0: for (int frame_index = 0; frame_index < frame_count; ++frame_index) { michael@0: const StackFrame *frame = stack->frames()->at(frame_index); michael@0: printf("%d%c%d%c", thread_num, kOutputSeparator, frame_index, michael@0: kOutputSeparator); michael@0: michael@0: uint64_t instruction_address = frame->ReturnAddress(); michael@0: michael@0: if (frame->module) { michael@0: assert(!frame->module->code_file().empty()); michael@0: printf("%s", StripSeparator(PathnameStripper::File( michael@0: frame->module->code_file())).c_str()); michael@0: if (!frame->function_name.empty()) { michael@0: printf("%c%s", kOutputSeparator, michael@0: StripSeparator(frame->function_name).c_str()); michael@0: if (!frame->source_file_name.empty()) { michael@0: printf("%c%s%c%d%c0x%" PRIx64, michael@0: kOutputSeparator, michael@0: StripSeparator(frame->source_file_name).c_str(), michael@0: kOutputSeparator, michael@0: frame->source_line, michael@0: kOutputSeparator, michael@0: instruction_address - frame->source_line_base); michael@0: } else { michael@0: printf("%c%c%c0x%" PRIx64, michael@0: kOutputSeparator, // empty source file michael@0: kOutputSeparator, // empty source line michael@0: kOutputSeparator, michael@0: instruction_address - frame->function_base); michael@0: } michael@0: } else { michael@0: printf("%c%c%c%c0x%" PRIx64, michael@0: kOutputSeparator, // empty function name michael@0: kOutputSeparator, // empty source file michael@0: kOutputSeparator, // empty source line michael@0: kOutputSeparator, michael@0: instruction_address - frame->module->base_address()); michael@0: } michael@0: } else { michael@0: // the printf before this prints a trailing separator for module name michael@0: printf("%c%c%c%c0x%" PRIx64, michael@0: kOutputSeparator, // empty function name michael@0: kOutputSeparator, // empty source file michael@0: kOutputSeparator, // empty source line michael@0: kOutputSeparator, michael@0: instruction_address); michael@0: } michael@0: printf("\n"); michael@0: } michael@0: } michael@0: michael@0: // ContainsModule checks whether a given |module| is in the vector michael@0: // |modules_without_symbols|. michael@0: static bool ContainsModule( michael@0: const vector *modules, michael@0: const CodeModule *module) { michael@0: assert(modules); michael@0: assert(module); michael@0: vector::const_iterator iter; michael@0: for (iter = modules->begin(); iter != modules->end(); ++iter) { michael@0: if (module->debug_file().compare((*iter)->debug_file()) == 0 && michael@0: module->debug_identifier().compare((*iter)->debug_identifier()) == 0) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // PrintModule prints a single |module| to stdout. michael@0: // |modules_without_symbols| should contain the list of modules that were michael@0: // confirmed to be missing their symbols during the stack walk. michael@0: static void PrintModule( michael@0: const CodeModule *module, michael@0: const vector *modules_without_symbols, michael@0: uint64_t main_address) { michael@0: string missing_symbols; michael@0: if (ContainsModule(modules_without_symbols, module)) { michael@0: missing_symbols = " (WARNING: No symbols, " + michael@0: PathnameStripper::File(module->debug_file()) + ", " + michael@0: module->debug_identifier() + ")"; michael@0: } michael@0: uint64_t base_address = module->base_address(); michael@0: printf("0x%08" PRIx64 " - 0x%08" PRIx64 " %s %s%s%s\n", michael@0: base_address, base_address + module->size() - 1, michael@0: PathnameStripper::File(module->code_file()).c_str(), michael@0: module->version().empty() ? "???" : module->version().c_str(), michael@0: main_address != 0 && base_address == main_address ? " (main)" : "", michael@0: missing_symbols.c_str()); michael@0: } michael@0: michael@0: // PrintModules prints the list of all loaded |modules| to stdout. michael@0: // |modules_without_symbols| should contain the list of modules that were michael@0: // confirmed to be missing their symbols during the stack walk. michael@0: static void PrintModules( michael@0: const CodeModules *modules, michael@0: const vector *modules_without_symbols) { michael@0: if (!modules) michael@0: return; michael@0: michael@0: printf("\n"); michael@0: printf("Loaded modules:\n"); michael@0: michael@0: uint64_t main_address = 0; michael@0: const CodeModule *main_module = modules->GetMainModule(); michael@0: if (main_module) { michael@0: main_address = main_module->base_address(); michael@0: } michael@0: michael@0: unsigned int module_count = modules->module_count(); michael@0: for (unsigned int module_sequence = 0; michael@0: module_sequence < module_count; michael@0: ++module_sequence) { michael@0: const CodeModule *module = modules->GetModuleAtSequence(module_sequence); michael@0: PrintModule(module, modules_without_symbols, main_address); michael@0: } michael@0: } michael@0: michael@0: // PrintModulesMachineReadable outputs a list of loaded modules, michael@0: // one per line, in the following machine-readable pipe-delimited michael@0: // text format: michael@0: // Module|{Module Filename}|{Version}|{Debug Filename}|{Debug Identifier}| michael@0: // {Base Address}|{Max Address}|{Main} michael@0: static void PrintModulesMachineReadable(const CodeModules *modules) { michael@0: if (!modules) michael@0: return; michael@0: michael@0: uint64_t main_address = 0; michael@0: const CodeModule *main_module = modules->GetMainModule(); michael@0: if (main_module) { michael@0: main_address = main_module->base_address(); michael@0: } michael@0: michael@0: unsigned int module_count = modules->module_count(); michael@0: for (unsigned int module_sequence = 0; michael@0: module_sequence < module_count; michael@0: ++module_sequence) { michael@0: const CodeModule *module = modules->GetModuleAtSequence(module_sequence); michael@0: uint64_t base_address = module->base_address(); michael@0: printf("Module%c%s%c%s%c%s%c%s%c0x%08" PRIx64 "%c0x%08" PRIx64 "%c%d\n", michael@0: kOutputSeparator, michael@0: StripSeparator(PathnameStripper::File(module->code_file())).c_str(), michael@0: kOutputSeparator, StripSeparator(module->version()).c_str(), michael@0: kOutputSeparator, michael@0: StripSeparator(PathnameStripper::File(module->debug_file())).c_str(), michael@0: kOutputSeparator, michael@0: StripSeparator(module->debug_identifier()).c_str(), michael@0: kOutputSeparator, base_address, michael@0: kOutputSeparator, base_address + module->size() - 1, michael@0: kOutputSeparator, michael@0: main_module != NULL && base_address == main_address ? 1 : 0); michael@0: } michael@0: } michael@0: michael@0: static void PrintProcessState(const ProcessState& process_state) { michael@0: // Print OS and CPU information. michael@0: string cpu = process_state.system_info()->cpu; michael@0: string cpu_info = process_state.system_info()->cpu_info; michael@0: printf("Operating system: %s\n", process_state.system_info()->os.c_str()); michael@0: printf(" %s\n", michael@0: process_state.system_info()->os_version.c_str()); michael@0: printf("CPU: %s\n", cpu.c_str()); michael@0: if (!cpu_info.empty()) { michael@0: // This field is optional. michael@0: printf(" %s\n", cpu_info.c_str()); michael@0: } michael@0: printf(" %d CPU%s\n", michael@0: process_state.system_info()->cpu_count, michael@0: process_state.system_info()->cpu_count != 1 ? "s" : ""); michael@0: printf("\n"); michael@0: michael@0: // Print crash information. michael@0: if (process_state.crashed()) { michael@0: printf("Crash reason: %s\n", process_state.crash_reason().c_str()); michael@0: printf("Crash address: 0x%" PRIx64 "\n", process_state.crash_address()); michael@0: } else { michael@0: printf("No crash\n"); michael@0: } michael@0: michael@0: string assertion = process_state.assertion(); michael@0: if (!assertion.empty()) { michael@0: printf("Assertion: %s\n", assertion.c_str()); michael@0: } michael@0: michael@0: // If the thread that requested the dump is known, print it first. michael@0: int requesting_thread = process_state.requesting_thread(); michael@0: if (requesting_thread != -1) { michael@0: printf("\n"); michael@0: printf("Thread %d (%s)\n", michael@0: requesting_thread, michael@0: process_state.crashed() ? "crashed" : michael@0: "requested dump, did not crash"); michael@0: PrintStack(process_state.threads()->at(requesting_thread), cpu); michael@0: } michael@0: michael@0: // Print all of the threads in the dump. michael@0: int thread_count = process_state.threads()->size(); michael@0: for (int thread_index = 0; thread_index < thread_count; ++thread_index) { michael@0: if (thread_index != requesting_thread) { michael@0: // Don't print the crash thread again, it was already printed. michael@0: printf("\n"); michael@0: printf("Thread %d\n", thread_index); michael@0: PrintStack(process_state.threads()->at(thread_index), cpu); michael@0: } michael@0: } michael@0: michael@0: PrintModules(process_state.modules(), michael@0: process_state.modules_without_symbols()); michael@0: } michael@0: michael@0: static void PrintProcessStateMachineReadable(const ProcessState& process_state) michael@0: { michael@0: // Print OS and CPU information. michael@0: // OS|{OS Name}|{OS Version} michael@0: // CPU|{CPU Name}|{CPU Info}|{Number of CPUs} michael@0: printf("OS%c%s%c%s\n", kOutputSeparator, michael@0: StripSeparator(process_state.system_info()->os).c_str(), michael@0: kOutputSeparator, michael@0: StripSeparator(process_state.system_info()->os_version).c_str()); michael@0: printf("CPU%c%s%c%s%c%d\n", kOutputSeparator, michael@0: StripSeparator(process_state.system_info()->cpu).c_str(), michael@0: kOutputSeparator, michael@0: // this may be empty michael@0: StripSeparator(process_state.system_info()->cpu_info).c_str(), michael@0: kOutputSeparator, michael@0: process_state.system_info()->cpu_count); michael@0: michael@0: int requesting_thread = process_state.requesting_thread(); michael@0: michael@0: // Print crash information. michael@0: // Crash|{Crash Reason}|{Crash Address}|{Crashed Thread} michael@0: printf("Crash%c", kOutputSeparator); michael@0: if (process_state.crashed()) { michael@0: printf("%s%c0x%" PRIx64 "%c", michael@0: StripSeparator(process_state.crash_reason()).c_str(), michael@0: kOutputSeparator, process_state.crash_address(), kOutputSeparator); michael@0: } else { michael@0: // print assertion info, if available, in place of crash reason, michael@0: // instead of the unhelpful "No crash" michael@0: string assertion = process_state.assertion(); michael@0: if (!assertion.empty()) { michael@0: printf("%s%c%c", StripSeparator(assertion).c_str(), michael@0: kOutputSeparator, kOutputSeparator); michael@0: } else { michael@0: printf("No crash%c%c", kOutputSeparator, kOutputSeparator); michael@0: } michael@0: } michael@0: michael@0: if (requesting_thread != -1) { michael@0: printf("%d\n", requesting_thread); michael@0: } else { michael@0: printf("\n"); michael@0: } michael@0: michael@0: PrintModulesMachineReadable(process_state.modules()); michael@0: michael@0: // blank line to indicate start of threads michael@0: printf("\n"); michael@0: michael@0: // If the thread that requested the dump is known, print it first. michael@0: if (requesting_thread != -1) { michael@0: PrintStackMachineReadable(requesting_thread, michael@0: process_state.threads()->at(requesting_thread)); michael@0: } michael@0: michael@0: // Print all of the threads in the dump. michael@0: int thread_count = process_state.threads()->size(); michael@0: for (int thread_index = 0; thread_index < thread_count; ++thread_index) { michael@0: if (thread_index != requesting_thread) { michael@0: // Don't print the crash thread again, it was already printed. michael@0: PrintStackMachineReadable(thread_index, michael@0: process_state.threads()->at(thread_index)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Processes |minidump_file| using MinidumpProcessor. |symbol_path|, if michael@0: // non-empty, is the base directory of a symbol storage area, laid out in michael@0: // the format required by SimpleSymbolSupplier. If such a storage area michael@0: // is specified, it is made available for use by the MinidumpProcessor. michael@0: // michael@0: // Returns the value of MinidumpProcessor::Process. If processing succeeds, michael@0: // prints identifying OS and CPU information from the minidump, crash michael@0: // information if the minidump was produced as a result of a crash, and michael@0: // call stacks for each thread contained in the minidump. All information michael@0: // is printed to stdout. michael@0: static bool PrintMinidumpProcess(const string &minidump_file, michael@0: const vector &symbol_paths, michael@0: bool machine_readable) { michael@0: scoped_ptr symbol_supplier; michael@0: if (!symbol_paths.empty()) { michael@0: // TODO(mmentovai): check existence of symbol_path if specified? michael@0: symbol_supplier.reset(new SimpleSymbolSupplier(symbol_paths)); michael@0: } michael@0: michael@0: BasicSourceLineResolver resolver; michael@0: MinidumpProcessor minidump_processor(symbol_supplier.get(), &resolver); michael@0: michael@0: // Process the minidump. michael@0: ProcessState process_state; michael@0: if (minidump_processor.Process(minidump_file, &process_state) != michael@0: google_breakpad::PROCESS_OK) { michael@0: BPLOG(ERROR) << "MinidumpProcessor::Process failed"; michael@0: return false; michael@0: } michael@0: michael@0: if (machine_readable) { michael@0: PrintProcessStateMachineReadable(process_state); michael@0: } else { michael@0: PrintProcessState(process_state); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: static void usage(const char *program_name) { michael@0: fprintf(stderr, "usage: %s [-m] [symbol-path ...]\n" michael@0: " -m : Output in machine-readable format\n", michael@0: program_name); michael@0: } michael@0: michael@0: int main(int argc, char **argv) { michael@0: BPLOG_INIT(&argc, &argv); michael@0: michael@0: if (argc < 2) { michael@0: usage(argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: const char *minidump_file; michael@0: bool machine_readable; michael@0: int symbol_path_arg; michael@0: michael@0: if (strcmp(argv[1], "-m") == 0) { michael@0: if (argc < 3) { michael@0: usage(argv[0]); michael@0: return 1; michael@0: } michael@0: michael@0: machine_readable = true; michael@0: minidump_file = argv[2]; michael@0: symbol_path_arg = 3; michael@0: } else { michael@0: machine_readable = false; michael@0: minidump_file = argv[1]; michael@0: symbol_path_arg = 2; michael@0: } michael@0: michael@0: // extra arguments are symbol paths michael@0: std::vector symbol_paths; michael@0: if (argc > symbol_path_arg) { michael@0: for (int argi = symbol_path_arg; argi < argc; ++argi) michael@0: symbol_paths.push_back(argv[argi]); michael@0: } michael@0: michael@0: return PrintMinidumpProcess(minidump_file, michael@0: symbol_paths, michael@0: machine_readable) ? 0 : 1; michael@0: }