-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
crash.cpp
348 lines (291 loc) · 9.78 KB
/
crash.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2020-2024 The Defold Foundation
// Copyright 2014-2020 King
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#include <dlib/dlib.h>
#include <dlib/log.h>
#include <dlib/sys.h>
#include <dlib/dstrings.h>
#include <dlib/math.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "crash.h"
#include "crash_private.h"
namespace dmCrash
{
AppState g_AppState;
AppState g_PreviousAppState;
// The default path is where the dumps are stored before the application configures a different path.
static char g_FilePathDefault[AppState::FILEPATH_MAX];
static bool g_CrashDumpEnabled = true;
void SetExtraInfoCallback(FCallstackExtraInfoCallback cbk);
char g_FilePath[AppState::FILEPATH_MAX];
bool IsInitialized()
{
return g_AppState.m_EngineVersion[0] != 0x0;
}
void Init(const char *version, const char *hash)
{
memset(g_FilePath, 0x0, sizeof(g_FilePath));
memset(&g_AppState, 0x0, sizeof(g_AppState));
// Construct a file path with the app name 'Defold' until it is modified by the application;
// this also means crash files can be stored in two different places. At the default path or in the
// application-specific location. So need to look for both when loading.
dmSys::Result r = dmSys::GetApplicationSupportPath("Defold", g_FilePathDefault, sizeof(g_FilePathDefault));
if (r == dmSys::RESULT_OK)
{
dmStrlCat(g_FilePathDefault, "/", sizeof(g_FilePathDefault));
dmStrlCat(g_FilePathDefault, "_crash", sizeof(g_FilePathDefault));
dmStrlCpy(g_FilePath, g_FilePathDefault, sizeof(g_FilePath));
dmSys::SystemInfo info;
dmSys::GetSystemInfo(&info);
#define COPY_FIELD(x) dmStrlCpy(g_AppState.x, info.x, sizeof(g_AppState.x))
COPY_FIELD(m_DeviceModel);
COPY_FIELD(m_Manufacturer);
COPY_FIELD(m_SystemName);
COPY_FIELD(m_SystemVersion);
COPY_FIELD(m_Language);
COPY_FIELD(m_DeviceLanguage);
COPY_FIELD(m_Territory);
#undef COPY_FIELD
dmStrlCpy(g_AppState.m_EngineVersion, version, sizeof(g_AppState.m_EngineVersion));
dmStrlCpy(g_AppState.m_EngineHash, hash, sizeof(g_AppState.m_EngineHash));
SetLoadAddrs(&g_AppState);
SetCrashFilename(g_FilePath);
InstallHandler();
EnableHandler(g_CrashDumpEnabled);
}
}
bool IsEnabled()
{
return g_CrashDumpEnabled;
}
void SetEnabled(bool enable)
{
g_CrashDumpEnabled = enable;
EnableHandler(enable);
}
void SetExtraInfoCallback(FCallstackExtraInfoCallback cbk, void* ctx)
{
HandlerSetExtraInfoCallback(cbk, ctx);
}
void SetFilePath(const char *filepath)
{
dmStrlCpy(g_FilePath, filepath, sizeof(g_FilePath));
SetCrashFilename(g_FilePath);
}
Result SetUserField(uint32_t index, const char *value)
{
if (index < AppState::USERDATA_SLOTS)
{
dmStrlCpy(g_AppState.m_UserData[index], value, sizeof(g_AppState.m_UserData[index]));
return RESULT_OK;
}
return RESULT_INVALID_PARAM;
}
static HDump LoadPrevious(FILE *f)
{
AppStateHeader header;
memset(&header, 0x0, sizeof(AppStateHeader));
if (fread(&header, 1, sizeof(AppStateHeader), f) == sizeof(AppStateHeader))
{
memset(&g_PreviousAppState, 0x0, sizeof(AppState));
if (header.version == AppState::VERSION && header.struct_size == sizeof(AppState))
{
if (fread(&g_PreviousAppState, 1, sizeof(AppState), f) == sizeof(AppState))
{
return 1;
}
else
{
dmLogError("Crashdump is incomplete.");
}
}
else
{
dmLogWarning("Crashdump version or format does not match: Crash version: %d.%d Tool Version: %d.%d", header.version, (int)header.struct_size, AppState::VERSION, (int)sizeof(AppState));
}
}
else
{
dmLogError("Crashdump does not contain a valid header.");
}
return 0;
}
HDump LoadPreviousPath(const char *where)
{
HDump ret = 0;
FILE* fhandle = fopen(where, "rb");
if (fhandle)
{
ret = LoadPrevious(fhandle);
fclose(fhandle);
}
return ret;
}
// Load previous dump (if exists)
HDump LoadPrevious()
{
HDump dump = 0;
if ((dump = LoadPreviousPath(g_FilePathDefault)) != 0)
{
return dump;
}
return LoadPreviousPath(g_FilePath);
}
void Release(HDump dump)
{
if (dump == 1)
{
memset(&g_PreviousAppState, 0x0, sizeof(AppState));
}
}
AppState* Check(HDump dump)
{
if (dump == 1 && g_PreviousAppState.m_EngineVersion[0] != 0x0)
{
return &g_PreviousAppState;
}
return NULL;
}
bool IsValidHandle(HDump dump)
{
return Check(dump) != NULL;
}
const char* GetExtraData(HDump dump)
{
AppState* state = Check(dump);
return state ? state->m_Extra : 0;
}
int GetSignum(HDump dump)
{
AppState* state = Check(dump);
return state ? state->m_Signum : 0;
}
const char* GetSysField(HDump dump, SysField fld)
{
AppState* state = Check(dump);
if (state != NULL)
{
// Cannot trust anything in the dump so terminate before returning.
#define TERM_RET(x) x[sizeof(x)-1] = 0; return x;
switch (fld)
{
case SYSFIELD_ENGINE_VERSION: TERM_RET(state->m_EngineVersion);
case SYSFIELD_ENGINE_HASH: TERM_RET(state->m_EngineHash);
case SYSFIELD_DEVICE_MODEL: TERM_RET(state->m_DeviceModel);
case SYSFIELD_MANUFACTURER: TERM_RET(state->m_Manufacturer);
case SYSFIELD_SYSTEM_NAME: TERM_RET(state->m_SystemName);
case SYSFIELD_SYSTEM_VERSION: TERM_RET(state->m_SystemVersion);
case SYSFIELD_LANGUAGE: TERM_RET(state->m_Language);
case SYSFIELD_DEVICE_LANGUAGE: TERM_RET(state->m_DeviceLanguage);
case SYSFIELD_TERRITORY: TERM_RET(state->m_Territory);
case SYSFIELD_ANDROID_BUILD_FINGERPRINT: TERM_RET(state->m_AndroidBuildFingerprint);
default: return 0;
}
#undef TERM_RET
}
return 0;
}
const char* GetUserField(HDump dump, uint32_t index)
{
AppState* state = Check(dump);
if (state != NULL)
{
if (index < AppState::USERDATA_SLOTS)
{
state->m_UserData[index][AppState::USERDATA_SIZE - 1] = 0;
return state->m_UserData[index];
}
}
return 0;
}
void Purge()
{
dmSys::Unlink(g_FilePath);
dmSys::Unlink(g_FilePathDefault);
PlatformPurge();
}
uint32_t GetBacktraceAddrCount(HDump dump)
{
AppState* state = Check(dump);
if (state != NULL)
{
return dmMath::Min(AppState::PTRS_MAX, state->m_PtrCount);
}
return 0;
}
void* GetBacktraceAddr(HDump dump, uint32_t index)
{
AppState* state = Check(dump);
if (state != NULL)
{
if (index < dmMath::Min(AppState::PTRS_MAX, state->m_PtrCount))
{
return state->m_Ptr[index];
}
}
return 0;
}
const char *GetModuleName(HDump dump, uint32_t index)
{
if (index < AppState::MODULES_MAX)
{
AppState* state = Check(dump);
if (state != NULL && state->m_ModuleName[index][0])
{
char* field = state->m_ModuleName[index];
field[AppState::MODULE_NAME_SIZE - 1] = 0;
return field;
}
}
return 0;
}
void* GetModuleAddr(HDump dump, uint32_t index)
{
if (index < AppState::MODULES_MAX)
{
AppState* state = Check(dump);
if (state != NULL)
{
return state->m_ModuleAddr[index];
}
}
return 0;
}
void LogCallstack(char* extras)
{
bool is_debug_mode = dLib::IsDebugMode();
dLib::SetDebugMode(true);
dmLogError("CALL STACK:\n\n");
char* p = extras;
char* end = extras + strlen(extras);
while( p < end )
{
char* lineend = strchr(p, '\n');
if (!lineend)
lineend = strchr(p, '\r');
if (lineend && lineend < end)
*lineend = 0;
dmLogError("%s", p);
if (!lineend)
break;
p = lineend+1;
}
dmLogError("\n");
dLib::SetDebugMode(is_debug_mode);
}
}