1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_ipc_CrashReporterHelper_h
6 #define mozilla_ipc_CrashReporterHelper_h
8 #include "CrashReporterHost.h"
9 #include "mozilla/UniquePtr.h"
10 #include "nsExceptionHandler.h"
11 #include "nsICrashService.h"
12 #include "nsPrintfCString.h"
18 * This class encapsulates the common elements of crash report handling for
19 * toplevel protocols representing processes. To use this class, you should:
21 * 1. Declare a method to initialize the crash reporter in your IPDL:
22 * `async InitCrashReporter(NativeThreadId threadId)`
24 * 2. Inherit from this class, providing the appropriate `GeckoProcessType`
25 * enum value for the template parameter PT.
27 * 3. When your protocol actor is destroyed with a reason of `AbnormalShutdown`,
28 * you should call `GenerateCrashReport(OtherPid())`. If you need the crash
29 * report ID it will be copied in the second optional parameter upon
30 * successful crash report generation.
32 template <GeckoProcessType PT
>
33 class CrashReporterHelper
{
35 CrashReporterHelper() : mCrashReporter(nullptr) {}
36 IPCResult
RecvInitCrashReporter(const CrashReporter::ThreadId
& aThreadId
) {
37 mCrashReporter
= MakeUnique
<ipc::CrashReporterHost
>(PT
, aThreadId
);
42 void GenerateCrashReport(base::ProcessId aPid
,
43 nsString
* aMinidumpId
= nullptr) {
44 nsAutoString minidumpId
;
45 if (!mCrashReporter
) {
46 HandleOrphanedMinidump(aPid
, minidumpId
);
47 } else if (mCrashReporter
->GenerateCrashReport(aPid
)) {
48 minidumpId
= mCrashReporter
->MinidumpID();
52 *aMinidumpId
= minidumpId
;
55 mCrashReporter
= nullptr;
59 void HandleOrphanedMinidump(base::ProcessId aPid
, nsString
& aMinidumpId
) {
60 if (CrashReporter::FinalizeOrphanedMinidump(aPid
, PT
, &aMinidumpId
)) {
61 CrashReporterHost::RecordCrash(PT
, nsICrashService::CRASH_TYPE_CRASH
,
64 NS_WARNING(nsPrintfCString("child process pid = %d crashed without "
65 "leaving a minidump behind",
72 UniquePtr
<ipc::CrashReporterHost
> mCrashReporter
;
76 } // namespace mozilla
78 #endif // mozilla_ipc_CrashReporterHelper_h