39

I'm trying to get the NDK debugger working but with no success so far.

To make sure my debug symbols are present and valid, I use the compiler options -O0 and -g, and the ndk-build parameter NDK_DEBUG=1.

The ndk-gdb script runs with out issues and launches GDB. When do a "sharedlibrary" command, I get this:

Symbols already loaded for /bla/bla/libMySharedLib.so

However when I try breaking execution or e.g. adding a segfault to test, I never get any of the symbols from that library in the call stack. The only symbols I've gotten are from libc, if I break execution while it's waiting for a mutex for instance. Also tried adding breakpoints with no luck. GDB lets me add the breakpoints, and the code runs fine, but the breakpoints are never triggered.

I'm using API level 8 as I need to support Android 2.2 (Froyo).

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Simplex
  • 930
  • 1
  • 9
  • 20

1 Answers1

73

You don't need to use -O0 or -g switches. You need to do one of following:

  1. put android:debuggable="true" to the <application> tag in AndroidManifest.xml file
  2. use NDK_DEBUG=1 after ndk-build
  3. put APP_OPTIM := debug in Application.mk file

Doing anyone of these three things will automatically use -O0 and -g switches.

Can you try running gdb manually, without gdb script? It involves following steps:

  1. pushing gdbserver file to /data/local folder on device
  2. running your application & invoking in adb shell following command gdbserver :5055 --attach PID, where PID is your application process id.
  3. running adb forward tcp:5055 tcp:5055 on host
  4. running arm-linux-androideabi-gdb.exe from your app folder
  5. entering following commands in gdb
  6. set solib-search-path obj/local/armeabi
  7. file obj/local/armeabi/libMySharedLib.so
  8. target remote :5055

And see if you can debug then.

If you want see symbols for other shared libraries your library is using like libc.so, then pull them from device (from /system/lib folder) to your obj/local/armeabi folder.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
Mārtiņš Možeiko
  • 12,733
  • 2
  • 45
  • 45
  • 1
    For an unrelated reason I had to recreate my project from scratch; after doing so and trying to set up everything by the book, including your suggestions Mārtiņš, NDK debugging worked. I'm not sure what the problem was unfortunately. Btw APP_OPTIM had to be set to debug (lowercase) rather than Debug. – Simplex May 17 '12 at 06:49
  • [Re]built with the NDK_DEBUG=1 option **when** calling 'ndk-build'; not after. – soulseekah Nov 20 '12 at 18:14
  • where is the "ndk-build" ? – android developer Jan 22 '13 at 12:29
  • ndk-build is command you invoke to build native code. ndk-build script is provided by Android NDK an is located in root of zip/tar.bz2 archive. – Mārtiņš Možeiko Jan 22 '13 at 18:10
  • ok i've done all 3 steps (part of the ndk-build was done by looking at http://tools.android.com/recent/usingthendkplugin ) . however , when i start to debug , i get some errors , which start with "Unable to launch cygpath. Is Cygwin on the path?] java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, The system cannot find the file specified" . how should i fix it ? does it mean i have to use cygwin ? – android developer Jan 22 '13 at 21:05
  • Step (2) only works if you have rooted device. If your device is not rooted then `adb shell run-as` is needed, see: http://ian-ni-lewis.blogspot.com/2011/05/ndk-debugging-without-root-access.html – Peter Tran Aug 09 '13 at 19:15
  • This answer was very helpful, but the actual problem I had turned out to be something else: The NDK's GDB didn't support debugging threads other than the main thread at the time. It seems this is supported in later NDK versions though. – Simplex Feb 11 '14 at 12:14
  • 2
    How do you "push the gdbserver on device" ? – revolutionary Sep 25 '14 at 09:46
  • Using `adb push ` or copying to e.g. `/sdcard` space and moving it manually through `adb shell` (because files cannot be executed from SD card directly). – Yirkha Oct 27 '15 at 10:12
  • @revolutionary adb push {SDK}/prebuilt/android-arm/gdbserver /data/local – KunMing Xie Aug 30 '16 at 01:27
  • "ERROR: Failed to retrieve application ABI from Android.mk" on Android O this gradle app: https://github.com/googlesamples/android-ndk/tree/2020d9674a6601e8219eed2921f5028beb856a24/hello-gl2/ – Ciro Santilli OurBigBook.com Nov 10 '17 at 17:09