Skip to content

Commit 080a39b

Browse files
authored
backtrace-android: add support for dynamic attachments with native crashes (#208)
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 080a39b

9 files changed

Lines changed: 70 additions & 15 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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ public BacktraceBase(Context context, BacktraceCredentials credentials) {
102102
* @param context context of current state of the application
103103
* @param credentials Backtrace credentials to access Backtrace API
104104
* @param attachments File attachment paths to consider for reports
105-
* @note Attachments for native crashes must be specified here, and cannot be
106-
* changed during runtime
107105
*/
108106
public BacktraceBase(Context context, BacktraceCredentials credentials, List<String> attachments) {
109107
this(context, credentials, (Database) null, attachments);
@@ -127,8 +125,6 @@ public BacktraceBase(Context context, BacktraceCredentials credentials, Map<Stri
127125
* @param credentials Backtrace credentials to access Backtrace API
128126
* @param attributes additional information about current application
129127
* @param attachments File attachment paths to consider for reports
130-
* @note Attachments for native crashes must be specified here, and cannot be
131-
* changed during runtime
132128
*/
133129
public BacktraceBase(
134130
Context context,
@@ -157,8 +153,6 @@ public BacktraceBase(
157153
* @param credentials Backtrace credentials to access Backtrace API
158154
* @param databaseSettings Backtrace database settings
159155
* @param attachments File attachment paths to consider for reports
160-
* @note Attachments for native crashes must be specified here, and cannot be
161-
* changed during runtime
162156
*/
163157
public BacktraceBase(
164158
Context context,
@@ -192,8 +186,6 @@ public BacktraceBase(
192186
* @param databaseSettings Backtrace database settings
193187
* @param attributes additional information about current application
194188
* @param attachments File attachment paths to consider for reports
195-
* @note Attachments for native crashes must be specified here, and cannot be
196-
* changed during runtime
197189
*/
198190
public BacktraceBase(
199191
Context context,
@@ -222,8 +214,6 @@ public BacktraceBase(Context context, BacktraceCredentials credentials, Database
222214
* @param credentials Backtrace credentials to access Backtrace API
223215
* @param database Backtrace database
224216
* @param attachments File attachment paths to consider for reports
225-
* @note Attachments for native crashes must be specified here, and cannot be
226-
* changed during runtime
227217
*/
228218
public BacktraceBase(
229219
Context context, BacktraceCredentials credentials, Database database, List<String> attachments) {
@@ -251,8 +241,6 @@ public BacktraceBase(
251241
* @param database Backtrace database
252242
* @param attributes additional information about current application
253243
* @param attachments File attachment paths to consider for reports
254-
* @note Attachments for native crashes must be specified here, and cannot be
255-
* changed during runtime
256244
*/
257245
public BacktraceBase(
258246
Context context,
@@ -336,6 +324,10 @@ public void enableProguard() {
336324
*/
337325
public void addAttachment(String attachmentPath) {
338326
this.attachments.add(attachmentPath);
327+
328+
if (database != null) {
329+
database.addNativeAttachment(attachmentPath);
330+
}
339331
}
340332

341333
/**

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)