Index: components/browser_watcher/postmortem.cc |
diff --git a/components/browser_watcher/postmortem.cc b/components/browser_watcher/postmortem.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2ccaf3c5c033660bbf57249e8b7a88f29dee153b |
--- /dev/null |
+++ b/components/browser_watcher/postmortem.cc |
@@ -0,0 +1,107 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/browser_watcher/postmortem.h" |
+ |
+#include <memory> |
+ |
+#include "base/debug/activity_analyzer.h" |
+#include "base/files/file_enumerator.h" |
+#include "base/files/file_util.h" |
+#include "components/browser_watcher/postmortem_minidump_writer.h" |
+ |
+using base::FilePath; |
+ |
+namespace browser_watcher { |
+ |
+using base::debug::GlobalActivityAnalyzer; |
+using base::debug::ThreadActivityAnalyzer; |
+ |
+PostmortemReportCollector::PostmortemReportCollector( |
+ const FilePath& debug_dir, |
+ const FilePath::StringType& debug_file_pattern, |
+ ReporterDelegate* delegate) |
+ : debug_state_dir_(debug_dir), |
+ debug_file_pattern_(debug_file_pattern), |
+ delegate_(delegate) { |
+ DCHECK_NE(true, debug_dir.empty()); |
+ DCHECK_NE(true, debug_file_pattern.empty()); |
+ DCHECK_NE(nullptr, delegate); |
+} |
+ |
+void PostmortemReportCollector::CollectAndSubmitForUpload( |
+ const std::set<FilePath>& excluded_debug_files) { |
+ std::vector<FilePath> debug_files = |
+ GetDebugStateFilePaths(excluded_debug_files); |
+ |
+ for (const FilePath& file : debug_files) { |
+ // Collect the report, then delete the file. There is no retry. |
+ std::unique_ptr<StabilityReport> report = Collect(file); |
+ base::DeleteFile(file, false); |
+ if (report.get() == nullptr) |
+ continue; |
+ |
+ base::FilePath minidump_path = |
+ file.ReplaceExtension(FILE_PATH_LITERAL(".dmp")); |
+ if (CreateReport(*report, minidump_path)) |
+ delegate_->SubmitReportForUpload(minidump_path); |
+ } |
+} |
+ |
+PostmortemReportCollector::PostmortemReportCollector() : delegate_(nullptr) {} |
+ |
+std::vector<FilePath> PostmortemReportCollector::GetDebugStateFilePaths( |
+ const std::set<FilePath>& excluded_debug_files) { |
+ std::vector<FilePath> paths; |
+ base::FileEnumerator enumerator(debug_state_dir_, false /* recursive */, |
+ base::FileEnumerator::FILES, |
+ debug_file_pattern_); |
+ FilePath path; |
+ for (path = enumerator.Next(); !path.empty(); path = enumerator.Next()) { |
+ if (excluded_debug_files.find(path) == excluded_debug_files.end()) |
+ paths.push_back(path); |
+ } |
+ return paths; |
+} |
+ |
+std::unique_ptr<StabilityReport> PostmortemReportCollector::Collect( |
+ const base::FilePath& debug_state_file) { |
+ // Create a global analyzer. |
+ std::unique_ptr<GlobalActivityAnalyzer> global_analyzer = |
+ GlobalActivityAnalyzer::CreateWithFile(debug_state_file); |
+ if (global_analyzer == nullptr) |
bcwhite
2016/08/04 13:38:11
!global_analyzer?
manzagop (departed)
2016/08/10 15:59:51
Done. Throughout the file.
|
+ return nullptr; |
+ |
+ // Early exit if there is no data. |
+ ThreadActivityAnalyzer* thread_analyzer = global_analyzer->GetFirstAnalyzer(); |
+ if (thread_analyzer == nullptr) |
+ return nullptr; // No data. |
+ |
+ // Iterate through the thread analyzers, fleshing out the report. |
+ std::unique_ptr<StabilityReport> report(new StabilityReport()); |
+ ProcessState* process_state = report->add_process_states(); |
+ |
+ while (thread_analyzer != nullptr) { |
+ // Only valid analyzers are expected. |
+ DCHECK(thread_analyzer->IsValid()); |
+ |
+ ThreadState* thread_state = process_state->add_threads(); |
+ thread_state->set_thread_name(thread_analyzer->GetThreadName()); |
+ |
+ // TODO(manzagop): flesh this out. |
+ |
+ thread_analyzer = global_analyzer->GetNextAnalyzer(); |
+ } |
+ |
+ return report; |
+} |
+ |
+bool PostmortemReportCollector::CreateReport( |
+ const StabilityReport& report, |
+ const base::FilePath& minidump_path) { |
+ PostmortemMinidumpWriter writer; |
+ return writer.WriteDump(minidump_path, report); |
+} |
+ |
+} // namespace browser_watcher |