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.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/debug/activity_analyzer.h" | |
10 #include "base/files/file_enumerator.h" | |
11 #include "base/files/file_util.h" | |
12 #include "components/browser_watcher/postmortem_minidump_writer.h" | |
13 | |
14 using base::FilePath; | |
15 | |
16 namespace browser_watcher { | |
17 | |
18 using base::debug::GlobalActivityAnalyzer; | |
19 using base::debug::ThreadActivityAnalyzer; | |
20 | |
21 PostmortemReportCollector::PostmortemReportCollector( | |
22 const FilePath& debug_dir, | |
23 const FilePath::StringType& debug_file_pattern, | |
24 ReporterDelegate* delegate) | |
25 : debug_state_dir_(debug_dir), | |
26 debug_file_pattern_(debug_file_pattern), | |
27 delegate_(delegate) { | |
28 DCHECK_NE(true, debug_dir.empty()); | |
29 DCHECK_NE(true, debug_file_pattern.empty()); | |
30 DCHECK_NE(nullptr, delegate); | |
31 } | |
32 | |
33 void PostmortemReportCollector::CollectAndSubmitForUpload( | |
34 const std::set<FilePath>& excluded_debug_files) { | |
35 std::vector<FilePath> debug_files = | |
36 GetDebugStateFilePaths(excluded_debug_files); | |
37 | |
38 for (const FilePath& file : debug_files) { | |
39 // Collect the report, then delete the file. There is no retry. | |
40 std::unique_ptr<StabilityReport> report = Collect(file); | |
41 base::DeleteFile(file, false); | |
42 if (report.get() == nullptr) | |
43 continue; | |
44 | |
45 base::FilePath minidump_path = | |
46 file.ReplaceExtension(FILE_PATH_LITERAL(".dmp")); | |
47 if (CreateReport(*report, minidump_path)) | |
48 delegate_->SubmitReportForUpload(minidump_path); | |
49 } | |
50 } | |
51 | |
52 PostmortemReportCollector::PostmortemReportCollector() : delegate_(nullptr) {} | |
53 | |
54 std::vector<FilePath> PostmortemReportCollector::GetDebugStateFilePaths( | |
55 const std::set<FilePath>& excluded_debug_files) { | |
56 std::vector<FilePath> paths; | |
57 base::FileEnumerator enumerator(debug_state_dir_, false /* recursive */, | |
58 base::FileEnumerator::FILES, | |
59 debug_file_pattern_); | |
60 FilePath path; | |
61 for (path = enumerator.Next(); !path.empty(); path = enumerator.Next()) { | |
62 if (excluded_debug_files.find(path) == excluded_debug_files.end()) | |
63 paths.push_back(path); | |
64 } | |
65 return paths; | |
66 } | |
67 | |
68 std::unique_ptr<StabilityReport> PostmortemReportCollector::Collect( | |
69 const base::FilePath& debug_state_file) { | |
70 // Create a global analyzer. | |
71 std::unique_ptr<GlobalActivityAnalyzer> global_analyzer = | |
72 GlobalActivityAnalyzer::CreateWithFile(debug_state_file); | |
73 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.
| |
74 return nullptr; | |
75 | |
76 // Early exit if there is no data. | |
77 ThreadActivityAnalyzer* thread_analyzer = global_analyzer->GetFirstAnalyzer(); | |
78 if (thread_analyzer == nullptr) | |
79 return nullptr; // No data. | |
80 | |
81 // Iterate through the thread analyzers, fleshing out the report. | |
82 std::unique_ptr<StabilityReport> report(new StabilityReport()); | |
83 ProcessState* process_state = report->add_process_states(); | |
84 | |
85 while (thread_analyzer != nullptr) { | |
86 // Only valid analyzers are expected. | |
87 DCHECK(thread_analyzer->IsValid()); | |
88 | |
89 ThreadState* thread_state = process_state->add_threads(); | |
90 thread_state->set_thread_name(thread_analyzer->GetThreadName()); | |
91 | |
92 // TODO(manzagop): flesh this out. | |
93 | |
94 thread_analyzer = global_analyzer->GetNextAnalyzer(); | |
95 } | |
96 | |
97 return report; | |
98 } | |
99 | |
100 bool PostmortemReportCollector::CreateReport( | |
101 const StabilityReport& report, | |
102 const base::FilePath& minidump_path) { | |
103 PostmortemMinidumpWriter writer; | |
104 return writer.WriteDump(minidump_path, report); | |
105 } | |
106 | |
107 } // namespace browser_watcher | |
OLD | NEW |