• 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 #include <unistd.h>
36 
37 #include <fstream>
38 #include <string>
39 #include <vector>
40 
41 #include "common/path_helper.h"
42 #include "common/scoped_ptr.h"
43 #include "common/using_std_string.h"
44 #include "google_breakpad/processor/basic_source_line_resolver.h"
45 #include "google_breakpad/processor/microdump.h"
46 #include "google_breakpad/processor/microdump_processor.h"
47 #include "google_breakpad/processor/process_state.h"
48 #include "google_breakpad/processor/stack_frame_symbolizer.h"
49 #include "processor/logging.h"
50 #include "processor/simple_symbol_supplier.h"
51 #include "processor/stackwalk_common.h"
52 
53 
54 namespace {
55 
56 struct Options {
57   bool machine_readable;
58   bool output_stack_contents;
59 
60   string microdump_file;
61   std::vector<string> symbol_paths;
62 };
63 
64 using google_breakpad::BasicSourceLineResolver;
65 using google_breakpad::Microdump;
66 using google_breakpad::MicrodumpProcessor;
67 using google_breakpad::ProcessResult;
68 using google_breakpad::ProcessState;
69 using google_breakpad::scoped_ptr;
70 using google_breakpad::SimpleSymbolSupplier;
71 using google_breakpad::StackFrameSymbolizer;
72 
73 // Processes |options.microdump_file| using
74 // MicrodumpProcessor. |options.symbol_path|, if non-empty, is the
75 // base directory of a symbol storage area, laid out in the format
76 // required by SimpleSymbolSupplier.  If such a storage area is
77 // specified, it is made available for use by the MicrodumpProcessor.
78 //
79 // Returns the value of MicrodumpProcessor::Process. If processing succeeds,
80 // prints identifying OS and CPU information from the microdump, crash
81 // information and call stacks for the crashing thread.
82 // All information is printed to stdout.
PrintMicrodumpProcess(const Options & options)83 int PrintMicrodumpProcess(const Options& options) {
84   std::ifstream file_stream(options.microdump_file);
85   std::vector<char> bytes;
86   file_stream.seekg(0, std::ios_base::end);
87   bytes.resize(file_stream.tellg());
88   if (bytes.empty()) {
89     BPLOG(ERROR) << "Microdump is empty.";
90     return 1;
91   }
92   file_stream.seekg(0, std::ios_base::beg);
93   file_stream.read(&bytes[0], bytes.size());
94   string microdump_content(&bytes[0], bytes.size());
95 
96   scoped_ptr<SimpleSymbolSupplier> symbol_supplier;
97   if (!options.symbol_paths.empty()) {
98     symbol_supplier.reset(new SimpleSymbolSupplier(options.symbol_paths));
99   }
100 
101   BasicSourceLineResolver resolver;
102   StackFrameSymbolizer frame_symbolizer(symbol_supplier.get(), &resolver);
103   ProcessState process_state;
104   MicrodumpProcessor microdump_processor(&frame_symbolizer);
105   Microdump microdump(microdump_content);
106   ProcessResult res = microdump_processor.Process(&microdump,
107                                                   &process_state);
108 
109   if (res == google_breakpad::PROCESS_OK) {
110     if (options.machine_readable) {
111       PrintProcessStateMachineReadable(process_state);
112     } else {
113       PrintProcessState(process_state, options.output_stack_contents, &resolver);
114     }
115     return 0;
116   }
117 
118   BPLOG(ERROR) << "MicrodumpProcessor::Process failed (code = " << res << ")";
119   return 1;
120 }
121 
122 }  // namespace
123 
Usage(int argc,const char * argv[],bool error)124 static void Usage(int argc, const char *argv[], bool error) {
125   fprintf(error ? stderr : stdout,
126           "Usage: %s [options] <microdump-file> [symbol-path ...]\n"
127           "\n"
128           "Output a stack trace for the provided microdump\n"
129           "\n"
130           "Options:\n"
131           "\n"
132           "  -m         Output in machine-readable format\n"
133           "  -s         Output stack contents\n",
134           google_breakpad::BaseName(argv[0]).c_str());
135 }
136 
SetupOptions(int argc,const char * argv[],Options * options)137 static void SetupOptions(int argc, const char *argv[], Options* options) {
138   int ch;
139 
140   options->machine_readable = false;
141   options->output_stack_contents = false;
142 
143   while ((ch = getopt(argc, (char * const *)argv, "hms")) != -1) {
144     switch (ch) {
145       case 'h':
146         Usage(argc, argv, false);
147         exit(0);
148         break;
149 
150       case 'm':
151         options->machine_readable = true;
152         break;
153       case 's':
154         options->output_stack_contents = true;
155         break;
156 
157       case '?':
158         Usage(argc, argv, true);
159         exit(1);
160         break;
161     }
162   }
163 
164   if ((argc - optind) == 0) {
165     fprintf(stderr, "%s: Missing microdump file\n", argv[0]);
166     Usage(argc, argv, true);
167     exit(1);
168   }
169 
170   options->microdump_file = argv[optind];
171 
172   for (int argi = optind + 1; argi < argc; ++argi)
173     options->symbol_paths.push_back(argv[argi]);
174 }
175 
main(int argc,const char * argv[])176 int main(int argc, const char* argv[]) {
177   Options options;
178   SetupOptions(argc, argv, &options);
179 
180   return PrintMicrodumpProcess(options);
181 }
182