【工具】使用VS Code调试Docker Container中的代码

使用VS Code调试Docker Container中的Autoware.ai代码

Part 1 – 在VS Code中设置并进行Debug

在用这个方法时踩到了一些坑,一度搞得我很无奈。后面解决的方法也有点莫名其妙。下面详细叙述下。

Step 1

首先创建docker container,这里我是用命令行创建的。然后运行autoware提供的/docker/generic/下的run.sh,即会自动创建docker container并进入到container中。此时docker --version为Docker version 20.10.18, build b40c2f6.
更新vs code到最新版本,安装docker 插件
在这里插入图片描述

Step 2

点击左侧任务栏的docker按钮, 可以看到显示所有的container,右击选择Attach Visual Studio Code

在这里插入图片描述
会弹出一个新的窗口,可以在左侧任务栏看到DEV CONTAINERS已经连接,如下图所示。
在这里插入图片描述
这里曾经遇到两个坑
第一个是在点击Attach Visual Studio Code后,VS Code出现弹框报错,内容为“Remote - Containers Docker version 17.12.0 or later required.”
但其实Docker的版本已经是20往上了,搜到了这个解决办法,但是貌似没啥用。。
第二个坑是,把后面的都配置好之后,点击Debug按钮调试cpp文件,调用的竟然是python的debugger,然后发现是在文档里面,调试过python代码,不知道为啥默认就用了那个,选择gdb也不好使。把之前的目录删了以后,发现可以了。。。
而且同时,再点Attach Visual Studio Code就可以用了。。。所以不知道是不是因为这个影响了第一个问题。
总而言之解决的莫名其妙。
按理说,VS Code这边的Docker插件都能检测到container里面的内容,所有代码均可查看,而且执行Attach to Shell命令,也可以正常进入到docker container里面的命令行,应该说vscode是连接到了container了,不知道为啥会出现第一个问题。
总之解决了。

Step 3

点击File按钮,打开/home/autoware/Autoware/src/autoware目录作为工作目录。
在这里插入图片描述

Step 4

/home/autoware/Autoware/src/autoware/目录下创建文件夹.vscode
分别创建四个文件,文件名和内容分别如下

launch.json

VS Code Official: Configure C/C++ debugging
书写launch.json文件时,可以按住Ctrl+Space来查看推荐的设置值

// {
//     // Use IntelliSense to learn about possible attributes.
//     // Hover to view descriptions of existing attributes.
//     // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
//     "version": "0.2.0",
//     "configurations": []
// }
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", // 配置名称,将会在调试配置下拉列表中显示
            "type": "cppdbg",  // 调试器类型 该值自动生成
            "request": "launch",  // 调试方式,还可以选择attach
            "program": "/home/autoware/Autoware/build/gnss_localizer/devel/lib/gnss_localizer/fix2tfpose", //要调试的程序(完整路径,支持相对路径)
            "args": [],  // 传递给上面程序的参数,没有参数留空即可
            "stopAtEntry": false,  // 是否停在程序入口点(停在main函数开始)
            "cwd": "${workspaceRoot}",  // 调试程序时的工作目录
            "environment": [], //针对调试的程序,要添加到环境中的环境变量. 例如: [ { "name": "squid", "value": "clam" } ]
            "externalConsole": false,   //如果设置为true,则为应用程序启动外部控制台。 如果为false,则不会启动控制台,并使用VS Code的内置调试控制台。
            "MIMode": "gdb",  // VSCode要使用的调试工具名称
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

设置完成后发现,貌似最有用的就是launch.json,其他的好像影响不大。。。

tasks.json

VS Code Official: Schema for tasks.json

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "catkin_make",
        "args": [
          "--directory",
          "/home/autoware/Autoware/src/autoware/",
          "-j4",
          "-DCMAKE_BUILD_TYPE=Debug",
          "-DCATKIN_WHITELIST_PACKAGES=<package_name>"
        ],
        "problemMatcher": [
          "$catkin-gcc"
        ],
        "group": {
          "kind":"build",
          "isDefault":true
          },
        "label": "catkin_make: build"
      }
    ]
  }
c_cpp_properties.json

VS Code Official: c_cpp_properties.json reference

