forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrashReporterHost.cpp
290 lines (247 loc) · 8.28 KB
/
CrashReporterHost.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "CrashReporterHost.h"
#include "CrashReporterMetadataShmem.h"
#include "mozilla/ipc/GeckoChildProcessHost.h"
#include "mozilla/Sprintf.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/Telemetry.h"
#include "nsExceptionHandler.h"
#include "nsIAsyncShutdown.h"
#include "nsICrashService.h"
namespace mozilla {
namespace ipc {
CrashReporterHost::CrashReporterHost(GeckoProcessType aProcessType,
const Shmem& aShmem,
ThreadId aThreadId)
: mProcessType(aProcessType),
mShmem(aShmem),
mThreadId(aThreadId),
mStartTime(::time(nullptr)),
mFinalized(false)
{
}
bool
CrashReporterHost::GenerateCrashReport(base::ProcessId aPid)
{
if (!TakeCrashedChildMinidump(aPid, nullptr)) {
return false;
}
return FinalizeCrashReport();
}
RefPtr<nsIFile>
CrashReporterHost::TakeCrashedChildMinidump(base::ProcessId aPid, uint32_t* aOutSequence)
{
MOZ_ASSERT(!HasMinidump());
RefPtr<nsIFile> crashDump;
if (!XRE_TakeMinidumpForChild(aPid, getter_AddRefs(crashDump), aOutSequence)) {
return nullptr;
}
if (!AdoptMinidump(crashDump)) {
return nullptr;
}
return crashDump.get();
}
bool
CrashReporterHost::AdoptMinidump(nsIFile* aFile)
{
return CrashReporter::GetIDFromMinidump(aFile, mDumpID);
}
bool
CrashReporterHost::FinalizeCrashReport()
{
MOZ_ASSERT(!mFinalized);
MOZ_ASSERT(HasMinidump());
CrashReporter::AnnotationTable notes;
nsAutoCString type;
switch (mProcessType) {
case GeckoProcessType_Content:
type = NS_LITERAL_CSTRING("content");
break;
case GeckoProcessType_Plugin:
case GeckoProcessType_GMPlugin:
type = NS_LITERAL_CSTRING("plugin");
break;
case GeckoProcessType_GPU:
type = NS_LITERAL_CSTRING("gpu");
break;
default:
NS_ERROR("unknown process type");
break;
}
notes.Put(NS_LITERAL_CSTRING("ProcessType"), type);
char startTime[32];
SprintfLiteral(startTime, "%lld", static_cast<long long>(mStartTime));
notes.Put(NS_LITERAL_CSTRING("StartupTime"), nsDependentCString(startTime));
// We might not have shmem (for example, when running crashreporter tests).
if (mShmem.IsReadable()) {
CrashReporterMetadataShmem::ReadAppNotes(mShmem, ¬es);
}
CrashReporter::AppendExtraData(mDumpID, mExtraNotes);
CrashReporter::AppendExtraData(mDumpID, notes);
// Use mExtraNotes, since NotifyCrashService looks for "PluginHang" which is
// set in the parent process.
NotifyCrashService(mProcessType, mDumpID, &mExtraNotes);
mFinalized = true;
return true;
}
namespace {
class GenerateMinidumpShutdownBlocker : public nsIAsyncShutdownBlocker {
public:
GenerateMinidumpShutdownBlocker() = default;
NS_IMETHOD BlockShutdown(nsIAsyncShutdownClient* aBarrierClient) override
{
return NS_OK;
}
NS_IMETHOD GetName(nsAString& aName) override
{
aName = NS_LITERAL_STRING("Crash Reporter: blocking on minidump"
"generation.");
return NS_OK;
}
NS_IMETHOD GetState(nsIPropertyBag**) override
{
return NS_OK;
}
NS_DECL_THREADSAFE_ISUPPORTS
private:
virtual ~GenerateMinidumpShutdownBlocker() = default;
};
NS_IMPL_ISUPPORTS(GenerateMinidumpShutdownBlocker, nsIAsyncShutdownBlocker)
}
static nsCOMPtr<nsIAsyncShutdownClient> GetShutdownBarrier()
{
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdown();
nsCOMPtr<nsIAsyncShutdownClient> barrier;
nsresult rv = svc->GetProfileBeforeChange(getter_AddRefs(barrier));
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}
return barrier.forget();
}
void
CrashReporterHost::GenerateMinidumpAndPair(GeckoChildProcessHost* aChildProcess,
nsIFile* aMinidumpToPair,
const nsACString& aPairName,
std::function<void(bool)>&& aCallback,
bool aAsync)
{
base::ProcessHandle childHandle;
#ifdef XP_MACOSX
childHandle = aChildProcess->GetChildTask();
#else
childHandle = aChildProcess->GetChildProcessHandle();
#endif
if (!mCreateMinidumpCallback.IsEmpty()) {
aCallback(false);
return;
}
mCreateMinidumpCallback.Init(Move(aCallback), aAsync);
if (!childHandle) {
NS_WARNING("Failed to get child process handle.");
mCreateMinidumpCallback.Invoke(false);
return;
}
nsCOMPtr<nsIAsyncShutdownBlocker> shutdownBlocker;
if (aAsync && NS_IsMainThread()) {
nsCOMPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();
if (!barrier) {
mCreateMinidumpCallback.Invoke(false);
return;
}
shutdownBlocker = new GenerateMinidumpShutdownBlocker();
nsresult rv = barrier->AddBlocker(shutdownBlocker,
NS_LITERAL_STRING(__FILE__), __LINE__,
NS_LITERAL_STRING("Minidump generation"));
Unused << NS_WARN_IF(NS_FAILED(rv));
}
std::function<void(bool)> callback =
[this, shutdownBlocker](bool aResult) {
if (aResult &&
CrashReporter::GetIDFromMinidump(this->mTargetDump, this->mDumpID)) {
this->mCreateMinidumpCallback.Invoke(true);
} else {
this->mCreateMinidumpCallback.Invoke(false);
}
if (shutdownBlocker) {
nsCOMPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();
if (barrier) {
barrier->RemoveBlocker(shutdownBlocker);
}
}
};
CrashReporter::CreateMinidumpsAndPair(childHandle,
mThreadId,
aPairName,
aMinidumpToPair,
getter_AddRefs(mTargetDump),
Move(callback),
aAsync);
}
/* static */ void
CrashReporterHost::NotifyCrashService(GeckoProcessType aProcessType,
const nsString& aChildDumpID,
const AnnotationTable* aNotes)
{
if (!NS_IsMainThread()) {
RefPtr<Runnable> runnable = NS_NewRunnableFunction(
"ipc::CrashReporterHost::NotifyCrashService", [=]() -> void {
CrashReporterHost::NotifyCrashService(
aProcessType, aChildDumpID, aNotes);
});
RefPtr<nsIThread> mainThread = do_GetMainThread();
SyncRunnable::DispatchToThread(mainThread, runnable);
return;
}
MOZ_ASSERT(!aChildDumpID.IsEmpty());
nsCOMPtr<nsICrashService> crashService =
do_GetService("@mozilla.org/crashservice;1");
if (!crashService) {
return;
}
int32_t processType;
int32_t crashType = nsICrashService::CRASH_TYPE_CRASH;
nsCString telemetryKey;
switch (aProcessType) {
case GeckoProcessType_Content:
processType = nsICrashService::PROCESS_TYPE_CONTENT;
telemetryKey.AssignLiteral("content");
break;
case GeckoProcessType_Plugin: {
processType = nsICrashService::PROCESS_TYPE_PLUGIN;
telemetryKey.AssignLiteral("plugin");
nsAutoCString val;
if (aNotes->Get(NS_LITERAL_CSTRING("PluginHang"), &val) &&
val.EqualsLiteral("1")) {
crashType = nsICrashService::CRASH_TYPE_HANG;
telemetryKey.AssignLiteral("pluginhang");
}
break;
}
case GeckoProcessType_GMPlugin:
processType = nsICrashService::PROCESS_TYPE_GMPLUGIN;
telemetryKey.AssignLiteral("gmplugin");
break;
case GeckoProcessType_GPU:
processType = nsICrashService::PROCESS_TYPE_GPU;
telemetryKey.AssignLiteral("gpu");
break;
default:
NS_ERROR("unknown process type");
return;
}
nsCOMPtr<nsISupports> promise;
crashService->AddCrash(processType, crashType, aChildDumpID, getter_AddRefs(promise));
Telemetry::Accumulate(Telemetry::SUBPROCESS_CRASHES_WITH_DUMP, telemetryKey, 1);
}
void
CrashReporterHost::AddNote(const nsCString& aKey, const nsCString& aValue)
{
mExtraNotes.Put(aKey, aValue);
}
} // namespace ipc
} // namespace mozilla