首页
社区
课程
招聘
[原创]Android - 系统级源码调试
2022-9-14 10:48 10482

[原创]Android - 系统级源码调试

2022-9-14 10:48
10482

Android - 系统源码调试

  • 理论上 只要有编译好的 idegen 三个操作系统都能跑

目录

Android Java层源码调试

编译idegen

  • 成功会在源码根目录生成android.imlandroid.ipr两个文件
1
2
3
4
5
6
7
8
# 在源码根目录执行
source build/envsetup.sh
lunch 22
mmm development/tools/idegen
# or make idegen
sudo development/tools/idegen/idegen.sh
sudo chmod 777 android.iml
sudo chmod 777 android.ipr

编辑导入配置

sudo deepin-editor android.iml

  • 搜索excludeFolder,在下面加入这些配置
  • 过滤不需要的源码模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<excludeFolder url="file://$MODULE_DIR$/bionic" />
<excludeFolder url="file://$MODULE_DIR$/bootable" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/cts" />
<excludeFolder url="file://$MODULE_DIR$/dalvik" />
<excludeFolder url="file://$MODULE_DIR$/developers" />
<excludeFolder url="file://$MODULE_DIR$/development" />
<excludeFolder url="file://$MODULE_DIR$/device" />
<excludeFolder url="file://$MODULE_DIR$/docs" />
<excludeFolder url="file://$MODULE_DIR$/external" />
<excludeFolder url="file://$MODULE_DIR$/hardware" />
<excludeFolder url="file://$MODULE_DIR$/kernel" />
<excludeFolder url="file://$MODULE_DIR$/out" />
<excludeFolder url="file://$MODULE_DIR$/pdk" />
<excludeFolder url="file://$MODULE_DIR$/platform_testing" />
<excludeFolder url="file://$MODULE_DIR$/prebuilts" />
<excludeFolder url="file://$MODULE_DIR$/sdk" />
<excludeFolder url="file://$MODULE_DIR$/system" />
<excludeFolder url="file://$MODULE_DIR$/test" />
<excludeFolder url="file://$MODULE_DIR$/toolchain" />
<excludeFolder url="file://$MODULE_DIR$/tools" />
<excludeFolder url="file://$MODULE_DIR$/.repo" />

导入Android Studio

  • 通过AS的Open an existing Android Studio project选项选择源码根目录的android.ipr就可以导入源码

image

排除tests 目录 右键

  • mark Directory as Excluded

配置 Android源码项目

点击File -> Project Structure–>SDKs配置项目的JDK、SDK。

image

根据源码版本选择对应API级别 这里使用的Android10 对应29

代号 版本 API 级别/NDK 版本
Android13 13 API 级别 33
Android12L 12 API 级别 32
Android12 12 API 级别 31
Android11 11 API 级别 30
Android10 10 API 级别 29
Pie 9 API 级别 28
Oreo 8.1.0 API 级别 27
Oreo 8.0.0 API 级别 26
Nougat 7.1 API 级别 25
Nougat 7.0 API 级别 24
Marshmallow 6.0 API 级别 23

点击 Edit API SDK

image

  • 不用管下面的配置选项

image

Modules Structure

1
2
3
/android/android/android-10.0.0_r2/frameworks/base/core/res/AndroidManifest.xml
/android/android/android-10.0.0_r2/frameworks/base/core/res/res
/android/android/android-10.0.0_r2/frameworks/base/core/res/assets

image

配置Run/Debug Configurations

image

开始调试 - 使用教程

在源码的根目录创建start_emulator.sh脚本,为了方便的启动模拟器,输入以下内容 后执行

1
2
3
4
5
6
#!/bin/bash
source build/envsetup.sh
lunch 6
emulator
# sudo chmod 777 ./start_emulator.sh
# ./start_emulator.sh

常用命令

