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