Android C++ exceptions and broken backtrace in tombstones

548 views Asked by At

I'm trying to make exceptions working inside our native library in Android (connected by JNI to the main Java program).
The problem is: if I enable exceptions (which is easy since ndk-r5), I lose ability to deduce crash reason from backtrace. I.e. when using exceptions, crash backtrace contains only 2 lines which is not enough to understand the context.

I've searched a lot but it looks like either nobody encountered this problem, or just didn't consider broken backtrace a problem.
Hoped they'll fix it in new versions, but it's the same since ndk-r5c up to r10b, and Android from 2.3 up to 4.4. Tried gcc and clang.

Though maybe it's something related to our flags or sources or something else.
But it easily reproduces on simplest case:
1. Take original NDK sample, bitmap-plasma
2. in JNI, rename plasma.c to plasma.cpp (so, It's now C++)
3. Add APP_CPPFLAGS += -fexceptions
4. Add APP_STL := gnustl_static (this step isn't really necessary)
5. In sources, place some code that deliberately crashes
6. Compile (recently tried gcc 4.6, ndk-r9 and ndk-r10b) and run

At this point, still have nice-looking backtrace in logs (10-something items). But if:
7. Place try{} block in some place (not even around the crash point)!

After this, backtrace only has 2 lines. Please note, I didn't even tried to throw!

My question is: anybody was able to get both things (exceptions and decent crash backtrace) working in the same build?

Details: I add 2 functions

extern void crash(){
    int a;
    static volatile int* crash = 0;
    *crash = 5;
}

extern void nocrash(){
}

And then, inside of fill_plasma function:

crash();

//    try{
        nocrash();
//    }catch(const std::exception& e){}

So, with this code, backtrace is OK. Uncomment try -> 2 lines of backtrace.

I've tried to analyse the disassembly for possible idea what's different, but the compiler only adds the code at the end of the function, which is never called in this case (no throw).

Why I need the exceptions: there's tons of error-checking code we want to get rid of. This code is in macros which behaves differently on platforms with good exceptions support (so that C++ users of library could write their code without error-checking after almost every line).

Why I need standard backtrace in tombstones: our code is part of much bigger solution. It's tested on other site on the whole. If they got the crash, they collect all standard logs and some description of what they did before crash.
In worst case, no reproduction. No way to use gdb.
I though about using some third-party library for getting backtrace (like google breakhpad), but I'll have to set signal handler (not sure it will go well with other parts of system), and they (e.g. breakpad) dumps binary info to additional location (not sure we could force these remote testers to collect this file).

Any idea to e.g. try some different compiler flags, or maybe I'm terribly wrong with something?

0

There are 0 answers