toolkit/crashreporter/google-breakpad/src/processor/minidump_stackwalk.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/minidump_stackwalk.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,644 @@
     1.4 +// Copyright (c) 2010 Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +// minidump_stackwalk.cc: Process a minidump with MinidumpProcessor, printing
    1.34 +// the results, including stack traces.
    1.35 +//
    1.36 +// Author: Mark Mentovai
    1.37 +
    1.38 +#include <stdio.h>
    1.39 +#include <stdlib.h>
    1.40 +#include <string.h>
    1.41 +
    1.42 +#include <string>
    1.43 +#include <vector>
    1.44 +
    1.45 +#include "common/scoped_ptr.h"
    1.46 +#include "common/using_std_string.h"
    1.47 +#include "google_breakpad/processor/basic_source_line_resolver.h"
    1.48 +#include "google_breakpad/processor/call_stack.h"
    1.49 +#include "google_breakpad/processor/code_module.h"
    1.50 +#include "google_breakpad/processor/code_modules.h"
    1.51 +#include "google_breakpad/processor/minidump.h"
    1.52 +#include "google_breakpad/processor/minidump_processor.h"
    1.53 +#include "google_breakpad/processor/process_state.h"
    1.54 +#include "google_breakpad/processor/stack_frame_cpu.h"
    1.55 +#include "processor/logging.h"
    1.56 +#include "processor/pathname_stripper.h"
    1.57 +#include "processor/simple_symbol_supplier.h"
    1.58 +
    1.59 +namespace {
    1.60 +
    1.61 +using std::vector;
    1.62 +using google_breakpad::BasicSourceLineResolver;
    1.63 +using google_breakpad::CallStack;
    1.64 +using google_breakpad::CodeModule;
    1.65 +using google_breakpad::CodeModules;
    1.66 +using google_breakpad::MinidumpModule;
    1.67 +using google_breakpad::MinidumpProcessor;
    1.68 +using google_breakpad::PathnameStripper;
    1.69 +using google_breakpad::ProcessState;
    1.70 +using google_breakpad::scoped_ptr;
    1.71 +using google_breakpad::SimpleSymbolSupplier;
    1.72 +using google_breakpad::StackFrame;
    1.73 +using google_breakpad::StackFramePPC;
    1.74 +using google_breakpad::StackFrameSPARC;
    1.75 +using google_breakpad::StackFrameX86;
    1.76 +using google_breakpad::StackFrameAMD64;
    1.77 +using google_breakpad::StackFrameARM;
    1.78 +
    1.79 +// Separator character for machine readable output.
    1.80 +static const char kOutputSeparator = '|';
    1.81 +
    1.82 +// PrintRegister prints a register's name and value to stdout.  It will
    1.83 +// print four registers on a line.  For the first register in a set,
    1.84 +// pass 0 for |start_col|.  For registers in a set, pass the most recent
    1.85 +// return value of PrintRegister.
    1.86 +// The caller is responsible for printing the final newline after a set
    1.87 +// of registers is completely printed, regardless of the number of calls
    1.88 +// to PrintRegister.
    1.89 +static const int kMaxWidth = 80;  // optimize for an 80-column terminal
    1.90 +static int PrintRegister(const char *name, uint32_t value, int start_col) {
    1.91 +  char buffer[64];
    1.92 +  snprintf(buffer, sizeof(buffer), " %5s = 0x%08x", name, value);
    1.93 +
    1.94 +  if (start_col + static_cast<ssize_t>(strlen(buffer)) > kMaxWidth) {
    1.95 +    start_col = 0;
    1.96 +    printf("\n ");
    1.97 +  }
    1.98 +  fputs(buffer, stdout);
    1.99 +
   1.100 +  return start_col + strlen(buffer);
   1.101 +}
   1.102 +
   1.103 +// PrintRegister64 does the same thing, but for 64-bit registers.
   1.104 +static int PrintRegister64(const char *name, uint64_t value, int start_col) {
   1.105 +  char buffer[64];
   1.106 +  snprintf(buffer, sizeof(buffer), " %5s = 0x%016" PRIx64 , name, value);
   1.107 +
   1.108 +  if (start_col + static_cast<ssize_t>(strlen(buffer)) > kMaxWidth) {
   1.109 +    start_col = 0;
   1.110 +    printf("\n ");
   1.111 +  }
   1.112 +  fputs(buffer, stdout);
   1.113 +
   1.114 +  return start_col + strlen(buffer);
   1.115 +}
   1.116 +
   1.117 +// StripSeparator takes a string |original| and returns a copy
   1.118 +// of the string with all occurences of |kOutputSeparator| removed.
   1.119 +static string StripSeparator(const string &original) {
   1.120 +  string result = original;
   1.121 +  string::size_type position = 0;
   1.122 +  while ((position = result.find(kOutputSeparator, position)) != string::npos) {
   1.123 +    result.erase(position, 1);
   1.124 +  }
   1.125 +  position = 0;
   1.126 +  while ((position = result.find('\n', position)) != string::npos) {
   1.127 +    result.erase(position, 1);
   1.128 +  }
   1.129 +  return result;
   1.130 +}
   1.131 +
   1.132 +// PrintStack prints the call stack in |stack| to stdout, in a reasonably
   1.133 +// useful form.  Module, function, and source file names are displayed if
   1.134 +// they are available.  The code offset to the base code address of the
   1.135 +// source line, function, or module is printed, preferring them in that
   1.136 +// order.  If no source line, function, or module information is available,
   1.137 +// an absolute code offset is printed.
   1.138 +//
   1.139 +// If |cpu| is a recognized CPU name, relevant register state for each stack
   1.140 +// frame printed is also output, if available.
   1.141 +static void PrintStack(const CallStack *stack, const string &cpu) {
   1.142 +  int frame_count = stack->frames()->size();
   1.143 +  if (frame_count == 0) {
   1.144 +    printf(" <no frames>\n");
   1.145 +  }
   1.146 +  for (int frame_index = 0; frame_index < frame_count; ++frame_index) {
   1.147 +    const StackFrame *frame = stack->frames()->at(frame_index);
   1.148 +    printf("%2d  ", frame_index);
   1.149 +
   1.150 +    uint64_t instruction_address = frame->ReturnAddress();
   1.151 +
   1.152 +    if (frame->module) {
   1.153 +      printf("%s", PathnameStripper::File(frame->module->code_file()).c_str());
   1.154 +      if (!frame->function_name.empty()) {
   1.155 +        printf("!%s", frame->function_name.c_str());
   1.156 +        if (!frame->source_file_name.empty()) {
   1.157 +          string source_file = PathnameStripper::File(frame->source_file_name);
   1.158 +          printf(" [%s : %d + 0x%" PRIx64 "]",
   1.159 +                 source_file.c_str(),
   1.160 +                 frame->source_line,
   1.161 +                 instruction_address - frame->source_line_base);
   1.162 +        } else {
   1.163 +          printf(" + 0x%" PRIx64, instruction_address - frame->function_base);
   1.164 +        }
   1.165 +      } else {
   1.166 +        printf(" + 0x%" PRIx64,
   1.167 +               instruction_address - frame->module->base_address());
   1.168 +      }
   1.169 +    } else {
   1.170 +      printf("0x%" PRIx64, instruction_address);
   1.171 +    }
   1.172 +    printf("\n ");
   1.173 +
   1.174 +    int sequence = 0;
   1.175 +    if (cpu == "x86") {
   1.176 +      const StackFrameX86 *frame_x86 =
   1.177 +        reinterpret_cast<const StackFrameX86*>(frame);
   1.178 +
   1.179 +      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EIP)
   1.180 +        sequence = PrintRegister("eip", frame_x86->context.eip, sequence);
   1.181 +      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESP)
   1.182 +        sequence = PrintRegister("esp", frame_x86->context.esp, sequence);
   1.183 +      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EBP)
   1.184 +        sequence = PrintRegister("ebp", frame_x86->context.ebp, sequence);
   1.185 +      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EBX)
   1.186 +        sequence = PrintRegister("ebx", frame_x86->context.ebx, sequence);
   1.187 +      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESI)
   1.188 +        sequence = PrintRegister("esi", frame_x86->context.esi, sequence);
   1.189 +      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EDI)
   1.190 +        sequence = PrintRegister("edi", frame_x86->context.edi, sequence);
   1.191 +      if (frame_x86->context_validity == StackFrameX86::CONTEXT_VALID_ALL) {
   1.192 +        sequence = PrintRegister("eax", frame_x86->context.eax, sequence);
   1.193 +        sequence = PrintRegister("ecx", frame_x86->context.ecx, sequence);
   1.194 +        sequence = PrintRegister("edx", frame_x86->context.edx, sequence);
   1.195 +        sequence = PrintRegister("efl", frame_x86->context.eflags, sequence);
   1.196 +      }
   1.197 +    } else if (cpu == "ppc") {
   1.198 +      const StackFramePPC *frame_ppc =
   1.199 +        reinterpret_cast<const StackFramePPC*>(frame);
   1.200 +
   1.201 +      if (frame_ppc->context_validity & StackFramePPC::CONTEXT_VALID_SRR0)
   1.202 +        sequence = PrintRegister("srr0", frame_ppc->context.srr0, sequence);
   1.203 +      if (frame_ppc->context_validity & StackFramePPC::CONTEXT_VALID_GPR1)
   1.204 +        sequence = PrintRegister("r1", frame_ppc->context.gpr[1], sequence);
   1.205 +    } else if (cpu == "amd64") {
   1.206 +      const StackFrameAMD64 *frame_amd64 =
   1.207 +        reinterpret_cast<const StackFrameAMD64*>(frame);
   1.208 +
   1.209 +      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RBX)
   1.210 +        sequence = PrintRegister64("rbx", frame_amd64->context.rbx, sequence);
   1.211 +      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R12)
   1.212 +        sequence = PrintRegister64("r12", frame_amd64->context.r12, sequence);
   1.213 +      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R13)
   1.214 +        sequence = PrintRegister64("r13", frame_amd64->context.r13, sequence);
   1.215 +      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R14)
   1.216 +        sequence = PrintRegister64("r14", frame_amd64->context.r14, sequence);
   1.217 +      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R15)
   1.218 +        sequence = PrintRegister64("r15", frame_amd64->context.r15, sequence);
   1.219 +      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RIP)
   1.220 +        sequence = PrintRegister64("rip", frame_amd64->context.rip, sequence);
   1.221 +      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RSP)
   1.222 +        sequence = PrintRegister64("rsp", frame_amd64->context.rsp, sequence);
   1.223 +      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RBP)
   1.224 +        sequence = PrintRegister64("rbp", frame_amd64->context.rbp, sequence);
   1.225 +    } else if (cpu == "sparc") {
   1.226 +      const StackFrameSPARC *frame_sparc =
   1.227 +        reinterpret_cast<const StackFrameSPARC*>(frame);
   1.228 +
   1.229 +      if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_SP)
   1.230 +        sequence = PrintRegister("sp", frame_sparc->context.g_r[14], sequence);
   1.231 +      if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_FP)
   1.232 +        sequence = PrintRegister("fp", frame_sparc->context.g_r[30], sequence);
   1.233 +      if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_PC)
   1.234 +        sequence = PrintRegister("pc", frame_sparc->context.pc, sequence);
   1.235 +    } else if (cpu == "arm") {
   1.236 +      const StackFrameARM *frame_arm =
   1.237 +        reinterpret_cast<const StackFrameARM*>(frame);
   1.238 +
   1.239 +      // Argument registers (caller-saves), which will likely only be valid
   1.240 +      // for the youngest frame.
   1.241 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R0)
   1.242 +        sequence = PrintRegister("r0", frame_arm->context.iregs[0], sequence);
   1.243 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R1)
   1.244 +        sequence = PrintRegister("r1", frame_arm->context.iregs[1], sequence);
   1.245 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R2)
   1.246 +        sequence = PrintRegister("r2", frame_arm->context.iregs[2], sequence);
   1.247 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R3)
   1.248 +        sequence = PrintRegister("r3", frame_arm->context.iregs[3], sequence);
   1.249 +
   1.250 +      // General-purpose callee-saves registers.
   1.251 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R4)
   1.252 +        sequence = PrintRegister("r4", frame_arm->context.iregs[4], sequence);
   1.253 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R5)
   1.254 +        sequence = PrintRegister("r5", frame_arm->context.iregs[5], sequence);
   1.255 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R6)
   1.256 +        sequence = PrintRegister("r6", frame_arm->context.iregs[6], sequence);
   1.257 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R7)
   1.258 +        sequence = PrintRegister("r7", frame_arm->context.iregs[7], sequence);
   1.259 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R8)
   1.260 +        sequence = PrintRegister("r8", frame_arm->context.iregs[8], sequence);
   1.261 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R9)
   1.262 +        sequence = PrintRegister("r9", frame_arm->context.iregs[9], sequence);
   1.263 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R10)
   1.264 +        sequence = PrintRegister("r10", frame_arm->context.iregs[10], sequence);
   1.265 +
   1.266 +      // Registers with a dedicated or conventional purpose.
   1.267 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_FP)
   1.268 +        sequence = PrintRegister("fp", frame_arm->context.iregs[11], sequence);
   1.269 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_SP)
   1.270 +        sequence = PrintRegister("sp", frame_arm->context.iregs[13], sequence);
   1.271 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_LR)
   1.272 +        sequence = PrintRegister("lr", frame_arm->context.iregs[14], sequence);
   1.273 +      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_PC)
   1.274 +        sequence = PrintRegister("pc", frame_arm->context.iregs[15], sequence);
   1.275 +    }
   1.276 +    printf("\n    Found by: %s\n", frame->trust_description().c_str());
   1.277 +  }
   1.278 +}
   1.279 +
   1.280 +// PrintStackMachineReadable prints the call stack in |stack| to stdout,
   1.281 +// in the following machine readable pipe-delimited text format:
   1.282 +// thread number|frame number|module|function|source file|line|offset
   1.283 +//
   1.284 +// Module, function, source file, and source line may all be empty
   1.285 +// depending on availability.  The code offset follows the same rules as
   1.286 +// PrintStack above.
   1.287 +static void PrintStackMachineReadable(int thread_num, const CallStack *stack) {
   1.288 +  int frame_count = stack->frames()->size();
   1.289 +  for (int frame_index = 0; frame_index < frame_count; ++frame_index) {
   1.290 +    const StackFrame *frame = stack->frames()->at(frame_index);
   1.291 +    printf("%d%c%d%c", thread_num, kOutputSeparator, frame_index,
   1.292 +           kOutputSeparator);
   1.293 +
   1.294 +    uint64_t instruction_address = frame->ReturnAddress();
   1.295 +
   1.296 +    if (frame->module) {
   1.297 +      assert(!frame->module->code_file().empty());
   1.298 +      printf("%s", StripSeparator(PathnameStripper::File(
   1.299 +                     frame->module->code_file())).c_str());
   1.300 +      if (!frame->function_name.empty()) {
   1.301 +        printf("%c%s", kOutputSeparator,
   1.302 +               StripSeparator(frame->function_name).c_str());
   1.303 +        if (!frame->source_file_name.empty()) {
   1.304 +          printf("%c%s%c%d%c0x%" PRIx64,
   1.305 +                 kOutputSeparator,
   1.306 +                 StripSeparator(frame->source_file_name).c_str(),
   1.307 +                 kOutputSeparator,
   1.308 +                 frame->source_line,
   1.309 +                 kOutputSeparator,
   1.310 +                 instruction_address - frame->source_line_base);
   1.311 +        } else {
   1.312 +          printf("%c%c%c0x%" PRIx64,
   1.313 +                 kOutputSeparator,  // empty source file
   1.314 +                 kOutputSeparator,  // empty source line
   1.315 +                 kOutputSeparator,
   1.316 +                 instruction_address - frame->function_base);
   1.317 +        }
   1.318 +      } else {
   1.319 +        printf("%c%c%c%c0x%" PRIx64,
   1.320 +               kOutputSeparator,  // empty function name
   1.321 +               kOutputSeparator,  // empty source file
   1.322 +               kOutputSeparator,  // empty source line
   1.323 +               kOutputSeparator,
   1.324 +               instruction_address - frame->module->base_address());
   1.325 +      }
   1.326 +    } else {
   1.327 +      // the printf before this prints a trailing separator for module name
   1.328 +      printf("%c%c%c%c0x%" PRIx64,
   1.329 +             kOutputSeparator,  // empty function name
   1.330 +             kOutputSeparator,  // empty source file
   1.331 +             kOutputSeparator,  // empty source line
   1.332 +             kOutputSeparator,
   1.333 +             instruction_address);
   1.334 +    }
   1.335 +    printf("\n");
   1.336 +  }
   1.337 +}
   1.338 +
   1.339 +// ContainsModule checks whether a given |module| is in the vector
   1.340 +// |modules_without_symbols|.
   1.341 +static bool ContainsModule(
   1.342 +    const vector<const CodeModule*> *modules,
   1.343 +    const CodeModule *module) {
   1.344 +  assert(modules);
   1.345 +  assert(module);
   1.346 +  vector<const CodeModule*>::const_iterator iter;
   1.347 +  for (iter = modules->begin(); iter != modules->end(); ++iter) {
   1.348 +    if (module->debug_file().compare((*iter)->debug_file()) == 0 &&
   1.349 +        module->debug_identifier().compare((*iter)->debug_identifier()) == 0) {
   1.350 +      return true;
   1.351 +    }
   1.352 +  }
   1.353 +  return false;
   1.354 +}
   1.355 +
   1.356 +// PrintModule prints a single |module| to stdout.
   1.357 +// |modules_without_symbols| should contain the list of modules that were
   1.358 +// confirmed to be missing their symbols during the stack walk.
   1.359 +static void PrintModule(
   1.360 +    const CodeModule *module,
   1.361 +    const vector<const CodeModule*> *modules_without_symbols,
   1.362 +    uint64_t main_address) {
   1.363 +  string missing_symbols;
   1.364 +  if (ContainsModule(modules_without_symbols, module)) {
   1.365 +    missing_symbols = "  (WARNING: No symbols, " +
   1.366 +        PathnameStripper::File(module->debug_file()) + ", " +
   1.367 +        module->debug_identifier() + ")";
   1.368 +  }
   1.369 +  uint64_t base_address = module->base_address();
   1.370 +  printf("0x%08" PRIx64 " - 0x%08" PRIx64 "  %s  %s%s%s\n",
   1.371 +         base_address, base_address + module->size() - 1,
   1.372 +         PathnameStripper::File(module->code_file()).c_str(),
   1.373 +         module->version().empty() ? "???" : module->version().c_str(),
   1.374 +         main_address != 0 && base_address == main_address ? "  (main)" : "",
   1.375 +         missing_symbols.c_str());
   1.376 +}
   1.377 +
   1.378 +// PrintModules prints the list of all loaded |modules| to stdout.
   1.379 +// |modules_without_symbols| should contain the list of modules that were
   1.380 +// confirmed to be missing their symbols during the stack walk.
   1.381 +static void PrintModules(
   1.382 +    const CodeModules *modules,
   1.383 +    const vector<const CodeModule*> *modules_without_symbols) {
   1.384 +  if (!modules)
   1.385 +    return;
   1.386 +
   1.387 +  printf("\n");
   1.388 +  printf("Loaded modules:\n");
   1.389 +
   1.390 +  uint64_t main_address = 0;
   1.391 +  const CodeModule *main_module = modules->GetMainModule();
   1.392 +  if (main_module) {
   1.393 +    main_address = main_module->base_address();
   1.394 +  }
   1.395 +
   1.396 +  unsigned int module_count = modules->module_count();
   1.397 +  for (unsigned int module_sequence = 0;
   1.398 +       module_sequence < module_count;
   1.399 +       ++module_sequence) {
   1.400 +    const CodeModule *module = modules->GetModuleAtSequence(module_sequence);
   1.401 +    PrintModule(module, modules_without_symbols, main_address);
   1.402 +  }
   1.403 +}
   1.404 +
   1.405 +// PrintModulesMachineReadable outputs a list of loaded modules,
   1.406 +// one per line, in the following machine-readable pipe-delimited
   1.407 +// text format:
   1.408 +// Module|{Module Filename}|{Version}|{Debug Filename}|{Debug Identifier}|
   1.409 +// {Base Address}|{Max Address}|{Main}
   1.410 +static void PrintModulesMachineReadable(const CodeModules *modules) {
   1.411 +  if (!modules)
   1.412 +    return;
   1.413 +
   1.414 +  uint64_t main_address = 0;
   1.415 +  const CodeModule *main_module = modules->GetMainModule();
   1.416 +  if (main_module) {
   1.417 +    main_address = main_module->base_address();
   1.418 +  }
   1.419 +
   1.420 +  unsigned int module_count = modules->module_count();
   1.421 +  for (unsigned int module_sequence = 0;
   1.422 +       module_sequence < module_count;
   1.423 +       ++module_sequence) {
   1.424 +    const CodeModule *module = modules->GetModuleAtSequence(module_sequence);
   1.425 +    uint64_t base_address = module->base_address();
   1.426 +    printf("Module%c%s%c%s%c%s%c%s%c0x%08" PRIx64 "%c0x%08" PRIx64 "%c%d\n",
   1.427 +           kOutputSeparator,
   1.428 +           StripSeparator(PathnameStripper::File(module->code_file())).c_str(),
   1.429 +           kOutputSeparator, StripSeparator(module->version()).c_str(),
   1.430 +           kOutputSeparator,
   1.431 +           StripSeparator(PathnameStripper::File(module->debug_file())).c_str(),
   1.432 +           kOutputSeparator,
   1.433 +           StripSeparator(module->debug_identifier()).c_str(),
   1.434 +           kOutputSeparator, base_address,
   1.435 +           kOutputSeparator, base_address + module->size() - 1,
   1.436 +           kOutputSeparator,
   1.437 +           main_module != NULL && base_address == main_address ? 1 : 0);
   1.438 +  }
   1.439 +}
   1.440 +
   1.441 +static void PrintProcessState(const ProcessState& process_state) {
   1.442 +  // Print OS and CPU information.
   1.443 +  string cpu = process_state.system_info()->cpu;
   1.444 +  string cpu_info = process_state.system_info()->cpu_info;
   1.445 +  printf("Operating system: %s\n", process_state.system_info()->os.c_str());
   1.446 +  printf("                  %s\n",
   1.447 +         process_state.system_info()->os_version.c_str());
   1.448 +  printf("CPU: %s\n", cpu.c_str());
   1.449 +  if (!cpu_info.empty()) {
   1.450 +    // This field is optional.
   1.451 +    printf("     %s\n", cpu_info.c_str());
   1.452 +  }
   1.453 +  printf("     %d CPU%s\n",
   1.454 +         process_state.system_info()->cpu_count,
   1.455 +         process_state.system_info()->cpu_count != 1 ? "s" : "");
   1.456 +  printf("\n");
   1.457 +
   1.458 +  // Print crash information.
   1.459 +  if (process_state.crashed()) {
   1.460 +    printf("Crash reason:  %s\n", process_state.crash_reason().c_str());
   1.461 +    printf("Crash address: 0x%" PRIx64 "\n", process_state.crash_address());
   1.462 +  } else {
   1.463 +    printf("No crash\n");
   1.464 +  }
   1.465 +
   1.466 +  string assertion = process_state.assertion();
   1.467 +  if (!assertion.empty()) {
   1.468 +    printf("Assertion: %s\n", assertion.c_str());
   1.469 +  }
   1.470 +
   1.471 +  // If the thread that requested the dump is known, print it first.
   1.472 +  int requesting_thread = process_state.requesting_thread();
   1.473 +  if (requesting_thread != -1) {
   1.474 +    printf("\n");
   1.475 +    printf("Thread %d (%s)\n",
   1.476 +          requesting_thread,
   1.477 +          process_state.crashed() ? "crashed" :
   1.478 +                                    "requested dump, did not crash");
   1.479 +    PrintStack(process_state.threads()->at(requesting_thread), cpu);
   1.480 +  }
   1.481 +
   1.482 +  // Print all of the threads in the dump.
   1.483 +  int thread_count = process_state.threads()->size();
   1.484 +  for (int thread_index = 0; thread_index < thread_count; ++thread_index) {
   1.485 +    if (thread_index != requesting_thread) {
   1.486 +      // Don't print the crash thread again, it was already printed.
   1.487 +      printf("\n");
   1.488 +      printf("Thread %d\n", thread_index);
   1.489 +      PrintStack(process_state.threads()->at(thread_index), cpu);
   1.490 +    }
   1.491 +  }
   1.492 +
   1.493 +  PrintModules(process_state.modules(),
   1.494 +               process_state.modules_without_symbols());
   1.495 +}
   1.496 +
   1.497 +static void PrintProcessStateMachineReadable(const ProcessState& process_state)
   1.498 +{
   1.499 +  // Print OS and CPU information.
   1.500 +  // OS|{OS Name}|{OS Version}
   1.501 +  // CPU|{CPU Name}|{CPU Info}|{Number of CPUs}
   1.502 +  printf("OS%c%s%c%s\n", kOutputSeparator,
   1.503 +         StripSeparator(process_state.system_info()->os).c_str(),
   1.504 +         kOutputSeparator,
   1.505 +         StripSeparator(process_state.system_info()->os_version).c_str());
   1.506 +  printf("CPU%c%s%c%s%c%d\n", kOutputSeparator,
   1.507 +         StripSeparator(process_state.system_info()->cpu).c_str(),
   1.508 +         kOutputSeparator,
   1.509 +         // this may be empty
   1.510 +         StripSeparator(process_state.system_info()->cpu_info).c_str(),
   1.511 +         kOutputSeparator,
   1.512 +         process_state.system_info()->cpu_count);
   1.513 +
   1.514 +  int requesting_thread = process_state.requesting_thread();
   1.515 +
   1.516 +  // Print crash information.
   1.517 +  // Crash|{Crash Reason}|{Crash Address}|{Crashed Thread}
   1.518 +  printf("Crash%c", kOutputSeparator);
   1.519 +  if (process_state.crashed()) {
   1.520 +    printf("%s%c0x%" PRIx64 "%c",
   1.521 +           StripSeparator(process_state.crash_reason()).c_str(),
   1.522 +           kOutputSeparator, process_state.crash_address(), kOutputSeparator);
   1.523 +  } else {
   1.524 +    // print assertion info, if available, in place of crash reason,
   1.525 +    // instead of the unhelpful "No crash"
   1.526 +    string assertion = process_state.assertion();
   1.527 +    if (!assertion.empty()) {
   1.528 +      printf("%s%c%c", StripSeparator(assertion).c_str(),
   1.529 +             kOutputSeparator, kOutputSeparator);
   1.530 +    } else {
   1.531 +      printf("No crash%c%c", kOutputSeparator, kOutputSeparator);
   1.532 +    }
   1.533 +  }
   1.534 +
   1.535 +  if (requesting_thread != -1) {
   1.536 +    printf("%d\n", requesting_thread);
   1.537 +  } else {
   1.538 +    printf("\n");
   1.539 +  }
   1.540 +
   1.541 +  PrintModulesMachineReadable(process_state.modules());
   1.542 +
   1.543 +  // blank line to indicate start of threads
   1.544 +  printf("\n");
   1.545 +
   1.546 +  // If the thread that requested the dump is known, print it first.
   1.547 +  if (requesting_thread != -1) {
   1.548 +    PrintStackMachineReadable(requesting_thread,
   1.549 +                              process_state.threads()->at(requesting_thread));
   1.550 +  }
   1.551 +
   1.552 +  // Print all of the threads in the dump.
   1.553 +  int thread_count = process_state.threads()->size();
   1.554 +  for (int thread_index = 0; thread_index < thread_count; ++thread_index) {
   1.555 +    if (thread_index != requesting_thread) {
   1.556 +      // Don't print the crash thread again, it was already printed.
   1.557 +      PrintStackMachineReadable(thread_index,
   1.558 +                                process_state.threads()->at(thread_index));
   1.559 +    }
   1.560 +  }
   1.561 +}
   1.562 +
   1.563 +// Processes |minidump_file| using MinidumpProcessor.  |symbol_path|, if
   1.564 +// non-empty, is the base directory of a symbol storage area, laid out in
   1.565 +// the format required by SimpleSymbolSupplier.  If such a storage area
   1.566 +// is specified, it is made available for use by the MinidumpProcessor.
   1.567 +//
   1.568 +// Returns the value of MinidumpProcessor::Process.  If processing succeeds,
   1.569 +// prints identifying OS and CPU information from the minidump, crash
   1.570 +// information if the minidump was produced as a result of a crash, and
   1.571 +// call stacks for each thread contained in the minidump.  All information
   1.572 +// is printed to stdout.
   1.573 +static bool PrintMinidumpProcess(const string &minidump_file,
   1.574 +                                 const vector<string> &symbol_paths,
   1.575 +                                 bool machine_readable) {
   1.576 +  scoped_ptr<SimpleSymbolSupplier> symbol_supplier;
   1.577 +  if (!symbol_paths.empty()) {
   1.578 +    // TODO(mmentovai): check existence of symbol_path if specified?
   1.579 +    symbol_supplier.reset(new SimpleSymbolSupplier(symbol_paths));
   1.580 +  }
   1.581 +
   1.582 +  BasicSourceLineResolver resolver;
   1.583 +  MinidumpProcessor minidump_processor(symbol_supplier.get(), &resolver);
   1.584 +
   1.585 +  // Process the minidump.
   1.586 +  ProcessState process_state;
   1.587 +  if (minidump_processor.Process(minidump_file, &process_state) !=
   1.588 +      google_breakpad::PROCESS_OK) {
   1.589 +    BPLOG(ERROR) << "MinidumpProcessor::Process failed";
   1.590 +    return false;
   1.591 +  }
   1.592 +
   1.593 +  if (machine_readable) {
   1.594 +    PrintProcessStateMachineReadable(process_state);
   1.595 +  } else {
   1.596 +    PrintProcessState(process_state);
   1.597 +  }
   1.598 +
   1.599 +  return true;
   1.600 +}
   1.601 +
   1.602 +}  // namespace
   1.603 +
   1.604 +static void usage(const char *program_name) {
   1.605 +  fprintf(stderr, "usage: %s [-m] <minidump-file> [symbol-path ...]\n"
   1.606 +          "    -m : Output in machine-readable format\n",
   1.607 +          program_name);
   1.608 +}
   1.609 +
   1.610 +int main(int argc, char **argv) {
   1.611 +  BPLOG_INIT(&argc, &argv);
   1.612 +
   1.613 +  if (argc < 2) {
   1.614 +    usage(argv[0]);
   1.615 +    return 1;
   1.616 +  }
   1.617 +
   1.618 +  const char *minidump_file;
   1.619 +  bool machine_readable;
   1.620 +  int symbol_path_arg;
   1.621 +
   1.622 +  if (strcmp(argv[1], "-m") == 0) {
   1.623 +    if (argc < 3) {
   1.624 +      usage(argv[0]);
   1.625 +      return 1;
   1.626 +    }
   1.627 +
   1.628 +    machine_readable = true;
   1.629 +    minidump_file = argv[2];
   1.630 +    symbol_path_arg = 3;
   1.631 +  } else {
   1.632 +    machine_readable = false;
   1.633 +    minidump_file = argv[1];
   1.634 +    symbol_path_arg = 2;
   1.635 +  }
   1.636 +
   1.637 +  // extra arguments are symbol paths
   1.638 +  std::vector<string> symbol_paths;
   1.639 +  if (argc > symbol_path_arg) {
   1.640 +    for (int argi = symbol_path_arg; argi < argc; ++argi)
   1.641 +      symbol_paths.push_back(argv[argi]);
   1.642 +  }
   1.643 +
   1.644 +  return PrintMinidumpProcess(minidump_file,
   1.645 +                              symbol_paths,
   1.646 +                              machine_readable) ? 0 : 1;
   1.647 +}

mercurial