Debugging native code in android app using the android ndk gdb.

This is a small tutorial which combines the information that got over Google for making use of the "ndk-gdb" present in the Android NDK

AVD: 2.3.3 API Level 10
NDK: r6b (or) r7

Prerequisites:
Basic knowledge of Android ndk
Knowledge of GDB


Steps for using the android gdb:
1) Go to the build_binary.mk file present in the location ...\android-ndk\build\core\build-binary.mk and do the following changes (present in the last lines of the build-binary.mk file)


#Commented - This is the original code
#    $(hide) $(call cmd-strip, $(PRIVATE_DST))
#Added
ifneq ($(APP_OPTIM),debug)
    @ echo "Stripping the library for the release mode....."
    $(hide) $(call cmd-strip, $(PRIVATE_DST))
endif


2) Build the library with ndk-build NDK_DEBUG=1(Build command) and LOCAL_EXPORT_CFLAGS += -g ( in Android.mk of your application) and APP_OPTIM=debug (of your Application.mk). And also set this flag in your AndroidManifest.xml file "android:debuggable="true"" and sdk version in the same AndriodManifest.xml file as
"uses-sdk android:minSdkVersion="10" android:targetSdkVersion="9" "

3) Open the command prompt in the root folder (parent of jni (or) libs (or) obj)

Ex: CallbackJava here

4) Now if you are in Linux machine it's fine, else go to shell prompt by making use of the cygwin in windows machine. and you will get a prompt like this

sh-4.1$

Tip: It will be better if "bin" folder of the cygwin is added to the "path" environmental variable in windows.

5) Now run the command in shell prompt like below

After executing the above command make sure that you will get the "(gdb)" prompt and not the following error 
    ERROR: Could not extract package's data directory. Are you sure that
           your installed application is debuggable?
    sh-4.1$ 


If you are getting the error, either device is not debuggable or your package is not built with debugging information. Please make sure step 2.

6) Once you get the “(gdb)” prompt set the break point  Ex: break : and then type “c” or “continue” and hit enter. Application starts running and the moment it hits the break point you will be able to see in the command prompt. And debug as usual like gdb.


Once it hits the break point like showed in the above image you can make use of gdp commands "step, next, list....." and so on and start debugging.

Observations:
This ndk-gdb is bit slow. Sorry lying its tooooo slow :D :) :P

Happy debugging.....



FAQ:
1) If you are getting an error message like
"No symbol table is loaded.  Use the "file" command."
It can be due to creation of the gdb.setup problem.
Try to convert the file to unix format by making use of the dos2unix command.
Ex: dos2unix gdb.setup

2) Code changes for the Android NDK - 8c for build_binary.mk file in step 1.

$(LOCAL_INSTALLED): $(LOCAL_BUILT_MODULE) clean-installed-binaries
@$(HOST_ECHO) "Install        : $(PRIVATE_NAME) => $(call pretty-dir,$(PRIVATE_DST))"
$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
#Suman - Start
#Commented
# $(hide) $(PRIVATE_STRIP_CMD)
#Added
@ echo "APP_OPTIM is............................. $(APP_OPTIM)"
ifneq ($(APP_OPTIM), debug)
@ echo "Stripping the library for the release mode....."
$(hide) $(PRIVATE_STRIP_CMD)
endif
#Suman - End

$(call generate-dir,$(NDK_APP_DST_DIR))
$(LOCAL_INSTALLED): $(NDK_APP_DST_DIR)




9 comments:

madan said...

what does the -g option mean ? can you provide more info on it please ?

Suman said...

-g is used for adding the symbols to the object files which will be used by the GDB debugger while debugging.

Suman said...

Good guide lines for debugging in Eclipse.....

Click Here

hart said...

Is there a specific reason you have min version as 10 and target as 9?

In reference to:
"uses-sdk android:minSdkVersion="10" android:targetSdkVersion="9"

Suman said...

ndk-gdb is supported from the gingerbread versions of target models (Mobile, tablet.....). Application can be older version as well. I just demonstrated with those versions, no specific reason.

Unknown said...

--strip this is my problem. solved !!
thank you

Suman said...

On Windows ARM D5 Debugger wont work for the 64bit Java & 64bit Eclipse.

You need 32 bit versions for the same.

Its me said...

i am using eclipse gallileo and running it on windows xp please tell how to do it on window platform

Suman said...

Follow the link in the above posts. Keeping it here again for your convenience

http://kernel.ozandroid.info/?p=83