Skip to content

Instantly share code, notes, and snippets.

@gnail737
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save gnail737/a363b3c6b45e99881afd to your computer and use it in GitHub Desktop.

Select an option

Save gnail737/a363b3c6b45e99881afd to your computer and use it in GitHub Desktop.
How to use GDB to debug NDK code in windows without cygwin/mingw
1) Install latest NDK and SDK
2) Unzip ndk archive to %NDK_ROOT%, sdk archive to %SDK_ROOT%
3) Setup your windows PATH environment variable to contain %NDK_ROOT%, and SDK path that contains adb command(normally located at %SDK_ROOT%/platform-tools
Because I don't want to add new directories to System Path, so used following two one-time session command to add PATH:
set PATH=%PATH%;C:\Users\liawei\Android Dev\adt-bundle-windows-x86_64-20130717\sdk\platform-tools
set NDK_ROOT=C:\Users\liawei\android-ndk-r9
4) Change default script file ndk-gdb-py.cmd to use the correct path on your system:
A sample changed file as below:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@echo off
rem This is a Windows cmd.exe script used to invoke the NDK-specific Python executable
set NDK_ROOT=C:\Users\gnail737\android-ndk-r9
set NDK_MAKE=%NDK_ROOT%/prebuilt/windows-x86_64/bin/make.exe
rem Check if %NDK_ROOT% contains space
goto :L
:FOO
if "%2"=="" goto:EOF
echo ERROR: NDK path cannot contain space!
exit /b 1
:L
call :FOO %NDK_ROOT%
if ERRORLEVEL 1 exit /b 1
%NDK_ROOT%\prebuilt\windows-x86_64\bin\python.exe -u %NDK_ROOT%\ndk-gdb.py %*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Note the third line and fourth line are changed to match the absolute directory pointing to NDK-GDB and prebuild make/python executable. Otherwise when you run script it will complain "Directory not found".
5)Before use debugging, add this line to the <Application/> tag in your AndroidManifest.xml:
android:debuggable=”true”
Rebuild your shared lib binary with ndk-build, you will see gdb server messages in console output.
6)You need to launch application first before running gdb in the project's root directory (parent directory of JNI), gdb will automatically detect the pid to attach. Or you can use command line
%NDK_ROOT%\ndk-gdb-py.cmd --nowait --start #the start command option will start debugging application session for you.
Happy debugging~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment