title: Getting a backtrace on Android from native threads?
body: <p>I am working on a codebase that is not originally my own (the open source ARDrone library by Parrot). I have stripped out unnecessary threads and have isolated a crash to a particular thread, namely the one in charge of streaming video packets from the drone [a Parrot ARDrone 2.0].</p>
  
  <p>I have changed the drone from TCP video output to UDP on a different port. When I load up the android application [AR.Freeflight 2], video streams smoothly for a while, but the application universally crashes with a fatal SIGBUS somewhere from execution in the very long and complex video pipeline. It takes anywhere from 20 seconds to 15 minutes to crash.</p>
  
  <p>I have a signal handler set up on the relevant thread, which can successfully capture the signal. I went through the process of attaching the video input thread to the JVM, and I am able to use a cached global reference to find a Java class with a static method that I made for printing the current thread's callstack (I have tested this both with throwing a new RuntimeException and with Thread.dumpStack()). The reason I went through this arduous process is because the version of bionic that is part of the NDK doesn't have a native backtrace() function that I should be able to call directly from the signal handler; it appears that this method has been shown elsewhere on stackoverflow to work perfectly well to trace the entire C callstack <em>if you are still in a Java-spawned thread</em>. However, in my application, the <em>only</em> call that shows up as occurring before my static void trace-printing function is</p>
  
  <pre><code>dalvik.system.NativeStart.run(Native Method)
  </code></pre>
  
  <p>Thus, I am left without knowledge of how to get the call stack for my android application to find the true location of this crash, because Java is treating everything that happened before the first Java invocation of the thread as "one big blob of C". Does anyone have a way around this?</p>
  
  <p>Update: I managed to extract the android-specific sigcontext object from the void* final argument to my signal handler, which means I now have the instruction pointer and stack pointer from before the signal occurred. In that case, does anyone know how I can call _Unwind_Backtrace to backtrace the call stack from there, instead of unwinding from the signal handler itself?</p>