1
2
3
4
5
# 第三方app需先以调试模式启动app 点击运行
adb shell am set-debug-app -w com.example.dexlassloaders
# 等待附加调试 会自动继续运行 直到触发断点
 
# 系统进程可直接进行附加调试

案例一

在系统源码找到ActivityStarter这个类,在startActivityMayWait这个方法打断点

 

image

 

点击菜单的Run–>Attach Debugger to Android Process,勾选Show all processer,选择system_process 随便启动app 触发断点

 

image

第三方app触发 系统java层 调试成功

image

Android Native层源码调试 - 需以源码编译的模拟器启动

前置配置 source build/envsetup.sh lunch 22

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 进入源码目录
cd /android/android/android-10.0.0_r2
# 先初始化环境 主要为lunch 目标
source build/envsetup.sh
lunch 22
# 进入gdbclient.py 脚本目录
cd development/scripts
# 调试模式启动 第三方app 此命令需手动点击
adb shell am set-debug-app -w com.example.dexlassloaders
# 以调试模式启动 无需手动点击
adb shell am start -D -n  com.example.dexlassloaders/.MainActivity
# 查看进程pid
adb shell "ps -ef | grep com.example.dexlassloaders"
# u0_a103       6018  1631 0 18:24:11 ?     00:00:00 com.example.dexlassloaders
# root          6046  1677 0 18:25:54 ?     00:00:00 sh -c ps -ef | grep com.example.dexlassloaders
# root          6049  6046 0 18:25:54 ?     00:00:00 grep com.example.dexlassloaders
# 执行此命令等待 输出 vscode launch.json配置 报错 请检查pid
gdbclient.py -p 6018 --setup-forwarding vscode
# 没调试完不要 按enter
# 接着使用as 附加调试 或者
adb forward tcp:12345 jdwp:6018  # (Where XXX is the PID of the debugged process.)
jdb -attach localhost:12345

配置 VScode 运行和调试

 获取vscodelunch.json 配置

  • 注意先选择C/C++ 源码 下好断点 此时按F5 触发
  • gdbclient.py -p 6018 --setup-forwarding vscode输出下面内容 把其中 {} 复制到 VScode launch.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
    "configurations": [
         // {} 复制到这里
{
    "miDebuggerPath": "/android/android/android-10.0.0_r2/prebuilts/gdb/linux-x86/bin/gdb",
    "program": "/android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/system/bin/app_process64",
    "setupCommands": [
        {
            "text": "-enable-pretty-printing",
            "description": "Enable pretty-printing for gdb",
            "ignoreFailures": true
        },
        {
            "text": "-environment-directory /android/android/android-10.0.0_r2",
            "description": "gdb command: dir",
            "ignoreFailures": false
        },
        {
            "text": "-gdb-set solib-search-path /android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/system/lib64/:/android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/system/lib64/hw:/android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/system/lib64/ssl/engines:/android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/system/lib64/drm:/android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/system/lib64/egl:/android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/system/lib64/soundfx:/android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/vendor/lib64/:/android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/vendor/lib64/hw:/android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols/vendor/lib64/egl",
            "description": "gdb command: set solib-search-path",
            "ignoreFailures": false
        },
        {
            "text": "-gdb-set solib-absolute-prefix /android/android/android-10.0.0_r2/out/target/product/generic_x86_64/symbols",
            "description": "gdb command: set solib-absolute-prefix",
            "ignoreFailures": false
        },
        {
            "text": "-interpreter-exec console \"source /android/android/android-10.0.0_r2/development/scripts/gdb/dalvik.gdb\"",
            "description": "gdb command: source art commands",
            "ignoreFailures": false
        }
    ],
    "name": "(gdbclient.py) Attach app_process64 (port: 5039)",
    "miDebuggerServerAddress": "localhost:5039",
    "request": "launch",
    "type": "cppdbg",
    "cwd": "/android/android/android-10.0.0_r2",
    "MIMode": "gdb"
}
    ]
}

启动native调试

  • 输出符号加载为连接调试gdb成功

