gecko-dev/dom/ipc/CrashReporterParent.h

259 строки
7.3 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
2012-05-21 15:12:37 +04:00
* 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/. */
#ifndef mozilla_dom_CrashReporterParent_h
#define mozilla_dom_CrashReporterParent_h
#include "mozilla/dom/PCrashReporterParent.h"
#include "mozilla/dom/TabMessageUtils.h"
#include "nsIFile.h"
#ifdef MOZ_CRASHREPORTER
#include "nsExceptionHandler.h"
#include "nsDataHashtable.h"
#endif
namespace mozilla {
namespace dom {
class CrashReporterParent :
public PCrashReporterParent
{
#ifdef MOZ_CRASHREPORTER
typedef CrashReporter::AnnotationTable AnnotationTable;
#endif
public:
CrashReporterParent();
virtual ~CrashReporterParent();
#ifdef MOZ_CRASHREPORTER
/*
* Attempt to create a bare-bones crash report, along with extra process-
* specific annotations present in the given AnnotationTable.
*
* @returns true if successful, false otherwise.
*/
template<class Toplevel>
bool
GenerateCrashReport(Toplevel* t, const AnnotationTable* processNotes);
/*
* Attempt to generate a parent/child pair of minidumps from the given
* toplevel actor. This calls CrashReporter::CreatePairedMinidumps to
* generate the minidumps. Crash reporter annotations set prior to this
* call will be saved via PairedDumpCallbackExtra into an .extra file
* under the proper crash id. AnnotateCrashReport annotations are not
* set in this call, use GenerateChildData.
*
* @returns true if successful, false otherwise.
*/
template<class Toplevel>
bool
GeneratePairedMinidump(Toplevel* t);
/**
* Apply child process annotations to an existing paired mindump generated
* with GeneratePairedMinidump.
*
* Be careful about calling generate apis immediately after this call,
* see FinalizeChildData.
*
* @param processNotes (optional) - Additional notes to append. Annotations
* stored in mNotes will also be applied. processNotes can be null.
* @returns true if successful, false otherwise.
*/
bool
GenerateChildData(const AnnotationTable* processNotes);
/**
* Handles main thread finalization tasks after a report has been
* generated. Does the following:
* - register the finished report with the crash service manager
* - records telemetry related data about crashes
*
* Be careful about calling generate apis immediately after this call,
* if this api is called on a non-main thread it will fire off a runnable
* to complete its work async.
*/
void
FinalizeChildData();
/*
* Attempt to generate a full paired dump complete with any child
* annoations, and finalizes the report. Note this call is only valid
* on the main thread. Calling on a background thread will fail.
*
* @returns true if successful, false otherwise.
*/
template<class Toplevel>
bool
GenerateCompleteMinidump(Toplevel* t);
/**
* Submits a raw minidump handed in, calls GenerateChildData. Used
* by plugins.
*
* @returns true if successful, false otherwise.
*/
bool
GenerateCrashReportForMinidump(nsIFile* minidump,
const AnnotationTable* processNotes);
/*
* Instantiate a new crash reporter actor from a given parent that manages
* the protocol.
*
* @returns true if successful, false otherwise.
*/
template<class Toplevel>
static bool CreateCrashReporter(Toplevel* actor);
#endif // MOZ_CRASHREPORTER
/*
* Initialize this reporter with data from the child process.
*/
void
SetChildData(const NativeThreadId& id, const uint32_t& processType);
/*
* Returns the ID of the child minidump.
* GeneratePairedMinidump or GenerateCrashReport must be called first.
*/
const nsString& ChildDumpID() {
return mChildDumpID;
}
/*
* Add an annotation to our internally tracked list of annotations.
* Callers must apply these notes using GenerateChildData otherwise
* the notes will get dropped.
*/
void
AnnotateCrashReport(const nsCString& key, const nsCString& data);
protected:
virtual void ActorDestroy(ActorDestroyReason aWhy) override;
virtual bool
RecvAnnotateCrashReport(const nsCString& key, const nsCString& data) override {
AnnotateCrashReport(key, data);
return true;
}
virtual bool
RecvAppendAppNotes(const nsCString& data) override;
virtual mozilla::ipc::IProtocol*
CloneProtocol(Channel* aChannel,
mozilla::ipc::ProtocolCloneContext *aCtx) override;
#ifdef MOZ_CRASHREPORTER
void
NotifyCrashService();
#endif
#ifdef MOZ_CRASHREPORTER
AnnotationTable mNotes;
#endif
nsCString mAppNotes;
nsString mChildDumpID;
NativeThreadId mMainThread;
time_t mStartTime;
uint32_t mProcessType;
bool mInitialized;
};
#ifdef MOZ_CRASHREPORTER
template<class Toplevel>
inline bool
CrashReporterParent::GeneratePairedMinidump(Toplevel* t)
{
mozilla::ipc::ScopedProcessHandle child;
#ifdef XP_MACOSX
child = t->Process()->GetChildTask();
#else
if (!base::OpenPrivilegedProcessHandle(t->OtherPid(), &child.rwget())) {
NS_WARNING("Failed to open child process handle.");
return false;
}
#endif
nsCOMPtr<nsIFile> childDump;
if (CrashReporter::CreatePairedMinidumps(child,
mMainThread,
getter_AddRefs(childDump)) &&
CrashReporter::GetIDFromMinidump(childDump, mChildDumpID)) {
return true;
}
return false;
}
template<class Toplevel>
inline bool
CrashReporterParent::GenerateCrashReport(Toplevel* t,
const AnnotationTable* processNotes)
{
nsCOMPtr<nsIFile> crashDump;
if (t->TakeMinidump(getter_AddRefs(crashDump), nullptr) &&
CrashReporter::GetIDFromMinidump(crashDump, mChildDumpID)) {
return GenerateChildData(processNotes);
}
return false;
}
template<class Toplevel>
inline bool
CrashReporterParent::GenerateCompleteMinidump(Toplevel* t)
{
mozilla::ipc::ScopedProcessHandle child;
if (!NS_IsMainThread()) {
NS_WARNING("GenerateCompleteMinidump can't be called on non-main thread.");
return false;
}
#ifdef XP_MACOSX
child = t->Process()->GetChildTask();
#else
if (!base::OpenPrivilegedProcessHandle(t->OtherPid(), &child.rwget())) {
NS_WARNING("Failed to open child process handle.");
return false;
}
#endif
nsCOMPtr<nsIFile> childDump;
if (CrashReporter::CreatePairedMinidumps(child,
mMainThread,
getter_AddRefs(childDump)) &&
CrashReporter::GetIDFromMinidump(childDump, mChildDumpID)) {
GenerateChildData(nullptr);
FinalizeChildData();
return true;
}
return false;
}
template<class Toplevel>
/* static */ bool
CrashReporterParent::CreateCrashReporter(Toplevel* actor)
{
#ifdef MOZ_CRASHREPORTER
NativeThreadId id;
uint32_t processType;
PCrashReporterParent* p =
actor->CallPCrashReporterConstructor(&id, &processType);
if (p) {
static_cast<CrashReporterParent*>(p)->SetChildData(id, processType);
} else {
NS_ERROR("Error creating crash reporter actor");
}
return !!p;
#endif
return false;
}
#endif
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_CrashReporterParent_h