Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: components/browser_watcher/print_report_main_win.cc

Issue 2128683002: Collect unclean shutdown debug information (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@tracker
Patch Set: Merge Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 tool for printing the contents of a stability report encapsulated
6 // within a minidump.
7
8 #include <windows.h> // NOLINT
9 #include <dbghelp.h>
10
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/files/scoped_file.h"
15 #include "base/logging.h"
16 #include "base/strings/stringprintf.h"
17 #include "components/browser_watcher/stability_report.pb.h"
18
19 namespace {
20
21 const char kUsage[] =
22 "Usage: %ls --minidump=<minidump file>\n"
23 "\n"
24 " Extracts a postmortem stability report from a minidump and prints a \n"
25 " human readable string.\n";
26
27 bool ParseCommandLine(const base::CommandLine* cmd,
28 base::FilePath* minidump_path) {
29 *minidump_path = cmd->GetSwitchValuePath("minidump");
30 if (minidump_path->empty()) {
31 LOG(ERROR) << "Missing minidump file.\n";
32 LOG(ERROR) << base::StringPrintf(kUsage, cmd->GetProgram().value().c_str());
33 return false;
34 }
35 return true;
36 }
37
38 void PrintProcessState(FILE* out,
39 const browser_watcher::ProcessState& process) {
40 fprintf(out, "Process:\n");
41 for (size_t i = 0; i < process.threads_size(); ++i) {
42 const browser_watcher::ThreadState thread = process.threads(i);
43 fprintf(out, thread.thread_name().c_str());
44 fputc('\n', out);
45 }
46 }
47
48 void PrintReport(FILE* out, const browser_watcher::StabilityReport& report) {
49 for (size_t i = 0; i < report.process_states_size(); ++i) {
50 const browser_watcher::ProcessState process = report.process_states(i);
51 PrintProcessState(out, process);
52 }
53 }
54
55 int Main(int argc, char** argv) {
56 base::CommandLine::Init(argc, argv);
57
58 // Get the dump.
59 base::FilePath minidump_path;
60 if (!ParseCommandLine(base::CommandLine::ForCurrentProcess(), &minidump_path))
61 return 1;
62
63 // Read the minidump to extract the proto.
64 base::ScopedFILE minidump_file;
65 minidump_file.reset(base::OpenFile(minidump_path, "rb"));
66 CHECK(minidump_file.get());
67
68 // Read the header.
69 MINIDUMP_HEADER header = {};
70 CHECK_EQ(1, fread(&header, sizeof(header), 1, minidump_file.get()));
71 CHECK_EQ(static_cast<ULONG32>(MINIDUMP_SIGNATURE), header.Signature);
72 CHECK_EQ(2U, header.NumberOfStreams);
73 RVA directory_rva = header.StreamDirectoryRva;
74
75 // Read the directory entry for the stability report's stream.
76 // Note: this hardcodes an expectation that the stability report is the first
77 // encountered stream. This is acceptable for a debug tool.
78 MINIDUMP_DIRECTORY directory = {};
79 CHECK_EQ(0, fseek(minidump_file.get(), directory_rva, SEEK_SET));
80 CHECK_EQ(1, fread(&directory, sizeof(directory), 1, minidump_file.get()));
81 CHECK_EQ(static_cast<ULONG32>(0x4B6B0002), directory.StreamType);
82 RVA report_rva = directory.Location.Rva;
83 ULONG32 report_size_bytes = directory.Location.DataSize;
84
85 // Read the serialized stability report.
86 std::string serialized_report;
87 serialized_report.resize(report_size_bytes);
88 CHECK_EQ(0, fseek(minidump_file.get(), report_rva, SEEK_SET));
89 CHECK_EQ(report_size_bytes, fread(&serialized_report.at(0), 1,
90 report_size_bytes, minidump_file.get()));
91
92 browser_watcher::StabilityReport report;
93 CHECK(report.ParseFromString(serialized_report));
94
95 // Note: we can't use the usual protocol buffer human readable API due to
96 // the use of optimize_for = LITE_RUNTIME.
97 PrintReport(stdout, report);
98
99 return 0;
100 }
101
102 } // namespace
103
104 int main(int argc, char** argv) {
105 return Main(argc, argv);
106 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698