PageRenderTime 72ms CodeModel.GetById 22ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/processor/minidump_stackwalk.cc

http://github.com/tomahawk-player/tomahawk
C++ | 587 lines | 429 code | 60 blank | 98 comment | 111 complexity | eec97b55611ca5472c84fcf70d59101c MD5 | raw file
  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// minidump_stackwalk.cc: Process a minidump with MinidumpProcessor, printing
 31// the results, including stack traces.
 32//
 33// Author: Mark Mentovai
 34
 35#include <stdio.h>
 36#include <stdlib.h>
 37#include <string.h>
 38
 39#include <string>
 40#include <vector>
 41
 42#include "google_breakpad/processor/basic_source_line_resolver.h"
 43#include "google_breakpad/processor/call_stack.h"
 44#include "google_breakpad/processor/code_module.h"
 45#include "google_breakpad/processor/code_modules.h"
 46#include "google_breakpad/processor/minidump.h"
 47#include "google_breakpad/processor/minidump_processor.h"
 48#include "google_breakpad/processor/process_state.h"
 49#include "google_breakpad/processor/stack_frame_cpu.h"
 50#include "processor/logging.h"
 51#include "processor/pathname_stripper.h"
 52#include "processor/scoped_ptr.h"
 53#include "processor/simple_symbol_supplier.h"
 54
 55namespace {
 56
 57using std::string;
 58using std::vector;
 59using google_breakpad::BasicSourceLineResolver;
 60using google_breakpad::CallStack;
 61using google_breakpad::CodeModule;
 62using google_breakpad::CodeModules;
 63using google_breakpad::MinidumpModule;
 64using google_breakpad::MinidumpProcessor;
 65using google_breakpad::PathnameStripper;
 66using google_breakpad::ProcessState;
 67using google_breakpad::scoped_ptr;
 68using google_breakpad::SimpleSymbolSupplier;
 69using google_breakpad::StackFrame;
 70using google_breakpad::StackFramePPC;
 71using google_breakpad::StackFrameSPARC;
 72using google_breakpad::StackFrameX86;
 73using google_breakpad::StackFrameAMD64;
 74using google_breakpad::StackFrameARM;
 75
 76// Separator character for machine readable output.
 77static const char kOutputSeparator = '|';
 78
 79// PrintRegister prints a register's name and value to stdout.  It will
 80// print four registers on a line.  For the first register in a set,
 81// pass 0 for |start_col|.  For registers in a set, pass the most recent
 82// return value of PrintRegister.
 83// The caller is responsible for printing the final newline after a set
 84// of registers is completely printed, regardless of the number of calls
 85// to PrintRegister.
 86static const int kMaxWidth = 80;  // optimize for an 80-column terminal
 87static int PrintRegister(const char *name, u_int32_t value, int start_col) {
 88  char buffer[64];
 89  snprintf(buffer, sizeof(buffer), " %5s = 0x%08x", name, value);
 90
 91  if (start_col + strlen(buffer) > kMaxWidth) {
 92    start_col = 0;
 93    printf("\n ");
 94  }
 95  fputs(buffer, stdout);
 96
 97  return start_col + strlen(buffer);
 98}
 99
100// PrintRegister64 does the same thing, but for 64-bit registers.
101static int PrintRegister64(const char *name, u_int64_t value, int start_col) {
102  char buffer[64];
103  snprintf(buffer, sizeof(buffer), " %5s = 0x%016" PRIx64 , name, value);
104
105  if (start_col + strlen(buffer) > kMaxWidth) {
106    start_col = 0;
107    printf("\n ");
108  }
109  fputs(buffer, stdout);
110
111  return start_col + strlen(buffer);
112}
113
114// StripSeparator takes a string |original| and returns a copy
115// of the string with all occurences of |kOutputSeparator| removed.
116static string StripSeparator(const string &original) {
117  string result = original;
118  string::size_type position = 0;
119  while ((position = result.find(kOutputSeparator, position)) != string::npos) {
120    result.erase(position, 1);
121  }
122  position = 0;
123  while ((position = result.find('\n', position)) != string::npos) {
124    result.erase(position, 1);
125  }
126  return result;
127}
128
129// PrintStack prints the call stack in |stack| to stdout, in a reasonably
130// useful form.  Module, function, and source file names are displayed if
131// they are available.  The code offset to the base code address of the
132// source line, function, or module is printed, preferring them in that
133// order.  If no source line, function, or module information is available,
134// an absolute code offset is printed.
135//
136// If |cpu| is a recognized CPU name, relevant register state for each stack
137// frame printed is also output, if available.
138static void PrintStack(const CallStack *stack, const string &cpu) {
139  int frame_count = stack->frames()->size();
140  for (int frame_index = 0; frame_index < frame_count; ++frame_index) {
141    const StackFrame *frame = stack->frames()->at(frame_index);
142    printf("%2d  ", frame_index);
143
144    if (frame->module) {
145      printf("%s", PathnameStripper::File(frame->module->code_file()).c_str());
146      if (!frame->function_name.empty()) {
147        printf("!%s", frame->function_name.c_str());
148        if (!frame->source_file_name.empty()) {
149          string source_file = PathnameStripper::File(frame->source_file_name);
150          printf(" [%s : %d + 0x%" PRIx64 "]",
151                 source_file.c_str(),
152                 frame->source_line,
153                 frame->instruction - frame->source_line_base);
154        } else {
155          printf(" + 0x%" PRIx64, frame->instruction - frame->function_base);
156        }
157      } else {
158        printf(" + 0x%" PRIx64,
159               frame->instruction - frame->module->base_address());
160      }
161    } else {
162      printf("0x%" PRIx64, frame->instruction);
163    }
164    printf("\n ");
165
166    int sequence = 0;
167    if (cpu == "x86") {
168      const StackFrameX86 *frame_x86 =
169        reinterpret_cast<const StackFrameX86*>(frame);
170
171      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EIP)
172        sequence = PrintRegister("eip", frame_x86->context.eip, sequence);
173      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESP)
174        sequence = PrintRegister("esp", frame_x86->context.esp, sequence);
175      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EBP)
176        sequence = PrintRegister("ebp", frame_x86->context.ebp, sequence);
177      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EBX)
178        sequence = PrintRegister("ebx", frame_x86->context.ebx, sequence);
179      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_ESI)
180        sequence = PrintRegister("esi", frame_x86->context.esi, sequence);
181      if (frame_x86->context_validity & StackFrameX86::CONTEXT_VALID_EDI)
182        sequence = PrintRegister("edi", frame_x86->context.edi, sequence);
183      if (frame_x86->context_validity == StackFrameX86::CONTEXT_VALID_ALL) {
184        sequence = PrintRegister("eax", frame_x86->context.eax, sequence);
185        sequence = PrintRegister("ecx", frame_x86->context.ecx, sequence);
186        sequence = PrintRegister("edx", frame_x86->context.edx, sequence);
187        sequence = PrintRegister("efl", frame_x86->context.eflags, sequence);
188      }
189    } else if (cpu == "ppc") {
190      const StackFramePPC *frame_ppc =
191        reinterpret_cast<const StackFramePPC*>(frame);
192
193      if (frame_ppc->context_validity & StackFramePPC::CONTEXT_VALID_SRR0)
194        sequence = PrintRegister("srr0", frame_ppc->context.srr0, sequence);
195      if (frame_ppc->context_validity & StackFramePPC::CONTEXT_VALID_GPR1)
196        sequence = PrintRegister("r1", frame_ppc->context.gpr[1], sequence);
197    } else if (cpu == "amd64") {
198      const StackFrameAMD64 *frame_amd64 =
199        reinterpret_cast<const StackFrameAMD64*>(frame);
200
201      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RBX)
202        sequence = PrintRegister64("rbx", frame_amd64->context.rbx, sequence);
203      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R12)
204        sequence = PrintRegister64("r12", frame_amd64->context.r12, sequence);
205      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R13)
206        sequence = PrintRegister64("r13", frame_amd64->context.r13, sequence);
207      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R14)
208        sequence = PrintRegister64("r14", frame_amd64->context.r14, sequence);
209      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R15)
210        sequence = PrintRegister64("r15", frame_amd64->context.r15, sequence);
211      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RIP)
212        sequence = PrintRegister64("rip", frame_amd64->context.rip, sequence);
213      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RSP)
214        sequence = PrintRegister64("rsp", frame_amd64->context.rsp, sequence);
215      if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RBP)
216        sequence = PrintRegister64("rbp", frame_amd64->context.rbp, sequence);
217    } else if (cpu == "sparc") {
218      const StackFrameSPARC *frame_sparc =
219        reinterpret_cast<const StackFrameSPARC*>(frame);
220
221      if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_SP)
222        sequence = PrintRegister("sp", frame_sparc->context.g_r[14], sequence);
223      if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_FP)
224        sequence = PrintRegister("fp", frame_sparc->context.g_r[30], sequence);
225      if (frame_sparc->context_validity & StackFrameSPARC::CONTEXT_VALID_PC)
226        sequence = PrintRegister("pc", frame_sparc->context.pc, sequence);
227    } else if (cpu == "arm") {
228      const StackFrameARM *frame_arm =
229        reinterpret_cast<const StackFrameARM*>(frame);
230
231      // General-purpose callee-saves registers.
232      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R4)
233        sequence = PrintRegister("r4", frame_arm->context.iregs[4], sequence);
234      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R5)
235        sequence = PrintRegister("r5", frame_arm->context.iregs[5], sequence);
236      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R6)
237        sequence = PrintRegister("r6", frame_arm->context.iregs[6], sequence);
238      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R7)
239        sequence = PrintRegister("r7", frame_arm->context.iregs[7], sequence);
240      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R8)
241        sequence = PrintRegister("r8", frame_arm->context.iregs[8], sequence);
242      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R9)
243        sequence = PrintRegister("r9", frame_arm->context.iregs[9], sequence);
244      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_R10)
245        sequence = PrintRegister("r10", frame_arm->context.iregs[10], sequence);
246
247      // Registers with a dedicated or conventional purpose.
248      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_FP)
249        sequence = PrintRegister("fp", frame_arm->context.iregs[11], sequence);
250      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_SP)
251        sequence = PrintRegister("sp", frame_arm->context.iregs[13], sequence);
252      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_LR)
253        sequence = PrintRegister("lr", frame_arm->context.iregs[14], sequence);
254      if (frame_arm->context_validity & StackFrameARM::CONTEXT_VALID_PC)
255        sequence = PrintRegister("pc", frame_arm->context.iregs[15], sequence);
256    }
257    printf("\n    Found by: %s\n", frame->trust_description().c_str());
258  }
259}
260
261// PrintStackMachineReadable prints the call stack in |stack| to stdout,
262// in the following machine readable pipe-delimited text format:
263// thread number|frame number|module|function|source file|line|offset
264//
265// Module, function, source file, and source line may all be empty
266// depending on availability.  The code offset follows the same rules as
267// PrintStack above.
268static void PrintStackMachineReadable(int thread_num, const CallStack *stack) {
269  int frame_count = stack->frames()->size();
270  for (int frame_index = 0; frame_index < frame_count; ++frame_index) {
271    const StackFrame *frame = stack->frames()->at(frame_index);
272    printf("%d%c%d%c", thread_num, kOutputSeparator, frame_index,
273           kOutputSeparator);
274
275    if (frame->module) {
276      assert(!frame->module->code_file().empty());
277      printf("%s", StripSeparator(PathnameStripper::File(
278                     frame->module->code_file())).c_str());
279      if (!frame->function_name.empty()) {
280        printf("%c%s", kOutputSeparator,
281               StripSeparator(frame->function_name).c_str());
282        if (!frame->source_file_name.empty()) {
283          printf("%c%s%c%d%c0x%" PRIx64,
284                 kOutputSeparator,
285                 StripSeparator(frame->source_file_name).c_str(),
286                 kOutputSeparator,
287                 frame->source_line,
288                 kOutputSeparator,
289                 frame->instruction - frame->source_line_base);
290        } else {
291          printf("%c%c%c0x%" PRIx64,
292                 kOutputSeparator,  // empty source file
293                 kOutputSeparator,  // empty source line
294                 kOutputSeparator,
295                 frame->instruction - frame->function_base);
296        }
297      } else {
298        printf("%c%c%c%c0x%" PRIx64,
299               kOutputSeparator,  // empty function name
300               kOutputSeparator,  // empty source file
301               kOutputSeparator,  // empty source line
302               kOutputSeparator,
303               frame->instruction - frame->module->base_address());
304      }
305    } else {
306      // the printf before this prints a trailing separator for module name
307      printf("%c%c%c%c0x%" PRIx64,
308             kOutputSeparator,  // empty function name
309             kOutputSeparator,  // empty source file
310             kOutputSeparator,  // empty source line
311             kOutputSeparator,
312             frame->instruction);
313    }
314    printf("\n");
315  }
316}
317
318static void PrintModules(const CodeModules *modules) {
319  if (!modules)
320    return;
321
322  printf("\n");
323  printf("Loaded modules:\n");
324
325  u_int64_t main_address = 0;
326  const CodeModule *main_module = modules->GetMainModule();
327  if (main_module) {
328    main_address = main_module->base_address();
329  }
330
331  unsigned int module_count = modules->module_count();
332  for (unsigned int module_sequence = 0;
333       module_sequence < module_count;
334       ++module_sequence) {
335    const CodeModule *module = modules->GetModuleAtSequence(module_sequence);
336    u_int64_t base_address = module->base_address();
337    printf("0x%08" PRIx64 " - 0x%08" PRIx64 "  %s  %s%s\n",
338           base_address, base_address + module->size() - 1,
339           PathnameStripper::File(module->code_file()).c_str(),
340           module->version().empty() ? "???" : module->version().c_str(),
341           main_module != NULL && base_address == main_address ?
342               "  (main)" : "");
343  }
344}
345
346// PrintModulesMachineReadable outputs a list of loaded modules,
347// one per line, in the following machine-readable pipe-delimited
348// text format:
349// Module|{Module Filename}|{Version}|{Debug Filename}|{Debug Identifier}|
350// {Base Address}|{Max Address}|{Main}
351static void PrintModulesMachineReadable(const CodeModules *modules) {
352  if (!modules)
353    return;
354
355  u_int64_t main_address = 0;
356  const CodeModule *main_module = modules->GetMainModule();
357  if (main_module) {
358    main_address = main_module->base_address();
359  }
360
361  unsigned int module_count = modules->module_count();
362  for (unsigned int module_sequence = 0;
363       module_sequence < module_count;
364       ++module_sequence) {
365    const CodeModule *module = modules->GetModuleAtSequence(module_sequence);
366    u_int64_t base_address = module->base_address();
367    printf("Module%c%s%c%s%c%s%c%s%c0x%08" PRIx64 "%c0x%08" PRIx64 "%c%d\n",
368           kOutputSeparator,
369           StripSeparator(PathnameStripper::File(module->code_file())).c_str(),
370           kOutputSeparator, StripSeparator(module->version()).c_str(),
371           kOutputSeparator,
372           StripSeparator(PathnameStripper::File(module->debug_file())).c_str(),
373           kOutputSeparator,
374           StripSeparator(module->debug_identifier()).c_str(),
375           kOutputSeparator, base_address,
376           kOutputSeparator, base_address + module->size() - 1,
377           kOutputSeparator,
378           main_module != NULL && base_address == main_address ? 1 : 0);
379  }
380}
381
382static void PrintProcessState(const ProcessState& process_state) {
383  // Print OS and CPU information.
384  string cpu = process_state.system_info()->cpu;
385  string cpu_info = process_state.system_info()->cpu_info;
386  printf("Operating system: %s\n", process_state.system_info()->os.c_str());
387  printf("                  %s\n",
388         process_state.system_info()->os_version.c_str());
389  printf("CPU: %s\n", cpu.c_str());
390  if (!cpu_info.empty()) {
391    // This field is optional.
392    printf("     %s\n", cpu_info.c_str());
393  }
394  printf("     %d CPU%s\n",
395         process_state.system_info()->cpu_count,
396         process_state.system_info()->cpu_count != 1 ? "s" : "");
397  printf("\n");
398
399  // Print crash information.
400  if (process_state.crashed()) {
401    printf("Crash reason:  %s\n", process_state.crash_reason().c_str());
402    printf("Crash address: 0x%" PRIx64 "\n", process_state.crash_address());
403  } else {
404    printf("No crash\n");
405  }
406
407  string assertion = process_state.assertion();
408  if (!assertion.empty()) {
409    printf("Assertion: %s\n", assertion.c_str());
410  }
411
412  // If the thread that requested the dump is known, print it first.
413  int requesting_thread = process_state.requesting_thread();
414  if (requesting_thread != -1) {
415    printf("\n");
416    printf("Thread %d (%s)\n",
417          requesting_thread,
418          process_state.crashed() ? "crashed" :
419                                    "requested dump, did not crash");
420    PrintStack(process_state.threads()->at(requesting_thread), cpu);
421  }
422
423  // Print all of the threads in the dump.
424  int thread_count = process_state.threads()->size();
425  for (int thread_index = 0; thread_index < thread_count; ++thread_index) {
426    if (thread_index != requesting_thread) {
427      // Don't print the crash thread again, it was already printed.
428      printf("\n");
429      printf("Thread %d\n", thread_index);
430      PrintStack(process_state.threads()->at(thread_index), cpu);
431    }
432  }
433
434  PrintModules(process_state.modules());
435}
436
437static void PrintProcessStateMachineReadable(const ProcessState& process_state)
438{
439  // Print OS and CPU information.
440  // OS|{OS Name}|{OS Version}
441  // CPU|{CPU Name}|{CPU Info}|{Number of CPUs}
442  printf("OS%c%s%c%s\n", kOutputSeparator,
443         StripSeparator(process_state.system_info()->os).c_str(),
444         kOutputSeparator,
445         StripSeparator(process_state.system_info()->os_version).c_str());
446  printf("CPU%c%s%c%s%c%d\n", kOutputSeparator,
447         StripSeparator(process_state.system_info()->cpu).c_str(),
448         kOutputSeparator,
449         // this may be empty
450         StripSeparator(process_state.system_info()->cpu_info).c_str(),
451         kOutputSeparator,
452         process_state.system_info()->cpu_count);
453
454  int requesting_thread = process_state.requesting_thread();
455
456  // Print crash information.
457  // Crash|{Crash Reason}|{Crash Address}|{Crashed Thread}
458  printf("Crash%c", kOutputSeparator);
459  if (process_state.crashed()) {
460    printf("%s%c0x%" PRIx64 "%c",
461           StripSeparator(process_state.crash_reason()).c_str(),
462           kOutputSeparator, process_state.crash_address(), kOutputSeparator);
463  } else {
464    // print assertion info, if available, in place of crash reason,
465    // instead of the unhelpful "No crash"
466    string assertion = process_state.assertion();
467    if (!assertion.empty()) {
468      printf("%s%c%c", StripSeparator(assertion).c_str(),
469             kOutputSeparator, kOutputSeparator);
470    } else {
471      printf("No crash%c%c", kOutputSeparator, kOutputSeparator);
472    }
473  }
474
475  if (requesting_thread != -1) {
476    printf("%d\n", requesting_thread);
477  } else {
478    printf("\n");
479  }
480
481  PrintModulesMachineReadable(process_state.modules());
482
483  // blank line to indicate start of threads
484  printf("\n");
485
486  // If the thread that requested the dump is known, print it first.
487  if (requesting_thread != -1) {
488    PrintStackMachineReadable(requesting_thread,
489                              process_state.threads()->at(requesting_thread));
490  }
491
492  // Print all of the threads in the dump.
493  int thread_count = process_state.threads()->size();
494  for (int thread_index = 0; thread_index < thread_count; ++thread_index) {
495    if (thread_index != requesting_thread) {
496      // Don't print the crash thread again, it was already printed.
497      PrintStackMachineReadable(thread_index,
498                                process_state.threads()->at(thread_index));
499    }
500  }
501}
502
503// Processes |minidump_file| using MinidumpProcessor.  |symbol_path|, if
504// non-empty, is the base directory of a symbol storage area, laid out in
505// the format required by SimpleSymbolSupplier.  If such a storage area
506// is specified, it is made available for use by the MinidumpProcessor.
507//
508// Returns the value of MinidumpProcessor::Process.  If processing succeeds,
509// prints identifying OS and CPU information from the minidump, crash
510// information if the minidump was produced as a result of a crash, and
511// call stacks for each thread contained in the minidump.  All information
512// is printed to stdout.
513static bool PrintMinidumpProcess(const string &minidump_file,
514                                 const vector<string> &symbol_paths,
515                                 bool machine_readable) {
516  scoped_ptr<SimpleSymbolSupplier> symbol_supplier;
517  if (!symbol_paths.empty()) {
518    // TODO(mmentovai): check existence of symbol_path if specified?
519    symbol_supplier.reset(new SimpleSymbolSupplier(symbol_paths));
520  }
521
522  BasicSourceLineResolver resolver;
523  MinidumpProcessor minidump_processor(symbol_supplier.get(), &resolver);
524
525  // Process the minidump.
526  ProcessState process_state;
527  if (minidump_processor.Process(minidump_file, &process_state) !=
528      google_breakpad::PROCESS_OK) {
529    BPLOG(ERROR) << "MinidumpProcessor::Process failed";
530    return false;
531  }
532
533  if (machine_readable) {
534    PrintProcessStateMachineReadable(process_state);
535  } else {
536    PrintProcessState(process_state);
537  }
538
539  return true;
540}
541
542}  // namespace
543
544static void usage(const char *program_name) {
545  fprintf(stderr, "usage: %s [-m] <minidump-file> [symbol-path ...]\n"
546          "    -m : Output in machine-readable format\n",
547          program_name);
548}
549
550int main(int argc, char **argv) {
551  BPLOG_INIT(&argc, &argv);
552
553  if (argc < 2) {
554    usage(argv[0]);
555    return 1;
556  }
557
558  const char *minidump_file;
559  bool machine_readable;
560  int symbol_path_arg;
561
562  if (strcmp(argv[1], "-m") == 0) {
563    if (argc < 3) {
564      usage(argv[0]);
565      return 1;
566    }
567
568    machine_readable = true;
569    minidump_file = argv[2];
570    symbol_path_arg = 3;
571  } else {
572    machine_readable = false;
573    minidump_file = argv[1];
574    symbol_path_arg = 2;
575  }
576
577  // extra arguments are symbol paths
578  std::vector<std::string> symbol_paths;
579  if (argc > symbol_path_arg) {
580    for (int argi = symbol_path_arg; argi < argc; ++argi)
581      symbol_paths.push_back(argv[argi]);
582  }
583
584  return PrintMinidumpProcess(minidump_file,
585                              symbol_paths,
586                              machine_readable) ? 0 : 1;
587}