views:

4735

answers:

3

I'm moving a project to the new Android Native Development Kit (i.e. JNI) and I'd like to catch SIGSEGV, should it occur (possibly also SIGILL, SIGABRT, SIGFPE) in order to present a nice crash reporting dialog, instead of (or before) what currently happens: the immediate unceremonious death of the process and possibly some attempt by the OS to restart it. (Edit: The JVM/Dalvik VM catches the signal and logs a stack trace and other useful information; I just want to offer the user the option to email that info to me really.)

The situation is: a large body of C code which I didn't write does most of the work in this application (all the game logic) and although it's well-tested on numerous other platforms, it's entirely possible that I, in my Android port, will feed it garbage and cause a crash in native code, so I want the crash dumps (both native and Java) that currently show up in the Android log (I guess it would be stderr in a non-Android situation). I'm free to modify both C and Java code arbitrarily, although the callbacks (both going in and coming out of JNI) number about 40 and obviously, bonus points for small diffs.

I've heard of the signal chaining library in J2SE, libjsig.so, and if I could safely install a signal handler like that on Android, that would solve the catching part of my question, but I see no such library for Android/Dalvik.

+2  A: 

In my limited experience (non-Android), SIGSEGV in JNI code will generally crash the JVM before control is returned to your Java code. I vaguely recall hearing about some non-Sun JVM which lets you catch SIGSEGV, but AFAICR you can't expect to be able to do so.

You can try to catch them in C (see sigaction(2)), although you can do very little after a SIGSEGV (or SIGFPE or SIGILL) handler as the ongoing behaviour of a process is officially undefined.

mas90
Well, behaviour is undefined after "ignor[ing] a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by kill(2) or raise(3)", but not necessarily during catching such a signal. Current plan is to try a C signal handler that calls back to Java and, somehow, terminates the thread without terminating the process. This may or may not be possible. :-)
Chris Boyle
C backtrace instructions: http://stackoverflow.com/questions/76822/how-to-generate-a-stacktrace-when-my-c-app-crashes-using-gcc-compiler/77281#77281
Chris Boyle
...except I can't use backtrace(), because Android doesn't use glibc, it uses Bionic. :-( Something involving `_Unwind_Backtrace` from `unwind.h` will be needed instead.
Chris Boyle
A: 

You could try to start your Java app (i.e. the Java VM) through some kind of wrapper script. That could then check if the app exited abnormally, and do the error reporting. That would allow you to cleanly catch all kinds of abnormal exits, be they SIGSEGV, SIGKILL or whatever.

Might be hard to determine the signal that killed the app...

sleske
If I weren't on Android, yes. Unfortunately Android doesn't allow such things. The OS starts the VM for you and the only supported way to run native code is in a JNI library.
Chris Boyle
+2  A: 

I actually got a signal handler working without doing anything too exotic, and have released code using it, which you can see on github. Here's how:

  1. Use sigaction() to catch the signals and store the old handlers. (android.c:570)
  2. Time passes, a segfault happens.
  3. In the signal handler, call up to JNI one last time and then call the old handler. (android.c:528)
  4. In that JNI call, log any useful debugging info, and call startActivity() on an activity that is flagged as needing to be in its own process. (SGTPuzzles.java:962, AndroidManifest.xml:28)
  5. When you come back from Java and call that old handler, the Android framework will connect to debuggerd to log a nice native trace for you, and then the process will die. (debugger.c, debuggerd.c)
  6. Meanwhile, your crash-handling activity is starting up. Really you should pass it the PID so it can wait for step 5 to complete; I don't do this. Here you apologise to the user and ask if you can send a log. If so, gather the output of logcat -d -v threadtime and launch an ACTION_SEND with recipient, subject and body filled in. The user will have to press Send. (CrashHandler.java, SGTPuzzles.java:462, strings.xml:41
  7. Watch out for logcat failing or taking more than a few seconds. I have encountered one device, the T-Mobile Pulse / Huawei U8220, where logcat immediately goes into the T (traced) state and hangs. (CrashHandler.java:70, strings.xml:51)

In a non-Android situation, some of this would be different. You'd need to gather your own native trace, see this other question, depending on what sort of libc you have. You'd need to handle dumping that trace, launching your separate crash-handler process, and sending the email in some appropriate ways for your platform, but I imagine the general approach should still work.

Chris Boyle
Ideally you'd check to see if the crash occurred in your library. If it occurred somewhere else (say, inside the VM), your JNI calls from the signal handler could confuse things rather badly. It's not the end of the world, since you're mid-crash anyway, but it might make diagnosis of a VM crash more difficult (or cause a bizarre VM crash that ends up in an Android bug report and baffles everyone).
fadden