| 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" | |
| 20 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/child_process_data.h" | 20 #include "content/public/browser/child_process_data.h" |
| 22 #include "content/public/browser/file_descriptor_info.h" | 21 #include "content/public/browser/file_descriptor_info.h" |
| 23 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/browser/notification_types.h" | 23 #include "content/public/browser/notification_types.h" |
| 25 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
| 26 | 25 |
| 27 using content::BrowserThread; | 26 using content::BrowserThread; |
| 28 | 27 |
| 29 namespace breakpad { | 28 namespace breakpad { |
| 30 | 29 |
| 31 CrashDumpManager::CrashDumpManager(const base::FilePath& crash_dump_dir, | 30 // static |
| 32 int descriptor_id) | 31 CrashDumpManager* CrashDumpManager::instance_ = NULL; |
| 33 : crash_dump_dir_(crash_dump_dir), descriptor_id_(descriptor_id) {} | 32 |
| 33 // static |
| 34 CrashDumpManager* CrashDumpManager::GetInstance() { |
| 35 CHECK(instance_); |
| 36 return instance_; |
| 37 } |
| 38 |
| 39 CrashDumpManager::CrashDumpManager(const base::FilePath& crash_dump_dir) |
| 40 : crash_dump_dir_(crash_dump_dir) { |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 42 DCHECK(!instance_); |
| 43 |
| 44 instance_ = this; |
| 45 |
| 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 } |
| 34 | 55 |
| 35 CrashDumpManager::~CrashDumpManager() { | 56 CrashDumpManager::~CrashDumpManager() { |
| 57 instance_ = NULL; |
| 58 |
| 59 BrowserChildProcessObserver::Remove(this); |
| 36 } | 60 } |
| 37 | 61 |
| 38 void CrashDumpManager::OnChildStart(int child_process_id, | 62 base::File CrashDumpManager::CreateMinidumpFile(int child_process_id) { |
| 39 content::FileDescriptorInfo* mappings) { | |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); | 63 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 41 | |
| 42 //if (!breakpad::IsCrashReporterEnabled()) | |
| 43 // return; | |
| 44 | |
| 45 base::FilePath minidump_path; | 64 base::FilePath minidump_path; |
| 46 if (!base::CreateTemporaryFile(&minidump_path)) { | 65 if (!base::CreateTemporaryFile(&minidump_path)) |
| 47 LOG(ERROR) << "Failed to create temporary file, crash won't be reported."; | 66 return base::File(); |
| 48 return; | |
| 49 } | |
| 50 | 67 |
| 51 // We need read permission as the minidump is generated in several phases | 68 // We need read permission as the minidump is generated in several phases |
| 52 // and needs to be read at some point. | 69 // and needs to be read at some point. |
| 53 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 70 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 54 base::File::FLAG_WRITE; | 71 base::File::FLAG_WRITE; |
| 55 base::File minidump_file(minidump_path, flags); | 72 base::File minidump_file(minidump_path, flags); |
| 56 if (!minidump_file.IsValid()) { | 73 if (!minidump_file.IsValid()) { |
| 57 LOG(ERROR) << "Failed to open temporary file, crash won't be reported."; | 74 LOG(ERROR) << "Failed to create temporary file, crash won't be reported."; |
| 58 return; | 75 return base::File(); |
| 59 } | 76 } |
| 60 | 77 |
| 61 { | 78 { |
| 62 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); | 79 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); |
| 63 DCHECK(!base::ContainsKey(child_process_id_to_minidump_path_, | 80 DCHECK(!base::ContainsKey(child_process_id_to_minidump_path_, |
| 64 child_process_id)); | 81 child_process_id)); |
| 65 child_process_id_to_minidump_path_[child_process_id] = minidump_path; | 82 child_process_id_to_minidump_path_[child_process_id] = minidump_path; |
| 66 } | 83 } |
| 67 mappings->Transfer(descriptor_id_, | 84 return minidump_file; |
| 68 base::ScopedFD(minidump_file.TakePlatformFile())); | |
| 69 } | 85 } |
| 70 | 86 |
| 87 // static |
| 71 void CrashDumpManager::ProcessMinidump( | 88 void CrashDumpManager::ProcessMinidump( |
| 72 const base::FilePath& minidump_path, | 89 const base::FilePath& minidump_path, |
| 73 base::ProcessHandle pid, | 90 base::ProcessHandle pid, |
| 74 content::ProcessType process_type, | 91 content::ProcessType process_type, |
| 75 base::TerminationStatus termination_status, | 92 base::TerminationStatus termination_status, |
| 76 base::android::ApplicationState app_state) { | 93 base::android::ApplicationState app_state) { |
| 94 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 95 CHECK(instance_); |
| 77 int64_t file_size = 0; | 96 int64_t file_size = 0; |
| 78 int r = base::GetFileSize(minidump_path, &file_size); | 97 int r = base::GetFileSize(minidump_path, &file_size); |
| 79 DCHECK(r) << "Failed to retrieve size for minidump " | 98 DCHECK(r) << "Failed to retrieve size for minidump " |
| 80 << minidump_path.value(); | 99 << minidump_path.value(); |
| 81 | 100 |
| 82 // TODO(wnwen): If these numbers match up to TabWebContentsObserver's | 101 // TODO(wnwen): If these numbers match up to TabWebContentsObserver's |
| 83 // TabRendererCrashStatus histogram, then remove that one as this is more | 102 // TabRendererCrashStatus histogram, then remove that one as this is more |
| 84 // accurate with more detail. | 103 // accurate with more detail. |
| 85 if (process_type == content::PROCESS_TYPE_RENDERER && | 104 if (process_type == content::PROCESS_TYPE_RENDERER && |
| 86 app_state != base::android::APPLICATION_STATE_UNKNOWN) { | 105 app_state != base::android::APPLICATION_STATE_UNKNOWN) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 if (file_size == 0) { | 139 if (file_size == 0) { |
| 121 // Empty minidump, this process did not crash. Just remove the file. | 140 // Empty minidump, this process did not crash. Just remove the file. |
| 122 r = base::DeleteFile(minidump_path, false); | 141 r = base::DeleteFile(minidump_path, false); |
| 123 DCHECK(r) << "Failed to delete temporary minidump file " | 142 DCHECK(r) << "Failed to delete temporary minidump file " |
| 124 << minidump_path.value(); | 143 << minidump_path.value(); |
| 125 return; | 144 return; |
| 126 } | 145 } |
| 127 | 146 |
| 128 // We are dealing with a valid minidump. Copy it to the crash report | 147 // We are dealing with a valid minidump. Copy it to the crash report |
| 129 // directory from where Java code will upload it later on. | 148 // directory from where Java code will upload it later on. |
| 130 if (crash_dump_dir_.empty()) { | 149 if (instance_->crash_dump_dir_.empty()) { |
| 131 NOTREACHED() << "Failed to retrieve the crash dump directory."; | 150 NOTREACHED() << "Failed to retrieve the crash dump directory."; |
| 132 return; | 151 return; |
| 133 } | 152 } |
| 134 const uint64_t rand = base::RandUint64(); | 153 const uint64_t rand = base::RandUint64(); |
| 135 const std::string filename = | 154 const std::string filename = |
| 136 base::StringPrintf("chromium-renderer-minidump-%016" PRIx64 ".dmp%d", | 155 base::StringPrintf("chromium-renderer-minidump-%016" PRIx64 ".dmp%d", |
| 137 rand, pid); | 156 rand, pid); |
| 138 base::FilePath dest_path = crash_dump_dir_.Append(filename); | 157 base::FilePath dest_path = instance_->crash_dump_dir_.Append(filename); |
| 139 r = base::Move(minidump_path, dest_path); | 158 r = base::Move(minidump_path, dest_path); |
| 140 if (!r) { | 159 if (!r) { |
| 141 LOG(ERROR) << "Failed to move crash dump from " << minidump_path.value() | 160 LOG(ERROR) << "Failed to move crash dump from " << minidump_path.value() |
| 142 << " to " << dest_path.value(); | 161 << " to " << dest_path.value(); |
| 143 base::DeleteFile(minidump_path, false); | 162 base::DeleteFile(minidump_path, false); |
| 144 return; | 163 return; |
| 145 } | 164 } |
| 146 VLOG(1) << "Crash minidump successfully generated: " | 165 VLOG(1) << "Crash minidump successfully generated: " << |
| 147 << crash_dump_dir_.Append(filename).value(); | 166 instance_->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); |
| 148 } | 226 } |
| 149 | 227 |
| 150 void CrashDumpManager::OnChildExit(int child_process_id, | 228 void CrashDumpManager::OnChildExit(int child_process_id, |
| 151 base::ProcessHandle pid, | 229 base::ProcessHandle pid, |
| 152 content::ProcessType process_type, | 230 content::ProcessType process_type, |
| 153 base::TerminationStatus termination_status, | 231 base::TerminationStatus termination_status, |
| 154 base::android::ApplicationState app_state) { | 232 base::android::ApplicationState app_state) { |
| 155 base::FilePath minidump_path; | 233 base::FilePath minidump_path; |
| 156 { | 234 { |
| 157 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); | 235 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); |
| 158 ChildProcessIDToMinidumpPath::iterator iter = | 236 ChildProcessIDToMinidumpPath::iterator iter = |
| 159 child_process_id_to_minidump_path_.find(child_process_id); | 237 child_process_id_to_minidump_path_.find(child_process_id); |
| 160 if (iter == child_process_id_to_minidump_path_.end()) { | 238 if (iter == child_process_id_to_minidump_path_.end()) { |
| 161 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a | 239 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a |
| 162 // NOTIFICATION_RENDERER_PROCESS_CLOSED. | 240 // NOTIFICATION_RENDERER_PROCESS_CLOSED. |
| 163 return; | 241 return; |
| 164 } | 242 } |
| 165 minidump_path = iter->second; | 243 minidump_path = iter->second; |
| 166 child_process_id_to_minidump_path_.erase(iter); | 244 child_process_id_to_minidump_path_.erase(iter); |
| 167 } | 245 } |
| 168 ProcessMinidump(minidump_path, pid, process_type, termination_status, | 246 BrowserThread::PostTask( |
| 169 app_state); | 247 BrowserThread::FILE, FROM_HERE, |
| 248 base::Bind(&CrashDumpManager::ProcessMinidump, |
| 249 minidump_path, |
| 250 pid, |
| 251 process_type, |
| 252 termination_status, |
| 253 app_state)); |
| 170 } | 254 } |
| 171 | 255 |
| 172 } // namespace breakpad | 256 } // namespace breakpad |
| OLD | NEW |