-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
SentryCrashMonitor.c
251 lines (225 loc) · 8.41 KB
/
SentryCrashMonitor.c
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
//
// SentryCrashMonitor.c
//
// Created by Karl Stenerud on 2012-02-12.
//
// Copyright (c) 2012 Karl Stenerud. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall remain in place
// in this source code.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "SentryCrashMonitor.h"
#include "SentryCrashMonitorContext.h"
#include "SentryCrashMonitorType.h"
#include "SentryCrashDebug.h"
#include "SentryCrashMonitor_AppState.h"
#include "SentryCrashMonitor_CPPException.h"
#include "SentryCrashMonitor_Deadlock.h"
#include "SentryCrashMonitor_MachException.h"
#include "SentryCrashMonitor_NSException.h"
#include "SentryCrashMonitor_Signal.h"
#include "SentryCrashMonitor_System.h"
#include "SentryCrashMonitor_User.h"
#include "SentryCrashMonitor_Zombie.h"
#include "SentryCrashSystemCapabilities.h"
#include "SentryCrashThread.h"
#include <memory.h>
//#define SentryCrashLogger_LocalLevel TRACE
#include "SentryCrashLogger.h"
// ============================================================================
#pragma mark - Globals -
// ============================================================================
typedef struct {
SentryCrashMonitorType monitorType;
SentryCrashMonitorAPI *(*getAPI)(void);
} Monitor;
static Monitor g_monitors[] = {
#if SentryCrashCRASH_HAS_MACH
{
.monitorType = SentryCrashMonitorTypeMachException,
.getAPI = sentrycrashcm_machexception_getAPI,
},
#endif
#if SentryCrashCRASH_HAS_SIGNAL
{
.monitorType = SentryCrashMonitorTypeSignal,
.getAPI = sentrycrashcm_signal_getAPI,
},
#endif
#if SentryCrashCRASH_HAS_OBJC
{
.monitorType = SentryCrashMonitorTypeNSException,
.getAPI = sentrycrashcm_nsexception_getAPI,
},
{
.monitorType = SentryCrashMonitorTypeMainThreadDeadlock,
.getAPI = sentrycrashcm_deadlock_getAPI,
},
{
.monitorType = SentryCrashMonitorTypeZombie,
.getAPI = sentrycrashcm_zombie_getAPI,
},
#endif
{
.monitorType = SentryCrashMonitorTypeCPPException,
.getAPI = sentrycrashcm_cppexception_getAPI,
},
{
.monitorType = SentryCrashMonitorTypeUserReported,
.getAPI = sentrycrashcm_user_getAPI,
},
{
.monitorType = SentryCrashMonitorTypeSystem,
.getAPI = sentrycrashcm_system_getAPI,
},
{
.monitorType = SentryCrashMonitorTypeApplicationState,
.getAPI = sentrycrashcm_appstate_getAPI,
},
};
static int g_monitorsCount = sizeof(g_monitors) / sizeof(*g_monitors);
static SentryCrashMonitorType g_activeMonitors = SentryCrashMonitorTypeNone;
static bool g_handlingFatalException = false;
static bool g_crashedDuringExceptionHandling = false;
static bool g_requiresAsyncSafety = false;
static void (*g_onExceptionEvent)(struct SentryCrash_MonitorContext *monitorContext);
// ============================================================================
#pragma mark - API -
// ============================================================================
static inline SentryCrashMonitorAPI *
getAPI(Monitor *monitor)
{
if (monitor != NULL && monitor->getAPI != NULL) {
return monitor->getAPI();
}
return NULL;
}
static inline void
setMonitorEnabled(Monitor *monitor, bool isEnabled)
{
SentryCrashMonitorAPI *api = getAPI(monitor);
if (api != NULL && api->setEnabled != NULL) {
api->setEnabled(isEnabled);
}
}
static inline bool
isMonitorEnabled(Monitor *monitor)
{
SentryCrashMonitorAPI *api = getAPI(monitor);
if (api != NULL && api->isEnabled != NULL) {
return api->isEnabled();
}
return false;
}
static inline void
addContextualInfoToEvent(Monitor *monitor, struct SentryCrash_MonitorContext *eventContext)
{
SentryCrashMonitorAPI *api = getAPI(monitor);
if (api != NULL && api->addContextualInfoToEvent != NULL) {
api->addContextualInfoToEvent(eventContext);
}
}
void
sentrycrashcm_setEventCallback(void (*onEvent)(struct SentryCrash_MonitorContext *monitorContext))
{
g_onExceptionEvent = onEvent;
}
void
sentrycrashcm_setActiveMonitors(SentryCrashMonitorType monitorTypes)
{
if (sentrycrashdebug_isBeingTraced() && (monitorTypes & SentryCrashMonitorTypeDebuggerUnsafe)) {
static bool hasWarned = false;
if (!hasWarned) {
hasWarned = true;
SentryCrashLOGBASIC_WARN(" ************************ Crash "
"Handler Notice ************************");
SentryCrashLOGBASIC_WARN(" * App is running in a debugger. "
"Masking out unsafe monitors. *");
SentryCrashLOGBASIC_WARN(" * This means that most crashes WILL "
"NOT BE RECORDED while debugging! *");
SentryCrashLOGBASIC_WARN(" "
"*****************************************"
"*****************************");
}
monitorTypes &= SentryCrashMonitorTypeDebuggerSafe;
}
if (g_requiresAsyncSafety && (monitorTypes & SentryCrashMonitorTypeAsyncUnsafe)) {
SentryCrashLOG_DEBUG("Async-safe environment detected. Masking out unsafe monitors.");
monitorTypes &= SentryCrashMonitorTypeAsyncSafe;
}
SentryCrashLOG_DEBUG(
"Changing active monitors from 0x%x tp 0x%x.", g_activeMonitors, monitorTypes);
SentryCrashMonitorType activeMonitors = SentryCrashMonitorTypeNone;
for (int i = 0; i < g_monitorsCount; i++) {
Monitor *monitor = &g_monitors[i];
bool isEnabled = monitor->monitorType & monitorTypes;
setMonitorEnabled(monitor, isEnabled);
if (isMonitorEnabled(monitor)) {
activeMonitors |= monitor->monitorType;
} else {
activeMonitors &= ~monitor->monitorType;
}
}
SentryCrashLOG_DEBUG("Active monitors are now 0x%x.", activeMonitors);
g_activeMonitors = activeMonitors;
}
SentryCrashMonitorType
sentrycrashcm_getActiveMonitors()
{
return g_activeMonitors;
}
// ============================================================================
#pragma mark - Private API -
// ============================================================================
bool
sentrycrashcm_notifyFatalExceptionCaptured(bool isAsyncSafeEnvironment)
{
g_requiresAsyncSafety |= isAsyncSafeEnvironment; // Don't let it be unset.
if (g_handlingFatalException) {
g_crashedDuringExceptionHandling = true;
}
g_handlingFatalException = true;
if (g_crashedDuringExceptionHandling) {
SentryCrashLOG_INFO("Detected crash in the crash reporter. Uninstalling SentryCrash.");
sentrycrashcm_setActiveMonitors(SentryCrashMonitorTypeNone);
}
return g_crashedDuringExceptionHandling;
}
void
sentrycrashcm_handleException(struct SentryCrash_MonitorContext *context)
{
context->requiresAsyncSafety = g_requiresAsyncSafety;
if (g_crashedDuringExceptionHandling) {
context->crashedDuringCrashHandling = true;
}
for (int i = 0; i < g_monitorsCount; i++) {
Monitor *monitor = &g_monitors[i];
if (isMonitorEnabled(monitor)) {
addContextualInfoToEvent(monitor, context);
}
}
g_onExceptionEvent(context);
if (context->currentSnapshotUserReported) {
g_handlingFatalException = false;
} else {
if (g_handlingFatalException && !g_crashedDuringExceptionHandling) {
SentryCrashLOG_DEBUG("Exception is fatal. Restoring original handlers.");
sentrycrashcm_setActiveMonitors(SentryCrashMonitorTypeNone);
}
}
}