image

最后 打开AS open Android ipr 项目

  • 按照java层 调试方法附加调试 触发 第三方app执行
  • 到断点处自然断下来

image

 

image

vscode 附加调试失败

gdbclient.py -p 6018 --setup-forwarding vscode 执行之后 vscode 附加 发现链接失败时

手动运行gdbserver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 调试模式启动 第三方app 此命令需手动点击
adb shell am set-debug-app -w com.example.dexlassloaders
# 以调试模式启动 无需手动点击
adb shell am start -D -n  com.example.dexlassloaders/.MainActivity
# 查看进程pid
adb shell "ps -ef | grep com.example.dexlassloaders"
# u0_a103       6018  1631 0 18:24:11 ?     00:00:00 com.example.dexlassloaders
# root          6046  1677 0 18:25:54 ?     00:00:00 sh -c ps -ef | grep com.example.dexlassloaders
# root          6049  6046 0 18:25:54 ?     00:00:00 grep com.example.dexlassloaders
# 1. 进入手机 shell
adb shell
# 2. 切换root模式 普通手机为su
su
# 3. 手动执行gdbserver
gdbserver64 :1234 --attach 6018
# 出现下面的表示 附加调试成功
# Remote debugging from host 127.0.0.1
# 4. 重新启动一个终端
# 4.1 进行端口映射
adb forward tcp:5039 tcp:1234
# 4.2  按照 获取vscodelunch.json 配置 这个做 配置vscode 检查5039端口
# 5. 启动vscode附加调试 - 先下断点
vscode 按 F5 启动调试 查看调试控制台,应该开始Loaded symbols 了
# 6. 使用as 附加调试或者 执行下面的jdb 开始调试
adb forward tcp:12345 jdwp:6018
jdb -attach localhost:12345

异常问题处理

  • 跳转到 .class
    在 Edit API SDK 的时候 选择 Sourcepath 删除原本的 ipr还是什么忘记了 全删 添加 源码目录的
    • frameworks
    • libcore
    • 需要其他再添加
  • lunch真机的时候 gdbclient.py 没有输出 必须模拟器才有 可修改模拟的输出 适配真机vscode lunch 自行尝试 。

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

最后于 2022-11-21 22:38 被iyue_t编辑 ,原因: 板块不对
收藏
点赞15
打赏
分享
最新回复 (8)
雪    币: 1993
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
赛文奥特曼 2022-9-14 11:15
2
0
感谢分享
雪    币: 3777
活跃值: (5535)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
huangjw 2022-9-14 14:42
3
0

太棒了,等源码调试的文章很久了,最好再来个源码下载。

最后于 2022-9-14 14:44 被huangjw编辑 ,原因:
雪    币: 1197
活跃值: (2629)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
iyue_t 2022-9-14 15:20
4
0
下载直接看清华源aosp帮助里面就行了.
雪    币: 1197
活跃值: (2629)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
iyue_t 2022-10-26 19:24
5
0
huangjw 太棒了,等源码调试的文章很久了,最好再来个源码下载。
https://bbs.pediy.com/thread-274882.htm 我把笔记整理了一下 要是看了还不会 我远程手把手教.
雪    币: 200
活跃值: (68)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
hslxy 2023-4-15 17:32
6
0
老哥,可以加个wx(hslxy417)交流一下吗,native的debug没有成功
雪    币: 19244
活跃值: (28872)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
秋狝 2023-4-15 22:38
7
1
感谢分享
雪    币: 27
活跃值: (532)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
Concord 2023-4-22 20:09
8
0
感谢分享,请问实机也是这个流程吗
雪    币: 1197
活跃值: (2629)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
iyue_t 2023-4-24 02:02
9
0
Concord 感谢分享,请问实机也是这个流程吗
对的,把模拟器生成的vscode路径改成真机的就行,其实改动很小
游客
登录 | 注册 方可回帖
返回