CLion调试AnroidNative的demo

                     



网上流传的使用Android Studio调试Framework的教程很多,很多同学已经可以使用AS轻松的attach任意进程并进行动态调试。但是对于调试Android Native,虽然有着传统的使用Console运行GDB Client来进行调试的方法,但是我一直在期望着Android Native也可以有一种类似于AS调试Framework调试体验:

[if !supportLists]1.     [endif]可以任意选择进程进行调试

[if !supportLists]2.     [endif]切换进程进行调试不需要大费周折,可以随切随调

[if !supportLists]3.     [endif]同时也可以支持代码跳转,方便阅读 Native 源码

[if !supportLists]4.     [endif]开启IDE的速度在可接受的范围内

Prerequisite

我目前成功搭建调试环境的配置是:

[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

搭建步骤

编译环境配置

环境配置和AS搭建Framework阅读环境一致

[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

下载的话需要账号,如果不想注册的话可以像我一样从网盘下载

[if !supportLists]·       [endif]设置可打开文件上限

vim~/.bash_profile

加入以下

#set the number of open files to be 1024

ulimit -S -n 1024

[if !supportLists]·       [endif]创建大小写敏感的磁盘映像

hdiutil create -type SPARSE -fs 'Case-sensitive

Journaled HFS+' -size 120g~/android.dmg.sparseimage

以上,还有一些编译的环境配置,自行安装吧

下载并编译

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 是为了避免下载调试不需要用到的tag,可以大量减少下载体积

[if !supportLists]3.     [endif]注3: 遇见Xcode SDK版本不符合编译所需的问题,可以到 https://github.com/phracker/MacOSX-SDKs 下载,解压后放置到对应目录。使用 xcrun --show-sdk-path 命令可以找到对应的目录

[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

调试环境配置

[if !supportLists]·       [endif]创建ipr, iml文件

cd/Volumes/Android

touch clion.ipr clion.iml

[if !supportLists]·       [endif]修改配置文件内容,这里参考我的写法:

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即可

调试 Native 源码

[if !supportLists]·       [endif]向手机push gdbserver

[if !supportLists]·       [endif]CLion配置

[if !vml]

[endif]

[if !vml]

[endif]

CLion调试AnroidNative的demo_第1张图片

以上配置完成后,CLion 已经具备使用GDB Client连接Server的能力了

[if !supportLists]·       [endif]attach手机进程

adbshell

# 这里调试一个gallery进程,使用的symbol正是app_process64

ps -A | grep gallery

gdbserver64 --attach :6666 24752


# 这里回到PC端,输入

adb forward tcp:6666

[if !vml]

[endif]

总结

CLion调试AnroidNative的demo_第2张图片

以上步骤完成后,以后就可以使用该环境调试各种Native进程,与AS调试Framework环境的不同点在于,CLion不能在IDE中通过选择进程名的方式来attach进程,需要手动获取进程PID并通过gdbserver attach进程

需要注意的是,为了保证每次开启CLion的速度,需要在 Preferences

-> Apperance & Behaviour -> System Settings -> StartUp/Shutdown 中将 Reopen last project on startup 的勾选去除,这样每次启动时可以手动open clion.ipr文件进行选择性的项目加载,可以提升 index load的速度。

你可能感兴趣的:(CLion调试AnroidNative的demo)