From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- ipc/glue/CrashReporterHelper.h | 94 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 ipc/glue/CrashReporterHelper.h (limited to 'ipc/glue/CrashReporterHelper.h') diff --git a/ipc/glue/CrashReporterHelper.h b/ipc/glue/CrashReporterHelper.h new file mode 100644 index 0000000000..c13901c2cc --- /dev/null +++ b/ipc/glue/CrashReporterHelper.h @@ -0,0 +1,94 @@ +/* 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 { +namespace 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, providing the appropriate `GeckoProcessType` + * enum value for the template parameter PT. + * + * 3. When your protocol actor is destroyed with a reason of `AbnormalShutdown`, + * you should call `GenerateCrashReport(OtherPid())`. If you need the crash + * report ID it will be copied in the second optional parameter upon + * successful crash report generation. + */ +template +class CrashReporterHelper { + public: + CrashReporterHelper() : mCrashReporter(nullptr) {} + IPCResult RecvInitCrashReporter(const CrashReporter::ThreadId& aThreadId) { + mCrashReporter = MakeUnique(PT, aThreadId); + return IPC_OK(); + } + + protected: + void GenerateCrashReport(base::ProcessId aPid, + nsString* aMinidumpId = nullptr) { + nsAutoString minidumpId; + if (!mCrashReporter) { + HandleOrphanedMinidump(aPid, minidumpId); + } else if (mCrashReporter->GenerateCrashReport(aPid)) { + 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 appService = + do_GetService("@mozilla.org/toolkit/app-startup;1"); + if (appService) { + bool userAllowedQuit = true; + appService->Quit(nsIAppStartup::eForceQuit, 1, &userAllowedQuit); + } + } + } + + private: + void HandleOrphanedMinidump(base::ProcessId aPid, nsString& aMinidumpId) { + if (CrashReporter::FinalizeOrphanedMinidump(aPid, PT, &aMinidumpId)) { + CrashReporterHost::RecordCrash(PT, nsICrashService::CRASH_TYPE_CRASH, + aMinidumpId); + } else { + NS_WARNING(nsPrintfCString("child process pid = %" PRIPID + " crashed without leaving a minidump behind", + aPid) + .get()); + } + } + + protected: + UniquePtr mCrashReporter; +}; + +} // namespace ipc +} // namespace mozilla + +#endif // mozilla_ipc_CrashReporterHelper_h -- cgit v1.2.3