-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcrashpad-backend.cpp
More file actions
263 lines (218 loc) · 10.1 KB
/
Copy pathcrashpad-backend.cpp
File metadata and controls
263 lines (218 loc) · 10.1 KB
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
#include "crashpad-backend.h"
#include "backtrace-native.h"
#include <jni.h>
#include <libgen.h>
//#include "client/crashpad_client.h"
extern std::string thread_id;
extern std::atomic_bool initialized;
extern std::mutex attribute_synchronization;
extern std::atomic_bool disabled;
static crashpad::CrashpadClient *client;
static std::unique_ptr<crashpad::CrashReportDatabase> database;
static int consecutive_crashes_count = 0;
bool InitializeCrashpad(jstring url,
jstring database_path,
jstring handler_path,
jobjectArray attributeKeys,
jobjectArray attributeValues,
jobjectArray attachmentPaths,
jboolean enableClientSideUnwinding,
jint unwindingMode) {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Initialize Crashpad Start");
// avoid multi initialization
if (initialized) {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Crashpad is already initialized");
return true;
}
JNIEnv *env = GetJniEnv();
if (env == nullptr) {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Cannot initialize JNIEnv");
return false;
}
// path to crashpad database
const char *filePath = env->GetStringUTFChars(database_path, 0);
base::FilePath db(filePath);
if (enableClientSideUnwinding) {
bool success = EnableClientSideUnwinding(env, filePath, unwindingMode);
if (!success) {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Cannot enable client side unwinding");
}
}
std::map<std::string, std::string> attributes;
attributes["format"] = "minidump";
// save native main thread id
if(!thread_id.empty()) {
attributes["thread.main"] = thread_id;
}
jint keyLength = env->GetArrayLength(attributeKeys);
jint valueLength = env->GetArrayLength(attributeValues);
if (keyLength == valueLength) {
for (int attributeIndex = 0; attributeIndex < keyLength; ++attributeIndex) {
jstring jstringKey = (jstring) env->GetObjectArrayElement(attributeKeys,
attributeIndex);
jboolean isCopy;
const char *convertedKey = (env)->GetStringUTFChars(jstringKey, &isCopy);
jstring stringValue = (jstring) env->GetObjectArrayElement(attributeValues,
attributeIndex);
const char *convertedValue = (env)->GetStringUTFChars(stringValue, &isCopy);
if (!convertedKey || !convertedValue)
continue;
attributes[convertedKey] = convertedValue;
env->ReleaseStringUTFChars(jstringKey, convertedKey);
env->ReleaseStringUTFChars(stringValue, convertedValue);
}
} else {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android",
"Attribute array length doesn't match. Attributes won't be available in the Crashpad integration");
}
std::vector<std::string> arguments;
arguments.push_back("--no-rate-limit");
// Backtrace url
const char *backtraceUrl = env->GetStringUTFChars(url, 0);
// path to crash handler executable
const char *handlerPath = env->GetStringUTFChars(handler_path, 0);
base::FilePath handler(handlerPath);
// paths to file attachments
if (attachmentPaths != nullptr) {
jint attachmentsLength = env->GetArrayLength(attachmentPaths);
for (int attachmentIndex = 0; attachmentIndex < attachmentsLength; ++attachmentIndex) {
jstring jstringAttachmentPath = (jstring) env->GetObjectArrayElement(
attachmentPaths,
attachmentIndex);
jboolean isCopy;
const char *convertedAttachmentPath = (env)->GetStringUTFChars(
jstringAttachmentPath, &isCopy);
if (!convertedAttachmentPath)
continue;
std::string attachmentBaseName = basename(convertedAttachmentPath);
std::string attachmentArgumentString("--attachment=");
attachmentArgumentString += convertedAttachmentPath;
arguments.push_back(attachmentArgumentString);
env->ReleaseStringUTFChars(jstringAttachmentPath, convertedAttachmentPath);
}
}
database = crashpad::CrashReportDatabase::Initialize(db);
if (database == nullptr || database->GetSettings() == NULL) {
return false;
}
// Enable automated uploads.
database->GetSettings()->SetUploadsEnabled(true);
// Start crash handler
client = new crashpad::CrashpadClient();
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Crashpad created.");
client->EnableCrashLoopDetection();
// Get consecutive crashes count BEFORE any handler started,
// as it writes extra line into CSV, what leads to getting 0 for each next ConsecutiveCrashesCount call
consecutive_crashes_count = crashpad::CrashpadClient::ConsecutiveCrashesCount(db);
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Crashpad initialize - ConsecutiveCrashesCount %d", consecutive_crashes_count);
// initialized = client->StartHandler(handler, db, db, backtraceUrl, attributes, arguments, false, false);
// handler, db, db, url, annotations, arguments, false, false, {}
// Original
initialized = client->StartHandlerAtCrash(handler, db, db, backtraceUrl, attributes, arguments);
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Crashpad initialized %s", (initialized ? "TRUE" : "FALSE"));
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Crashpad db: %s", filePath);
env->ReleaseStringUTFChars(url, backtraceUrl);
env->ReleaseStringUTFChars(handler_path, handlerPath);
env->ReleaseStringUTFChars(database_path, filePath);
if (enableClientSideUnwinding) {
SetCrashpadHandlerForClientSideUnwinding();
}
return initialized;
}
void DumpWithoutCrashCrashpad(jstring message, jboolean set_main_thread_as_faulting_thread) {
crashpad::NativeCPUContext context;
crashpad::CaptureContext(&context);
// set dump message for single report
crashpad::SimpleStringDictionary *annotations = NULL;
if (message != NULL || set_main_thread_as_faulting_thread == true) {
JNIEnv *env = GetJniEnv();
if (env == nullptr) {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Cannot initialize JNIEnv");
return;
}
const std::lock_guard<std::mutex> lock(attribute_synchronization);
crashpad::CrashpadInfo *info = crashpad::CrashpadInfo::GetCrashpadInfo();
annotations = info->simple_annotations();
if (!annotations) {
annotations = new crashpad::SimpleStringDictionary();
info->set_simple_annotations(annotations);
}
if (set_main_thread_as_faulting_thread == true) {
annotations->SetKeyValue("_mod_faulting_tid", thread_id);
}
if (message != NULL) {
// user can't override error.message - exception message that Crashpad/crash-reporting tool
// will set to tell user about error message. This code will set error.message only for single
// report and after creating a dump, method will clean up this attribute.
jboolean isCopy;
const char *rawMessage = env->GetStringUTFChars(message, &isCopy);
annotations->SetKeyValue("error.message", rawMessage);
env->ReleaseStringUTFChars(message, rawMessage);
}
}
client->DumpWithoutCrash(&context);
if (annotations != NULL) {
annotations->RemoveKey("error.message");
}
}
void AddAttributeCrashpad(jstring key, jstring value) {
if (initialized == false) {
__android_log_print(ANDROID_LOG_WARN, "Backtrace-Android",
"Crashpad integration isn't available. Please initialize the Crashpad integration first.");
return;
}
JNIEnv *env = GetJniEnv();
if (env == nullptr) {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Cannot initialize JNIEnv");
return;
}
const std::lock_guard<std::mutex> lock(attribute_synchronization);
crashpad::CrashpadInfo *info = crashpad::CrashpadInfo::GetCrashpadInfo();
crashpad::SimpleStringDictionary *annotations = info->simple_annotations();
if (!annotations) {
annotations = new crashpad::SimpleStringDictionary();
info->set_simple_annotations(annotations);
}
jboolean isCopy;
const char *crashpadKey = env->GetStringUTFChars(key, &isCopy);
const char *crashpadValue = env->GetStringUTFChars(value, &isCopy);
if (crashpadKey && crashpadValue)
annotations->SetKeyValue(crashpadKey, crashpadValue);
env->ReleaseStringUTFChars(key, crashpadKey);
env->ReleaseStringUTFChars(value, crashpadValue);
}
void DisableCrashpad() {
if (database == nullptr) {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Crashpad database is null, this should not happen");
return;
}
// Disable automated uploads.
database->GetSettings()->SetUploadsEnabled(false);
disabled = true;
}
void ReEnableCrashpad() {
// Re-enable uploads if disabled
if (disabled) {
if (database == nullptr) {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android",
"Crashpad database is null, this should not happen");
return;
}
database->GetSettings()->SetUploadsEnabled(true);
disabled = false;
}
}
bool EnableCrashLoopDetectionCrashpad() {
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Inside EnableCrashLoopDetectionCrashpad");
if (client != nullptr) {
return client->EnableCrashLoopDetection();
} else {
return false;
}
}
bool IsSafeModeRequiredCrashpad() {
return consecutive_crashes_count >= 5;
}
int ConsecutiveCrashesCountCrashpad() {
return consecutive_crashes_count;
}