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/path_service.h" | |
14 #include "components/browser_watcher/postmortem_minidump_writer.h" | |
15 #include "components/version_info/version_info.h" | |
16 #include "third_party/crashpad/crashpad/client/settings.h" | |
17 #include "third_party/crashpad/crashpad/util/misc/uuid.h" | |
18 | |
19 using base::FilePath; | |
20 | |
21 namespace browser_watcher { | |
22 | |
23 using base::debug::GlobalActivityAnalyzer; | |
24 using base::debug::ThreadActivityAnalyzer; | |
25 using crashpad::CrashReportDatabase; | |
26 | |
27 // TODO(manzagop): throttling and graceful handling of too much data. | |
Sigurður Ásgeirsson
2016/09/15 18:44:10
I think it makes sense for throttling to be centra
manzagop (departed)
2016/09/16 16:37:30
I've rewritten the TODO to more clearly express th
| |
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 // The number of postmortem reports recovered irrespective of whether they | |
51 // were successfully wrapped in a minidump and registered with the reporter. | |
52 int postmortem_cnt = 0; | |
53 | |
54 // Note: the code below involves two notions of report: | |
55 // - crashpad reports | |
56 // - chrome internal state reports, which are reported wrapped inside a | |
57 // crashpad report | |
58 for (const FilePath& file : debug_files) { | |
59 // Collect the data from the debug file to a proto. | |
60 std::unique_ptr<StabilityReport> report_proto = Collect(file); | |
Sigurður Ásgeirsson
2016/09/15 18:44:10
it would IMHO be much more readable to scoop the b
manzagop (departed)
2016/09/16 16:37:30
+1, and it's a nicely self contained unit. Done.
| |
61 | |
62 // The file was empty, or there was an error collecting the data. Detailed | |
63 // logging happens within the Collect function. | |
64 if (!report_proto) { | |
65 if (!base::DeleteFile(file, false)) | |
66 LOG(ERROR) << "Failed to delete " << file.value(); | |
67 continue; | |
68 } | |
69 | |
70 ++postmortem_cnt; | |
71 | |
72 // Prepare a crashpad report. | |
73 CrashReportDatabase::NewReport* new_report = nullptr; | |
74 CrashReportDatabase::OperationStatus database_status = | |
75 report_database->PrepareNewCrashReport(&new_report); | |
76 if (database_status != CrashReportDatabase::kNoError) { | |
77 LOG(ERROR) << "PrepareNewCrashReport failed"; | |
78 continue; | |
79 } | |
80 CrashReportDatabase::CallErrorWritingCrashReport | |
81 call_error_writing_crash_report(report_database, new_report); | |
82 | |
83 // Write the report to a minidump. | |
84 if (!WriteReportToMinidump(*report_proto, client_id, new_report->uuid, | |
85 reinterpret_cast<FILE*>(new_report->handle))) { | |
86 continue; | |
87 } | |
88 | |
89 // If the file cannot be deleted, do not report its contents. Note this can | |
90 // lead to under reporting and retries. However, under reporting is | |
91 // preferable to the over reporting that would happen with a file that | |
92 // cannot be deleted. | |
93 // TODO(manzagop): metrics for the number of non-deletable files. | |
Sigurður Ásgeirsson
2016/09/15 18:44:10
is there any reason you can't collect these metric
manzagop (departed)
2016/09/16 16:37:30
Done.
| |
94 if (!base::DeleteFile(file, false)) { | |
95 LOG(ERROR) << "Failed to delete " << file.value(); | |
96 continue; | |
97 } | |
98 | |
99 // Finalize the report wrt the report database. | |
100 call_error_writing_crash_report.Disarm(); | |
101 crashpad::UUID unused_report_id; | |
102 database_status = report_database->FinishedWritingCrashReport( | |
103 new_report, &unused_report_id); | |
104 if (database_status != CrashReportDatabase::kNoError) { | |
105 LOG(ERROR) << "FinishedWritingCrashReport failed"; | |
106 continue; | |
107 } | |
108 } | |
109 | |
110 return postmortem_cnt; | |
111 } | |
112 | |
113 std::vector<FilePath> PostmortemReportCollector::GetDebugStateFilePaths( | |
114 const base::FilePath& debug_info_dir, | |
115 const base::FilePath::StringType& debug_file_pattern, | |
116 const std::set<FilePath>& excluded_debug_files) { | |
117 DCHECK_NE(true, debug_info_dir.empty()); | |
118 DCHECK_NE(true, debug_file_pattern.empty()); | |
119 | |
120 std::vector<FilePath> paths; | |
121 base::FileEnumerator enumerator(debug_info_dir, false /* recursive */, | |
122 base::FileEnumerator::FILES, | |
123 debug_file_pattern); | |
124 FilePath path; | |
125 for (path = enumerator.Next(); !path.empty(); path = enumerator.Next()) { | |
126 if (excluded_debug_files.find(path) == excluded_debug_files.end()) | |
127 paths.push_back(path); | |
128 } | |
129 return paths; | |
130 } | |
131 | |
132 std::unique_ptr<StabilityReport> PostmortemReportCollector::Collect( | |
133 const base::FilePath& debug_state_file) { | |
134 // Create a global analyzer. | |
135 std::unique_ptr<GlobalActivityAnalyzer> global_analyzer = | |
136 GlobalActivityAnalyzer::CreateWithFile(debug_state_file); | |
137 if (!global_analyzer) | |
138 return nullptr; | |
139 | |
140 // Early exit if there is no data. | |
141 ThreadActivityAnalyzer* thread_analyzer = global_analyzer->GetFirstAnalyzer(); | |
142 if (!thread_analyzer) { | |
143 // No data. This case happens in the case of a clean exit. | |
144 return nullptr; | |
145 } | |
146 | |
147 // Iterate through the thread analyzers, fleshing out the report. | |
148 std::unique_ptr<StabilityReport> report(new StabilityReport()); | |
149 ProcessState* process_state = report->add_process_states(); | |
150 | |
151 while (thread_analyzer) { | |
Sigurður Ásgeirsson
2016/09/15 18:44:10
could write this as a for(;;)?
manzagop (departed)
2016/09/16 16:37:30
Done, but note I've left the init statement empty
Sigurður Ásgeirsson
2016/09/16 17:01:02
I like it - I often use the same idiom, mainly bec
manzagop (departed)
2016/09/16 18:06:41
Acknowledged.
| |
152 // Only valid analyzers are expected per contract of GetFirstAnalyzer / | |
153 // GetNextAnalyzer. | |
154 DCHECK(thread_analyzer->IsValid()); | |
155 | |
156 ThreadState* thread_state = process_state->add_threads(); | |
157 thread_state->set_thread_name(thread_analyzer->GetThreadName()); | |
158 | |
159 // TODO(manzagop): flesh this out. | |
160 | |
161 thread_analyzer = global_analyzer->GetNextAnalyzer(); | |
162 } | |
163 | |
164 return report; | |
165 } | |
166 | |
167 bool PostmortemReportCollector::WriteReportToMinidump( | |
168 const StabilityReport& report, | |
169 const crashpad::UUID& client_id, | |
170 const crashpad::UUID& report_id, | |
171 base::PlatformFile minidump_file) { | |
172 MinidumpInfo mindump_info; | |
173 mindump_info.client_id = client_id; | |
174 mindump_info.report_id = report_id; | |
175 mindump_info.product_name = version_info::GetProductName(); | |
176 mindump_info.version_number = version_info::GetVersionNumber(); | |
177 | |
178 return WritePostmortemDump(minidump_file, report, mindump_info); | |
179 } | |
180 | |
181 } // namespace browser_watcher | |
OLD | NEW |