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 #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. | |
134 call_error_writing_crash_report.Disarm(); | |
135 crashpad::UUID unused_report_id; | |
136 database_status = report_database->FinishedWritingCrashReport( | |
scottmg
2016/09/16 17:07:45
It'll take up to 15 minutes for this to get "notic
manzagop (departed)
2016/09/16 18:06:41
Great point! I've added a comment.
| |
137 new_report, &unused_report_id); | |
138 if (database_status != CrashReportDatabase::kNoError) { | |
139 LOG(ERROR) << "FinishedWritingCrashReport failed"; | |
140 return FINISHED_WRITING_CRASH_REPORT_FAILED; | |
141 } | |
142 | |
143 return SUCCESS; | |
144 } | |
145 | |
146 PostmortemReportCollector::CollectionStatus PostmortemReportCollector::Collect( | |
147 const base::FilePath& debug_state_file, | |
148 std::unique_ptr<StabilityReport>* report) { | |
149 DCHECK_NE(nullptr, report); | |
150 report->reset(); | |
151 | |
152 // Create a global analyzer. | |
153 std::unique_ptr<GlobalActivityAnalyzer> global_analyzer = | |
154 GlobalActivityAnalyzer::CreateWithFile(debug_state_file); | |
155 if (!global_analyzer) | |
156 return ANALYZER_CREATION_FAILED; | |
157 | |
158 // Early exit if there is no data. | |
159 ThreadActivityAnalyzer* thread_analyzer = global_analyzer->GetFirstAnalyzer(); | |
160 if (!thread_analyzer) { | |
161 // No data. This case happens in the case of a clean exit. | |
162 return DEBUG_FILE_NO_DATA; | |
163 } | |
164 | |
165 // Iterate through the thread analyzers, fleshing out the report. | |
166 report->reset(new StabilityReport()); | |
167 ProcessState* process_state = (*report)->add_process_states(); | |
168 | |
169 for (; thread_analyzer != nullptr; | |
170 thread_analyzer = global_analyzer->GetNextAnalyzer()) { | |
171 // Only valid analyzers are expected per contract of GetFirstAnalyzer / | |
172 // GetNextAnalyzer. | |
173 DCHECK(thread_analyzer->IsValid()); | |
174 | |
175 ThreadState* thread_state = process_state->add_threads(); | |
176 thread_state->set_thread_name(thread_analyzer->GetThreadName()); | |
177 // TODO(manzagop): flesh this out. | |
178 } | |
179 | |
180 return SUCCESS; | |
181 } | |
182 | |
183 bool PostmortemReportCollector::WriteReportToMinidump( | |
184 const StabilityReport& report, | |
185 const crashpad::UUID& client_id, | |
186 const crashpad::UUID& report_id, | |
187 base::PlatformFile minidump_file) { | |
188 MinidumpInfo mindump_info; | |
scottmg
2016/09/16 17:07:45
mindump -> minidump
manzagop (departed)
2016/09/16 18:06:41
Done.
| |
189 mindump_info.client_id = client_id; | |
190 mindump_info.report_id = report_id; | |
191 mindump_info.product_name = version_info::GetProductName(); | |
192 mindump_info.version_number = version_info::GetVersionNumber(); | |
193 | |
194 return WritePostmortemDump(minidump_file, report, mindump_info); | |
195 } | |
196 | |
197 } // namespace browser_watcher | |
OLD | NEW |