1
0
Fork 0
firefox/ipc/glue/CrashReporterHelper.h
Daniel Baumann 5e9a113729
Adding upstream version 140.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
2025-06-25 09:37:52 +02:00

105 lines
3.5 KiB
C++

/* 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/. */
#ifndef mozilla_ipc_CrashReporterHelper_h
#define mozilla_ipc_CrashReporterHelper_h
#include "CrashReporterHost.h"
#include "mozilla/UniquePtr.h"
#include "nsIAppStartup.h"
#include "nsExceptionHandler.h"
#include "nsICrashService.h"
#include "nsPrintfCString.h"
#include "nsServiceManagerUtils.h"
namespace mozilla::ipc {
/**
* This class encapsulates the common elements of crash report handling for
* toplevel protocols representing processes. To use this class, you should:
*
* 1. Declare a method to initialize the crash reporter in your IPDL:
* `async InitCrashReporter(NativeThreadId threadId)`
*
* 2. Inherit from this class with the name of your derived class as the
* type parameter. Ex: `class MyClass : public CrashReporterHelper<MyClass>`
*
* 3. Provide a public `PROCESS_TYPE` constant for your class. Ex:
*
* ```
* public:
* static constexpr GeckoProcessType PROCESS_TYPE =
* GeckoProcessType_GMPlugin;
* ```
*
* 4. When your protocol actor is destroyed with a reason of `AbnormalShutdown`,
* you should call `GenerateCrashReport()`. If you need the crash
* report ID it will be copied in the second optional parameter upon
* successful crash report generation.
*/
template <class Derived>
class CrashReporterHelper {
public:
CrashReporterHelper() : mCrashReporter(nullptr) {}
IPCResult RecvInitCrashReporter(
const CrashReporter::CrashReporterInitArgs& aInitArgs) {
base::ProcessId pid = static_cast<Derived*>(this)->OtherPid();
mCrashReporter = MakeUnique<ipc::CrashReporterHost>(Derived::PROCESS_TYPE,
pid, aInitArgs);
return IPC_OK();
}
protected:
void GenerateCrashReport(nsString* aMinidumpId = nullptr) {
nsAutoString minidumpId;
if (!mCrashReporter) {
HandleOrphanedMinidump(minidumpId);
} else if (mCrashReporter->GenerateCrashReport()) {
minidumpId = mCrashReporter->MinidumpID();
}
if (aMinidumpId) {
*aMinidumpId = minidumpId;
}
mCrashReporter = nullptr;
}
void MaybeTerminateProcess() {
if (PR_GetEnv("MOZ_CRASHREPORTER_SHUTDOWN")) {
NS_WARNING(nsPrintfCString("Shutting down due to %s process crash.",
XRE_GetProcessTypeString())
.get());
nsCOMPtr<nsIAppStartup> appService =
do_GetService("@mozilla.org/toolkit/app-startup;1");
if (appService) {
bool userAllowedQuit = true;
appService->Quit(nsIAppStartup::eForceQuit, 1, &userAllowedQuit);
}
}
}
private:
void HandleOrphanedMinidump(nsString& aMinidumpId) {
base::ProcessId pid = static_cast<Derived*>(this)->OtherPid();
if (CrashReporter::FinalizeOrphanedMinidump(pid, Derived::PROCESS_TYPE,
&aMinidumpId)) {
CrashReporterHost::RecordCrash(Derived::PROCESS_TYPE,
nsICrashService::CRASH_TYPE_CRASH,
aMinidumpId);
} else {
NS_WARNING(nsPrintfCString("child process pid = %" PRIPID
" crashed without leaving a minidump behind",
pid)
.get());
}
}
protected:
UniquePtr<ipc::CrashReporterHost> mCrashReporter;
};
} // namespace mozilla::ipc
#endif // mozilla_ipc_CrashReporterHelper_h