Index: components/browser_watcher/postmortem_report_collector.cc |
diff --git a/components/browser_watcher/postmortem_report_collector.cc b/components/browser_watcher/postmortem_report_collector.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3987d65c19d72b5de4dc00f935f3339a16a3d259 |
--- /dev/null |
+++ b/components/browser_watcher/postmortem_report_collector.cc |
@@ -0,0 +1,182 @@ |
+// 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_report_collector.h" |
+ |
+#include <utility> |
+ |
+#include "base/debug/activity_analyzer.h" |
+#include "base/files/file_enumerator.h" |
+#include "base/files/file_util.h" |
+#include "base/logging.h" |
+#include "base/path_service.h" |
+#include "components/browser_watcher/postmortem_minidump_writer.h" |
+#include "third_party/crashpad/crashpad/client/settings.h" |
+#include "third_party/crashpad/crashpad/util/misc/uuid.h" |
+ |
+using base::FilePath; |
+ |
+namespace browser_watcher { |
+ |
+using base::debug::GlobalActivityAnalyzer; |
+using base::debug::ThreadActivityAnalyzer; |
+using crashpad::CrashReportDatabase; |
+ |
+namespace { |
+ |
+// An object that deletes a file when it goes out of scope. |
+class FileDeleter { |
+ public: |
+ explicit FileDeleter(FilePath path) : file_path_(path) {} |
+ ~FileDeleter() { |
+ if (!base::DeleteFile(file_path_, false)) |
+ LOG(ERROR) << "Failed to delete " << file_path_.value(); |
+ } |
+ |
+ private: |
+ FilePath file_path_; |
+}; |
+ |
+} // namespace |
+ |
+PostmortemReportCollector::PostmortemReportCollector( |
+ const FilePath& debug_dir, |
+ const FilePath::StringType& debug_file_pattern, |
+ std::unique_ptr<crashpad::CrashReportDatabase> report_database) |
+ : debug_state_dir_(debug_dir), |
+ debug_file_pattern_(debug_file_pattern), |
+ report_database_(std::move(report_database)) { |
+ DCHECK_NE(true, debug_dir.empty()); |
+ DCHECK_NE(true, debug_file_pattern.empty()); |
+ DCHECK_NE(nullptr, report_database_.get()); |
+} |
+ |
+// TODO(manzagop): throttling and graceful handling of too much data. |
+void PostmortemReportCollector::CollectAndSubmitForUpload( |
+ const std::set<FilePath>& excluded_debug_files) { |
+ DCHECK_NE(nullptr, report_database_.get()); |
+ |
+ // Collect the list of files to harvest. |
+ std::vector<FilePath> debug_files = |
+ GetDebugStateFilePaths(excluded_debug_files); |
+ |
+ // Determine the crashpad client id. |
+ crashpad::UUID client_id; |
+ crashpad::Settings* settings = report_database_->GetSettings(); |
+ if (settings) { |
+ // If GetSettings() or GetClientID() fails client_id will be left at its |
+ // default value, all zeroes, which is appropriate. |
+ settings->GetClientID(&client_id); |
+ } |
+ |
+ // Note: the code below involves two notions of report: |
+ // - crashpad reports |
+ // - chrome internal state reports, which are reported wrapped inside a |
+ // crashpad report |
+ for (const FilePath& file : debug_files) { |
+ FileDeleter debug_file_deleter(file); |
Sigurður Ásgeirsson
2016/08/11 17:44:15
The intent here seems to be to make at most one re
manzagop (departed)
2016/08/12 21:23:22
Good point. Done.
|
+ |
+ // Collect the data from the debug file to a proto. |
+ std::unique_ptr<StabilityReport> report_proto = Collect(file); |
+ if (!report_proto) { |
+ // The file was empty, or there was an error collecting the data. Detailed |
+ // logging happens within the Collect function. |
+ continue; |
+ } |
+ |
+ // Prepare a crashpad report. |
+ CrashReportDatabase::NewReport* new_report; |
+ CrashReportDatabase::OperationStatus database_status = |
+ report_database_->PrepareNewCrashReport(&new_report); |
+ if (database_status != CrashReportDatabase::kNoError) { |
+ // Note that we delete the report even in this case, to prevent report |
+ // accumulation. |
+ LOG(ERROR) << "PrepareNewCrashReport failed"; |
+ continue; |
+ } |
+ CrashReportDatabase::CallErrorWritingCrashReport |
+ call_error_writing_crash_report(report_database_.get(), new_report); |
+ |
+ // Write the report to a minidump. |
+ if (!WriteReportToMinidump(*report_proto, client_id, new_report->uuid, |
+ reinterpret_cast<FILE*>(new_report->handle))) { |
+ // Note that we delete the report even in this case, to prevent report |
+ // accumulation. Detailed logging happens within the function. |
+ continue; |
+ } |
+ |
+ // Finalize the report wrt the report database. |
+ call_error_writing_crash_report.Disarm(); |
+ crashpad::UUID unused_report_id; |
+ database_status = report_database_->FinishedWritingCrashReport( |
+ new_report, &unused_report_id); |
+ if (database_status != CrashReportDatabase::kNoError) { |
+ // Note that we delete the report even in this case, to prevent report |
+ // accumulation. |
+ LOG(ERROR) << "FinishedWritingCrashReport failed"; |
+ continue; |
+ } |
+ } |
+} |
+ |
+PostmortemReportCollector::PostmortemReportCollector() {} |
+ |
+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) |
+ return nullptr; |
+ |
+ // Early exit if there is no data. |
+ ThreadActivityAnalyzer* thread_analyzer = global_analyzer->GetFirstAnalyzer(); |
+ if (!thread_analyzer) { |
+ // No data. This case happens in the case of a clean exit. |
+ return nullptr; |
+ } |
+ |
+ // 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) { |
+ // 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::WriteReportToMinidump( |
+ const StabilityReport& report, |
+ const crashpad::UUID& client_id, |
+ const crashpad::UUID& report_id, |
+ FILE* minidump_file) { |
+ PostmortemMinidumpWriter writer(minidump_file); |
+ return writer.WriteDump(report, client_id, report_id); |
+} |
+ |
+} // namespace browser_watcher |