Skip to content

Commit b1920a3

Browse files
committed
backtrace-android: add support for dynamic attachments with native crashes
Previously, crashpad, used for handling native crashes, did not support additional file attachments to the crash report context after initialization. Recent changes, however, have been made to the Backtrace fork of crashpad to allow this. The crashpad submodule reference has been updated in this commit to point to this change. This commit adds support for adding file attachments after initialization to native crash reports. The changes follow the existing pattern for adding attributes dynamically which forward the request to the crashpad backend in response to a call to `addAttachment` on the Backtrace client.
1 parent 48cfb33 commit b1920a3

9 files changed

Lines changed: 70 additions & 3 deletions

File tree

backtrace-library/src/main/cpp/backends/backend.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ void AddAttribute(jstring key, jstring value) {
108108
#endif
109109
}
110110

111+
void AddAttachment(jstring attachment) {
112+
#ifdef CRASHPAD_BACKEND
113+
AddAttachmentCrashpad(attachment);
114+
#else
115+
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android",
116+
"AddAttachment not supported on this backend");
117+
#endif
118+
}
119+
111120
void Disable() {
112121
#ifdef CRASHPAD_BACKEND
113122
DisableCrashpad();
@@ -116,4 +125,4 @@ void Disable() {
116125
"Disable not supported on this backend");
117126
#endif
118127
}
119-
}
128+
}

backtrace-library/src/main/cpp/backends/crashpad-backend.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,29 @@ void AddAttributeCrashpad(jstring key, jstring value) {
390390
env->ReleaseStringUTFChars(value, crashpadValue);
391391
}
392392

393+
void AddAttachmentCrashpad(jstring jattachment) {
394+
if (initialized == false || client == nullptr) {
395+
__android_log_print(ANDROID_LOG_WARN, "Backtrace-Android",
396+
"Crashpad integration isn't available. Please initialize the Crashpad integration first.");
397+
return;
398+
}
399+
JNIEnv *env = GetJniEnv();
400+
if (env == nullptr) {
401+
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android", "Unable to obtain JNI environment.");
402+
return;
403+
}
404+
405+
if (jattachment) {
406+
jboolean isCopy;
407+
const char *attachment = env->GetStringUTFChars(jattachment, &isCopy);
408+
409+
if (attachment != nullptr) {
410+
client->AddAttachment(attachment);
411+
env->ReleaseStringUTFChars(jattachment, attachment);
412+
}
413+
}
414+
}
415+
393416
void DisableCrashpad() {
394417
if (database == nullptr) {
395418
__android_log_print(ANDROID_LOG_ERROR, "Backtrace-Android",

backtrace-library/src/main/cpp/backtrace-native.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ Java_backtraceio_library_BacktraceDatabase_addAttribute(JNIEnv *env, jobject thi
161161
AddAttribute(name, value);
162162
}
163163

164+
JNIEXPORT void JNICALL
165+
Java_backtraceio_library_BacktraceDatabase_addAttachment(JNIEnv *env, jobject thiz,
166+
jstring jattachment) {
167+
AddAttachment(jattachment);
168+
}
169+
164170
JNIEXPORT void JNICALL
165171
Java_backtraceio_library_base_BacktraceBase_dumpWithoutCrash__Ljava_lang_String_2(JNIEnv *env,
166172
jobject thiz,
@@ -186,4 +192,4 @@ Java_backtraceio_library_nativeCalls_BacktraceCrashHandler_handleCrash(JNIEnv *e
186192
jobjectArray args) {
187193
return CaptureCrash(args);
188194
}
189-
}
195+
}

backtrace-library/src/main/cpp/include/backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bool CaptureCrash(jobjectArray args);
2727
void DumpWithoutCrash(jstring message, jboolean set_main_thread_as_faulting_thread);
2828

2929
void AddAttribute(jstring key, jstring value);
30+
void AddAttachment(jstring jattachment);
3031

3132
void Disable();
3233
}

backtrace-library/src/main/cpp/include/crashpad-backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ bool CaptureCrashCrashpad(jobjectArray args);
3333
void DumpWithoutCrashCrashpad(jstring message, jboolean set_main_thread_as_faulting_thread);
3434

3535
void AddAttributeCrashpad(jstring key, jstring value);
36+
void AddAttachmentCrashpad(jstring jattachment);
3637

3738
void DisableCrashpad();
3839

backtrace-library/src/main/java/backtraceio/library/BacktraceDatabase.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ public class BacktraceDatabase implements Database {
6464
*/
6565
public native void addAttribute(String name, String value);
6666

67+
/**
68+
* Add a file attachment to native reports.
69+
*
70+
* @param attachmentPath the file path to attach to native reports
71+
*/
72+
public native void addAttachment(String attachmentPath);
73+
6774
/**
6875
* Disable Backtrace-native integration
6976
*/
@@ -255,6 +262,14 @@ public Boolean addNativeAttribute(String key, Object value) {
255262
return true;
256263
}
257264

265+
public Boolean addNativeAttachment(String attachmentPath) {
266+
if (!_enabledNativeIntegration || attachmentPath == null) {
267+
return false;
268+
}
269+
addAttachment(attachmentPath);
270+
return true;
271+
}
272+
258273
public void start() {
259274
if (databaseSettings == null) {
260275
return;

backtrace-library/src/main/java/backtraceio/library/base/BacktraceBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ public void enableProguard() {
336336
*/
337337
public void addAttachment(String attachmentPath) {
338338
this.attachments.add(attachmentPath);
339+
340+
if (database != null) {
341+
database.addNativeAttachment(attachmentPath);
342+
}
339343
}
340344

341345
/**

backtrace-library/src/main/java/backtraceio/library/interfaces/Database.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,12 @@ Boolean setupNativeIntegration(
132132
* @return true, if attribute was added to the native report. Otherwise false.
133133
*/
134134
Boolean addNativeAttribute(String key, Object value);
135+
136+
/**
137+
* If the native integration is enabled then adds a file attachment to be included
138+
* with native crash reports.
139+
* @param attachmentPath the file path to attach to native reports.
140+
* @return whether the attachment was added to the native report or not.
141+
*/
142+
Boolean addNativeAttachment(String attachmentPath);
135143
}

0 commit comments

Comments
 (0)