OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/crash/browser/crash_dump_manager_android.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/format_macros.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/posix/global_descriptors.h" |
| 12 #include "base/process/process.h" |
| 13 #include "base/rand_util.h" |
| 14 #include "base/stl_util.h" |
| 15 #include "base/strings/stringprintf.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/child_process_data.h" |
| 18 #include "content/public/browser/file_descriptor_info.h" |
| 19 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/notification_types.h" |
| 21 #include "content/public/browser/render_process_host.h" |
| 22 |
| 23 using content::BrowserThread; |
| 24 |
| 25 namespace breakpad { |
| 26 |
| 27 // static |
| 28 CrashDumpManager* CrashDumpManager::instance_ = NULL; |
| 29 |
| 30 // static |
| 31 CrashDumpManager* CrashDumpManager::GetInstance() { |
| 32 CHECK(instance_); |
| 33 return instance_; |
| 34 } |
| 35 |
| 36 CrashDumpManager::CrashDumpManager(const base::FilePath& crash_dump_dir) |
| 37 : crash_dump_dir_(crash_dump_dir) { |
| 38 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 39 DCHECK(!instance_); |
| 40 |
| 41 instance_ = this; |
| 42 |
| 43 notification_registrar_.Add(this, |
| 44 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 45 content::NotificationService::AllSources()); |
| 46 notification_registrar_.Add(this, |
| 47 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 48 content::NotificationService::AllSources()); |
| 49 |
| 50 BrowserChildProcessObserver::Add(this); |
| 51 } |
| 52 |
| 53 CrashDumpManager::~CrashDumpManager() { |
| 54 instance_ = NULL; |
| 55 |
| 56 BrowserChildProcessObserver::Remove(this); |
| 57 } |
| 58 |
| 59 base::File CrashDumpManager::CreateMinidumpFile(int child_process_id) { |
| 60 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| 61 base::FilePath minidump_path; |
| 62 if (!base::CreateTemporaryFile(&minidump_path)) |
| 63 return base::File(); |
| 64 |
| 65 // We need read permission as the minidump is generated in several phases |
| 66 // and needs to be read at some point. |
| 67 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 68 base::File::FLAG_WRITE; |
| 69 base::File minidump_file(minidump_path, flags); |
| 70 if (!minidump_file.IsValid()) { |
| 71 LOG(ERROR) << "Failed to create temporary file, crash won't be reported."; |
| 72 return base::File(); |
| 73 } |
| 74 |
| 75 { |
| 76 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); |
| 77 DCHECK(!ContainsKey(child_process_id_to_minidump_path_, child_process_id)); |
| 78 child_process_id_to_minidump_path_[child_process_id] = minidump_path; |
| 79 } |
| 80 return minidump_file.Pass(); |
| 81 } |
| 82 |
| 83 // static |
| 84 void CrashDumpManager::ProcessMinidump(const base::FilePath& minidump_path, |
| 85 base::ProcessHandle pid) { |
| 86 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 87 CHECK(instance_); |
| 88 int64 file_size = 0; |
| 89 int r = base::GetFileSize(minidump_path, &file_size); |
| 90 DCHECK(r) << "Failed to retrieve size for minidump " |
| 91 << minidump_path.value(); |
| 92 |
| 93 if (file_size == 0) { |
| 94 // Empty minidump, this process did not crash. Just remove the file. |
| 95 r = base::DeleteFile(minidump_path, false); |
| 96 DCHECK(r) << "Failed to delete temporary minidump file " |
| 97 << minidump_path.value(); |
| 98 return; |
| 99 } |
| 100 |
| 101 // We are dealing with a valid minidump. Copy it to the crash report |
| 102 // directory from where Java code will upload it later on. |
| 103 if (instance_->crash_dump_dir_.empty()) { |
| 104 NOTREACHED() << "Failed to retrieve the crash dump directory."; |
| 105 return; |
| 106 } |
| 107 const uint64 rand = base::RandUint64(); |
| 108 const std::string filename = |
| 109 base::StringPrintf("chromium-renderer-minidump-%016" PRIx64 ".dmp%d", |
| 110 rand, pid); |
| 111 base::FilePath dest_path = instance_->crash_dump_dir_.Append(filename); |
| 112 r = base::Move(minidump_path, dest_path); |
| 113 if (!r) { |
| 114 LOG(ERROR) << "Failed to move crash dump from " << minidump_path.value() |
| 115 << " to " << dest_path.value(); |
| 116 base::DeleteFile(minidump_path, false); |
| 117 return; |
| 118 } |
| 119 VLOG(1) << "Crash minidump successfully generated: " << |
| 120 instance_->crash_dump_dir_.Append(filename).value(); |
| 121 } |
| 122 |
| 123 void CrashDumpManager::BrowserChildProcessHostDisconnected( |
| 124 const content::ChildProcessData& data) { |
| 125 OnChildExit(data.id, data.handle); |
| 126 } |
| 127 |
| 128 void CrashDumpManager::BrowserChildProcessCrashed( |
| 129 const content::ChildProcessData& data, |
| 130 int exit_code) { |
| 131 OnChildExit(data.id, data.handle); |
| 132 } |
| 133 |
| 134 void CrashDumpManager::Observe(int type, |
| 135 const content::NotificationSource& source, |
| 136 const content::NotificationDetails& details) { |
| 137 switch (type) { |
| 138 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
| 139 // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer |
| 140 // process is cleanly shutdown. However, we need to fallthrough so that |
| 141 // we close the minidump_fd we kept open. |
| 142 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 143 content::RenderProcessHost* rph = |
| 144 content::Source<content::RenderProcessHost>(source).ptr(); |
| 145 OnChildExit(rph->GetID(), rph->GetHandle()); |
| 146 break; |
| 147 } |
| 148 default: |
| 149 NOTREACHED(); |
| 150 return; |
| 151 } |
| 152 } |
| 153 |
| 154 void CrashDumpManager::OnChildExit(int child_process_id, |
| 155 base::ProcessHandle pid) { |
| 156 base::FilePath minidump_path; |
| 157 { |
| 158 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); |
| 159 ChildProcessIDToMinidumpPath::iterator iter = |
| 160 child_process_id_to_minidump_path_.find(child_process_id); |
| 161 if (iter == child_process_id_to_minidump_path_.end()) { |
| 162 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a |
| 163 // NOTIFICATION_RENDERER_PROCESS_CLOSED. |
| 164 return; |
| 165 } |
| 166 minidump_path = iter->second; |
| 167 child_process_id_to_minidump_path_.erase(iter); |
| 168 } |
| 169 BrowserThread::PostTask( |
| 170 BrowserThread::FILE, FROM_HERE, |
| 171 base::Bind(&CrashDumpManager::ProcessMinidump, minidump_path, pid)); |
| 172 } |
| 173 |
| 174 } // namespace breakpad |
OLD | NEW |