Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/crash/content/browser/crash_dump_manager_android.h" | 5 #include "components/crash/content/browser/crash_dump_manager_android.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/posix/global_descriptors.h" | 14 #include "base/posix/global_descriptors.h" |
| 15 #include "base/process/process.h" | 15 #include "base/process/process.h" |
| 16 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "components/crash/content/app/breakpad_linux.h" | |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/child_process_data.h" | 21 #include "content/public/browser/child_process_data.h" |
| 21 #include "content/public/browser/file_descriptor_info.h" | 22 #include "content/public/browser/file_descriptor_info.h" |
| 22 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 23 #include "content/public/browser/notification_types.h" | 24 #include "content/public/browser/notification_types.h" |
| 24 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
| 25 | 26 |
| 26 using content::BrowserThread; | 27 using content::BrowserThread; |
| 27 | 28 |
| 28 namespace breakpad { | 29 namespace breakpad { |
| 29 | 30 |
| 30 // static | 31 CrashDumpManager::CrashDumpManager(const base::FilePath& crash_dump_dir, |
| 31 CrashDumpManager* CrashDumpManager::instance_ = NULL; | 32 int descriptor_id) |
| 33 : crash_dump_dir_(crash_dump_dir), descriptor_id_(descriptor_id) {} | |
| 32 | 34 |
| 33 // static | 35 CrashDumpManager::~CrashDumpManager() { |
| 34 CrashDumpManager* CrashDumpManager::GetInstance() { | |
| 35 CHECK(instance_); | |
| 36 return instance_; | |
| 37 } | 36 } |
| 38 | 37 |
| 39 CrashDumpManager::CrashDumpManager(const base::FilePath& crash_dump_dir) | 38 void CrashDumpManager::OnChildStart(int child_process_id, |
| 40 : crash_dump_dir_(crash_dump_dir) { | 39 content::FileDescriptorInfo* mappings) { |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 40 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 42 DCHECK(!instance_); | |
| 43 | 41 |
| 44 instance_ = this; | 42 if (!breakpad::IsCrashReporterEnabled()) |
| 43 return; | |
| 45 | 44 |
| 46 notification_registrar_.Add(this, | |
| 47 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 48 content::NotificationService::AllSources()); | |
| 49 notification_registrar_.Add(this, | |
| 50 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 51 content::NotificationService::AllSources()); | |
| 52 | |
| 53 BrowserChildProcessObserver::Add(this); | |
| 54 } | |
| 55 | |
| 56 CrashDumpManager::~CrashDumpManager() { | |
| 57 instance_ = NULL; | |
| 58 | |
| 59 BrowserChildProcessObserver::Remove(this); | |
| 60 } | |
| 61 | |
| 62 base::File CrashDumpManager::CreateMinidumpFile(int child_process_id) { | |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); | |
| 64 base::FilePath minidump_path; | 45 base::FilePath minidump_path; |
| 65 if (!base::CreateTemporaryFile(&minidump_path)) | 46 if (!base::CreateTemporaryFile(&minidump_path)) { |
| 66 return base::File(); | 47 LOG(ERROR) << "Failed to create temporary file, crash won't be reported."; |
| 48 return; | |
| 49 } | |
| 67 | 50 |
| 68 // We need read permission as the minidump is generated in several phases | 51 // We need read permission as the minidump is generated in several phases |
| 69 // and needs to be read at some point. | 52 // and needs to be read at some point. |
| 70 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 53 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 71 base::File::FLAG_WRITE; | 54 base::File::FLAG_WRITE; |
| 72 base::File minidump_file(minidump_path, flags); | 55 base::File minidump_file(minidump_path, flags); |
| 73 if (!minidump_file.IsValid()) { | 56 if (!minidump_file.IsValid()) { |
| 74 LOG(ERROR) << "Failed to create temporary file, crash won't be reported."; | 57 LOG(ERROR) << "Failed to open temporary file, crash won't be reported."; |
| 75 return base::File(); | 58 return; |
| 76 } | 59 } |
| 77 | 60 |
| 78 { | 61 { |
| 79 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); | 62 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); |
| 80 DCHECK(!base::ContainsKey(child_process_id_to_minidump_path_, | 63 DCHECK(!base::ContainsKey(child_process_id_to_minidump_path_, |
| 81 child_process_id)); | 64 child_process_id)); |
| 82 child_process_id_to_minidump_path_[child_process_id] = minidump_path; | 65 child_process_id_to_minidump_path_[child_process_id] = minidump_path; |
| 83 } | 66 } |
| 84 return minidump_file; | 67 mappings->Transfer(descriptor_id_, |
| 68 base::ScopedFD(minidump_file.TakePlatformFile())); | |
| 85 } | 69 } |
| 86 | 70 |
| 87 // static | |
| 88 void CrashDumpManager::ProcessMinidump( | 71 void CrashDumpManager::ProcessMinidump( |
| 89 const base::FilePath& minidump_path, | 72 const base::FilePath& minidump_path, |
| 90 base::ProcessHandle pid, | 73 base::ProcessHandle pid, |
| 91 content::ProcessType process_type, | 74 content::ProcessType process_type, |
| 92 base::TerminationStatus termination_status, | 75 base::TerminationStatus termination_status, |
| 93 base::android::ApplicationState app_state) { | 76 base::android::ApplicationState app_state) { |
| 94 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
|
Lei Zhang
2016/10/05 18:00:00
I'm curious what thread CrashDumpManager actually
Tobias Sargeant
2016/10/06 17:42:22
It's created on the main thread, but callbacks occ
| |
| 95 CHECK(instance_); | |
| 96 int64_t file_size = 0; | 77 int64_t file_size = 0; |
| 97 int r = base::GetFileSize(minidump_path, &file_size); | 78 int r = base::GetFileSize(minidump_path, &file_size); |
| 98 DCHECK(r) << "Failed to retrieve size for minidump " | 79 DCHECK(r) << "Failed to retrieve size for minidump " |
| 99 << minidump_path.value(); | 80 << minidump_path.value(); |
| 100 | 81 |
| 101 // TODO(wnwen): If these numbers match up to TabWebContentsObserver's | 82 // TODO(wnwen): If these numbers match up to TabWebContentsObserver's |
| 102 // TabRendererCrashStatus histogram, then remove that one as this is more | 83 // TabRendererCrashStatus histogram, then remove that one as this is more |
| 103 // accurate with more detail. | 84 // accurate with more detail. |
| 104 if (process_type == content::PROCESS_TYPE_RENDERER && | 85 if (process_type == content::PROCESS_TYPE_RENDERER && |
| 105 app_state != base::android::APPLICATION_STATE_UNKNOWN) { | 86 app_state != base::android::APPLICATION_STATE_UNKNOWN) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 if (file_size == 0) { | 120 if (file_size == 0) { |
| 140 // Empty minidump, this process did not crash. Just remove the file. | 121 // Empty minidump, this process did not crash. Just remove the file. |
| 141 r = base::DeleteFile(minidump_path, false); | 122 r = base::DeleteFile(minidump_path, false); |
| 142 DCHECK(r) << "Failed to delete temporary minidump file " | 123 DCHECK(r) << "Failed to delete temporary minidump file " |
| 143 << minidump_path.value(); | 124 << minidump_path.value(); |
| 144 return; | 125 return; |
| 145 } | 126 } |
| 146 | 127 |
| 147 // We are dealing with a valid minidump. Copy it to the crash report | 128 // We are dealing with a valid minidump. Copy it to the crash report |
| 148 // directory from where Java code will upload it later on. | 129 // directory from where Java code will upload it later on. |
| 149 if (instance_->crash_dump_dir_.empty()) { | 130 if (crash_dump_dir_.empty()) { |
| 150 NOTREACHED() << "Failed to retrieve the crash dump directory."; | 131 NOTREACHED() << "Failed to retrieve the crash dump directory."; |
| 151 return; | 132 return; |
| 152 } | 133 } |
| 153 const uint64_t rand = base::RandUint64(); | 134 const uint64_t rand = base::RandUint64(); |
| 154 const std::string filename = | 135 const std::string filename = |
| 155 base::StringPrintf("chromium-renderer-minidump-%016" PRIx64 ".dmp%d", | 136 base::StringPrintf("chromium-renderer-minidump-%016" PRIx64 ".dmp%d", |
| 156 rand, pid); | 137 rand, pid); |
| 157 base::FilePath dest_path = instance_->crash_dump_dir_.Append(filename); | 138 base::FilePath dest_path = crash_dump_dir_.Append(filename); |
| 158 r = base::Move(minidump_path, dest_path); | 139 r = base::Move(minidump_path, dest_path); |
| 159 if (!r) { | 140 if (!r) { |
| 160 LOG(ERROR) << "Failed to move crash dump from " << minidump_path.value() | 141 LOG(ERROR) << "Failed to move crash dump from " << minidump_path.value() |
| 161 << " to " << dest_path.value(); | 142 << " to " << dest_path.value(); |
| 162 base::DeleteFile(minidump_path, false); | 143 base::DeleteFile(minidump_path, false); |
| 163 return; | 144 return; |
| 164 } | 145 } |
| 165 VLOG(1) << "Crash minidump successfully generated: " << | 146 VLOG(1) << "Crash minidump successfully generated: " |
| 166 instance_->crash_dump_dir_.Append(filename).value(); | 147 << crash_dump_dir_.Append(filename).value(); |
| 167 } | |
| 168 | |
| 169 void CrashDumpManager::BrowserChildProcessHostDisconnected( | |
| 170 const content::ChildProcessData& data) { | |
| 171 OnChildExit(data.id, | |
| 172 data.handle, | |
| 173 static_cast<content::ProcessType>(data.process_type), | |
| 174 base::TERMINATION_STATUS_MAX_ENUM, | |
| 175 base::android::APPLICATION_STATE_UNKNOWN); | |
| 176 } | |
| 177 | |
| 178 void CrashDumpManager::BrowserChildProcessCrashed( | |
| 179 const content::ChildProcessData& data, | |
| 180 int exit_code) { | |
| 181 OnChildExit(data.id, | |
| 182 data.handle, | |
| 183 static_cast<content::ProcessType>(data.process_type), | |
| 184 base::TERMINATION_STATUS_ABNORMAL_TERMINATION, | |
| 185 base::android::APPLICATION_STATE_UNKNOWN); | |
| 186 } | |
| 187 | |
| 188 void CrashDumpManager::Observe(int type, | |
| 189 const content::NotificationSource& source, | |
| 190 const content::NotificationDetails& details) { | |
| 191 content::RenderProcessHost* rph = | |
| 192 content::Source<content::RenderProcessHost>(source).ptr(); | |
| 193 base::TerminationStatus term_status = base::TERMINATION_STATUS_MAX_ENUM; | |
| 194 base::android::ApplicationState app_state = | |
| 195 base::android::APPLICATION_STATE_UNKNOWN; | |
| 196 switch (type) { | |
| 197 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | |
| 198 // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer | |
| 199 // process is cleanly shutdown. However, we still need to close the | |
| 200 // minidump_fd we kept open. | |
| 201 term_status = base::TERMINATION_STATUS_NORMAL_TERMINATION; | |
| 202 break; | |
| 203 } | |
| 204 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | |
| 205 // We do not care about android fast shutdowns as it is a known case where | |
| 206 // the renderer is intentionally killed when we are done with it. | |
| 207 if (rph->FastShutdownStarted()) { | |
| 208 break; | |
| 209 } | |
| 210 content::RenderProcessHost::RendererClosedDetails* process_details = | |
| 211 content::Details<content::RenderProcessHost::RendererClosedDetails>( | |
| 212 details).ptr(); | |
| 213 term_status = process_details->status; | |
| 214 app_state = base::android::ApplicationStatusListener::GetState(); | |
| 215 break; | |
| 216 } | |
| 217 default: | |
| 218 NOTREACHED(); | |
| 219 return; | |
| 220 } | |
| 221 OnChildExit(rph->GetID(), | |
| 222 rph->GetHandle(), | |
| 223 content::PROCESS_TYPE_RENDERER, | |
| 224 term_status, | |
| 225 app_state); | |
| 226 } | 148 } |
| 227 | 149 |
| 228 void CrashDumpManager::OnChildExit(int child_process_id, | 150 void CrashDumpManager::OnChildExit(int child_process_id, |
| 229 base::ProcessHandle pid, | 151 base::ProcessHandle pid, |
| 230 content::ProcessType process_type, | 152 content::ProcessType process_type, |
| 231 base::TerminationStatus termination_status, | 153 base::TerminationStatus termination_status, |
| 232 base::android::ApplicationState app_state) { | 154 base::android::ApplicationState app_state) { |
| 233 base::FilePath minidump_path; | 155 base::FilePath minidump_path; |
| 234 { | 156 { |
| 235 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); | 157 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); |
| 236 ChildProcessIDToMinidumpPath::iterator iter = | 158 ChildProcessIDToMinidumpPath::iterator iter = |
| 237 child_process_id_to_minidump_path_.find(child_process_id); | 159 child_process_id_to_minidump_path_.find(child_process_id); |
| 238 if (iter == child_process_id_to_minidump_path_.end()) { | 160 if (iter == child_process_id_to_minidump_path_.end()) { |
| 239 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a | 161 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a |
| 240 // NOTIFICATION_RENDERER_PROCESS_CLOSED. | 162 // NOTIFICATION_RENDERER_PROCESS_CLOSED. |
| 241 return; | 163 return; |
| 242 } | 164 } |
| 243 minidump_path = iter->second; | 165 minidump_path = iter->second; |
| 244 child_process_id_to_minidump_path_.erase(iter); | 166 child_process_id_to_minidump_path_.erase(iter); |
| 245 } | 167 } |
| 246 BrowserThread::PostTask( | 168 ProcessMinidump(minidump_path, pid, process_type, termination_status, |
| 247 BrowserThread::FILE, FROM_HERE, | 169 app_state); |
| 248 base::Bind(&CrashDumpManager::ProcessMinidump, | |
| 249 minidump_path, | |
| 250 pid, | |
| 251 process_type, | |
| 252 termination_status, | |
| 253 app_state)); | |
| 254 } | 170 } |
| 255 | 171 |
| 256 } // namespace breakpad | 172 } // namespace breakpad |
| OLD | NEW |