14

I've got a segfault but I have absolutely no idea how to locate it.

Tips? enter image description here

Brian D
  • 9,863
  • 18
  • 61
  • 96
  • I tried following this http://stackoverflow.com/questions/3902066/how-to-call-java-methods-from-c-in-jni/3902495#3902495 but I'm segfaulting before I even get a chance to make a call to logcat :/ – Brian D Jan 30 '11 at 23:01
  • Private variables cannot be passed to the native function. D'oh. – Brian D Jan 30 '11 at 23:20
  • Easy & not so quick way: Try isolating the calls in your native layer in a step by step manner and check which call is going wrong. Also, worth trying is : /docs/NDK-GDB.HTML — describes how to use the native code debugger – TheCottonSilk Jan 31 '11 at 07:14

2 Answers2

10

You can get the location of the C function that caused the crash using the Android NDK Stacktrace Analyzer.

The steps are on the wiki, but basically you need to get the stack trace from logcat into a file (adb logcat > mycrash.log), then dump your library to a text file, then run the program on the two of them. Here's the shell script I use to do the lot:

#!/bin/sh
if test $# -lt 2 ; then
    echo "Extract readable stack trace from Android logcat crash"
    echo "Usage $0 lib.so crash.log"
    exit 1
fi
of=$(mktemp)
echo "Disassemble $1"
~/tools/android-ndk-r5/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-objdump -S $1 > $of
echo "Parse stack trace in $2"
~/bin/parse_stack.py $of $2

Change the paths to the Android NDK and parse_stack.py file as you need.

richq
  • 55,548
  • 20
  • 150
  • 144
  • So glad to finally find something like this. I was tired of going through the gdbserver rigmarole just to get a trace from a segfault. – Jason LeBrun Jan 31 '11 at 20:30
  • I just submitted a patch to the project that shows the code segment where the crash occurred as well: http://code.google.com/p/android-ndk-stacktrace-analyzer/issues/detail?id=1 – Jason LeBrun Jan 31 '11 at 21:44
  • Are you following me Jason LeBrun? Haha. Thanks richq. That's awesome. – Brian D Feb 01 '11 at 01:18
6

How to Effectively Debug Android JNI C/C++ Code in Eclipse:

  1. Setup your project to be a mixed Java, C, and C++ project:

    http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/

  2. Setup your project to enable debugging:

    http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-debugging/#more-23

Note: The author of the site used Eclipse(Galileo) on Ubuntu. I found some differences when using Eclipse(Helios) on MacOS (especially with the debug setup). Once things are set up, Eclipse works very well as an IDE for JNI development.

Community
  • 1
  • 1
Chris
  • 3,577
  • 1
  • 21
  • 15
  • 4
    Okay can they make that more difficult to setup? I still can't get it to work for debugging. – JPM Oct 26 '11 at 19:59