Bug 1891340 - Part 1: Add parameters to customize the before and after icon tints...
[gecko.git] / ipc / glue / CrashReporterHost.h
blobdf0af17d435ad4421435d04d2d5f3e5f650cde33
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_ipc_CrashReporterHost_h
8 #define mozilla_ipc_CrashReporterHost_h
10 #include <functional>
12 #include "mozilla/UniquePtr.h"
13 #include "base/process.h"
14 #include "nsExceptionHandler.h"
15 #include "nsThreadUtils.h"
16 #include "mozilla/ipc/ProtocolUtils.h"
18 namespace mozilla {
19 namespace ipc {
21 // This is the newer replacement for CrashReporterParent. It is created in
22 // response to a InitCrashReporter message on a top-level actor. When the
23 // process terminates abnormally, the top-level should call GenerateCrashReport
24 // to automatically integrate metadata.
25 class CrashReporterHost {
26 typedef CrashReporter::AnnotationTable AnnotationTable;
28 public:
29 CrashReporterHost(GeckoProcessType aProcessType,
30 CrashReporter::ThreadId aThreadId);
32 // Helper function for generating a crash report for a process that probably
33 // crashed (i.e., had an AbnormalShutdown in ActorDestroy). Returns true if
34 // the process has a minidump attached and we were able to generate a report.
35 bool GenerateCrashReport(base::ProcessId aPid);
37 // Given an existing minidump for a crashed child process, take ownership of
38 // it from IPDL. After this, FinalizeCrashReport may be called.
39 RefPtr<nsIFile> TakeCrashedChildMinidump(base::ProcessId aPid,
40 uint32_t* aOutSequence);
42 // Replace the stored minidump with a new one. After this,
43 // FinalizeCrashReport may be called.
44 bool AdoptMinidump(nsIFile* aFile, const AnnotationTable& aAnnotations);
46 // If a minidump was already captured (e.g. via the hang reporter), this
47 // finalizes the existing report by attaching metadata, writing out the
48 // .extra file and notifying the crash service.
49 void FinalizeCrashReport();
51 // Delete any crash report we might have generated.
52 void DeleteCrashReport();
54 // Generate a paired minidump. This does not take the crash report, as
55 // GenerateCrashReport does. After this, FinalizeCrashReport may be called.
57 // This calls TakeCrashedChildMinidump and FinalizeCrashReport.
58 template <typename Toplevel>
59 bool GenerateMinidumpAndPair(Toplevel* aToplevelProtocol,
60 const nsACString& aPairName) {
61 auto childHandle = base::kInvalidProcessHandle;
62 const auto cleanup = MakeScopeExit([&]() {
63 if (childHandle && childHandle != base::kInvalidProcessHandle) {
64 base::CloseProcessHandle(childHandle);
66 });
67 #ifdef XP_MACOSX
68 childHandle = aToplevelProtocol->Process()->GetChildTask();
69 #else
70 if (!base::OpenPrivilegedProcessHandle(aToplevelProtocol->OtherPid(),
71 &childHandle)) {
72 NS_WARNING("Failed to open child process handle.");
73 return false;
75 #endif
77 nsCOMPtr<nsIFile> targetDump;
78 if (!CrashReporter::CreateMinidumpsAndPair(childHandle, mThreadId,
79 aPairName, mExtraAnnotations,
80 getter_AddRefs(targetDump))) {
81 return false;
84 return CrashReporter::GetIDFromMinidump(targetDump, mDumpID);
87 void AddAnnotationBool(CrashReporter::Annotation aKey, bool aValue);
88 void AddAnnotationU32(CrashReporter::Annotation aKey, uint32_t aValue);
89 void AddAnnotationNSCString(CrashReporter::Annotation aKey,
90 const nsACString& aValue);
92 bool HasMinidump() const { return !mDumpID.IsEmpty(); }
93 const nsString& MinidumpID() const {
94 MOZ_ASSERT(HasMinidump());
95 return mDumpID;
97 const nsCString& AdditionalMinidumps() const {
98 return mExtraAnnotations[CrashReporter::Annotation::additional_minidumps];
101 // This is a static helper function to notify the crash service that a
102 // crash has occurred and record the crash with telemetry. This can be called
103 // from any thread, and if not called from the main thread, will post a
104 // synchronous message to the main thread.
105 static void RecordCrash(GeckoProcessType aProcessType, int32_t aCrashType,
106 const nsString& aChildDumpID);
108 private:
109 // Get the nsICrashService crash type to use for an impending crash.
110 int32_t GetCrashType();
112 static void RecordCrashWithTelemetry(GeckoProcessType aProcessType,
113 int32_t aCrashType);
114 static void NotifyCrashService(GeckoProcessType aProcessType,
115 int32_t aCrashType,
116 const nsString& aChildDumpID);
118 private:
119 GeckoProcessType mProcessType;
120 CrashReporter::ThreadId mThreadId;
121 time_t mStartTime;
122 AnnotationTable mExtraAnnotations;
123 nsString mDumpID;
124 bool mFinalized;
127 } // namespace ipc
128 } // namespace mozilla
130 #endif // mozilla_ipc_CrashReporterHost_h