 Chromium Code Reviews
 Chromium Code Reviews Issue 1525023003:
  Distinguish in the browser between renderer crashes and kills  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1525023003:
  Distinguish in the browser between renderer crashes and kills  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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..853981d2b55f01a5caa4e12a492317e89a575ed4 | 
| --- /dev/null | 
| +++ b/components/crash/content/browser/crash_micro_dump_manager_android.cc | 
| @@ -0,0 +1,140 @@ | 
| +// 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 <unistd.h> | 
| + | 
| +#include "base/bind.h" | 
| +#include "base/logging.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" | 
| + | 
| +using content::BrowserThread; | 
| + | 
| +namespace breakpad { | 
| + | 
| +// static | 
| +CrashMicroDumpManager* CrashMicroDumpManager::instance_ = nullptr; | 
| + | 
| +// static | 
| +CrashMicroDumpManager* CrashMicroDumpManager::GetInstance() { | 
| 
Robert Sesek
2016/01/07 20:02:22
This object is both constructible and has singleto
 
mnaganov (inactive)
2016/01/07 21:39:50
AwBrowserContext isn't global enough -- we used to
 | 
| + CHECK(instance_); | 
| + return instance_; | 
| +} | 
| + | 
| +CrashMicroDumpManager::CrashMicroDumpManager() : weak_factory_(this) { | 
| + 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()); | 
| +} | 
| + | 
| +CrashMicroDumpManager::~CrashMicroDumpManager() { | 
| + instance_ = nullptr; | 
| + { | 
| + base::AutoLock auto_lock(child_process_id_to_pipe_lock_); | 
| + STLDeleteValues(&child_process_id_to_pipe_); | 
| 
Robert Sesek
2016/01/07 20:02:22
If you switch to scoped_ptr<> in the map, you'll s
 
mnaganov (inactive)
2016/01/07 21:39:50
Done.
 | 
| + } | 
| +} | 
| + | 
| +base::ScopedFD CrashMicroDumpManager::CreateCrashInfoChannel( | 
| + int child_process_id) { | 
| + DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); | 
| + scoped_ptr<base::SyncSocket> local_pipe(new base::SyncSocket()); | 
| + scoped_ptr<base::SyncSocket> child_pipe(new base::SyncSocket()); | 
| + if (!base::SyncSocket::CreatePair(local_pipe.get(), child_pipe.get())) | 
| + return base::ScopedFD(); | 
| + { | 
| + base::AutoLock auto_lock(child_process_id_to_pipe_lock_); | 
| + DCHECK(!ContainsKey(child_process_id_to_pipe_, child_process_id)); | 
| + child_process_id_to_pipe_[child_process_id] = local_pipe.release(); | 
| + } | 
| + return std::move(base::ScopedFD(dup(child_pipe->handle()))); | 
| +} | 
| + | 
| +void CrashMicroDumpManager::HandleChildTerminationOnFileThread( | 
| + int child_process_id, | 
| + base::TerminationStatus termination_status) { | 
| + DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 
| + | 
| + scoped_ptr<base::SyncSocket> pipe; | 
| + { | 
| + base::AutoLock auto_lock(child_process_id_to_pipe_lock_); | 
| + const auto& iter = child_process_id_to_pipe_.find(child_process_id); | 
| + if (iter == child_process_id_to_pipe_.end()) { | 
| + // We might get a NOTIFICATION_RENDERER_PROCESS_TERMINATED and a | 
| + // NOTIFICATION_RENDERER_PROCESS_CLOSED. | 
| + return; | 
| + } | 
| + pipe.reset(iter->second); | 
| + child_process_id_to_pipe_.erase(iter); | 
| + } | 
| + DCHECK(pipe->handle() != base::SyncSocket::kInvalidHandle); | 
| + | 
| + if (termination_status == base::TERMINATION_STATUS_NORMAL_TERMINATION) | 
| + return; | 
| + if (pipe->Peek() >= sizeof(int)) { | 
| + int exit_code; | 
| + pipe->Receive(&exit_code, sizeof(exit_code)); | 
| + LOG(FATAL) << "Renderer process crash detected (code " << exit_code | 
| + << "). Terminating browser."; | 
| + } else { | 
| + // The child process hasn't written anything into the pipe. This implies | 
| + // that it was terminated via SIGKILL by the low memory killer, and thus we | 
| + // need to perform a clean exit. | 
| + exit(0); | 
| + } | 
| +} | 
| + | 
| +void CrashMicroDumpManager::Observe( | 
| + int type, | 
| + const content::NotificationSource& source, | 
| + const content::NotificationDetails& details) { | 
| + content::RenderProcessHost* rph = | 
| + content::Source<content::RenderProcessHost>(source).ptr(); | 
| + // In case of a normal termination we just need to close the pipe_fd we kept | 
| + // open. | 
| + base::TerminationStatus termination_status = | 
| + base::TERMINATION_STATUS_NORMAL_TERMINATION; | 
| + switch (type) { | 
| + case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | 
| + // NOTIFICATION_RENDERER_PROCESS_TERMINATED is sent when the renderer | 
| + // process is cleanly shutdown. | 
| + break; | 
| + } | 
| + case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | 
| + // Android fast shutdowns is a known case where the renderer is | 
| + // intentionally killed when we are done with it. | 
| + if (!rph->FastShutdownStarted()) { | 
| + content::RenderProcessHost::RendererClosedDetails* process_details = | 
| + content::Details<content::RenderProcessHost::RendererClosedDetails>( | 
| + details).ptr(); | 
| + termination_status = process_details->status; | 
| + } | 
| + break; | 
| + } | 
| + default: | 
| + NOTREACHED(); | 
| + return; | 
| + } | 
| + BrowserThread::PostTask( | 
| + BrowserThread::FILE, FROM_HERE, | 
| + base::Bind(&CrashMicroDumpManager::HandleChildTerminationOnFileThread, | 
| + weak_factory_.GetWeakPtr(), rph->GetID(), | 
| + termination_status)); | 
| +} | 
| + | 
| +} // namespace breakpad |