Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit:</strong> From Jelly Bean onwards you can't get the stack trace, because <a href="https://groups.google.com/forum/?fromgroups=#!topic/android-developers/6U4A5irWang" rel="noreferrer"><code>READ_LOGS</code> went away</a>. :-(</p> <p>I actually got a signal handler working without doing anything too exotic, and have released code using it, which you can see <a href="https://github.com/chrisboyle/sgtpuzzles/tree/9561.1" rel="noreferrer">on github</a> (edit: linking to historical release; I removed the crash handler since then). Here's how:</p> <ol> <li>Use <code>sigaction()</code> to catch the signals and store the old handlers. (<a href="http://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android.c#L570" rel="noreferrer">android.c:570</a>)</li> <li>Time passes, a segfault happens.</li> <li>In the signal handler, call up to JNI one last time and then call the old handler. (<a href="http://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android.c#L528" rel="noreferrer">android.c:528</a>)</li> <li>In that JNI call, log any useful debugging info, and call <a href="http://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)" rel="noreferrer"><code>startActivity()</code></a> on an activity that is flagged as needing to be in its own process. (<a href="http://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android/src/name/boyle/chris/sgtpuzzles/SGTPuzzles.java#L962" rel="noreferrer">SGTPuzzles.java:962</a>, <a href="http://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android/AndroidManifest.xml#L28" rel="noreferrer">AndroidManifest.xml:28</a>)</li> <li>When you come back from Java and call that old handler, the Android framework will connect to <code>debuggerd</code> to log a nice native trace for you, and then the process will die. (<a href="http://android.git.kernel.org/?p=platform/bionic.git;a=blob;f=linker/debugger.c" rel="noreferrer">debugger.c</a>, <a href="http://android.git.kernel.org/?p=platform/system/core.git;a=blob;f=debuggerd/debuggerd.c" rel="noreferrer">debuggerd.c</a>)</li> <li>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 <code>logcat -d -v threadtime</code> and launch an <code>ACTION_SEND</code> with recipient, subject and body filled in. The user will have to press Send. (<a href="http://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android/src/name/boyle/chris/sgtpuzzles/CrashHandler.java" rel="noreferrer">CrashHandler.java</a>, <a href="http://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android/src/name/boyle/chris/sgtpuzzles/SGTPuzzles.java#L462" rel="noreferrer">SGTPuzzles.java:462</a>, <a href="http://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android/res/values/strings.xml#L41" rel="noreferrer">strings.xml:41</a></li> <li>Watch out for <code>logcat</code> 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 <code>T</code> (traced) state and hangs. (<a href="http://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android/src/name/boyle/chris/sgtpuzzles/CrashHandler.java#L70" rel="noreferrer">CrashHandler.java:70</a>, <a href="http://github.com/chrisboyle/sgtpuzzles/blob/8750.5/android/res/values/strings.xml#L51" rel="noreferrer">strings.xml:51</a>)</li> </ol> <p>In a non-Android situation, some of this would be different. You'd need to gather your own native trace, see <a href="https://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes">this other question</a>, 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload