Bug 1796811 - Update in-tree zlib to version 1.2.13. r=aosmond
[gecko.git] / ipc / glue / CrashReporterHost.h
blob2abf59dce77d65fa4dd697eaf8625a523d178a7b
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 ScopedProcessHandle childHandle;
62 #ifdef XP_MACOSX
63 childHandle = aToplevelProtocol->Process()->GetChildTask();
64 #else
65 if (!base::OpenPrivilegedProcessHandle(aToplevelProtocol->OtherPid(),
66 &childHandle.rwget())) {
67 NS_WARNING("Failed to open child process handle.");
68 return false;
70 #endif
72 nsCOMPtr<nsIFile> targetDump;
73 if (!CrashReporter::CreateMinidumpsAndPair(childHandle, mThreadId,
74 aPairName, mExtraAnnotations,
75 getter_AddRefs(targetDump))) {
76 return false;
79 return CrashReporter::GetIDFromMinidump(targetDump, mDumpID);
82 void AddAnnotation(CrashReporter::Annotation aKey, bool aValue);
83 void AddAnnotation(CrashReporter::Annotation aKey, int aValue);
84 void AddAnnotation(CrashReporter::Annotation aKey, unsigned int aValue);
85 void AddAnnotation(CrashReporter::Annotation aKey, const nsACString& aValue);
87 bool HasMinidump() const { return !mDumpID.IsEmpty(); }
88 const nsString& MinidumpID() const {
89 MOZ_ASSERT(HasMinidump());
90 return mDumpID;
92 const nsCString& AdditionalMinidumps() const {
93 return mExtraAnnotations[CrashReporter::Annotation::additional_minidumps];
96 // This is a static helper function to notify the crash service that a
97 // crash has occurred and record the crash with telemetry. This can be called
98 // from any thread, and if not called from the main thread, will post a
99 // synchronous message to the main thread.
100 static void RecordCrash(GeckoProcessType aProcessType, int32_t aCrashType,
101 const nsString& aChildDumpID);
103 private:
104 // Get the nsICrashService crash type to use for an impending crash.
105 int32_t GetCrashType();
107 static void RecordCrashWithTelemetry(GeckoProcessType aProcessType,
108 int32_t aCrashType);
109 static void NotifyCrashService(GeckoProcessType aProcessType,
110 int32_t aCrashType,
111 const nsString& aChildDumpID);
113 private:
114 GeckoProcessType mProcessType;
115 CrashReporter::ThreadId mThreadId;
116 time_t mStartTime;
117 AnnotationTable mExtraAnnotations;
118 nsString mDumpID;
119 bool mFinalized;
122 } // namespace ipc
123 } // namespace mozilla
125 #endif // mozilla_ipc_CrashReporterHost_h