vscode调试container中的程序的方法步骤

作者:passenger12234 时间:2022-03-06 14:20:25 

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

参考官方文档,利用ssh + pipeTransport来完成,下面是我的launch.jsontasks.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调试container中的程序的方法步骤

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

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

error: 'A' packet returned an error: 8

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

来源:https://blog.csdn.net/passenger12234/article/details/123266556

标签:vscode,调试,container
0
投稿

猜你喜欢

  • Python性能优化技巧

    2021-06-29 12:48:32
  • 三大Python翻译神器再也不用担心学不好英语

    2022-02-13 07:27:19
  • Python字符转换

    2021-08-23 04:47:13
  • Python中Playwright 与 pyunit 结合使用详解

    2022-04-30 10:42:42
  • windows下wxPython开发环境安装与配置方法

    2021-10-22 07:51:01
  • 关于DOM元素定位属性的深入学习

    2008-04-02 11:46:00
  • python numpy实现多次循环读取文件 等间隔过滤数据示例

    2022-10-30 09:44:13
  • PYTHON发送邮件YAGMAIL的简单实现解析

    2022-04-25 09:23:08
  • Python input()函数案例教程

    2021-12-21 15:45:02
  • 使用bandit对目标python代码进行安全函数扫描的案例分析

    2021-04-07 02:01:22
  • Python爬取新型冠状病毒“谣言”新闻进行数据分析

    2021-06-14 04:47:33
  • TensorFlow如何指定GPU训练模型

    2022-05-31 10:03:21
  • Python爬虫自动化爬取b站实时弹幕实例方法

    2023-03-21 04:45:42
  • Python条件语句与循环语句

    2023-01-03 05:07:21
  • Python flask-script 模块详解

    2023-03-09 06:20:20
  • python列表添加元素append(),extend(),insert(),+list的区别及说明

    2022-12-11 08:48:35
  • ASP 下载时重命名已上传文件的新下载文件名的实现代码

    2012-11-30 20:33:45
  • Python简单读写Xls格式文档的方法示例

    2021-11-02 13:27:30
  • django-rest-framework 自定义swagger过程详解

    2023-01-01 22:05:34
  • 18个超棒的Web和移动应用开发框架

    2011-03-31 17:04:00
  • asp之家 网络编程 m.aspxhome.com