导读 本文主要介绍了vscode调试container中的程序的方法步骤,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在写cmu14-445的project时,我希望在本地vscode编辑代码,然后在docker中编译和测试代码。但是如果测试出了问题,直接在本地调试就变得麻烦了。所以希望利用vscode进行远程调试。

参考官方文档,利用ssh + pipeTransport来完成,下面是我的launch.json和tasks.json最后的样子。

{
  // 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": [
    {
      "name": "g++-9 - Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "./build/test/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "sourceFileMap":{
        "/bustub": "${workspaceFolder}"
        /*remote src directory : local src directory*/
        /*文档里说这个是为了便于debugger找源码*/
      },
      "cwd": "/bustub",
      "environment": [],
      "pipeTransport": {
        "pipeCwd": "/usr/bin",
        "pipeProgram": "ssh",
        "pipeArgs": [
          "root@172.17.0.2"
        ],
        "debuggerPath": "/usr/bin/gdb"
      },
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: g++-9 build active file",
      "miDebuggerPath": "/usr/bin/gdb"
    }
  ]
}
{
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: g++-9 build active file",
      "command": "ssh",
      "args": [
        "root@172.17.0.2",
        "cd /bustub/build && make ${fileBasenameNoExtension}"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ],
  "version": "2.0.0"
}

编译时利用ssh,在docker的终端中进行编译。而在launch.json中利用ssh作为pipeProgram,传递调试信息(虽然原理我也不太懂就是了)。172.17.0.2是container的IP地址。

为了保证主机能够直接通过ssh登录container,需要修改一下dockerfile文件。最终我的dockerfile文件长这样:

FROM ubuntu:18.04
 
# Install Ubuntu packages.
# Please add packages in alphabetical order.
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get -y update && \
    apt-get -y install \
      build-essential \
      clang-8 \
      clang-format-8 \
      clang-tidy-8 \
      cmake \
      doxygen \
      git \
      g++-7 \
      pkg-config \
      valgrind \
      zlib1g-dev \
      ssh
RUN echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config && \
    echo 'PermitEmptyPasswords yes' >> /etc/ssh/sshd_config && \
    echo 'PasswordAuthentication yes' >> /etc/ssh/sshd_config && \
    echo 'PubkeyAuthentication yes' >> /etc/ssh/sshd_config && \
    mkdir /root/.ssh
 
COPY ./id_rsa.pub /root/.ssh/authorized_keys
 
CMD service ssh start && git config --global http.proxy "http://192.168.31.1:7890" && bash

修改的地方主要是安装ssh,然后把本地公钥copy过去,注意copy命令只能copy当前context下的文件,所以需要先复制一份公钥到源码目录中。然后CMD中显式启动ssh service。并且配置git代理(不然有时候clone github会失败)。

docker启动该镜像的时候就不要显式指定命令了,不然这样会覆盖默认的CMD指令。

最后还需要改一下.dockerignore文件,原来的.dockerignore文件会忽略源码目录下所有文件,导致COPY命令出错。OK,这样就可以愉快地在本地vscode下面调试container里面的程序了。

update:

发现上面的远程调试的方法挺麻烦的,vscode的docker插件提供了直接把vscode attach到container里的方法,然后直接在vscode里面调试就行了。这个方法唯一的弊端是每次开启容器后,都需要在容器中重新安装一次vscode的插件。

在bustub容器里装了一波C++的插件,因为bustub的根目录中已经有一个CmakeLists.txt,自动就配置好啦!

可以在vscode最下方的状态栏中选择cmake的build参数,比如我希望运行buffer_pool_manager_instance_test,选择相应的build对象,然后点击图上的小虫就可以断点调试了。

另外,之前用lldb调试的时候有如下报错

error: 'A' packet returned an error: 8

需要在运行容器时加上--security-opt seccomp=unconfined 参数,允许容器内的程序执行全部系统调用。

到此这篇关于vscode调试container中的程序的方法步骤的文章就介绍到这了。

原文来自:https://www.jb51.net/article/241479.htm

本文地址:https://www.linuxprobe.com/vscode-container-linux.html编辑:xiangping wu,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/