• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 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 // microdump_stackwalk.cc: Process a microdump with MicrodumpProcessor, printing
31 // the results, including stack traces.
32 
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include <fstream>
37 #include <string>
38 #include <vector>
39 
40 #include "common/scoped_ptr.h"
41 #include "common/using_std_string.h"
42 #include "google_breakpad/processor/basic_source_line_resolver.h"
43 #include "google_breakpad/processor/microdump_processor.h"
44 #include "google_breakpad/processor/process_state.h"
45 #include "google_breakpad/processor/stack_frame_symbolizer.h"
46 #include "processor/logging.h"
47 #include "processor/simple_symbol_supplier.h"
48 #include "processor/stackwalk_common.h"
49 
50 
51 namespace {
52 
53 using google_breakpad::BasicSourceLineResolver;
54 using google_breakpad::MicrodumpProcessor;
55 using google_breakpad::ProcessResult;
56 using google_breakpad::ProcessState;
57 using google_breakpad::scoped_ptr;
58 using google_breakpad::SimpleSymbolSupplier;
59 using google_breakpad::StackFrameSymbolizer;
60 
61 // Processes |microdump_file| using MicrodumpProcessor. |symbol_path|, if
62 // non-empty, is the base directory of a symbol storage area, laid out in
63 // the format required by SimpleSymbolSupplier.  If such a storage area
64 // is specified, it is made available for use by the MicrodumpProcessor.
65 //
66 // Returns the value of MicrodumpProcessor::Process. If processing succeeds,
67 // prints identifying OS and CPU information from the microdump, crash
68 // information and call stacks for the crashing thread.
69 // All information is printed to stdout.
PrintMicrodumpProcess(const char * microdump_file,const std::vector<string> & symbol_paths,bool machine_readable)70 int PrintMicrodumpProcess(const char* microdump_file,
71                           const std::vector<string>& symbol_paths,
72                           bool machine_readable) {
73   std::ifstream file_stream(microdump_file);
74   std::vector<char> bytes;
75   file_stream.seekg(0, std::ios_base::end);
76   bytes.resize(file_stream.tellg());
77   file_stream.seekg(0, std::ios_base::beg);
78   file_stream.read(&bytes[0], bytes.size());
79   string microdump_content(&bytes[0], bytes.size());
80 
81   scoped_ptr<SimpleSymbolSupplier> symbol_supplier;
82   if (!symbol_paths.empty()) {
83     symbol_supplier.reset(new SimpleSymbolSupplier(symbol_paths));
84   }
85 
86   BasicSourceLineResolver resolver;
87   StackFrameSymbolizer frame_symbolizer(symbol_supplier.get(), &resolver);
88   ProcessState process_state;
89   MicrodumpProcessor microdump_processor(&frame_symbolizer);
90   ProcessResult res = microdump_processor.Process(microdump_content,
91                                                   &process_state);
92 
93   if (res == google_breakpad::PROCESS_OK) {
94     if (machine_readable) {
95       PrintProcessStateMachineReadable(process_state);
96     } else {
97       PrintProcessState(process_state, false, &resolver);
98     }
99     return 0;
100   }
101 
102   BPLOG(ERROR) << "MicrodumpProcessor::Process failed (code = " << res << ")";
103   return 1;
104 }
105 
usage(const char * program_name)106 void usage(const char *program_name) {
107   fprintf(stderr, "usage: %s [-m] <microdump-file> [symbol-path ...]\n"
108           "    -m : Output in machine-readable format\n",
109           program_name);
110 }
111 
112 }  // namespace
113 
main(int argc,char ** argv)114 int main(int argc, char** argv) {
115   BPLOG_INIT(&argc, &argv);
116 
117   if (argc < 2) {
118     usage(argv[0]);
119     return 1;
120   }
121 
122   const char* microdump_file;
123   bool machine_readable;
124   int symbol_path_arg;
125 
126   if (strcmp(argv[1], "-m") == 0) {
127     if (argc < 3) {
128       usage(argv[0]);
129       return 1;
130     }
131 
132     machine_readable = true;
133     microdump_file = argv[2];
134     symbol_path_arg = 3;
135   } else {
136     machine_readable = false;
137     microdump_file = argv[1];
138     symbol_path_arg = 2;
139   }
140 
141   // extra arguments are symbol paths
142   std::vector<string> symbol_paths;
143   if (argc > symbol_path_arg) {
144     for (int argi = symbol_path_arg; argi < argc; ++argi)
145       symbol_paths.push_back(argv[argi]);
146   }
147 
148   return PrintMicrodumpProcess(microdump_file,
149                                symbol_paths,
150                                machine_readable);
151 }
152