OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // A utility for printing the contents of a postmortem stability minidump. | |
6 | |
7 #include <windows.h> // NOLINT | |
8 #include <dbghelp.h> | |
9 | |
10 #include "base/command_line.h" | |
11 #include "base/files/file_path.h" | |
12 #include "base/files/file_util.h" | |
13 #include "base/files/scoped_file.h" | |
14 #include "base/logging.h" | |
15 #include "base/strings/stringprintf.h" | |
16 #include "components/browser_watcher/stability_report.pb.h" | |
17 | |
18 namespace { | |
19 | |
20 const char kUsage[] = | |
21 "Usage: %ls --minidump=<minidump file>\n" | |
22 "\n" | |
23 " Dumps the contents of a postmortem minidump in a human readable way.\n"; | |
24 | |
25 bool ParseCommandLine(const base::CommandLine* cmd, | |
26 base::FilePath* minidump_path) { | |
27 *minidump_path = cmd->GetSwitchValuePath("minidump"); | |
28 if (minidump_path->empty()) { | |
29 LOG(ERROR) << "Missing minidump file.\n"; | |
30 LOG(ERROR) << base::StringPrintf(kUsage, cmd->GetProgram().value().c_str()); | |
31 return false; | |
32 } | |
33 return true; | |
34 } | |
35 | |
36 void PrintProcessState(FILE* out, | |
37 const browser_watcher::ProcessState& process) { | |
38 fprintf(out, "Process:\n"); | |
39 for (int i = 0; i < process.threads_size(); ++i) { | |
40 const browser_watcher::ThreadState thread = process.threads(i); | |
41 fprintf(out, thread.thread_name().c_str()); | |
42 fputc('\n', out); | |
43 } | |
44 } | |
45 | |
46 // TODO(manzagop): flesh out as StabilityReport gets fleshed out. | |
47 void PrintReport(FILE* out, const browser_watcher::StabilityReport& report) { | |
48 for (int i = 0; i < report.process_states_size(); ++i) { | |
49 const browser_watcher::ProcessState process = report.process_states(i); | |
50 PrintProcessState(out, process); | |
51 } | |
52 } | |
53 | |
54 int Main(int argc, char** argv) { | |
55 base::CommandLine::Init(argc, argv); | |
56 | |
57 // Get the dump. | |
58 base::FilePath minidump_path; | |
59 if (!ParseCommandLine(base::CommandLine::ForCurrentProcess(), &minidump_path)) | |
60 return 1; | |
61 | |
62 // Read the minidump to extract the proto. | |
63 base::ScopedFILE minidump_file; | |
64 minidump_file.reset(base::OpenFile(minidump_path, "rb")); | |
65 CHECK(minidump_file.get()); | |
66 | |
67 // Read the header. | |
68 MINIDUMP_HEADER header = {}; | |
Sigurður Ásgeirsson
2016/09/14 15:57:26
nit: a Todo to do this with CP?
manzagop (departed)
2016/09/14 18:19:06
Done.
| |
69 CHECK_EQ(1U, fread(&header, sizeof(header), 1U, minidump_file.get())); | |
70 CHECK_EQ(static_cast<ULONG32>(MINIDUMP_SIGNATURE), header.Signature); | |
71 CHECK_EQ(2U, header.NumberOfStreams); | |
72 RVA directory_rva = header.StreamDirectoryRva; | |
73 | |
74 // Read the directory entry for the stability report's stream. | |
75 // Note: this hardcodes an expectation that the stability report is the first | |
76 // encountered stream. This is acceptable for a debug tool. | |
77 MINIDUMP_DIRECTORY directory = {}; | |
78 CHECK_EQ(0, fseek(minidump_file.get(), directory_rva, SEEK_SET)); | |
79 CHECK_EQ(1U, fread(&directory, sizeof(directory), 1U, minidump_file.get())); | |
80 CHECK_EQ(static_cast<ULONG32>(0x4B6B0002), directory.StreamType); | |
81 RVA report_rva = directory.Location.Rva; | |
82 ULONG32 report_size_bytes = directory.Location.DataSize; | |
83 | |
84 // Read the serialized stability report. | |
85 std::string serialized_report; | |
86 serialized_report.resize(report_size_bytes); | |
87 CHECK_EQ(0, fseek(minidump_file.get(), report_rva, SEEK_SET)); | |
88 CHECK_EQ(report_size_bytes, fread(&serialized_report.at(0), 1, | |
89 report_size_bytes, minidump_file.get())); | |
90 | |
91 browser_watcher::StabilityReport report; | |
92 CHECK(report.ParseFromString(serialized_report)); | |
93 | |
94 // Note: we can't use the usual protocol buffer human readable API due to | |
95 // the use of optimize_for = LITE_RUNTIME. | |
96 PrintReport(stdout, report); | |
97 | |
98 return 0; | |
99 } | |
100 | |
101 } // namespace | |
102 | |
103 int main(int argc, char** argv) { | |
104 return Main(argc, argv); | |
105 } | |
OLD | NEW |