Debian Codegraph @

mozglue/misc/RuntimeExceptionModule.cpp

raw
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "RuntimeExceptionModule.h"
8
9 #include <cstdint>
10
11 #include "mozilla/ProcessType.h"
12
13 #if defined(XP_WIN)
14 # include <windows.h>
15 # if defined(__MINGW32__) || defined(__MINGW64__)
16 // Add missing constants and types for mingw builds
17 typedef HANDLE HREPORT;
18 # define WerReportSubmit(a, b, c, d) \
19 WerReportSubmit(a, b, c, WER_SUBMIT_RESULT* pSubmitResult)
20 # define WER_MAX_PREFERRED_MODULES_BUFFER 256
21 # endif // defined(__MINGW32__) || defined(__MINGW64__)
22 # include <werapi.h> // For WerRegisterRuntimeExceptionModule()
23 # if defined(__MINGW32__) || defined(__MINGW64__)
24 # undef WerReportSubmit
25 # endif // defined(__MINGW32__) || defined(__MINGW64__)
26 # include <stdlib.h>
27
28 # include "mozilla/Unused.h"
29
30 using mozilla::Unused;
31 #endif
32
33 namespace CrashReporter {
34
35 #ifdef XP_WIN
36
37 const static size_t kModulePathLength = MAX_PATH + 1;
38 static wchar_t sModulePath[kModulePathLength];
39
40 bool GetRuntimeExceptionModulePath(wchar_t* aPath, const size_t aLength) {
41 const wchar_t* kModuleName = L"mozwer.dll";
42 DWORD res = ::GetModuleFileNameW(nullptr, aPath, aLength);
43 if ((res > 0) && (res != aLength)) {
44 wchar_t* last_backslash = wcsrchr(aPath, L'\\');
45 if (last_backslash) {
46 *(last_backslash + 1) = L'\0';
47 if (wcscat_s(aPath, aLength, kModuleName) == 0) {
48 return true;
49 }
50 }
51 }
52
53 return false;
54 }
55
56 #endif // XP_WIN
57
58 void RegisterRuntimeExceptionModule() {
59 #ifdef XP_WIN
60 # if defined(DEBUG)
61 // In debug builds, disable the crash reporter by default, and allow to
62 // enable it with the MOZ_CRASHREPORTER environment variable.
63 const char* envvar = getenv("MOZ_CRASHREPORTER");
64 if (!envvar || !*envvar) {
65 return;
66 }
67 # else
68 // In other builds, enable the crash reporter by default, and allow
69 // disabling it with the MOZ_CRASHREPORTER_DISABLE environment variable.
70 const char* envvar = getenv("MOZ_CRASHREPORTER_DISABLE");
71 if (envvar && *envvar) {
72 return;
73 }
74 # endif
75
76 // If sModulePath is set we have already registerd the module.
77 if (*sModulePath) {
78 return;
79 }
80
81 // If we fail to get the path just return.
82 if (!GetRuntimeExceptionModulePath(sModulePath, kModulePathLength)) {
83 return;
84 }
85
86 if (FAILED(::WerRegisterRuntimeExceptionModule(
87 sModulePath,
88 reinterpret_cast<PVOID>(mozilla::GetGeckoProcessType())))) {
89 // The registration failed null out sModulePath to record this.
90 *sModulePath = L'\0';
91 return;
92 }
93 #endif // XP_WIN
94 }
95
96 void UnregisterRuntimeExceptionModule() {
97 #ifdef XP_WIN
98 // If sModulePath is set then we have registered the module.
99 if (*sModulePath) {
100 Unused << ::WerUnregisterRuntimeExceptionModule(
101 sModulePath, reinterpret_cast<PVOID>(mozilla::GetGeckoProcessType()));
102 *sModulePath = L'\0';
103 }
104 #endif // XP_WIN
105 }
106
107 } // namespace CrashReporter