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