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