Android NDK + GDB

mjollneer picture mjollneer · Apr 29, 2013 · Viewed 7k times · Source

Help me please to use ndk-gdb!

I searched through StackOverflow and other internets =) but still doing something wrong.

Configuration: MacOSX 64 + latest SDK + latest NDK + latest IDEA + Nexus 7 + huge C++ project.

I did all the common arrangement:

  • -g -ggdb -O0 to LOCAL_CFLAGS (also tried just -g)
  • APP_OPTIM := debug
  • debuggable=«true» in manifest
  • ndk-build NDK_DEBUG=1

I get expected gdb.setup and gdbserver files, BUT nm command give me zero output on my *.so files.

I run ndk-gdb in project's root folder and get working GDB - e.g. i can pause app and resume it, can get ASM code on adress and so on. Even when i set brakepoints with break Class::method, gdb tell me right file name and line number.

But breakpoints does not hit in 99%. Backstack is always obviously wrong (wrong method names). Looks like all the symbolic names and addresses mapped wrong.

What have I missed?

UPD. gdb output at start with two commands info sharedlibrary and C.

:ndk-gdb

/android-ndk-macosx/build/core/add-application.mk:128: Android NDK: WARNING: APP_PLATFORM android-14 is larger than >android:minSdkVersion 8 in ./AndroidManifest.xml
/android-ndk-macosx/build/core/add-application.mk:128: Android NDK: WARNING: APP_PLATFORM android-14 is larger than >android:minSdkVersion 8 in ./AndroidManifest.xml
/android-ndk-macosx/build/core/add-application.mk:128: Android NDK: WARNING: APP_PLATFORM android-14 is larger than >android:minSdkVersion 8 in ./AndroidManifest.xml
/android-ndk-macosx/build/core/add-application.mk:128: Android NDK: WARNING: APP_PLATFORM android-14 is larger than >android:minSdkVersion 8 in ./AndroidManifest.xml

GNU gdb (GDB) 7.3.1-gg2
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-apple-darwin --target=arm-linux-android".
For bug reporting instructions, please see:
<http://source.android.com/source/report-bugs.html>.

warning: .dynamic section for "/Users/<...>/android/obj/local/armeabi/lib1.so" is not at the expected address (wrong library or version >mismatch?)
warning: Could not load shared library symbols for 73 libraries, e.g. libstdc++.so.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
warning: Breakpoint address adjusted from 0x400aca53 to 0x400aca52.
0x401a7ee4 in epoll_wait () from /Users/<...>/android/obj/local/armeabi/libc.so
(gdb) info sharedlibrary
From        To          Syms Read   Shared Object Library
0x400aa220  0x400b2978  Yes (*)     /Users/<...>/android/obj/local/armeabi/linker
0x4019c860  0x401cc184  Yes (*)     /Users/<...>/android/obj/local/armeabi/libc.so
                        No          libstdc++.so
                        No          libm.so

<...dozens of system libs with "no"...>

                        No          libjnigraphics.so
0x6749c160  0x67527844  Yes (*)     /Users/<...>/android/obj/local/armeabi/lib1.so
0x65c487f8  0x65c6634c  Yes (*)     /Users/<...>/android/obj/local/armeabi/lib2.so
0x693e62e8  0x699dcd90  Yes         /Users/<...>/android/obj/local/armeabi/lib3.so
(*): Shared library is missing debugging information.
(gdb) C
Continuing.>

You can see that

  • lib3.so (main lib) is loaded most correctly (or not?)
  • breakpoint address adjusted (what does it mean?)

Answer

mjollneer picture mjollneer · May 13, 2013

Ok, finally I made it work. The point was in APP_ABI line in Application.mk. There was two ABI's and somehow this confused GDB. Now it works with one abi (I chose armeabi-v7a).

And something, that i also would like to notice

  • there is nothing you really need to do, to make GDB work. Just dont disturb him =)
  • every variable (cflag or manifest tag or etc.) has default value suitable for debugging, but thats no matter and i'll recommend you to explicitly set APP_OPTIM := debug and APP_CFLAG := -g -ggdb -O0 in Application.mk; android:debuggable="true" in manifest and NDK_DEBUG=1 for ndk-build. I have not figured out if they somehow helps GDB to work better. Some of them are duplicates of each other. And certainly none of them will not do worse.
  • It is normal, when Breakpoint address adjusted
  • NM command. I also did not understand it well, but it keeps giving me zero output on every file that I guessed to check. What is really important - output of info sharedlibrary command in working GDB. Check that for your library has symbols (like my lib3.so in example above). This is necessary, but not sufficient.