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
38 #include <string>
39 #include <vector>
40
41 #include "common/scoped_ptr.h"
42 #include "common/using_std_string.h"
43 #include "google_breakpad/processor/basic_source_line_resolver.h"
44 #include "google_breakpad/processor/minidump.h"
45 #include "google_breakpad/processor/minidump_processor.h"
46 #include "google_breakpad/processor/process_state.h"
47 #include "processor/logging.h"
48 #include "processor/simple_symbol_supplier.h"
49 #include "processor/stackwalk_common.h"
50
51
52 namespace {
53
54 using google_breakpad::BasicSourceLineResolver;
55 using google_breakpad::Minidump;
56 using google_breakpad::MinidumpProcessor;
57 using google_breakpad::ProcessState;
58 using google_breakpad::SimpleSymbolSupplier;
59 using google_breakpad::scoped_ptr;
60
61 // Processes |minidump_file| using MinidumpProcessor. |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 MinidumpProcessor.
65 //
66 // Returns the value of MinidumpProcessor::Process. If processing succeeds,
67 // prints identifying OS and CPU information from the minidump, crash
68 // information if the minidump was produced as a result of a crash, and
69 // call stacks for each thread contained in the minidump. All information
70 // is printed to stdout.
PrintMinidumpProcess(const string & minidump_file,const std::vector<string> & symbol_paths,bool machine_readable,bool output_stack_contents)71 bool PrintMinidumpProcess(const string &minidump_file,
72 const std::vector<string> &symbol_paths,
73 bool machine_readable,
74 bool output_stack_contents) {
75 scoped_ptr<SimpleSymbolSupplier> symbol_supplier;
76 if (!symbol_paths.empty()) {
77 // TODO(mmentovai): check existence of symbol_path if specified?
78 symbol_supplier.reset(new SimpleSymbolSupplier(symbol_paths));
79 }
80
81 BasicSourceLineResolver resolver;
82 MinidumpProcessor minidump_processor(symbol_supplier.get(), &resolver);
83
84 // Process the minidump.
85 Minidump dump(minidump_file);
86 if (!dump.Read()) {
87 BPLOG(ERROR) << "Minidump " << dump.path() << " could not be read";
88 return false;
89 }
90 ProcessState process_state;
91 if (minidump_processor.Process(&dump, &process_state) !=
92 google_breakpad::PROCESS_OK) {
93 BPLOG(ERROR) << "MinidumpProcessor::Process failed";
94 return false;
95 }
96
97 if (machine_readable) {
98 PrintProcessStateMachineReadable(process_state);
99 } else {
100 PrintProcessState(process_state, output_stack_contents, &resolver);
101 }
102
103 return true;
104 }
105
usage(const char * program_name)106 void usage(const char *program_name) {
107 fprintf(stderr, "usage: %s [-m|-s] <minidump-file> [symbol-path ...]\n"
108 " -m : Output in machine-readable format\n"
109 " -s : Output stack contents\n",
110 program_name);
111 }
112
113 } // namespace
114
main(int argc,char ** argv)115 int main(int argc, char **argv) {
116 BPLOG_INIT(&argc, &argv);
117
118 if (argc < 2) {
119 usage(argv[0]);
120 return 1;
121 }
122
123 const char *minidump_file;
124 bool machine_readable = false;
125 bool output_stack_contents = false;
126 int symbol_path_arg;
127
128 if (strcmp(argv[1], "-m") == 0) {
129 if (argc < 3) {
130 usage(argv[0]);
131 return 1;
132 }
133
134 machine_readable = true;
135 minidump_file = argv[2];
136 symbol_path_arg = 3;
137 } else if (strcmp(argv[1], "-s") == 0) {
138 if (argc < 3) {
139 usage(argv[0]);
140 return 1;
141 }
142
143 output_stack_contents = true;
144 minidump_file = argv[2];
145 symbol_path_arg = 3;
146 } else {
147 minidump_file = argv[1];
148 symbol_path_arg = 2;
149 }
150
151 // extra arguments are symbol paths
152 std::vector<string> symbol_paths;
153 if (argc > symbol_path_arg) {
154 for (int argi = symbol_path_arg; argi < argc; ++argi)
155 symbol_paths.push_back(argv[argi]);
156 }
157
158 return PrintMinidumpProcess(minidump_file,
159 symbol_paths,
160 machine_readable,
161 output_stack_contents) ? 0 : 1;
162 }
163