Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Catch C++ exceptions (and other types of exceptions) and forward to logcat #562

Closed
ljbade opened this issue Nov 7, 2014 · 9 comments
Closed
Labels
Android Mapbox Maps SDK for Android
Milestone

Comments

@ljbade
Copy link
Contributor

ljbade commented Nov 7, 2014

Currently if a C++, CPU exception, or bad memory read occurs in the C++ code the Android log (logcat), only outputs a generic SIGxxx message saying our process was terminated.

This is useless for tracing errors in the C++ code. We want a full stack trace instead that highlights the offending line of code, similar to what happens if an exception occurs in the Java code.

Some reading on how to handle this problem:

@ljbade ljbade added the Android Mapbox Maps SDK for Android label Nov 7, 2014
@ljbade ljbade added this to the Android merge milestone Nov 13, 2014
@ljbade
Copy link
Contributor Author

ljbade commented Nov 16, 2014

Hmm have encountered this problem: xroche/coffeecatch#13

Will use the patch for the moment.

Also need to watch xroche/coffeecatch#11 as we use multiple threads.

@ljbade
Copy link
Contributor Author

ljbade commented Nov 16, 2014

OK got all the JNI functions wrapped.

@ljbade
Copy link
Contributor Author

ljbade commented Nov 16, 2014

Done in 2aed807

@ljbade ljbade closed this as completed Nov 16, 2014
@ljbade
Copy link
Contributor Author

ljbade commented Nov 18, 2014

Encountered crash where we are not catching C++ exceptions.

Need to add.

@ljbade ljbade reopened this Nov 18, 2014
@ljbade
Copy link
Contributor Author

ljbade commented Nov 22, 2014

Hmm, I don't think we can much about catching exceptions thrown in other libuv threads without really hacking up the code with #ifdefs for Android.

If we try to catch the errors then log and rethrow, we lose the stack trace pointing to the line of code that threw.

So I suggest for every C++ exception we throw (or subclass from std::exception) we also log an error to Log.

But what do we do about places where stdlib throws an exception? Can we hook all exception throwing? Have seen some refs to __cxa_throw in http://stackoverflow.com/questions/11665829/how-can-i-print-stack-trace-for-caught-exceptions-in-c-code-injection-in-c

@jfirebaugh thoughts?

@jfirebaugh
Copy link
Contributor

At a language and runtime level, C++ exceptions don't really ever have a stack trace. Particular operating systems print a stack trace if the program crashes -- which is in essence what happens when an exception goes uncaught -- but there's no language level or portable way to get a stack trace from an exception. That's just par for the course in C++ development, and so I'm not concerned about "losing" a stack trace by catching an exception and translating it to another error signaling mechanism.

@ljbade
Copy link
Contributor Author

ljbade commented Nov 30, 2014

I tried various ways to print a log error when there is an unhandled exception but it always killed the program before it the log was flushed.

I have decided to just let exceptions crash, but will make sure all places we throw an exception, an error is logged first to aid debugging.

@ljbade ljbade closed this as completed Nov 30, 2014
@ljbade ljbade reopened this Jan 13, 2015
@ljbade
Copy link
Contributor Author

ljbade commented Jan 13, 2015

I think I am going to remove coffeecatch. it just doesn't seem to work very well and has yet to help me with a crash.

The default Android crash log seems good enough when combined with the addr2line tool.

@ljbade
Copy link
Contributor Author

ljbade commented Jan 13, 2015

Done.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android
Projects
None yet
Development

No branches or pull requests

2 participants