使用 vscode-insiders 的docker扩展 调试Apollo项目
- 一、vscode-insiders简介
- 二、安装 vscode-insiders 及扩展
- 2.1 安装 vscode-insiders
- 2.2 安装docker 和Remote - Containers 扩展
- 三、配置vscode-insiders
- 3.1 启动Apollo docker
- 3.2 在 vscode-insiders 中配置Apollo
- 四、调试Apollo模块和单元测试程序
- 4.1 调试Apollo单元测试程序
- 4.2 调试Apollo模块
- 在Apollo开发者社区中,经常被问及的一个问题是 用什么IDE工具进行开发。
- 知行合一2018大神曾经写过一篇博客使用Visual Studio Code编译、调试Apollo项目 介绍了如何用vscode结合gdb进行调试,为开发者提供了调试方法。
- 笔者近期发现一种更为简便的方式可以用vscode-insiders进行Apollo项目调试,操作体验非常接近IDE,故写一篇教程分享给大家,希望能给广大开发者带来一定的帮助。
一、vscode-insiders简介
- VSCode是微软第一款支持Linux的轻量级代码编辑器,其功能介于编辑器与IDE之间,但更倾向于一个编辑器。VSCode有两个版本,蓝色的vscode是非常稳定的发行版本,vscode-insiders,相当于bate版本,测试版,会有一些新的功能。
- 本文使用vscode-insiders 的docker 和Remote - Containers 扩展,可以便捷地调试Apollo项目,而蓝色的vscode不具备该调试功能。
二、安装 vscode-insiders 及扩展
2.1 安装 vscode-insiders
- vscode-insiders有两种安装方式,分别是apt安装和deb包安装。vscode-insiders 和蓝色的vscode 可以同时使用,安装vscode-insiders 无需卸载蓝色的vscode。
- apt方式安装
sudo apt-get update
sudo apt-get install code-insiders
- deb方式安装
在 https://code.visualstudio.com/insiders/ 下载 vscode-insiders 的deb包,
sudo dpkg -i code-insiders_xxx.deb
2.2 安装docker 和Remote - Containers 扩展
- 运行vscode-insiders,在扩展栏中搜索 docker 和 Remote - Containers 扩展并安装。
在 https://code.visualstudio.com/docs/remote/containers 中可查看docker扩展的详细教程。 - 由于Apollo项目是用C++进行开发,所以还需安装C++扩展。
- 安装完毕后如图所示:
三、配置vscode-insiders
3.1 启动Apollo docker
- 关于如何构建 Apollo3.5或5.0 已在 Ubuntu14.04搭建GPU版本的百度Apollo3.5自动驾驶平台 详细介绍。
cd apollo
bash docker/scripts/dev_start.sh
bash docker/scripts/dev_into.sh
bash scripts/bootstrap.sh
3.2 在 vscode-insiders 中配置Apollo
- Apollo docker 启动后,打开vscode-insiders,选中docker图标,出现Apollo镜像,如图所示:
- 右键选中 apolloauto/apollo:dev-x86_64-xxx 镜像,选中 Attach visual studio code (蓝色的vscode即使安装了docker扩展也没有此选项)
- 此时vscode会自动打开一个新窗口加载container ,若左下角出现 Attached container 字样,则表示 vscode 已加载完成。
- 然后选择 【打开文件夹】,键入 /apollo/ 后确定。
- 此时vscode-insiders 已经加载 Apollo 镜像中的/apollo 文件夹,接着打开扩展栏,在docker中 安装C++ 扩展(部分电脑会自动安装),安装后点击 【重新加载】。
- 加载完成后即配置好环境
四、调试Apollo模块和单元测试程序
4.1 调试Apollo单元测试程序
- 完成环境配置后,选择【调试】-【打开配置】,自动打开 /apollo/.vscode/launch.json 配置文件:
- 根据自己的需要,修改 program 和 args 参数,即可调试Apollo单元测试程序,以下给出一个例子:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/apollo/bazel-bin/modules/perception/camera/test/camera_lib_lane_postprocessor_denseline_lane_postprocessor_test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
- 打开对应的测试代码文件,加入断点,选择【调试】-【启动调试】,即可像其他IDE进行方便的代码调试。
- F10 :单步调试 F11:单步跳入 shift+F11:单步跳出
- 左侧可直观地看到堆栈和变量信息
修改代码后在docker中运行build,完成编译后即可再次启动调试。
./apollo.sh build
4.2 调试Apollo模块
- 知行合一2018大神曾经写过一篇博客Apollo 3.5 各功能模块的启动过程解析 详细地介绍了Apollo3.5 的模块启动过程。
- 通过在 /apollo/cyber/mainboard/mainboard.cc
- PS: Apollo3.0及以前的版本仍然可用此种方式调试,只需把 program 改成ros可执行程序路径,以及 args改成对应的参数。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/apollo/bazel-bin/cyber/mainboard",
"args": ["-d","/apollo/modules/drivers/gnss/dag/gnss.dag"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
参考文档:
vscode-insiders 的 docker扩展:https://code.visualstudio.com/docs/remote/containers
知行合一2018:使用Visual Studio Code编译、调试Apollo项目
知行合一2018:Apollo 3.5 各功能模块的启动过程解析