Catch all cpp exceptions from app

Catch all cpp exceptions from app
0.0 0

#1

Where I can place global catch(…) in android cocos2dx project in order to able to collect all the cpp exceptions in my project?

I have a cocos2dx project for presentation display for android.
When my cpp code crashed I want to be able to catch all the exceptions and at least shutdown gracefuly.
(And at max - collect the logs and save them \ send to server).

I know that cpp exceptions can’t go to JAVA on android. So I think solution can be, at first, to be able to catch all cpp exceptions. At windows I have main.cpp when I can place global catch(...) like that:

but at android its different.

Maybe there is some good solution for such, I suppose, often problem?


#2

There are prebuilt frameworks you can use:
ACRA - http://www.acra.ch/
Crashlytics - http://try.crashlytics.com/

And a discussion of these and others
http://stackoverflow.com/questions/601503/how-do-i-obtain-crash-data-from-my-android-application?rq=1 .


#3

Thanks for replay,
can crashlitics collect NDK errors? Can it works without android Activity?

…And whats even more important for me, can crashlitics send logs to my own server, rather then crashlitics servers?


#4

I’d suggest putting in some research. Nobody can tell you if a product meets your needs, except you.


#5

Ok then,
first - crashliticas can’t do send logs to another server, and its works highly rely on android Activity, so my original question is still remains active.
The questions is what is the simple way of implementing exception intercepting above cocos app?

I wonder, maybe catch exceptions somewhere at Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp around cocos2d::Director::getInstance()->mainLoop(); call ? Or in that case I will miss something?


#6

did you find an answer to this? I’m looking doing similar too.


#7

Unless you’re trying to get rid of Crashlytics so that you don’t have to include their SDK or rely on them I would recommend just using their SDK and integration.

First off I’d try to catch errors where they occur. Possibly compile w/exceptions and try/catch around code you know may fail. Check for error values or errno after suspect calls.

You’ll prob want to catch Java’s unhandled exceptions and NDK’s signals (SIGSEGV, et al) for all crashes within your or cocos2d-x’s C/C++ code.


LMGTFY (prob repeat some links, but did a quick research session)

You should be able to set up two different handlers for global exceptions. You’ll need to serialize/parse the stack traces and figure out how to send the data you want back to your server.

While stackoverflow isn’t always the best advice here’s one with a handful of options:
https://stackoverflow.com/questions/601503/how-do-i-obtain-crash-data-from-my-android-application.

Hook into Crashlytics
If you want to try and use Crashlytics, but also send to your server (this will only catch Java-side errors, most of which won’t be very useful to you):
https://stackoverflow.com/questions/25203616/crashlytics-android-sdk-custom-uncaughtexceptionhandler .

Android Java Exceptions
https://developer.android.com/reference/java/lang/Thread.html#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) .
https://forums.xamarin.com/discussion/13784/catching-global-exceptions-in-android.

Android NDK (C++) Exceptions
http://blog.httrack.com/blog/2013/08/23/catching-posix-signals-on-android/ .
https://stackoverflow.com/questions/17266379/catch-c-signal-in-android-ndk-and-still-print-crash-dump .
https://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes .
https://stackoverflow.com/questions/8115192/android-ndk-getting-the-backtrace/35585744#35585744.

https://stackoverflow.com/questions/1083154/how-can-i-catch-sigsegv-segmentation-fault-and-get-a-stack-trace-under-jni-on?rq=1 .
Which links to source example: https://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android.c#L570


#8

Thanks for the links! I will try it soon and report how it will go (together with integrate android sturdo build - debug (cocos 15 where are you?))).
Frankly, I doubt that crashlitics can provide good stacktrace for cpp crash (more than just SIGFAULT somewhere but It would be great if it can).


#9

Google has acquired Crashlytics to make it part of their Firebase exception reporting. It is really hard to get C++ stack traces and Crashlytics is very good at unwinding the stack.