Android gdb 调试

时间:2023-12-09 15:15:37
[1].终端目录设置到: proj.android
[2].make文件的编译选项加上: -g -gstabs+
[3].执行编译脚本: sh ./build_native.sh NDK_DEBUG=1
[4].将生产的gdbserver push到手机上,找个能写的目录: adb push ./libs/armeabi/gdbserver data/data/tmp
[5].启动app
[6].终端: adb shell
[7].终端: su
[8].终端: cd data/data/tmp (到存放gdbserver的目录)
[9].获得app进程号: ps com.fun.funm
[10].终端: ./gdbserver :1080[端口] —attach 6280[进程号]
[11].切换到proj.android终端
[12].终端: adb forward tcp:1080 tcp:1080
[13].启动: ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gdb
[14].终端: target remote: 1080
[15].打开 ./lib/armeabi/gdb.setup, 将里面的两个命令贴入终端执行
1.配置环境变量,.bash_profile文件
## andorid
export ANDROID_HOME=/Volumes/work/program
export ANDROID_SDK=${ANDROID_HOME}/adt-bundle-mac-x86_64-20140702/sdk
export ANDROID_NDK=${ANDROID_HOME}/android-ndk-r10
export NDK=${ANDROID_NDK}
export PATH=${PATH}:${ANDROID_SDK}/tools
export PATH=${PATH}:${ANDROID_SDK}/platform-tools
export PATH=${PATH}:${ANDROID_NDK}/platforms
export PATH=${PATH}:${ANDROID_NDK}/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin ## ant
export ANT_HOME=${ANDROID_HOME}/apache-ant-1.9.6
export PATH=${PATH}:${ANT_HOME}/bin ## python path
export PYTHONHOME=/usr/lib/python2.7
export PYTHONPATH=.:$PYTHONHOME:$PYTHONHOME/site-packages
export PATH=${PATH}:$PYTHONHOME:$PYTHONPATH ## custom path
export WORKSPACE=/Volumes/work/projecj
export FUNM_PROJ_ROOT=/Volumes/work/project/v3.1.0.1-20160222/funm/proj.android

2.android版本设置,local.properties, project.properties, ant.properties

设置完执行创建ant编译需要的文件build.xml:

android update project -t android-20 -p .

3.mk文件配置:

Application.mk文件
NDK_TOOLCHAIN_VERSION := 4.8
APP_ABI := armeabi
Android.mk文件
优选选项 -o0

4.native代码文件编译

## build debug
$NDK_ROOT/ndk-build NDK_DEBUG=1

5.ant 项目目录下打包apk:

ant debug;ant release;

6.adb 安装apk:

adb install Funm-debug.apk

7.启动apk,开启gdb调试

## gdb attach
$NDK_ROOT/ndk-gdb

然后就可以开始设置断点调试了

继续:c(continue)
设置断点:break test.cpp:100
查看断点:info breakpoints
删除断点:clear / clear test.cpp:100 / delete 1 2 3 
需要工具:
ndk编译及调试:android-ndk-r10
ant打包:apache-ant-1.9.6(适合有专门编译机时使用此方式打包)
eclipse打包:adt-bundle-mac-x86_64-20140702(集成版,本地调试用eclipse即可)
gdb调试不用每次重新打包,只要打包出apk,启动后设置断点即可。