Bug 1858509 add thread-safety annotations around MediaSourceDemuxer::mMonitor r=alwu
[gecko.git] / ipc / glue / CrashReporterHelper.h
blobc13901c2ccfd509b096de8575922f55242a1fdc5
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 "nsIAppStartup.h"
11 #include "nsExceptionHandler.h"
12 #include "nsICrashService.h"
13 #include "nsPrintfCString.h"
14 #include "nsServiceManagerUtils.h"
16 namespace mozilla {
17 namespace ipc {
19 /**
20 * This class encapsulates the common elements of crash report handling for
21 * toplevel protocols representing processes. To use this class, you should:
23 * 1. Declare a method to initialize the crash reporter in your IPDL:
24 * `async InitCrashReporter(NativeThreadId threadId)`
26 * 2. Inherit from this class, providing the appropriate `GeckoProcessType`
27 * enum value for the template parameter PT.
29 * 3. When your protocol actor is destroyed with a reason of `AbnormalShutdown`,
30 * you should call `GenerateCrashReport(OtherPid())`. If you need the crash
31 * report ID it will be copied in the second optional parameter upon
32 * successful crash report generation.
34 template <GeckoProcessType PT>
35 class CrashReporterHelper {
36 public:
37 CrashReporterHelper() : mCrashReporter(nullptr) {}
38 IPCResult RecvInitCrashReporter(const CrashReporter::ThreadId& aThreadId) {
39 mCrashReporter = MakeUnique<ipc::CrashReporterHost>(PT, aThreadId);
40 return IPC_OK();
43 protected:
44 void GenerateCrashReport(base::ProcessId aPid,
45 nsString* aMinidumpId = nullptr) {
46 nsAutoString minidumpId;
47 if (!mCrashReporter) {
48 HandleOrphanedMinidump(aPid, minidumpId);
49 } else if (mCrashReporter->GenerateCrashReport(aPid)) {
50 minidumpId = mCrashReporter->MinidumpID();
53 if (aMinidumpId) {
54 *aMinidumpId = minidumpId;
57 mCrashReporter = nullptr;
60 void MaybeTerminateProcess() {
61 if (PR_GetEnv("MOZ_CRASHREPORTER_SHUTDOWN")) {
62 NS_WARNING(nsPrintfCString("Shutting down due to %s process crash.",
63 XRE_GetProcessTypeString())
64 .get());
65 nsCOMPtr<nsIAppStartup> appService =
66 do_GetService("@mozilla.org/toolkit/app-startup;1");
67 if (appService) {
68 bool userAllowedQuit = true;
69 appService->Quit(nsIAppStartup::eForceQuit, 1, &userAllowedQuit);
74 private:
75 void HandleOrphanedMinidump(base::ProcessId aPid, nsString& aMinidumpId) {
76 if (CrashReporter::FinalizeOrphanedMinidump(aPid, PT, &aMinidumpId)) {
77 CrashReporterHost::RecordCrash(PT, nsICrashService::CRASH_TYPE_CRASH,
78 aMinidumpId);
79 } else {
80 NS_WARNING(nsPrintfCString("child process pid = %" PRIPID
81 " crashed without leaving a minidump behind",
82 aPid)
83 .get());
87 protected:
88 UniquePtr<ipc::CrashReporterHost> mCrashReporter;
91 } // namespace ipc
92 } // namespace mozilla
94 #endif // mozilla_ipc_CrashReporterHelper_h