{
    "configurations": [
        {
            "browse": {
                "databaseFilename": "",
                "limitSymbolsToIncludedHeaders": true
            },
            "includePath": [
                "/opt/ros/melodic/include/**",
                "/usr/include/**"
            ],
            "name": "ROS",
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++14"
            //"compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}
settings.json

VS Code Official : settings

{
    "files.associations": {
        "iostream": "cpp"
    }
}

Step 5

在docker container中安装gdb debugger

$ gdb -help 
$ sudo apt-get install libc6-dbg gdb valgrind  # to install

确保gdb的地址是正确的。检查launch.json文件中包含miDebuggerPath的一行。

Step 6

launch.json文件中,编辑program这一行,指定在/build目录下,比如

/home/autoware/Autoware/build/op_global_planner/devel/lib/op_global_planner/op_global_planner

Step 7

/src目录下找到对应的cpp文件,比如

/home/autoware/Autoware/src/autoware/core_planning/op_global_planner/nodes/op_global_planner.cpp

确保所有的必要topic信息都可以被订阅到,点击debug按钮,并选择"(gdb)launch" debugger

Error Solutions

error description:
click debug in op_global_planner_core.cpp

  1. cannot include “.h” file => in PROBLEMS
  2. cannot find liblibway_point_follower =>in TERMINAL

Solutions:

  1. in c_cpp_properties.json =>in “includePath”, add “/home/autoware/Autoware/**”
  2. search the error in GoG and follow this solution Linux error while loading shared libraries: cannot open shared object file: No such file or directory
    find that inside docker container, under “autoware” user, the target library path is included. but the VSCode using the root user to run the script by default. Thus:
    a. print the $LD_LIBRARY_PATH under autoware user
    b. switch to root user under VSCode (notice that the VSCode will user the current user selected in terminal)
    c. under root user => vi /etc/profile, add export LD_LIBRARY_PATH={path copied from autoware user}

参考链接

Open container fails with “Docker version 17.12.0 or later is required” #5396
Attached container configuration reference
VSCode代码调试器
【VSCode】调试器debugger详细使用手册

Part 2 – cmake重新编译

Autoware默认编译的版本为release版本,因此需要编译为debug模式来进行调试。
要完成这个任务,需要做几个方面的工作。

  • 学习cmake
  • 了解Autoware中的编译结构
  • 修改CMakeLists.txt文件,并重新编译为debug模式

cmake

Autoware项目是用cmake编译的,首先需要对cmake的用法有所了解。
众所周知,C++中cpp文件无法直接运行,需要编译成.o.obj这种object目标文件,才能够执行。

使用gcc命令可以分别编译每个cpp文件,但这样很麻烦,cmake则提供了批量编译很多文件的简便方法。

使用方法(简介)

为一个项目建立CMakeLists.txt文件,在文件里按照规定的语法编写,然后执行

$ cmake ..
$ make

命令,会生成编译文件,主要的(应该也是最基础的文件)包括

CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile

并可以用make clean命令来清楚生成的object文件

cmake常用目录结构
build

通常为项目创建build目录,在这个目录下执行cmakemake命令,生成的文件都在这下面(除了目标文件),将编译生成的文件都放在build目录下还有一个好处是,重新编译需要删除这些编译文件时,可以直接删除,不会和其他需要的文件混在一起。

bin

通常用来存放生成的object文件,但是也不一定,比如Autoware就放在每个小模块的CMakeFiles文件夹下面

lib

通常用来存放库文件,包括.a静态库和.so动态库。

src

用来存放cpp源文件。

教程

【C++】Cmake使用教程(看这一篇就够了)
【C++】静态库和动态库文件的生成和使用

Autoware编译结构

Autoware路径下的目录结构为

build/ install/ log/ src/

其中:
build文件夹存放了各个模块编译相关的文件,模块目录下的CMakeFiles存放了生成的object文件。
src文件夹存放了CMakeLists.txt文件。比如/home/autoware/Autoware/src/autoware/core_planning/op_global_planner/路径

查看Autoware的编译类型

How to check if program was compiled with debug symbols?

修改CMakeList.txt文件

在CMakeLists.txt文件里增加两行,

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE RelWithDebInfo)

然后回到build目录,执行以下命令进行编译

$ colcon build --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo

如果要编译为Release版本,则执行
Without CUDA Support

$ colcon build --cmake-args -DCMAKE_BUILD_TYPE=Release

With CUDA support

$ AUTOWARE_COMPILE_WITH_CUDA=1 colcon build --cmake-args -DCMAKE_BUILD_TYPE=Release

这里用的是colcon build
如果只编译某一个包,且在不支持CUDA的模式下编译,并编译为debug版本,则使用以下命令:

$ AUTOWARE_COMPILE_WITH_CUDA=0 colcon build --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo --packages-select op_global_planner

参考链接

OpenPlanner项目独立的github链接

https://github.com/hatem-darweesh/autoware.ai.openplanner/tree/dd9bda08e2bb13b0ad501514098f853a38be7732

colcon使用文档

https://colcon.readthedocs.io/en/released/user/quick-start.html

build Autoware from source

https://gitlab.com/autowarefoundation/autoware.ai/autoware/-/wikis/Source-Build?version_id=a33764ab4b6e7a1798c9f79465c74d565e92904b

How to check if program was compiled with debug symbols? [duplicate]

https://stackoverflow.com/questions/3284112/how-to-check-if-program-was-compiled-with-debug-symbols

VSCode debug cpp ROS node - compile package with debug mode

https://answers.ros.org/question/313371/vscode-debug-cpp-ros-node/

C/C++: How do you set GDB debug flag (-g) with cmake?

https://bytefreaks.net/programming-2/cc-how-do-you-set-gdb-debug-flag-g-with-cmake

VSCode 官方 Debugging模块文档

https://code.visualstudio.com/docs/editor/debugging

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值