Files
palemoon27/dom/ipc/ProcessHangMonitor.cpp
T
roytam1 d56ce7399f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1232506: Make dom/devicestorage really work with e10s. r=alchen (7f95105c5e)
- Bug 1208944 - Part 1. Dispatch CompositionEvent to Plugin. r=masayuki (85c062b417)
- Bug 1208944 - Part 2-a. Handle CompositionEvent on plugin. r=masayuki (ea2cebfca9)
- Bug 1208944 - Part 2-b. Workaround for OSX. r=masayuki (ca401cbc04)
- Bug 1208944 - Part 3. Allow IME window messages on plugin process. r=jmathies (571fd75010)
- Bug 1192844: Accept 0xCC padding in WindowsDllDetourPatcher::CreateTrampoline. r=m_kato (b34b6173d3)
- Bug 1201205 part 1: Add an AutoVirtualProtect helper class to make the next patch easier. r=m_kato (b384bd2412)
- Bug 1201205 part 2: Restore protection on the nop space separately from the function. r=m_kato (a822b2414b)
- Bug 1208944 - Part 4. nsWindowsDllInterceptor supports IMM32 API hook. r=ehsan (9b409ff15a)
- Bug 1208944 - Part 5. Send PluginEvent to content process. r=jmathies (4ee0341190)
- Bug 1208944 - Part 6. Get vaild TextRangeArray on compositionupdate. r=masayuki (18f184931c)
- Bug 1208944 - Part 7. Don't post WM_IME_REQUEST on windowless plugin since we don't convert pointer over process. r=masayuki (6b1a9ce71f)
- Bug 1208944 - Part 8. Don't get selection on start compostion when plugin has foucs. r=masayuki (daf400620e)
- Add Telemetry for the drawing models that plugins use. (bug 1229961 part 1, r=aklotz,vladan) (c9645a68ec)
- Enable direct plugin drawing by default. (bug 1229961 part 2, r=aklotz) (080c9337cb)
- Bug 1213845 - enable osk support on windows 8, but hide it behind a preference, r=jaws (fcb8dfb10b)
- Bug 1192248 - Fix wchar_t/char16_t mismatch WinIMEHandler.cpp. r=masayuki (13e61f3f0c)
- Bug 1226145 - actually check whether the on-screen keyboard is up rather than relying on internal state, r=masayuki (da090b605d)
- Bug 1208944 - Part 9. Hook IMM32 APIs on plugin process. r=masayuki (f15d73721f)
- Bug 1208944 - Part 10-a. Call CallWindowProc when WidgetPluginEvent isn't handled by plugin. r=masayuki (c54eaa50ed)
- Bug 1208944 - Part 10-b. Call DefaultProc When CompositionEvent isn't handled correctly by plugin. r=masayuki (0e77eaa40f)
- Bug 1157046 - Remove ARRAY_LENGTH in favor of MOZ_ARRAY_LENGTH; r=Waldo (a7f8ce3684)
- Bug 1196834 - Add a test that confirms plugin windows are hidden after switching from a remote to local tab. r=roc (a09d4a32af)
- Bug 1208944 - Part 11. Add test. r=jmathies (ed8e4c87d2)
- Bug 1231378 - part 1 - Fix uninitialized members of classes in docshell/*, r=smaug (fcd7615d97)
- Bug 1231378 - part 2 - Fix uninitialized members of classes in dom/*,r=smaug (40b354438f)
- Bug 1231378 - part 3 - Fix uninitialized members of classes in module/libjar and mfbt, r=smaug (ee15d8739a)
- Bug 1231378 - part 4 - Fix uninitialized members of classes in netwerk/widget/storage/uriloader/memory/tools, r=smaug (6aec559dd8)
- Bug 1238082 - Fix mode lines in dom/ipc. r=baku (a49aa3555b)
- Bug 1024149 - Use element size for upload texture when using SVG image. r=jgilbert r=roc (531bdd2406)
- Bug 1233922 - Skip Camera preinit for browser elements. r=fabrice (cd8ffbb112)
- Bug 1159327 - Enable accessibility more broadly with e10s and add an e10s a11y blacklist for clients with known issues. r=tbsaunde (374f49a942)
- Bug 1198459 - Prevent accessibility from initializing in content processes when e10s is running. r=tbsaunde (33bdfccd56)
- Bug 1115956 - Improve notice string for when e10s was disabled for accessibility. r=mconley (e5ff276d9e)
- Bug 1172491 - Let e10s be enabled in safe mode. r=felipe (bd0a3fbbd5)
- Bug 1226487 - Allow e10s to run on Beta. r=mconley (05103c816d)
- Bug 634063 - Use gfxPrefs for some layers acceleration prefs. r=nical (9a20bcd8d2)
- Bug 1198459 - Add support for disabling e10s if a11y was run in the previous session, or run in a session over the previous week. r=felipe (7154a021bd)
- Bug 1171171 - Make sure the graphics preferences are initialized early enough. r=billm (34801701aa)
- Bug 1171171: Move nsAppRunner's gfxPrefs #include out of windows-specific section. r=milan a=KWierso (492fa00f99)
- Bug 1182048 - Part 1: Allow e10s to be enabled,r=vlad (628949ad83)
- Bug 1182048 - Part 2: Implement e10s support for WebVR,r=vlad (a77d280b48)
- Bug 1207221 - Do not prevent the system app from vibrating when it is hidden. r=bz r=dhylands (f3d2306f41)
- Bug 1233902 - Check the TP list in sendBeacon(). r=gcp (bc279755ab)
- Bug 1216207 - Modify navigator.hasFeature method to implement new (9b4571468e)
- Bug 1217187 - Modify navigator.hasFeature method to detect new (d9ea58e65c)
- Bug 1125477 - Part 1: Modify dom/tv mochitest in order to enable e10s test. r=seanlin (7b535fddaa)
- Bug 1125477 - Part 2: Modify permission check of TV Manager API in order to remove dom.testing.tv_enabled_for_hosted_apps. r=seanlin;r=smaug (5719fdf3a7)
- Bug 1125477 - Part3: Remove permission removing code. r=seanlin (614de5dfd1)
- Bug 1223672 - NSec package cannot use device storage API. r=kchen (f1fc6711ea)
- Bug 1224609 - Ensure the primary screen is always returned in RecvScreenForBrowser if all else fails. r=wmccloskey (d562935754)
- Bug 1180288 - Use native filepickers for Graphene. r=khuey (0b9c78ce13)
- Bug 1201394 - Use cached preferences value in ProcessPriorityManager. r=gsvelto (d211492014)
- Bug 1212833 - Delay the MemoryPressure when an application goes to background. r=gsvelto (ce33de4324)
- Bug 1186812 (part 1) - Replace nsBaseHashtable::EnumerateRead() calls in dom/{ipc,plugins}/. r=jimm. (c061bb9d1b)
- Bug 1212984 - HangMonitorChild should delete its Transport. r=billm (3331593b2d)
- Bug 1186812 (part 2) - Replace nsBaseHashtable::EnumerateRead() calls in dom/{ipc,plugins}/. r=jimm. (fdac3f52cb)
- Bug 1215239 - Do not set ownApp for a browser element. r=bholley (e3cc250c3f)
- Bug 1241278 - Change Notification.requestPermission() to return a promise. r=baku (b0a86eeeb5)
- Bug 1249102. Make overrides of WorkerRunnable::PostRun a bit more consistent. r=khuey (a23bf9468f)
- Bug 1209812 (part 6) - Convert all gfxImageFormat values to SurfaceFormat equivalents. r=jrmuizel. (9a5da3597f)
- Bug 1239225 - Remove unused args from TextureImage's constructor and related functions. r=mattwoodrow. (26ff005ecb)
- Bug 1240708 - Various trivial coverity warning fixes. r=kats (74a485c2b8)
- Bug 1240708 - Various trivial coverity warning fixes (part 2). r=kats (34bcd568dc)
- Bug 1219494 - Part 3 gfx/gl with gfxCrash. r=mchang (268968a3d9)
- Bug 1232456 - Create EGL surface through widget; r=snorp (13634bebc6)
- Bug 1232456 - Renew EGL surface using existing compositor widget; r=snorp (2649b088d3)
- Bug 1187322 - Don't require accelerated OpenGL contexts for BasicCompositor on OS X. r=jrmuizel (87f92af2c8)
- Bug 1238753 - Use StartRemoteDrawingInRegion on Mac. r=mattwoodrow (22870ed043)
- Bug 1238755 - Avoid a copy when uploading the BasicCompositor result to a texture. r=mattwoodrow (0b6663ab61)
- Bug 890156 - patch 0.2 - Remove the (unused) aRect parameter from nsBaseWidget::BaseCreate. r=kats (1120763eef)
- Bug 1241983 - Make DOM Identity observe inner-window-destroyed not dom-window-destroyed; r=ferjm (7cf4cf3211)
- Bug 1236282 - Clip color layer drawing in BasicCompositor so that unbounded operators don't erase stuff outside the layer. r=Bas (a7e73d1d8d)
- Bug 1238753 - Make BasicCompositor respect changes to mInvalidRegion through StartRemoteDrawingWithRegion properly. r=mattwoodrow (36e20bccce)
- Bug 1238753 - Don't skip the call to StartRemoteDrawing in from BasicCompositor::BeginFrame if the invalid region is empty. r=mattwoodrow (783b25775f)
- Bug 1239137 - Return early from BasicCompositor::DrawQuad if transformBounds is empty. r=mattwoodrow (0815cfc28a)
- Bug 1239530 (part 1) - Remove PuppetWidget::Scroll(), which is dead. r=kats. (63ab8c32b6)
- Bug 1239530 (part 2) - Use LayoutDevice coordinates in {Start,End}RemoteDrawingInRegion() and related functions. r=kats. (609001767d)
- Bug 1239537 - Remove Compositor::GetWidgetSize(), which is unused. r=mattwoodrow. (25732d37b1)
- Bug 1242293 - Don't call EndRemoteDrawingInRegion if StartRemoteDrawingInRegion returns a null DrawTarget. r=mstange (8e297c3b84)
- import changes from mozilla: Bug 1211642: Whitelist test plugin for async plugin init; r=jimm (f02deaaeb8) (6689ce99c8)
2023-08-15 16:18:14 +08:00

1060 lines
26 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ProcessHangMonitor.h"
#include "mozilla/ProcessHangMonitorIPC.h"
#include "mozilla/Atomics.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/TabChild.h"
#include "mozilla/dom/TabParent.h"
#include "mozilla/Monitor.h"
#include "mozilla/plugins/PluginBridge.h"
#include "mozilla/Preferences.h"
#include "mozilla/unused.h"
#include "nsIFrameLoader.h"
#include "nsIHangReport.h"
#include "nsITabParent.h"
#include "nsPluginHost.h"
#include "nsThreadUtils.h"
#ifdef MOZ_CRASHREPORTER
#include "nsExceptionHandler.h"
#endif
#include "base/task.h"
#include "base/thread.h"
#ifdef XP_WIN
// For IsDebuggerPresent()
#include <windows.h>
#endif
using namespace mozilla;
using namespace mozilla::dom;
/*
* Basic architecture:
*
* Each process has its own ProcessHangMonitor singleton. This singleton exists
* as long as there is at least one content process in the system. Each content
* process has a HangMonitorChild and the chrome process has one
* HangMonitorParent per process. Each process (including the chrome process)
* runs a hang monitoring thread. The PHangMonitor actors are bound to this
* thread so that they never block on the main thread.
*
* When the content process detects a hang, it posts a task to its hang thread,
* which sends an IPC message to the hang thread in the parent. The parent
* cancels any ongoing CPOW requests and then posts a runnable to the main
* thread that notifies Firefox frontend code of the hang. The frontend code is
* passed an nsIHangReport, which can be used to terminate the hang.
*
* If the user chooses to terminate a script, a task is posted to the chrome
* process's hang monitoring thread, which sends an IPC message to the hang
* thread in the content process. That thread sets a flag to indicate that JS
* execution should be terminated the next time it hits the interrupt
* callback. A similar scheme is used for debugging slow scripts. If a content
* process or plug-in needs to be terminated, the chrome process does so
* directly, without messaging the content process.
*/
namespace {
/* Child process objects */
class HangMonitorChild
: public PProcessHangMonitorChild
{
public:
explicit HangMonitorChild(ProcessHangMonitor* aMonitor);
virtual ~HangMonitorChild();
void Open(Transport* aTransport, ProcessId aOtherPid,
MessageLoop* aIOLoop);
typedef ProcessHangMonitor::SlowScriptAction SlowScriptAction;
SlowScriptAction NotifySlowScript(nsITabChild* aTabChild,
const char* aFileName,
unsigned aLineNo);
void NotifySlowScriptAsync(TabId aTabId,
const nsCString& aFileName,
unsigned aLineNo);
bool IsDebuggerStartupComplete();
void NotifyPluginHang(uint32_t aPluginId);
void NotifyPluginHangAsync(uint32_t aPluginId);
void ClearHang();
virtual bool RecvTerminateScript() override;
virtual bool RecvBeginStartingDebugger() override;
virtual bool RecvEndStartingDebugger() override;
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
void Shutdown();
static HangMonitorChild* Get() { return sInstance; }
MessageLoop* MonitorLoop() { return mHangMonitor->MonitorLoop(); }
private:
void ShutdownOnThread();
static HangMonitorChild* sInstance;
const RefPtr<ProcessHangMonitor> mHangMonitor;
Monitor mMonitor;
// Main thread-only.
bool mSentReport;
// These fields must be accessed with mMonitor held.
bool mTerminateScript;
bool mStartDebugger;
bool mFinishedStartingDebugger;
bool mShutdownDone;
// This field is only accessed on the hang thread.
bool mIPCOpen;
};
HangMonitorChild* HangMonitorChild::sInstance;
/* Parent process objects */
class HangMonitorParent;
class HangMonitoredProcess final
: public nsIHangReport
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
HangMonitoredProcess(HangMonitorParent* aActor,
ContentParent* aContentParent)
: mActor(aActor), mContentParent(aContentParent) {}
NS_IMETHOD GetHangType(uint32_t* aHangType) override;
NS_IMETHOD GetScriptBrowser(nsIDOMElement** aBrowser) override;
NS_IMETHOD GetScriptFileName(nsACString& aFileName) override;
NS_IMETHOD GetScriptLineNo(uint32_t* aLineNo) override;
NS_IMETHOD GetPluginName(nsACString& aPluginName) override;
NS_IMETHOD TerminateScript() override;
NS_IMETHOD BeginStartingDebugger() override;
NS_IMETHOD EndStartingDebugger() override;
NS_IMETHOD TerminatePlugin() override;
NS_IMETHOD TerminateProcess() override;
NS_IMETHOD UserCanceled() override;
NS_IMETHOD IsReportForBrowser(nsIFrameLoader* aFrameLoader, bool* aResult) override;
// Called when a content process shuts down.
void Clear() {
mContentParent = nullptr;
mActor = nullptr;
}
void SetHangData(const HangData& aHangData) { mHangData = aHangData; }
void SetBrowserDumpId(nsAutoString& aId) {
mBrowserDumpId = aId;
}
private:
~HangMonitoredProcess() {}
// Everything here is main thread-only.
HangMonitorParent* mActor;
ContentParent* mContentParent;
HangData mHangData;
nsAutoString mBrowserDumpId;
};
class HangMonitorParent
: public PProcessHangMonitorParent
{
public:
explicit HangMonitorParent(ProcessHangMonitor* aMonitor);
virtual ~HangMonitorParent();
void Open(Transport* aTransport, ProcessId aPid, MessageLoop* aIOLoop);
virtual bool RecvHangEvidence(const HangData& aHangData) override;
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
void SetProcess(HangMonitoredProcess* aProcess) { mProcess = aProcess; }
void Shutdown();
void TerminateScript();
void BeginStartingDebugger();
void EndStartingDebugger();
void CleanupPluginHang(uint32_t aPluginId, bool aRemoveFiles);
MessageLoop* MonitorLoop() { return mHangMonitor->MonitorLoop(); }
private:
void ShutdownOnThread();
const RefPtr<ProcessHangMonitor> mHangMonitor;
// This field is read-only after construction.
bool mReportHangs;
// This field is only accessed on the hang thread.
bool mIPCOpen;
Monitor mMonitor;
// Must be accessed with mMonitor held.
RefPtr<HangMonitoredProcess> mProcess;
bool mShutdownDone;
// Map from plugin ID to crash dump ID. Protected by mBrowserCrashDumpHashLock.
nsDataHashtable<nsUint32HashKey, nsString> mBrowserCrashDumpIds;
Mutex mBrowserCrashDumpHashLock;
};
} // namespace
template<>
struct RunnableMethodTraits<HangMonitorChild>
{
typedef HangMonitorChild Class;
static void RetainCallee(Class* obj) { }
static void ReleaseCallee(Class* obj) { }
};
template<>
struct RunnableMethodTraits<HangMonitorParent>
{
typedef HangMonitorParent Class;
static void RetainCallee(Class* obj) { }
static void ReleaseCallee(Class* obj) { }
};
/* HangMonitorChild implementation */
HangMonitorChild::HangMonitorChild(ProcessHangMonitor* aMonitor)
: mHangMonitor(aMonitor),
mMonitor("HangMonitorChild lock"),
mSentReport(false),
mTerminateScript(false),
mStartDebugger(false),
mFinishedStartingDebugger(false),
mShutdownDone(false),
mIPCOpen(true)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
}
HangMonitorChild::~HangMonitorChild()
{
// For some reason IPDL doesn't automatically delete the channel for a
// bridged protocol (bug 1090570). So we have to do it ourselves.
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, new DeleteTask<Transport>(GetTransport()));
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MOZ_ASSERT(sInstance == this);
sInstance = nullptr;
}
void
HangMonitorChild::Shutdown()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MonitorAutoLock lock(mMonitor);
while (!mShutdownDone) {
mMonitor.Wait();
}
}
void
HangMonitorChild::ShutdownOnThread()
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
MonitorAutoLock lock(mMonitor);
mShutdownDone = true;
mMonitor.Notify();
}
void
HangMonitorChild::ActorDestroy(ActorDestroyReason aWhy)
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
mIPCOpen = false;
// We use a task here to ensure that IPDL is finished with this
// HangMonitorChild before it gets deleted on the main thread.
MonitorLoop()->PostTask(
FROM_HERE,
NewRunnableMethod(this, &HangMonitorChild::ShutdownOnThread));
}
bool
HangMonitorChild::RecvTerminateScript()
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
MonitorAutoLock lock(mMonitor);
mTerminateScript = true;
return true;
}
bool
HangMonitorChild::RecvBeginStartingDebugger()
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
MonitorAutoLock lock(mMonitor);
mStartDebugger = true;
return true;
}
bool
HangMonitorChild::RecvEndStartingDebugger()
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
MonitorAutoLock lock(mMonitor);
mFinishedStartingDebugger = true;
return true;
}
void
HangMonitorChild::Open(Transport* aTransport, ProcessId aPid,
MessageLoop* aIOLoop)
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
MOZ_ASSERT(!sInstance);
sInstance = this;
DebugOnly<bool> ok = PProcessHangMonitorChild::Open(aTransport, aPid, aIOLoop);
MOZ_ASSERT(ok);
}
void
HangMonitorChild::NotifySlowScriptAsync(TabId aTabId,
const nsCString& aFileName,
unsigned aLineNo)
{
if (mIPCOpen) {
Unused << SendHangEvidence(SlowScriptData(aTabId, aFileName, aLineNo));
}
}
HangMonitorChild::SlowScriptAction
HangMonitorChild::NotifySlowScript(nsITabChild* aTabChild,
const char* aFileName,
unsigned aLineNo)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
mSentReport = true;
{
MonitorAutoLock lock(mMonitor);
if (mTerminateScript) {
mTerminateScript = false;
return SlowScriptAction::Terminate;
}
if (mStartDebugger) {
mStartDebugger = false;
return SlowScriptAction::StartDebugger;
}
}
TabId id;
if (aTabChild) {
RefPtr<TabChild> tabChild = static_cast<TabChild*>(aTabChild);
id = tabChild->GetTabId();
}
nsAutoCString filename(aFileName);
MonitorLoop()->PostTask(
FROM_HERE,
NewRunnableMethod(this, &HangMonitorChild::NotifySlowScriptAsync,
id, filename, aLineNo));
return SlowScriptAction::Continue;
}
bool
HangMonitorChild::IsDebuggerStartupComplete()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MonitorAutoLock lock(mMonitor);
if (mFinishedStartingDebugger) {
mFinishedStartingDebugger = false;
return true;
}
return false;
}
void
HangMonitorChild::NotifyPluginHang(uint32_t aPluginId)
{
// main thread in the child
MOZ_RELEASE_ASSERT(NS_IsMainThread());
mSentReport = true;
// bounce to background thread
MonitorLoop()->PostTask(
FROM_HERE,
NewRunnableMethod(this,
&HangMonitorChild::NotifyPluginHangAsync,
aPluginId));
}
void
HangMonitorChild::NotifyPluginHangAsync(uint32_t aPluginId)
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
// bounce back to parent on background thread
if (mIPCOpen) {
Unused << SendHangEvidence(PluginHangData(aPluginId));
}
}
void
HangMonitorChild::ClearHang()
{
MOZ_ASSERT(NS_IsMainThread());
if (mSentReport) {
MonitorAutoLock lock(mMonitor);
mSentReport = false;
mTerminateScript = false;
mStartDebugger = false;
mFinishedStartingDebugger = false;
}
}
/* HangMonitorParent implementation */
HangMonitorParent::HangMonitorParent(ProcessHangMonitor* aMonitor)
: mHangMonitor(aMonitor),
mIPCOpen(true),
mMonitor("HangMonitorParent lock"),
mShutdownDone(false),
mBrowserCrashDumpHashLock("mBrowserCrashDumpIds lock")
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
mReportHangs = mozilla::Preferences::GetBool("dom.ipc.reportProcessHangs", false);
}
HangMonitorParent::~HangMonitorParent()
{
// For some reason IPDL doesn't automatically delete the channel for a
// bridged protocol (bug 1090570). So we have to do it ourselves.
XRE_GetIOMessageLoop()->PostTask(FROM_HERE, new DeleteTask<Transport>(GetTransport()));
#ifdef MOZ_CRASHREPORTER
MutexAutoLock lock(mBrowserCrashDumpHashLock);
for (auto iter = mBrowserCrashDumpIds.Iter(); !iter.Done(); iter.Next()) {
nsString crashId = iter.UserData();
if (!crashId.IsEmpty()) {
CrashReporter::DeleteMinidumpFilesForID(crashId);
}
}
#endif
}
void
HangMonitorParent::Shutdown()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MonitorAutoLock lock(mMonitor);
if (mProcess) {
mProcess->Clear();
mProcess = nullptr;
}
MonitorLoop()->PostTask(
FROM_HERE,
NewRunnableMethod(this, &HangMonitorParent::ShutdownOnThread));
while (!mShutdownDone) {
mMonitor.Wait();
}
}
void
HangMonitorParent::ShutdownOnThread()
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
// mIPCOpen is only written from this thread, so need need to take the lock
// here. We'd be shooting ourselves in the foot, because ActorDestroy takes
// it.
if (mIPCOpen) {
Close();
}
MonitorAutoLock lock(mMonitor);
mShutdownDone = true;
mMonitor.Notify();
}
void
HangMonitorParent::ActorDestroy(ActorDestroyReason aWhy)
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
mIPCOpen = false;
}
void
HangMonitorParent::Open(Transport* aTransport, ProcessId aPid,
MessageLoop* aIOLoop)
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
DebugOnly<bool> ok = PProcessHangMonitorParent::Open(aTransport, aPid, aIOLoop);
MOZ_ASSERT(ok);
}
class HangObserverNotifier final : public nsRunnable
{
public:
HangObserverNotifier(HangMonitoredProcess* aProcess,
const HangData& aHangData,
const nsString& aBrowserDumpId)
: mProcess(aProcess),
mHangData(aHangData),
mBrowserDumpId(aBrowserDumpId)
{}
NS_IMETHOD
Run()
{
// chrome process, main thread
MOZ_RELEASE_ASSERT(NS_IsMainThread());
mProcess->SetHangData(mHangData);
mProcess->SetBrowserDumpId(mBrowserDumpId);
nsCOMPtr<nsIObserverService> observerService =
mozilla::services::GetObserverService();
observerService->NotifyObservers(mProcess, "process-hang-report", nullptr);
return NS_OK;
}
private:
RefPtr<HangMonitoredProcess> mProcess;
HangData mHangData;
nsAutoString mBrowserDumpId;
};
bool
HangMonitorParent::RecvHangEvidence(const HangData& aHangData)
{
// chrome process, background thread
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
if (!mReportHangs) {
return true;
}
#ifdef XP_WIN
// Don't report hangs if we're debugging the process. You can comment this
// line out for testing purposes.
if (IsDebuggerPresent()) {
return true;
}
#endif
// Before we wake up the browser main thread we want to take a
// browser minidump.
nsAutoString crashId;
#ifdef MOZ_CRASHREPORTER
if (aHangData.type() == HangData::TPluginHangData) {
MutexAutoLock lock(mBrowserCrashDumpHashLock);
const PluginHangData& phd = aHangData.get_PluginHangData();
if (!mBrowserCrashDumpIds.Get(phd.pluginId(), &crashId)) {
nsCOMPtr<nsIFile> browserDump;
if (CrashReporter::TakeMinidump(getter_AddRefs(browserDump), true)) {
if (!CrashReporter::GetIDFromMinidump(browserDump, crashId) || crashId.IsEmpty()) {
browserDump->Remove(false);
NS_WARNING("Failed to generate timely browser stack, this is bad for plugin hang analysis!");
} else {
mBrowserCrashDumpIds.Put(phd.pluginId(), crashId);
}
}
}
}
#endif
mHangMonitor->InitiateCPOWTimeout();
MonitorAutoLock lock(mMonitor);
nsCOMPtr<nsIRunnable> notifier =
new HangObserverNotifier(mProcess, aHangData, crashId);
NS_DispatchToMainThread(notifier);
return true;
}
void
HangMonitorParent::TerminateScript()
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
if (mIPCOpen) {
Unused << SendTerminateScript();
}
}
void
HangMonitorParent::BeginStartingDebugger()
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
if (mIPCOpen) {
Unused << SendBeginStartingDebugger();
}
}
void
HangMonitorParent::EndStartingDebugger()
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
if (mIPCOpen) {
Unused << SendEndStartingDebugger();
}
}
void
HangMonitorParent::CleanupPluginHang(uint32_t aPluginId, bool aRemoveFiles)
{
MutexAutoLock lock(mBrowserCrashDumpHashLock);
nsAutoString crashId;
if (!mBrowserCrashDumpIds.Get(aPluginId, &crashId)) {
return;
}
mBrowserCrashDumpIds.Remove(aPluginId);
#ifdef MOZ_CRASHREPORTER
if (aRemoveFiles && !crashId.IsEmpty()) {
CrashReporter::DeleteMinidumpFilesForID(crashId);
}
#endif
}
/* HangMonitoredProcess implementation */
NS_IMPL_ISUPPORTS(HangMonitoredProcess, nsIHangReport)
NS_IMETHODIMP
HangMonitoredProcess::GetHangType(uint32_t* aHangType)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
switch (mHangData.type()) {
case HangData::TSlowScriptData:
*aHangType = SLOW_SCRIPT;
break;
case HangData::TPluginHangData:
*aHangType = PLUGIN_HANG;
break;
default:
MOZ_ASSERT_UNREACHABLE("Unexpected HangData type");
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::GetScriptBrowser(nsIDOMElement** aBrowser)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_NOT_AVAILABLE;
}
TabId tabId = mHangData.get_SlowScriptData().tabId();
if (!mContentParent) {
return NS_ERROR_NOT_AVAILABLE;
}
nsTArray<PBrowserParent*> tabs;
mContentParent->ManagedPBrowserParent(tabs);
for (size_t i = 0; i < tabs.Length(); i++) {
TabParent* tp = TabParent::GetFrom(tabs[i]);
if (tp->GetTabId() == tabId) {
nsCOMPtr<nsIDOMElement> node = do_QueryInterface(tp->GetOwnerElement());
node.forget(aBrowser);
return NS_OK;
}
}
*aBrowser = nullptr;
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::GetScriptFileName(nsACString& aFileName)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_NOT_AVAILABLE;
}
aFileName = mHangData.get_SlowScriptData().filename();
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::GetScriptLineNo(uint32_t* aLineNo)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_NOT_AVAILABLE;
}
*aLineNo = mHangData.get_SlowScriptData().lineno();
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::GetPluginName(nsACString& aPluginName)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TPluginHangData) {
return NS_ERROR_NOT_AVAILABLE;
}
uint32_t id = mHangData.get_PluginHangData().pluginId();
RefPtr<nsPluginHost> host = nsPluginHost::GetInst();
nsPluginTag* tag = host->PluginWithId(id);
if (!tag) {
return NS_ERROR_UNEXPECTED;
}
aPluginName = tag->Name();
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::TerminateScript()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_UNEXPECTED;
}
if (!mActor) {
return NS_ERROR_UNEXPECTED;
}
ProcessHangMonitor::Get()->MonitorLoop()->PostTask(
FROM_HERE,
NewRunnableMethod(mActor, &HangMonitorParent::TerminateScript));
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::BeginStartingDebugger()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_UNEXPECTED;
}
if (!mActor) {
return NS_ERROR_UNEXPECTED;
}
ProcessHangMonitor::Get()->MonitorLoop()->PostTask(
FROM_HERE,
NewRunnableMethod(mActor, &HangMonitorParent::BeginStartingDebugger));
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::EndStartingDebugger()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TSlowScriptData) {
return NS_ERROR_UNEXPECTED;
}
if (!mActor) {
return NS_ERROR_UNEXPECTED;
}
ProcessHangMonitor::Get()->MonitorLoop()->PostTask(
FROM_HERE,
NewRunnableMethod(mActor, &HangMonitorParent::EndStartingDebugger));
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::TerminatePlugin()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TPluginHangData) {
return NS_ERROR_UNEXPECTED;
}
uint32_t id = mHangData.get_PluginHangData().pluginId();
plugins::TerminatePlugin(id, NS_LITERAL_CSTRING("HangMonitor"),
mBrowserDumpId);
if (mActor) {
mActor->CleanupPluginHang(id, false);
}
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::TerminateProcess()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (!mContentParent) {
return NS_ERROR_UNEXPECTED;
}
if (mActor && mHangData.type() == HangData::TPluginHangData) {
uint32_t id = mHangData.get_PluginHangData().pluginId();
mActor->CleanupPluginHang(id, true);
}
mContentParent->KillHard("HangMonitor");
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::IsReportForBrowser(nsIFrameLoader* aFrameLoader, bool* aResult)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (!mActor) {
*aResult = false;
return NS_OK;
}
TabParent* tp = TabParent::GetFrom(aFrameLoader);
if (!tp) {
*aResult = false;
return NS_OK;
}
*aResult = mContentParent == tp->Manager();
return NS_OK;
}
NS_IMETHODIMP
HangMonitoredProcess::UserCanceled()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mHangData.type() != HangData::TPluginHangData) {
return NS_OK;
}
if (mActor) {
uint32_t id = mHangData.get_PluginHangData().pluginId();
mActor->CleanupPluginHang(id, true);
}
return NS_OK;
}
ProcessHangMonitor* ProcessHangMonitor::sInstance;
ProcessHangMonitor::ProcessHangMonitor()
: mCPOWTimeout(false)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MOZ_COUNT_CTOR(ProcessHangMonitor);
if (XRE_IsContentProcess()) {
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
obs->AddObserver(this, "xpcom-shutdown", false);
}
mThread = new base::Thread("ProcessHangMonitor");
if (!mThread->Start()) {
delete mThread;
mThread = nullptr;
}
}
ProcessHangMonitor::~ProcessHangMonitor()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
MOZ_COUNT_DTOR(ProcessHangMonitor);
MOZ_ASSERT(sInstance == this);
sInstance = nullptr;
delete mThread;
}
ProcessHangMonitor*
ProcessHangMonitor::GetOrCreate()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (!sInstance) {
sInstance = new ProcessHangMonitor();
}
return sInstance;
}
NS_IMPL_ISUPPORTS(ProcessHangMonitor, nsIObserver)
NS_IMETHODIMP
ProcessHangMonitor::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (!strcmp(aTopic, "xpcom-shutdown")) {
if (HangMonitorChild* child = HangMonitorChild::Get()) {
child->Shutdown();
delete child;
}
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->RemoveObserver(this, "xpcom-shutdown");
}
}
return NS_OK;
}
ProcessHangMonitor::SlowScriptAction
ProcessHangMonitor::NotifySlowScript(nsITabChild* aTabChild,
const char* aFileName,
unsigned aLineNo)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
return HangMonitorChild::Get()->NotifySlowScript(aTabChild, aFileName, aLineNo);
}
bool
ProcessHangMonitor::IsDebuggerStartupComplete()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
return HangMonitorChild::Get()->IsDebuggerStartupComplete();
}
bool
ProcessHangMonitor::ShouldTimeOutCPOWs()
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mCPOWTimeout) {
mCPOWTimeout = false;
return true;
}
return false;
}
void
ProcessHangMonitor::InitiateCPOWTimeout()
{
MOZ_RELEASE_ASSERT(MessageLoop::current() == MonitorLoop());
mCPOWTimeout = true;
}
void
ProcessHangMonitor::NotifyPluginHang(uint32_t aPluginId)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
return HangMonitorChild::Get()->NotifyPluginHang(aPluginId);
}
PProcessHangMonitorParent*
mozilla::CreateHangMonitorParent(ContentParent* aContentParent,
mozilla::ipc::Transport* aTransport,
base::ProcessId aOtherPid)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
ProcessHangMonitor* monitor = ProcessHangMonitor::GetOrCreate();
HangMonitorParent* parent = new HangMonitorParent(monitor);
HangMonitoredProcess* process = new HangMonitoredProcess(parent, aContentParent);
parent->SetProcess(process);
monitor->MonitorLoop()->PostTask(
FROM_HERE,
NewRunnableMethod(parent, &HangMonitorParent::Open,
aTransport, aOtherPid, XRE_GetIOMessageLoop()));
return parent;
}
PProcessHangMonitorChild*
mozilla::CreateHangMonitorChild(mozilla::ipc::Transport* aTransport,
base::ProcessId aOtherPid)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
ProcessHangMonitor* monitor = ProcessHangMonitor::GetOrCreate();
HangMonitorChild* child = new HangMonitorChild(monitor);
monitor->MonitorLoop()->PostTask(
FROM_HERE,
NewRunnableMethod(child, &HangMonitorChild::Open,
aTransport, aOtherPid, XRE_GetIOMessageLoop()));
return child;
}
MessageLoop*
ProcessHangMonitor::MonitorLoop()
{
return mThread->message_loop();
}
/* static */ void
ProcessHangMonitor::AddProcess(ContentParent* aContentParent)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
if (mozilla::Preferences::GetBool("dom.ipc.processHangMonitor", false)) {
DebugOnly<bool> opened = PProcessHangMonitor::Open(aContentParent);
MOZ_ASSERT(opened);
}
}
/* static */ void
ProcessHangMonitor::RemoveProcess(PProcessHangMonitorParent* aParent)
{
MOZ_RELEASE_ASSERT(NS_IsMainThread());
auto parent = static_cast<HangMonitorParent*>(aParent);
parent->Shutdown();
delete parent;
}
/* static */ void
ProcessHangMonitor::ClearHang()
{
MOZ_ASSERT(NS_IsMainThread());
if (HangMonitorChild* child = HangMonitorChild::Get()) {
child->ClearHang();
}
}