Chromium Code Reviews| Index: components/crash/content/browser/crash_micro_dump_manager_android.cc |
| diff --git a/components/crash/content/browser/crash_micro_dump_manager_android.cc b/components/crash/content/browser/crash_micro_dump_manager_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..66132bb56d147df5758f6558f48c8ea4c6144fac |
| --- /dev/null |
| +++ b/components/crash/content/browser/crash_micro_dump_manager_android.cc |
| @@ -0,0 +1,171 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/crash/content/browser/crash_micro_dump_manager_android.h" |
| + |
| +#include <fcntl.h> |
| +#include <stdlib.h> |
| +#include <sys/socket.h> |
| +#include <sys/types.h> |
| +#include <unistd.h> |
| + |
| +#include "base/bind.h" |
| +#include "base/files/scoped_file.h" |
| +#include "base/logging.h" |
| +#include "base/posix/eintr_wrapper.h" |
| +#include "base/stl_util.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/child_process_data.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_types.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "ipc/ipc_channel.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace breakpad { |
| + |
| +// static |
| +CrashMicroDumpManager* CrashMicroDumpManager::instance_ = nullptr; |
| + |
| +// static |
| +CrashMicroDumpManager* CrashMicroDumpManager::GetInstance() { |
| + CHECK(instance_); |
| + return instance_; |
| +} |
| + |
| +CrashMicroDumpManager::CrashMicroDumpManager() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + DCHECK(!instance_); |
| + |
| + instance_ = this; |
| + |
| + notification_registrar_.Add(this, |
| + content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| + content::NotificationService::AllSources()); |
| + notification_registrar_.Add(this, |
| + content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| + content::NotificationService::AllSources()); |
| + BrowserChildProcessObserver::Add(this); |
| +} |
| + |
| +CrashMicroDumpManager::~CrashMicroDumpManager() { |
| + instance_ = nullptr; |
| + BrowserChildProcessObserver::Remove(this); |
| + { |
| + base::AutoLock auto_lock(child_process_id_to_pipe_fd_lock_); |
| + for (const auto& iter : child_process_id_to_pipe_fd_) { |
| + IGNORE_EINTR(close(iter.second)); |
| + } |
| + } |
| +} |
| + |
| +base::File CrashMicroDumpManager::CreateCrashInfoChannel(int child_process_id) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); |
| + int local_pipe_fd = -1, child_pipe_fd = -1; |
| + // IPC::SocketPair creates a pipe in non-blocking mode. |
| + if (!IPC::SocketPair(&local_pipe_fd, &child_pipe_fd)) { |
| + return base::File(); |
| + } |
| + { |
| + base::AutoLock auto_lock(child_process_id_to_pipe_fd_lock_); |
| + DCHECK(!ContainsKey(child_process_id_to_pipe_fd_, child_process_id)); |
| + child_process_id_to_pipe_fd_[child_process_id] = local_pipe_fd; |
| + } |
| + return base::File(child_pipe_fd); |
| +} |
| + |
| +// static |
| +void CrashMicroDumpManager::HandleChildTermination( |
| + int pipe_fd, |
| + base::TerminationStatus termination_status) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| + CHECK(instance_); |
| + |
| + base::ScopedFD pipe(pipe_fd); |
|
Peter Wen
2015/12/17 16:47:10
nit: not necessary here, can move further down if
mnaganov (inactive)
2015/12/17 17:30:30
It's necessary to close the pipe fd in the case if
|
| + if (termination_status == base::TERMINATION_STATUS_NORMAL_TERMINATION) { |
| + // Just close our end of the pipe via ScopedFD. |
| + return; |
| + } |
| + int exit_code; |
| + ssize_t num_read = HANDLE_EINTR(read(pipe_fd, &exit_code, sizeof(exit_code))); |
| + if (num_read <= 0) { |
| + // The child process hasn't written anything into the pipe. Assuming |
| + // that it didn't crash, thus we need to perform a clean exit. |
|
Peter Wen
2015/12/17 16:47:09
Please add the implied OOM SIGKILL to be clear for
mnaganov (inactive)
2015/12/17 17:30:30
Done.
|
| + exit(0); |
| + } else { |
| + LOG(FATAL) << "Renderer process crash detected. Terminating browser."; |
| + } |
| +} |
| + |
| +void CrashMicroDumpManager::BrowserChildProcessHostDisconnected( |
|
Peter Wen
2015/12/17 16:47:09
Same as above, are these necessary in webview?
mnaganov (inactive)
2015/12/17 17:30:30
You are right. I missed the comment on BrowserChil
|
| + const content::ChildProcessData& data) { |
| + LOG(WARNING) << "WebView: BrowserChildProcessHostDisconnected!"; |
| + OnChildExit(data.id, base::TERMINATION_STATUS_MAX_ENUM); |
| +} |
| + |
| +void CrashMicroDumpManager::BrowserChildProcessCrashed( |
| + const content::ChildProcessData& data, |
| + int exit_code) { |
| + LOG(WARNING) << "WebView: BrowserChildProcessCrashed!"; |
| + OnChildExit(data.id, base::TERMINATION_STATUS_ABNORMAL_TERMINATION); |
| +} |
| + |
| +void CrashMicroDumpManager::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + content::RenderProcessHost* rph = |
| + content::Source<content::RenderProcessHost>(source).ptr(); |
| + base::TerminationStatus term_status = base::TERMINATION_STATUS_MAX_ENUM; |
| + switch (type) { |
| + case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
| + // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer |
| + // process is cleanly shutdown. However, we still need to close the |
| + // minidump_fd we kept open. |
|
Peter Wen
2015/12/17 16:47:09
Old comments. :)
mnaganov (inactive)
2015/12/17 17:30:30
Changed to pipe_fd. Thanks!
|
| + term_status = base::TERMINATION_STATUS_NORMAL_TERMINATION; |
| + break; |
| + } |
| + case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| + // We do not care about android fast shutdowns as it is a known case where |
| + // the renderer is intentionally killed when we are done with it. |
| + if (rph->FastShutdownStarted()) { |
|
Peter Wen
2015/12/17 16:47:09
Does webview use fast shutdown? If so this may res
mnaganov (inactive)
2015/12/17 17:30:30
Good point -- we forgot to update the code in our
|
| + break; |
| + } |
| + content::RenderProcessHost::RendererClosedDetails* process_details = |
| + content::Details<content::RenderProcessHost::RendererClosedDetails>( |
| + details).ptr(); |
| + term_status = process_details->status; |
| + break; |
| + } |
| + default: |
| + NOTREACHED(); |
| + return; |
| + } |
| + OnChildExit(rph->GetID(), term_status); |
| +} |
| + |
| +void CrashMicroDumpManager::OnChildExit( |
| + int child_process_id, |
| + base::TerminationStatus termination_status) { |
| + int pipe_fd = -1; |
| + { |
| + base::AutoLock auto_lock(child_process_id_to_pipe_fd_lock_); |
| + const auto& iter = child_process_id_to_pipe_fd_.find(child_process_id); |
| + if (iter == child_process_id_to_pipe_fd_.end()) { |
| + // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a |
| + // NOTIFICATION_RENDERER_PROCESS_CLOSED. |
| + return; |
| + } |
| + pipe_fd = iter->second; |
| + child_process_id_to_pipe_fd_.erase(iter); |
| + } |
| + DCHECK(pipe_fd != -1); |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&CrashMicroDumpManager::HandleChildTermination, pipe_fd, |
| + termination_status)); |
| +} |
| + |
| +} // namespace breakpad |