Debugging Android NDK C/C++ code in Eclipse - breakpoints are not hit

后端 未结 6 622
遥遥无期
遥遥无期 2020-12-01 14:59

I downloaded Android SDK Bundle for Linux and Android NDK. ADT was installed, I installed CDT.

I created a Android project and added native support (jni). Then I wro

相关标签:
6条回答
  • 2020-12-01 15:41

    Your app executes the JNI function very early, so that the debugger is not ready yet. Unfortunately, it takes a while for gdb to establish the remote connection, see http://visualgdb.com/documentation/appstartup

    Instead of fighting the windmills, add a button to your activity, and call the same native method onClick() of that button - it will be easier to catch the breakpoint.

    BTW, the warning about 95 libraries is perfectly normal. These are the system libraries which you don't want to debug, and don't have sources for.

    0 讨论(0)
  • 2020-12-01 15:42

    You can use DS-5 CE Android Debug tool provided by ARM as a plugin to your eclipse. It works really well and provides a very good and easy UI for debugging. From my personal experience it is lots better than the traditional way of debugging the ndk app.

    Please refer the below link which will provide you with the details of how to use the DS-5 debugger:

    https://developer.arm.com/products/software-development-tools/ds-5-development-studio/resources/tutorials/android-native-app-debug-tutorial

    0 讨论(0)
  • 2020-12-01 15:44

    I had this issue, and the only thing that worked for me was putting

    Thread.sleep(2000);
    

    before the JNI library is loaded. This gave enough time for the debugger to attach before the call to System.loadLibrary crashed the app. Helped me to locate the problem C++ code.

    0 讨论(0)
  • 2020-12-01 16:02

    The trick I use is to put a usleep call as the very first native line in my debug code.

    This makes your thread sleep and gives the debugger a chance to be ready for you.

    #include <unistd.h>
    
    .
    .
    .
    
    #ifndef NDEBUG
    usleep(5000 * 1000);
    #endif
    
    0 讨论(0)
  • 2020-12-01 16:03

    After struggling a lot to debug on Eclipse this is my recipe:

    Do the usual steps:

    • Add android.os.Debug.waitForDebugger(); before loading your native library. This might help.
    • Add APP_OPTIM := debug in Application.mk
    • Build with ndk-build NDK_DEBUG=1

    Then, what I found different:

    • Open a console and run:

    adb pull /system/bin/linker <your_project_base_dir>/obj/local/armeabi/linker

    Depending on your device you might have to write armeabi or armeabi-v7a. You only need to do it once (I noticed that running ndk-gdb manually was doing that. Executing that command manually the breakpoints started to work)

    • Finally, to debug use the menu "Run -> Debug As -> Android Native Application"
    0 讨论(0)
  • 2020-12-01 16:03

    consider adding:

    android.os.Debug.waitForDebugger();

    before your native call, this makes your app wait until the debugger attaches, could help you avoid sleeping / using a button.

    0 讨论(0)
提交回复
热议问题