summaryrefslogtreecommitdiff
path: root/dom/ipc/CrashReporterParent.h
blob: 25824f2793511d02265786ef2fd251bac664c197 (plain)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* -*- 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/. */

#ifndef mozilla_dom_CrashReporterParent_h
#define mozilla_dom_CrashReporterParent_h

#include "mozilla/dom/PCrashReporterParent.h"
#include "mozilla/dom/TabMessageUtils.h"
#include "nsIFile.h"
#ifdef MOZ_CRASHREPORTER
#include "nsExceptionHandler.h"
#include "nsDataHashtable.h"
#endif

namespace mozilla {
namespace dom {

class CrashReporterParent : public PCrashReporterParent
{
#ifdef MOZ_CRASHREPORTER
  typedef CrashReporter::AnnotationTable AnnotationTable;
#endif
public:
  CrashReporterParent();
  virtual ~CrashReporterParent();

#ifdef MOZ_CRASHREPORTER

  /*
   * Attempt to create a bare-bones crash report, along with extra process-
   * specific annotations present in the given AnnotationTable. Calls
   * GenerateChildData and FinalizeChildData.
   *
   * @returns true if successful, false otherwise.
   */
  template<class Toplevel>
  bool
  GenerateCrashReport(Toplevel* t, const AnnotationTable* processNotes);

  /*
   * Attempt to generate a parent/child pair of minidumps from the given
   * toplevel actor. This calls CrashReporter::CreateMinidumpsAndPair to
   * generate the minidumps. Crash reporter annotations set prior to this
   * call will be saved via PairedDumpCallbackExtra into an .extra file
   * under the proper crash id. AnnotateCrashReport annotations are not
   * set in this call and the report is not finalized.
   *
   * @returns true if successful, false otherwise.
   */
  template<class Toplevel>
  bool
  GeneratePairedMinidump(Toplevel* t);

  /*
   * Attempts to take a minidump of the current process and pair that with
   * a named minidump handed in by the caller.
   *
   * @param aTopLevel - top level actor this reporter is associated with.
   * @param aMinidump - the minidump to associate with.
   * @param aPairName - the name of the additional minidump.
   * @returns true if successful, false otherwise.
   */
  template<class Toplevel>
  bool
  GenerateMinidumpAndPair(Toplevel* aTopLevel, nsIFile* aMinidump,
                          const nsACString& aPairName);

  /**
   * Apply child process annotations to an existing paired mindump generated
   * with GeneratePairedMinidump.
   *
   * Be careful about calling generate apis immediately after this call,
   * see FinalizeChildData.
   *
   * @param processNotes (optional) - Additional notes to append. Annotations
   *   stored in mNotes will also be applied. processNotes can be null.
   * @returns true if successful, false otherwise.
   */
  bool
  GenerateChildData(const AnnotationTable* processNotes);

  /**
   * Handles main thread finalization tasks after a report has been
   * generated. Does the following:
   *  - register the finished report with the crash service manager
   *  - records telemetry related data about crashes
   *
   * Be careful about calling generate apis immediately after this call,
   * if this api is called on a non-main thread it will fire off a runnable
   * to complete its work async.
   */
  void
  FinalizeChildData();

  /*
   * Attempt to generate a full paired dump complete with any child
   * annoations, and finalizes the report. Note this call is only valid
   * on the main thread. Calling on a background thread will fail.
   *
   * @returns true if successful, false otherwise.
   */
  template<class Toplevel>
  bool
  GenerateCompleteMinidump(Toplevel* t);

  /**
   * Submits a raw minidump handed in, calls GenerateChildData and
   * FinalizeChildData. Used by content plugins and gmp.
   *
   * @returns true if successful, false otherwise.
   */
  bool
  GenerateCrashReportForMinidump(nsIFile* minidump,
                                 const AnnotationTable* processNotes);

  /*
   * Instantiate a new crash reporter actor from a given parent that manages
   * the protocol.
   *
   * @returns true if successful, false otherwise.
   */
  template<class Toplevel>
  static bool CreateCrashReporter(Toplevel* actor);
#endif // MOZ_CRASHREPORTER

  /*
   * Initialize this reporter with data from the child process.
   */
  void
  SetChildData(const NativeThreadId& id, const uint32_t& processType);

  /*
   * Returns the ID of the child minidump.
   * GeneratePairedMinidump or GenerateCrashReport must be called first.
   */
  const nsString& ChildDumpID() const {
    return mChildDumpID;
  }

  /*
   * Add an annotation to our internally tracked list of annotations.
   * Callers must apply these notes using GenerateChildData otherwise
   * the notes will get dropped.
   */
  void
  AnnotateCrashReport(const nsCString& aKey, const nsCString& aData);

 protected:
  virtual void ActorDestroy(ActorDestroyReason aWhy) override;

