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
54namespace {
55
56struct Options {
57 bool machine_readable;
58 bool output_stack_contents;
59
60 string microdump_file;
61 std::vector<string> symbol_paths;
62};
63
64using google_breakpad::BasicSourceLineResolver;
65using google_breakpad::Microdump;
66using google_breakpad::MicrodumpProcessor;
67using google_breakpad::ProcessResult;
68using google_breakpad::ProcessState;
69using google_breakpad::scoped_ptr;
70using google_breakpad::SimpleSymbolSupplier;
71using 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.
83int 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 // Microdump has only one thread, |output_requesting_thread_only|'s value
114 // has no effect.
115 PrintProcessState(process_state, options.output_stack_contents,
116 /*output_requesting_thread_only=*/false, &resolver);
117 }
118 return 0;
119 }
120
121 BPLOG(ERROR) << "MicrodumpProcessor::Process failed (code = " << res << ")";
122 return 1;
123}
124
125} // namespace
126
127static void Usage(int argc, const char *argv[], bool error) {
128 fprintf(error ? stderr : stdout,
129 "Usage: %s [options] <microdump-file> [symbol-path ...]\n"
130 "\n"
131 "Output a stack trace for the provided microdump\n"
132 "\n"
133 "Options:\n"
134 "\n"
135 " -m Output in machine-readable format\n"
136 " -s Output stack contents\n",
137 google_breakpad::BaseName(argv[0]).c_str());
138}
139
140static void SetupOptions(int argc, const char *argv[], Options* options) {
141 int ch;
142
143 options->machine_readable = false;
144 options->output_stack_contents = false;
145
146 while ((ch = getopt(argc, (char * const*)argv, "hms")) != -1) {
147 switch (ch) {
148 case 'h':
149 Usage(argc, argv, false);
150 exit(0);
151 break;
152
153 case 'm':
154 options->machine_readable = true;
155 break;
156 case 's':
157 options->output_stack_contents = true;
158 break;
159
160 case '?':
161 Usage(argc, argv, true);
162 exit(1);
163 break;
164 }
165 }
166
167 if ((argc - optind) == 0) {
168 fprintf(stderr, "%s: Missing microdump file\n", argv[0]);
169 Usage(argc, argv, true);
170 exit(1);
171 }
172
173 options->microdump_file = argv[optind];
174
175 for (int argi = optind + 1; argi < argc; ++argi)
176 options->symbol_paths.push_back(argv[argi]);
177}
178
179int main(int argc, const char* argv[]) {
180 Options options;
181 SetupOptions(argc, argv, &options);
182
183 return PrintMicrodumpProcess(options);
184}
185