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