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 | 71 // static |
88 void CrashDumpManager::ProcessMinidump( | 72 void CrashDumpManager::ProcessMinidump( |
89 const base::FilePath& minidump_path, | 73 const base::FilePath& minidump_path, |
| 74 const base::FilePath& crash_dump_dir, |
90 base::ProcessHandle pid, | 75 base::ProcessHandle pid, |
91 content::ProcessType process_type, | 76 content::ProcessType process_type, |
92 base::TerminationStatus termination_status, | 77 base::TerminationStatus termination_status, |
93 base::android::ApplicationState app_state) { | 78 base::android::ApplicationState app_state) { |
94 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 79 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
95 CHECK(instance_); | |
96 int64_t file_size = 0; | 80 int64_t file_size = 0; |
97 int r = base::GetFileSize(minidump_path, &file_size); | 81 int r = base::GetFileSize(minidump_path, &file_size); |
98 DCHECK(r) << "Failed to retrieve size for minidump " | 82 DCHECK(r) << "Failed to retrieve size for minidump " |
99 << minidump_path.value(); | 83 << minidump_path.value(); |
100 | 84 |
101 // TODO(wnwen): If these numbers match up to TabWebContentsObserver's | 85 // TODO(wnwen): If these numbers match up to TabWebContentsObserver's |
102 // TabRendererCrashStatus histogram, then remove that one as this is more | 86 // TabRendererCrashStatus histogram, then remove that one as this is more |
103 // accurate with more detail. | 87 // accurate with more detail. |
104 if ((process_type == content::PROCESS_TYPE_RENDERER || | 88 if ((process_type == content::PROCESS_TYPE_RENDERER || |
105 process_type == content::PROCESS_TYPE_GPU) && | 89 process_type == content::PROCESS_TYPE_GPU) && |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 if (file_size == 0) { | 130 if (file_size == 0) { |
147 // Empty minidump, this process did not crash. Just remove the file. | 131 // Empty minidump, this process did not crash. Just remove the file. |
148 r = base::DeleteFile(minidump_path, false); | 132 r = base::DeleteFile(minidump_path, false); |
149 DCHECK(r) << "Failed to delete temporary minidump file " | 133 DCHECK(r) << "Failed to delete temporary minidump file " |
150 << minidump_path.value(); | 134 << minidump_path.value(); |
151 return; | 135 return; |
152 } | 136 } |
153 | 137 |
154 // We are dealing with a valid minidump. Copy it to the crash report | 138 // We are dealing with a valid minidump. Copy it to the crash report |
155 // directory from where Java code will upload it later on. | 139 // directory from where Java code will upload it later on. |
156 if (instance_->crash_dump_dir_.empty()) { | 140 if (crash_dump_dir.empty()) { |
157 NOTREACHED() << "Failed to retrieve the crash dump directory."; | 141 NOTREACHED() << "Failed to retrieve the crash dump directory."; |
158 return; | 142 return; |
159 } | 143 } |
160 const uint64_t rand = base::RandUint64(); | 144 const uint64_t rand = base::RandUint64(); |
161 const std::string filename = | 145 const std::string filename = |
162 base::StringPrintf("chromium-renderer-minidump-%016" PRIx64 ".dmp%d", | 146 base::StringPrintf("chromium-renderer-minidump-%016" PRIx64 ".dmp%d", |
163 rand, pid); | 147 rand, pid); |
164 base::FilePath dest_path = instance_->crash_dump_dir_.Append(filename); | 148 base::FilePath dest_path = crash_dump_dir.Append(filename); |
165 r = base::Move(minidump_path, dest_path); | 149 r = base::Move(minidump_path, dest_path); |
166 if (!r) { | 150 if (!r) { |
167 LOG(ERROR) << "Failed to move crash dump from " << minidump_path.value() | 151 LOG(ERROR) << "Failed to move crash dump from " << minidump_path.value() |
168 << " to " << dest_path.value(); | 152 << " to " << dest_path.value(); |
169 base::DeleteFile(minidump_path, false); | 153 base::DeleteFile(minidump_path, false); |
170 return; | 154 return; |
171 } | 155 } |
172 VLOG(1) << "Crash minidump successfully generated: " << | 156 VLOG(1) << "Crash minidump successfully generated: " |
173 instance_->crash_dump_dir_.Append(filename).value(); | 157 << crash_dump_dir.Append(filename).value(); |
174 } | |
175 | |
176 void CrashDumpManager::BrowserChildProcessHostDisconnected( | |
177 const content::ChildProcessData& data) { | |
178 OnChildExit(data.id, | |
179 data.handle, | |
180 static_cast<content::ProcessType>(data.process_type), | |
181 base::TERMINATION_STATUS_MAX_ENUM, | |
182 base::android::ApplicationStatusListener::GetState()); | |
183 } | |
184 | |
185 void CrashDumpManager::BrowserChildProcessCrashed( | |
186 const content::ChildProcessData& data, | |
187 int exit_code) { | |
188 OnChildExit(data.id, | |
189 data.handle, | |
190 static_cast<content::ProcessType>(data.process_type), | |
191 base::TERMINATION_STATUS_ABNORMAL_TERMINATION, | |
192 base::android::APPLICATION_STATE_UNKNOWN); | |
193 } | |
194 | |
195 void CrashDumpManager::Observe(int type, | |
196 const content::NotificationSource& source, | |
197 const content::NotificationDetails& details) { | |
198 content::RenderProcessHost* rph = | |
199 content::Source<content::RenderProcessHost>(source).ptr(); | |
200 base::TerminationStatus term_status = base::TERMINATION_STATUS_MAX_ENUM; | |
201 base::android::ApplicationState app_state = | |
202 base::android::APPLICATION_STATE_UNKNOWN; | |
203 switch (type) { | |
204 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | |
205 // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer | |
206 // process is cleanly shutdown. However, we still need to close the | |
207 // minidump_fd we kept open. | |
208 term_status = base::TERMINATION_STATUS_NORMAL_TERMINATION; | |
209 break; | |
210 } | |
211 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | |
212 // We do not care about android fast shutdowns as it is a known case where | |
213 // the renderer is intentionally killed when we are done with it. | |
214 if (rph->FastShutdownStarted()) { | |
215 break; | |
216 } | |
217 content::RenderProcessHost::RendererClosedDetails* process_details = | |
218 content::Details<content::RenderProcessHost::RendererClosedDetails>( | |
219 details).ptr(); | |
220 term_status = process_details->status; | |
221 app_state = base::android::ApplicationStatusListener::GetState(); | |
222 break; | |
223 } | |
224 default: | |
225 NOTREACHED(); | |
226 return; | |
227 } | |
228 OnChildExit(rph->GetID(), | |
229 rph->GetHandle(), | |
230 content::PROCESS_TYPE_RENDERER, | |
231 term_status, | |
232 app_state); | |
233 } | 158 } |
234 | 159 |
235 void CrashDumpManager::OnChildExit(int child_process_id, | 160 void CrashDumpManager::OnChildExit(int child_process_id, |
236 base::ProcessHandle pid, | 161 base::ProcessHandle pid, |
237 content::ProcessType process_type, | 162 content::ProcessType process_type, |
238 base::TerminationStatus termination_status, | 163 base::TerminationStatus termination_status, |
239 base::android::ApplicationState app_state) { | 164 base::android::ApplicationState app_state) { |
240 base::FilePath minidump_path; | 165 base::FilePath minidump_path; |
241 { | 166 { |
242 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); | 167 base::AutoLock auto_lock(child_process_id_to_minidump_path_lock_); |
243 ChildProcessIDToMinidumpPath::iterator iter = | 168 ChildProcessIDToMinidumpPath::iterator iter = |
244 child_process_id_to_minidump_path_.find(child_process_id); | 169 child_process_id_to_minidump_path_.find(child_process_id); |
245 if (iter == child_process_id_to_minidump_path_.end()) { | 170 if (iter == child_process_id_to_minidump_path_.end()) { |
246 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a | 171 // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a |
247 // NOTIFICATION_RENDERER_PROCESS_CLOSED. | 172 // NOTIFICATION_RENDERER_PROCESS_CLOSED. |
248 return; | 173 return; |
249 } | 174 } |
250 minidump_path = iter->second; | 175 minidump_path = iter->second; |
251 child_process_id_to_minidump_path_.erase(iter); | 176 child_process_id_to_minidump_path_.erase(iter); |
252 } | 177 } |
253 BrowserThread::PostTask( | 178 BrowserThread::PostTask( |
254 BrowserThread::FILE, FROM_HERE, | 179 BrowserThread::FILE, FROM_HERE, |
255 base::Bind(&CrashDumpManager::ProcessMinidump, | 180 base::Bind(&CrashDumpManager::ProcessMinidump, minidump_path, |
256 minidump_path, | 181 crash_dump_dir_, pid, process_type, termination_status, |
257 pid, | |
258 process_type, | |
259 termination_status, | |
260 app_state)); | 182 app_state)); |
261 } | 183 } |
262 | 184 |
263 } // namespace breakpad | 185 } // namespace breakpad |
OLD | NEW |