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