Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/browser_watcher/stability_paths.h" | 5 #include "components/browser_watcher/stability_paths.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #endif // defined(OS_WIN) | 9 #endif // defined(OS_WIN) |
| 10 | 10 |
| 11 #include <memory> | |
| 11 #include <string> | 12 #include <string> |
| 13 #include <utility> | |
| 12 | 14 |
| 15 #include "base/debug/activity_tracker.h" | |
| 13 #include "base/feature_list.h" | 16 #include "base/feature_list.h" |
| 14 #include "base/files/file.h" | 17 #include "base/files/file.h" |
| 18 #include "base/files/memory_mapped_file.h" | |
| 19 #include "base/metrics/histogram_macros.h" | |
|
rkaplow
2017/06/01 14:53:48
don't think this include is used
manzagop (departed)
2017/06/01 14:58:27
Done.
| |
| 15 #include "base/metrics/persistent_memory_allocator.h" | 20 #include "base/metrics/persistent_memory_allocator.h" |
| 16 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 17 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 18 #include "components/browser_watcher/features.h" | 23 #include "components/browser_watcher/features.h" |
| 24 #include "components/browser_watcher/stability_metrics.h" | |
| 19 | 25 |
| 20 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
| 21 | 27 |
| 22 #include "third_party/crashpad/crashpad/util/win/time.h" | 28 #include "third_party/crashpad/crashpad/util/win/time.h" |
| 23 | 29 |
| 24 namespace browser_watcher { | 30 namespace browser_watcher { |
| 31 | |
| 32 using base::FilePersistentMemoryAllocator; | |
| 33 using base::MemoryMappedFile; | |
| 34 using base::PersistentMemoryAllocator; | |
| 35 | |
| 25 namespace { | 36 namespace { |
| 26 | 37 |
| 27 bool GetCreationTime(const base::Process& process, FILETIME* creation_time) { | 38 bool GetCreationTime(const base::Process& process, FILETIME* creation_time) { |
| 28 FILETIME ignore; | 39 FILETIME ignore; |
| 29 return ::GetProcessTimes(process.Handle(), creation_time, &ignore, &ignore, | 40 return ::GetProcessTimes(process.Handle(), creation_time, &ignore, &ignore, |
| 30 &ignore) != 0; | 41 &ignore) != 0; |
| 31 } | 42 } |
| 32 | 43 |
| 44 bool SetPmaFileDeleted(const base::FilePath& file_path) { | |
| 45 // Map the file read-write so it can guarantee consistency between | |
| 46 // the analyzer and any trackers that may still be active. | |
| 47 std::unique_ptr<MemoryMappedFile> mmfile(new MemoryMappedFile()); | |
| 48 mmfile->Initialize(file_path, MemoryMappedFile::READ_WRITE); | |
| 49 if (!mmfile->IsValid()) | |
| 50 return false; | |
| 51 if (!FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile, true)) | |
| 52 return false; | |
| 53 FilePersistentMemoryAllocator allocator(std::move(mmfile), 0, 0, | |
| 54 base::StringPiece(), true); | |
| 55 allocator.SetMemoryState(PersistentMemoryAllocator::MEMORY_DELETED); | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 33 } // namespace | 59 } // namespace |
| 34 | 60 |
| 35 base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) { | 61 base::FilePath GetStabilityDir(const base::FilePath& user_data_dir) { |
| 36 return user_data_dir.AppendASCII("Stability"); | 62 return user_data_dir.AppendASCII("Stability"); |
| 37 } | 63 } |
| 38 | 64 |
| 39 base::FilePath GetStabilityFileForProcess(base::ProcessId pid, | 65 base::FilePath GetStabilityFileForProcess(base::ProcessId pid, |
| 40 timeval creation_time, | 66 timeval creation_time, |
| 41 const base::FilePath& user_data_dir) { | 67 const base::FilePath& user_data_dir) { |
| 42 base::FilePath stability_dir = GetStabilityDir(user_data_dir); | 68 base::FilePath stability_dir = GetStabilityDir(user_data_dir); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 65 process.Pid(), crashpad::FiletimeToTimevalEpoch(creation_time), | 91 process.Pid(), crashpad::FiletimeToTimevalEpoch(creation_time), |
| 66 user_data_dir); | 92 user_data_dir); |
| 67 return true; | 93 return true; |
| 68 } | 94 } |
| 69 | 95 |
| 70 base::FilePath::StringType GetStabilityFilePattern() { | 96 base::FilePath::StringType GetStabilityFilePattern() { |
| 71 return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) + | 97 return base::FilePath::StringType(FILE_PATH_LITERAL("*-*")) + |
| 72 base::PersistentMemoryAllocator::kFileExtension; | 98 base::PersistentMemoryAllocator::kFileExtension; |
| 73 } | 99 } |
| 74 | 100 |
| 75 void MarkStabilityFileForDeletion(const base::FilePath& user_data_dir) { | 101 void MarkOwnStabilityFileDeleted(const base::FilePath& user_data_dir) { |
| 76 if (!base::FeatureList::IsEnabled( | 102 base::debug::GlobalActivityTracker* global_tracker = |
| 77 browser_watcher::kStabilityDebuggingFeature)) { | 103 base::debug::GlobalActivityTracker::Get(); |
| 78 return; | 104 if (!global_tracker) |
| 79 } | 105 return; // No stability instrumentation. |
| 80 | 106 |
| 81 base::FilePath stability_file; | 107 global_tracker->MarkDeleted(); |
| 82 if (!GetStabilityFileForProcess(base::Process::Current(), user_data_dir, | 108 LogStabilityRecordEvent(StabilityRecordEvent::kMarkDeleted); |
| 83 &stability_file)) { | |
| 84 // TODO(manzagop): add a metric for this. | |
| 85 return; | |
| 86 } | |
| 87 | 109 |
| 88 // Open (with delete) and then immediately close the file by going out of | 110 // Open (with delete) and then immediately close the file by going out of |
| 89 // scope. This should cause the stability debugging file to be deleted prior | 111 // scope. This should cause the stability debugging file to be deleted prior |
| 90 // to the next execution. | 112 // to the next execution. |
| 91 base::File file(stability_file, base::File::FLAG_OPEN | | 113 base::FilePath stability_file; |
| 92 base::File::FLAG_READ | | 114 if (!GetStabilityFileForProcess(base::Process::Current(), user_data_dir, |
| 93 base::File::FLAG_DELETE_ON_CLOSE); | 115 &stability_file)) { |
| 116 return; | |
| 117 } | |
| 118 LogStabilityRecordEvent(StabilityRecordEvent::kMarkDeletedGotFile); | |
| 119 | |
| 120 base::File deleter(stability_file, base::File::FLAG_OPEN | | |
| 121 base::File::FLAG_READ | | |
| 122 base::File::FLAG_DELETE_ON_CLOSE); | |
| 123 if (!deleter.IsValid()) | |
| 124 LogStabilityRecordEvent(StabilityRecordEvent::kOpenForDeleteFailed); | |
| 125 } | |
| 126 | |
| 127 void MarkStabilityFileDeletedOnCrash(const base::FilePath& file_path) { | |
| 128 if (!SetPmaFileDeleted(file_path)) | |
| 129 LogCollectOnCrashEvent(CollectOnCrashEvent::kPmaSetDeletedFailed); | |
| 130 | |
| 131 base::File deleter(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ | | |
| 132 base::File::FLAG_DELETE_ON_CLOSE); | |
| 133 if (!deleter.IsValid()) | |
| 134 LogCollectOnCrashEvent(CollectOnCrashEvent::kOpenForDeleteFailed); | |
| 94 } | 135 } |
| 95 | 136 |
| 96 } // namespace browser_watcher | 137 } // namespace browser_watcher |
| 97 | 138 |
| 98 #endif // defined(OS_WIN) | 139 #endif // defined(OS_WIN) |
| OLD | NEW |