FlutterEngineDebug环境搭建

FlutterEngine开发环境

https://github.com/flutter/flutter/wiki/Setting-up-the-Engine-development-environment

Editor autocomplete support

VSCode with C/C++ Intellisense

VSCode can provide some IDE features using the C/C++ extension. It will provide basic support on install without needing any additional configuration. There will probably be some issues, like header not found errors and incorrect jump to definitions.

Intellisense can also use our compile_commands.json for more robust functionality. Either symlink src/out/compile_commands.json to the project root at src/flutter or provide an absolute path to it in the c_cpp_properties.json config file. See “compile commands” in the c_cpp_properties.json reference. This will likely resolve the basic issues mentioned above.

For adding IDE support to the Java code in the engine with VSCode, see “Using VSCode as an IDE for the Android Embedding”.

Using VSCode as an IDE for the Android Embedding

Create a .classpath file in engine/src/flutter/shell/platform/android so that the IDE can understand the project structure. Here’s an example file that worked as of the time of this writing.实测.classpath是放入engine\shell\platform\android下

It should contain links to all of the engine’s JAR dependencies in third_party, including the Android SDK. It should also list the test/ subdirectory as a src directory to fix incorrect package errors on the unit test files.

FlutterEngine编译

搭建Flutter Engine源码编译环境

https://chromium.googlesource.com/external/github.com/flutter/engine/+/b7358b33dbd61e124720165dd939fa49cbd0ecb6/CONTRIBUTING.md

https://github.com/flutter/flutter/wiki/Compiling-the-engine#general-compilation-tips

按照上述官网步骤配置(位置/Users/qianpianpian/git/flutter/fork/engine):

  1. git pull upstream master in src/flutter to update the Flutter Engine repo.
  2. gclient sync in engine directory to update dependencies.
  3. ./flutter/tools/gn –android –android-cpu x64 –unoptimized in src directory
  4. ninja -C out/android_debug_unopt_x64 in src directory
  5. ./flutter/tools/gn –unoptimized in src directory
  6. ninja -C out/host_debug_unopt in src directory

还需:

  1. 将本地用户目录/.ssh/中的公钥复制添加到GitHub个人settings下的ssh keys配置中。
  2. 安装Xcode:

2.1. Install Xcode (get it from https://appstore.com/mac/apple/xcode) if you don’t have it yet.

2.2. Accept the Terms and Conditions.

2.3. Ensure Xcode app is in the /Applications directory (NOT /Users/{user}/Applications).

2.4. Point xcode-select to the Xcode app Developer directory using the following command: sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

输出的unstripped so:/Users/qianpianpian/git/flutter/fork/engine/src/out/android_debug_unopt_x64/libflutter.so

FlutterEngine Debug

https://github.com/flutter/flutter/wiki/Debugging-the-engine

Flutter Engine源码调试

Flutter Engine C++ 源码调试初探

https://github.com/lizhangqu/flutter_lldb

java和dart使用Android Studio调试,两边对应native层用VSCode调试

Visual Studio Code LLDB Debug

VSCode中调试相对比较简单(且支持源码点击跳转,AS不支持),你除了要安装VSCode外,你还需要安装两个VSCode的插件。VS Code调试时栈帧源码显示为汇编的原因是没有找到源码文件,找到之后就会是正确的源码文件

  1. 在需要启动的flutter app目录(例如flutterboost项目)下执行
flutter run --local-engine-src-path=/Users/qianpianpian/git/flutter/fork/engine/src --local-engine=/Users/qianpianpian/git/flutter/fork/engine/src/out/android_debug_unopt_x64
  1. 将out目录下的compile_commands.json文件拷贝到src/futter目录下,然后用VSCode打开src/flutter目录即可。

  2. 在/Users/qianpianpian/git/flutter/flutter_lldb下执行

./flutter_lldb \
--local-engine-src-path=/Users/qianpianpian/git/flutter/fork/engine/src \
--local-engine=android_debug_unopt_x64 \
com.idlefish.flutterboost.example

这里提示killall: lldb-server: No such process也不影响正常调试

  • failed to listen: Address already in use…..returned non-zero exit status 255,需要虚拟机重新启动

  • 更换编译的cpu架构时,需要修改flutter_lldb脚本中的_get_android_lldb_server方法,返回正确的lldb_server路径

  1. 复制第一个json到.vscode/launch.json中
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "remote_lldb",
            "type": "lldb",
            "request": "attach",
            "pid": "4427",
            "initCommands": [
                "platform select remote-android",
                "platform connect unix-abstract-connect:///data/data/com.idlefish.flutterboost.example/debug.socket"
            ],
            "postRunCommands": [
                "add-dsym /Users/qianpianpian/git/flutter/fork/engine/src/out/android_debug_unopt_x64/libflutter.so",
                "settings set target.source-map /Users/qianpianpian/git/flutter/fork/engine/src /Users/qianpianpian/git/flutter/fork/engine/src"
            ],
        }
    ]
}
  1. 开始调试remote_lldb,在debug tab中点击remote_lldb启动attach,同时配合着lldb命令设置断点(未涉及到pause和resume debugger)

  2. 操作flutter app,能够触发前面设置的断点处流程

VSCode调试时查看字符数组内容:

print message_data
(uint8_t *) $7 = 0x00000000730ac820 "{\"textScaleFactor\":1,\"alwaysUse24HourFormat\":false,\"platformBrightness\":\"light\"}"

Android Studio LLDB Debug(可调试,但堆栈有误)

  1. debug前导入engine/src/flutter/shell/platform/android到android studio,提示framework detected,不要点击进行配置,如果不小心点击了,删除.idea和gen即可(这个步骤没多大用,c代码不能点击跳转到定义)

  2. 在需要启动的flutter app目录下执行: flutter run –local-engine=android_debug_unopt_x64

  3. AS打开android文件夹,attach debugger(dual Java+native),此时AS弹出Java和C两个调试窗口,进入C的lldb调试窗口,依次选择debugger,LLDB选项卡;pause调试器

(lldb) add-dsym /Users/qianpianpian/git/flutter/fork/engine/src/out/android_debug_unopt_x64/libflutter.so
symbol file '/Users/qianpianpian/git/flutter/fork/engine/src/out/android_debug_unopt_x64/libflutter.so' has been added to '/Users/qianpianpian/.lldb/module_cache/remote-android/.cache/688819DF-8C91-5E9A-5BD5-CD0B02CB2AE1-B35E2C42/libflutter.so'
  1. br s -n DispatchPlatformMessage
  2. resume debugger
  3. 操作flutter app,能够触发前面设置的断点处流程

其他参考

https://flutter.dev/docs/development/tools/sdk/upgrading