File: CrashReporterHelper.h

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (105 lines) | stat: -rw-r--r-- 3,633 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* 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