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