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