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
12 #include "mozilla/UniquePtr.h"
13 #include "base/process.h"
14 #include "nsExceptionHandler.h"
16 #include "nsThreadUtils.h"
17 #include "mozilla/ipc/GeckoChildProcessHost.h"
18 #include "mozilla/ipc/ProtocolUtils.h"
23 // This is the newer replacement for CrashReporterParent. It is created in
24 // response to a InitCrashReporter message on a top-level actor. When the
25 // process terminates abnormally, the top-level should call GenerateCrashReport
26 // to automatically integrate metadata.
27 class CrashReporterHost
{
28 typedef CrashReporter::AnnotationTable AnnotationTable
;
31 CrashReporterHost(GeckoProcessType aProcessType
,
32 CrashReporter::ThreadId aThreadId
);
34 // Helper function for generating a crash report for a process that probably
35 // crashed (i.e., had an AbnormalShutdown in ActorDestroy). Returns true if
36 // the process has a minidump attached and we were able to generate a report.
37 bool GenerateCrashReport(base::ProcessId aPid
);
39 // Given an existing minidump for a crashed child process, take ownership of
40 // it from IPDL. After this, FinalizeCrashReport may be called.
41 RefPtr
<nsIFile
> TakeCrashedChildMinidump(base::ProcessId aPid
);
43 // Replace the stored minidump with a new one. After this,
44 // FinalizeCrashReport may be called.
45 bool AdoptMinidump(nsIFile
* aFile
, const AnnotationTable
& aAnnotations
);
47 // If a minidump was already captured (e.g. via the hang reporter), this
48 // finalizes the existing report by attaching metadata, writing out the
49 // .extra file and notifying the crash service.
50 void FinalizeCrashReport();
52 // Delete any crash report we might have generated.
53 void DeleteCrashReport();
55 // Generate a paired minidump. This does not take the crash report, as
56 // GenerateCrashReport does. After this, FinalizeCrashReport may be called.
58 // This calls TakeCrashedChildMinidump and FinalizeCrashReport.
59 bool GenerateMinidumpAndPair(GeckoChildProcessHost
* aChildProcessHost
,
60 const nsACString
& aPairName
) {
61 auto childHandle
= base::kInvalidProcessHandle
;
62 const auto cleanup
= MakeScopeExit([&]() {
63 if (childHandle
&& childHandle
!= base::kInvalidProcessHandle
) {
64 base::CloseProcessHandle(childHandle
);
68 childHandle
= aChildProcessHost
->GetChildTask();
70 if (!base::OpenPrivilegedProcessHandle(
71 aChildProcessHost
->GetChildProcessId(), &childHandle
)) {
72 NS_WARNING("Failed to open child process handle.");
77 nsCOMPtr
<nsIFile
> targetDump
;
78 if (!CrashReporter::CreateMinidumpsAndPair(childHandle
, mThreadId
,
79 aPairName
, mExtraAnnotations
,
80 getter_AddRefs(targetDump
))) {
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());
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
);
109 // Get the nsICrashService crash type to use for an impending crash.
110 int32_t GetCrashType();
112 static void RecordCrashWithTelemetry(GeckoProcessType aProcessType
,
114 static void NotifyCrashService(GeckoProcessType aProcessType
,
116 const nsString
& aChildDumpID
);
119 GeckoProcessType mProcessType
;
120 CrashReporter::ThreadId mThreadId
;
122 AnnotationTable mExtraAnnotations
;
128 } // namespace mozilla
130 #endif // mozilla_ipc_CrashReporterHost_h