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

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

Issue 2339193003: A collector for postmortem reports (Closed)
Patch Set: Address nits 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 #include "components/browser_watcher/postmortem_report_collector.h"
6
7 #include <utility>
8
9 #include "base/debug/activity_analyzer.h"
10 #include "base/files/file_enumerator.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram_macros.h"
14 #include "base/path_service.h"
15 #include "components/browser_watcher/postmortem_minidump_writer.h"
16 #include "components/version_info/version_info.h"
17 #include "third_party/crashpad/crashpad/client/settings.h"
18 #include "third_party/crashpad/crashpad/util/misc/uuid.h"
19
20 using base::FilePath;
21
22 namespace browser_watcher {
23
24 using base::debug::GlobalActivityAnalyzer;
25 using base::debug::ThreadActivityAnalyzer;
26 using crashpad::CrashReportDatabase;
27
28 int PostmortemReportCollector::CollectAndSubmitForUpload(
29 const base::FilePath& debug_info_dir,
30 const base::FilePath::StringType& debug_file_pattern,
31 const std::set<base::FilePath>& excluded_debug_files,
32 crashpad::CrashReportDatabase* report_database) {
33 DCHECK_NE(true, debug_info_dir.empty());
34 DCHECK_NE(true, debug_file_pattern.empty());
35 DCHECK_NE(nullptr, report_database);
36
37 // Collect the list of files to harvest.
38 std::vector<FilePath> debug_files = GetDebugStateFilePaths(
39 debug_info_dir, debug_file_pattern, excluded_debug_files);
40
41 // Determine the crashpad client id.
42 crashpad::UUID client_id;
43 crashpad::Settings* settings = report_database->GetSettings();
44 if (settings) {
45 // If GetSettings() or GetClientID() fails client_id will be left at its
46 // default value, all zeroes, which is appropriate.
47 settings->GetClientID(&client_id);
48 }
49
50 // Process each stability file.
51 int success_cnt = 0;
52 for (const FilePath& file : debug_files) {
53 CollectionStatus status =
54 CollectAndSubmit(client_id, file, report_database);
55 // TODO(manzagop): consider making this a stability metric.
56 UMA_HISTOGRAM_ENUMERATION("ActivityTracker.Collect.Status", status,
57 COLLECTION_STATUS_MAX);
58 if (status == SUCCESS)
59 ++success_cnt;
60 }
61
62 return success_cnt;
63 }
64
65 std::vector<FilePath> PostmortemReportCollector::GetDebugStateFilePaths(
66 const base::FilePath& debug_info_dir,
67 const base::FilePath::StringType& debug_file_pattern,
68 const std::set<FilePath>& excluded_debug_files) {
69 DCHECK_NE(true, debug_info_dir.empty());
70 DCHECK_NE(true, debug_file_pattern.empty());
71
72 std::vector<FilePath> paths;
73 base::FileEnumerator enumerator(debug_info_dir, false /* recursive */,
74 base::FileEnumerator::FILES,
75 debug_file_pattern);
76 FilePath path;
77 for (path = enumerator.Next(); !path.empty(); path = enumerator.Next()) {
78 if (excluded_debug_files.find(path) == excluded_debug_files.end())
79 paths.push_back(path);
80 }
81 return paths;
82 }
83
84 PostmortemReportCollector::CollectionStatus
85 PostmortemReportCollector::CollectAndSubmit(
86 const crashpad::UUID& client_id,
87 const FilePath& file,
88 crashpad::CrashReportDatabase* report_database) {
89 DCHECK_NE(nullptr, report_database);
90
91 // Note: the code below involves two notions of report: chrome internal state
92 // reports and the crashpad reports they get wrapped into.
93
94 // Collect the data from the debug file to a proto.
95 std::unique_ptr<StabilityReport> report_proto;
96 CollectionStatus status = Collect(file, &report_proto);
97 if (status != SUCCESS) {
98 // The file was empty, or there was an error collecting the data. Detailed
99 // logging happens within the Collect function.
100 if (!base::DeleteFile(file, false))
101 LOG(ERROR) << "Failed to delete " << file.value();
102 return status;
103 }
104 DCHECK_NE(nullptr, report_proto.get());
105
106 // Prepare a crashpad report.
107 CrashReportDatabase::NewReport* new_report = nullptr;
108 CrashReportDatabase::OperationStatus database_status =
109 report_database->PrepareNewCrashReport(&new_report);
110 if (database_status != CrashReportDatabase::kNoError) {
111 LOG(ERROR) << "PrepareNewCrashReport failed";
112 return PREPARE_NEW_CRASH_REPORT_FAILED;
113 }
114 CrashReportDatabase::CallErrorWritingCrashReport
115 call_error_writing_crash_report(report_database, new_report);
116
117 // Write the report to a minidump.
118 if (!WriteReportToMinidump(*report_proto, client_id, new_report->uuid,
119 reinterpret_cast<FILE*>(new_report->handle))) {
120 return WRITE_TO_MINIDUMP_FAILED;
121 }
122
123 // If the file cannot be deleted, do not report its contents. Note this can
124 // lead to under reporting and retries. However, under reporting is
125 // preferable to the over reporting that would happen with a file that
126 // cannot be deleted.
127 // TODO(manzagop): metrics for the number of non-deletable files.
128 if (!base::DeleteFile(file, false)) {
129 LOG(ERROR) << "Failed to delete " << file.value();
130 return DEBUG_FILE_DELETION_FAILED;
131 }
132
133 // Finalize the report wrt the report database. Note that this doesn't trigger
134 // an immediate upload, but Crashpad will eventually upload the report (as of
135 // writing, the delay is on the order of up to 15 minutes).
136 call_error_writing_crash_report.Disarm();
137 crashpad::UUID unused_report_id;
138 database_status = report_database->FinishedWritingCrashReport(
139 new_report, &unused_report_id);
140 if (database_status != CrashReportDatabase::kNoError) {
141 LOG(ERROR) << "FinishedWritingCrashReport failed";
142 return FINISHED_WRITING_CRASH_REPORT_FAILED;
143 }
144
145 return SUCCESS;
146 }
147
148 PostmortemReportCollector::CollectionStatus PostmortemReportCollector::Collect(
149 const base::FilePath& debug_state_file,
150 std::unique_ptr<StabilityReport>* report) {
151 DCHECK_NE(nullptr, report);
152 report->reset();
153
154 // Create a global analyzer.
155 std::unique_ptr<GlobalActivityAnalyzer> global_analyzer =
156 GlobalActivityAnalyzer::CreateWithFile(debug_state_file);
157 if (!global_analyzer)
158 return ANALYZER_CREATION_FAILED;
159
160 // Early exit if there is no data.
161 ThreadActivityAnalyzer* thread_analyzer = global_analyzer->GetFirstAnalyzer();
162 if (!thread_analyzer) {
163 // No data. This case happens in the case of a clean exit.
164 return DEBUG_FILE_NO_DATA;
165 }
166
167 // Iterate through the thread analyzers, fleshing out the report.
168 report->reset(new StabilityReport());
169 ProcessState* process_state = (*report)->add_process_states();
170
171 for (; thread_analyzer != nullptr;
172 thread_analyzer = global_analyzer->GetNextAnalyzer()) {
173 // Only valid analyzers are expected per contract of GetFirstAnalyzer /
174 // GetNextAnalyzer.
175 DCHECK(thread_analyzer->IsValid());
176
177 ThreadState* thread_state = process_state->add_threads();
178 thread_state->set_thread_name(thread_analyzer->GetThreadName());
179 // TODO(manzagop): flesh this out.
180 }
181
182 return SUCCESS;
183 }
184
185 bool PostmortemReportCollector::WriteReportToMinidump(
186 const StabilityReport& report,
187 const crashpad::UUID& client_id,
188 const crashpad::UUID& report_id,
189 base::PlatformFile minidump_file) {
190 MinidumpInfo minidump_info;
191 minidump_info.client_id = client_id;
192 minidump_info.report_id = report_id;
193 minidump_info.product_name = version_info::GetProductName();
194 minidump_info.version_number = version_info::GetVersionNumber();
195
196 return WritePostmortemDump(minidump_file, report, minidump_info);
197 }
198
199 } // namespace browser_watcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698