  virtual bool RecvAnnotateCrashReport(const nsCString& aKey,
                                       const nsCString& aData) override
  {
    AnnotateCrashReport(aKey, aData);
    return true;
  }

  virtual bool RecvAppendAppNotes(const nsCString& aData) override;

#ifdef MOZ_CRASHREPORTER
  void
  NotifyCrashService();
#endif

#ifdef MOZ_CRASHREPORTER
  AnnotationTable mNotes;
#endif
  nsCString mAppNotes;
  nsString mChildDumpID;
  // stores the child main thread id
  NativeThreadId mMainThread;
  time_t mStartTime;
  // stores the child process type
  GeckoProcessType mProcessType;
  bool mInitialized;
};

#ifdef MOZ_CRASHREPORTER
template<class Toplevel>
inline bool
CrashReporterParent::GeneratePairedMinidump(Toplevel* t)
{
  mozilla::ipc::ScopedProcessHandle child;
#ifdef XP_MACOSX
  child = t->Process()->GetChildTask();
#else
  if (!base::OpenPrivilegedProcessHandle(t->OtherPid(), &child.rwget())) {
    NS_WARNING("Failed to open child process handle.");
    return false;
  }
#endif
  nsCOMPtr<nsIFile> childDump;
  if (CrashReporter::CreateMinidumpsAndPair(child,
                                            mMainThread,
                                            NS_LITERAL_CSTRING("browser"),
                                            nullptr, // pair with a dump of this process and thread
                                            getter_AddRefs(childDump)) &&
      CrashReporter::GetIDFromMinidump(childDump, mChildDumpID)) {
    return true;
  }
  return false;
}

template<class Toplevel>
inline bool
CrashReporterParent::GenerateMinidumpAndPair(Toplevel* aTopLevel,
                                             nsIFile* aMinidumpToPair,
                                             const nsACString& aPairName)
{
  mozilla::ipc::ScopedProcessHandle childHandle;
#ifdef XP_MACOSX
  childHandle = aTopLevel->Process()->GetChildTask();
#else
  if (!base::OpenPrivilegedProcessHandle(aTopLevel->OtherPid(),
                                         &childHandle.rwget())) {
    NS_WARNING("Failed to open child process handle.");
    return false;
  }
#endif
  nsCOMPtr<nsIFile> targetDump;
  if (CrashReporter::CreateMinidumpsAndPair(childHandle,
                                            mMainThread, // child thread id
                                            aPairName,
                                            aMinidumpToPair,
                                            getter_AddRefs(targetDump)) &&
      CrashReporter::GetIDFromMinidump(targetDump, mChildDumpID)) {
    return true;
  }
  return false;
}

template<class Toplevel>
inline bool
CrashReporterParent::GenerateCrashReport(Toplevel* t,
                                         const AnnotationTable* processNotes)
{
  nsCOMPtr<nsIFile> crashDump;
  if (t->TakeMinidump(getter_AddRefs(crashDump), nullptr) &&
      CrashReporter::GetIDFromMinidump(crashDump, mChildDumpID)) {
    bool result = GenerateChildData(processNotes);
    FinalizeChildData();
    return result;
  }
  return false;
}

template<class Toplevel>
inline bool
CrashReporterParent::GenerateCompleteMinidump(Toplevel* t)
{
  mozilla::ipc::ScopedProcessHandle child;
  if (!NS_IsMainThread()) {
    NS_WARNING("GenerateCompleteMinidump can't be called on non-main thread.");
    return false;
  }

#ifdef XP_MACOSX
  child = t->Process()->GetChildTask();
#else
  if (!base::OpenPrivilegedProcessHandle(t->OtherPid(), &child.rwget())) {
    NS_WARNING("Failed to open child process handle.");
    return false;
  }
#endif
  nsCOMPtr<nsIFile> childDump;
  if (CrashReporter::CreateMinidumpsAndPair(child,
                                            mMainThread,
                                            NS_LITERAL_CSTRING("browser"),
                                            nullptr, // pair with a dump of this process and thread
                                            getter_AddRefs(childDump)) &&
      CrashReporter::GetIDFromMinidump(childDump, mChildDumpID)) {
    bool result = GenerateChildData(nullptr);
    FinalizeChildData();
    return result;
  }
  return false;
}

template<class Toplevel>
/* static */ bool
CrashReporterParent::CreateCrashReporter(Toplevel* actor)
{
#ifdef MOZ_CRASHREPORTER
  NativeThreadId id;
  uint32_t processType;
  PCrashReporterParent* p =
      actor->CallPCrashReporterConstructor(&id, &processType);
  if (p) {
    static_cast<CrashReporterParent*>(p)->SetChildData(id, processType);
  } else {
    NS_ERROR("Error creating crash reporter actor");
  }
  return !!p;
#endif
  return false;
}

#endif

} // namespace dom
} // namespace mozilla

#endif // mozilla_dom_CrashReporterParent_h