? ? ? ? ? ? ? ? ? ? ?
網(wǎng)上流傳的使用Android Studio調(diào)試Framework的教程很多,很多同學(xué)已經(jīng)可以使用AS輕松的attach任意進程并進行動態(tài)調(diào)試。但是對于調(diào)試Android Native,雖然有著傳統(tǒng)的使用Console運行GDB Client來進行調(diào)試的方法,但是我一直在期望著Android Native也可以有一種類似于AS調(diào)試Framework調(diào)試體驗:
[if !supportLists]1.???? [endif]可以任意選擇進程進行調(diào)試
[if !supportLists]2.???? [endif]切換進程進行調(diào)試不需要大費周折,可以隨切隨調(diào)
[if !supportLists]3.???? [endif]同時也可以支持代碼跳轉(zhuǎn),方便閱讀 Native 源碼
[if !supportLists]4.???? [endif]開啟IDE的速度在可接受的范圍內(nèi)
Prerequisite
我目前成功搭建調(diào)試環(huán)境的配置是:
[if !supportLists]1.???? [endif]MacOs High
Sierra(10.13.1)
[if !supportLists]2.???? [endif]Xcode 9.3
[if !supportLists]3.???? [endif]IDE: CLion2018.1
[if !supportLists]4.???? [endif]手機: Nexus 6P
[if !supportLists]5.???? [endif]編譯源碼:android-8.1.0_r25
[if !supportLists]6.???? [endif]VPN
搭建步驟
編譯環(huán)境配置
環(huán)境配置和AS搭建Framework閱讀環(huán)境一致
[if !supportLists]·???????[endif]下載并配置repo
mkdir ~/bin
PATH=~/bin:$PATH
curl https://storage.googleapis.com/git-repo-downloads/repo
> ~/bin/repo
chmod a+x ~/bin/repo
[if !supportLists]·???????[endif]安裝JDK1.8
地址:?http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
下載的話需要賬號,如果不想注冊的話可以像我一樣從網(wǎng)盤下載
[if !supportLists]·???????[endif]設(shè)置可打開文件上限
vim~/.bash_profile
加入以下
#set the number of open files to be 1024
ulimit -S -n 1024
[if !supportLists]·???????[endif]創(chuàng)建大小寫敏感的磁盤映像
hdiutil create -type SPARSE -fs 'Case-sensitive
Journaled HFS+' -size 120g~/android.dmg.sparseimage
以上,還有一些編譯的環(huán)境配置,自行安裝吧
下載并編譯
repo init -u https://android.googlesource.com/platform/manifest
-b android-8.1.0_r25
repo sync -c -d --force-sync --no-tags--prune -j12
make-j12
[if !supportLists]1.???? [endif]注1:android-8.1.0_r25 是我選擇的下載分支,自行選擇可以到?https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds?按需挑選
[if !supportLists]2.???? [endif]注2: sync 后加入 –no-tags 是為了避免下載調(diào)試不需要用到的tag,可以大量減少下載體積
[if !supportLists]3.???? [endif]注3: 遇見Xcode SDK版本不符合編譯所需的問題,可以到?https://github.com/phracker/MacOSX-SDKs?下載,解壓后放置到對應(yīng)目錄。使用?xcrun --show-sdk-path?命令可以找到對應(yīng)的目錄
[if !supportLists]4.???? [endif]注4: High Sierra目前編譯存在bison的BUG,需要cherry-pick代碼,重編bison并替換原文件,再次重編即可。該change地址:?lib: vasnprintf: Fix illegal
instruction
編譯完成后對手機進行刷機:
# 重啟并進入bootloader模式
adbrebootbootloader
# 刷機后重啟
sudo fastboot -w
sudo fastboot flash boot boot.img
sudo fastboot flash system system.img
sudo fastboot flash userdatauserdata.img
sudo fastboot reboot
調(diào)試環(huán)境配置
[if !supportLists]·???????[endif]創(chuàng)建ipr, iml文件
cd/Volumes/Android
touch clion.ipr clion.iml
[if !supportLists]·???????[endif]修改配置文件內(nèi)容,這里參考我的寫法:
clion.ipr
<project version="4">
?<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$"/>
?<component name="CidrRootsConfiguration">
???<excludeRoots>
?????<file path="$PROJECT_DIR$/.repo"/>
?????<file path="$PROJECT_DIR$/abi"/>
?????<file path="$PROJECT_DIR$/bionic"/>
?????<file path="$PROJECT_DIR$/bootable"/>
?????<file path="$PROJECT_DIR$/build"/>
?????<file path="$PROJECT_DIR$/cts"/>
?????<file path="$PROJECT_DIR$/dalvik"/>
?????<file path="$PROJECT_DIR$/device"/>
?????<file path="$PROJECT_DIR$/external"/>
?????<file path="$PROJECT_DIR$/gen"/>
?????<file path="$PROJECT_DIR$/kernel"/>
?????<file path="$PROJECT_DIR$/libnativehelper"/>
?????<file path="$PROJECT_DIR$/ndk"/>
?????<file path="$PROJECT_DIR$/out"/>
?????<file path="$PROJECT_DIR$/packages"/>
?????<file path="$PROJECT_DIR$/pdk"/>
?????<file path="$PROJECT_DIR$/platform_testing"/>
?????<file path="$PROJECT_DIR$/prebuilts"/>
?????<file path="$PROJECT_DIR$/res"/>
?????<file path="$PROJECT_DIR$/rkst"/>
?????<file path="$PROJECT_DIR$/rockdev"/>
?????<file path="$PROJECT_DIR$/sdk"/>
?????<file path="$PROJECT_DIR$/tools"/>
?????<file path="$PROJECT_DIR$/u-boot"/>
?????<file path="$PROJECT_DIR$/vendor"/>
???excludeRoots>
?component>
?<component name="InspectionProjectProfileManager">
???<profile version="1.0">
?????<option name="myName" value="Project
Default"/>
?????<inspection_tool class="LossyEncoding" enabled="false" level="WARNING" enabled_by_default="false"/>
???profile>
???<version value="1.0"/>
?component>
?<component name="ProjectCodeStyleConfiguration">
???<code_scheme name="Project" version="173">
?????<Objective-C-extensions>
???????<file>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function"/>
???????file>
???????<class>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod"/>
?????????<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod"/>
???????class>
???????<extensions>
?????????<pair source="cpp" header="h"fileNamingConvention="NONE"/>
?????????<pair source="c" header="h"fileNamingConvention="NONE"/>
???????extensions>
?????Objective-C-extensions>
???code_scheme>
?component>
?<component name="ProjectModuleManager">
???<modules>
?????<module fileurl="file://$PROJECT_DIR$/clion.iml" filepath="$PROJECT_DIR$/clion.iml"/>
???modules>
?component>
project>
android.iml
<module classpath="CMake" type="CPP_MODULE" version="4"/>
[if !supportLists]·???????[endif]打開CLion,選擇打開clion.ipr即可
調(diào)試 Native 源碼
[if !supportLists]·???????[endif]向手機push gdbserver
[if !supportLists]·???????[endif]CLion配置
[if !vml]
[endif]
[if !vml]
[endif]

以上配置完成后,CLion 已經(jīng)具備使用GDB Client連接Server的能力了
[if !supportLists]·???????[endif]attach手機進程
adbshell
# 這里調(diào)試一個gallery進程,使用的symbol正是app_process64
ps -A | grep gallery
gdbserver64 --attach :6666 24752
# 這里回到PC端,輸入
adb forward tcp:6666
[if !vml]
[endif]
總結(jié)

以上步驟完成后,以后就可以使用該環(huán)境調(diào)試各種Native進程,與AS調(diào)試Framework環(huán)境的不同點在于,CLion不能在IDE中通過選擇進程名的方式來attach進程,需要手動獲取進程PID并通過gdbserver attach進程
需要注意的是,為了保證每次開啟CLion的速度,需要在 Preferences
-> Apperance & Behaviour -> System Settings -> StartUp/Shutdown 中將 Reopen last project on startup 的勾選去除,這樣每次啟動時可以手動open clion.ipr文件進行選擇性的項目加載,可以提升 index load的速度。
、