Android Java层源码调试

编译idegen

  • 成功会在源码根目录生成android.iml 和 android.ipr两个文件。
# 在源码根目录执行source build/envsetup.shlunch 22mmm development/tools/idegen# or make idegensudo development/tools/idegen/idegen.shsudo chmod 777 android.imlsudo chmod 777 android.ipr

编辑导入配置

sudo deepin-editor android.iml

  • 搜索excludeFolder,在下面加入这些配置。
  • 过滤不需要的源码模块。
"file://$MODULE_DIR$/bionic" />"file://$MODULE_DIR$/bootable" />"file://$MODULE_DIR$/build" />"file://$MODULE_DIR$/cts" />"file://$MODULE_DIR$/dalvik" />"file://$MODULE_DIR$/developers" />"file://$MODULE_DIR$/development" />"file://$MODULE_DIR$/device" />"file://$MODULE_DIR$/docs" />"file://$MODULE_DIR$/external" />"file://$MODULE_DIR$/hardware" />"file://$MODULE_DIR$/kernel" />"file://$MODULE_DIR$/out" />"file://$MODULE_DIR$/pdk" />"file://$MODULE_DIR$/platform_testing" />"file://$MODULE_DIR$/prebuilts" />"file://$MODULE_DIR$/sdk" />"file://$MODULE_DIR$/system" />"file://$MODULE_DIR$/test" />"file://$MODULE_DIR$/toolchain" />"file://$MODULE_DIR$/tools" />"file://$MODULE_DIR$/.repo" />

导入Android Studio

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

排除tests 目录 右键

  • mark Directory as Excluded

配置 Android源码项目

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

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

点击 Edit API SDK。

  • 不用管下面的配置选项

Modules Structure

/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

配置Run/Debug Configurations

开始调试 - 使用教程

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

#!/bin/bashsource build/envsetup.shlunch 6emulator# sudo chmod 777 ./start_emulator.sh# ./start_emulator.sh

常用命令

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

案例一

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

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

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

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

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

# 进入源码目录cd /android/android/android-10.0.0_r2# 先初始化环境 主要为lunch 目标source build/envsetup.shlunch 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# 查看进程pidadb 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配置 报错 请检查pidgdbclient.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。

{    "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成功。

最后 打开AS open Android ipr 项目

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

vscode 附加调试失败

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

手动运行gdbserver

# 调试模式启动 第三方app 此命令需手动点击adb shell am set-debug-app -w com.example.dexlassloaders# 以调试模式启动 无需手动点击adb shell am start -D -n  com.example.dexlassloaders/.MainActivity# 查看进程pidadb 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. 进入手机 shelladb shell# 2. 切换root模式 普通手机为susu# 3. 手动执行gdbservergdbserver64 :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:6018jdb -attach localhost:12345