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 已经具备使用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]

总结

以上步骤完成后,以后就可以使用该环境调试各种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的速度。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 226,828评论 6 526
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,669评论 3 412
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 174,467评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,238评论 1 306
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,039评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,561评论 1 319
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,658评论 3 433
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,806评论 0 285
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,316评论 1 329
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,293评论 3 353
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,443评论 1 364
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,998评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,698评论 3 342
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,097评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,333评论 1 281
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,012评论 3 385
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,434评论 2 370

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,857评论 0 38
  • 相遇是那样偶然 在茫茫的人海里初相见 你走进了我的心田 从此 我的视线里 写满了深情的念恋 记忆的贮藏室里 往事幕...
    诗韵钟鸣阅读 470评论 3 13
  • 是的,能有这样心情的人,我想最大原因莫过于你也是过来人,你知道十年寒窗的艰辛,你理解一考定人生的说法。特别是,你如...
    刘树华阅读 271评论 0 0
  • 子亮事件过去以后,同学出现了前所没有的学习热情,大家之间变的空前友好,很少有人再为了一点小事争执,并且好笑的是,有...
    咖啡里的雲阅读 316评论 0 0
  • 高考,几家欢乐几家愁! 高考,寒门学子改变命运的唯一途径! 高考,简简单单两个字,却包含了太多太多...
    简晓爱阅读 304评论 0 2