日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

vscode調(diào)試container中的程序的方法步驟_相關(guān)技巧

作者:passenger12234 ? 更新時(shí)間: 2022-05-22 編程語言

在寫cmu14-445的project時(shí),我希望在本地vscode編輯代碼,然后在docker中編譯和測(cè)試代碼。但是如果測(cè)試出了問題,直接在本地調(diào)試就變得麻煩了。所以希望利用vscode進(jìn)行遠(yuǎn)程調(diào)試。

參考官方文檔,利用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*/
        /*文檔里說這個(gè)是為了便于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"
}

編譯時(shí)利用ssh,在docker的終端中進(jìn)行編譯。而在launch.json中利用ssh作為pipeProgram,傳遞調(diào)試信息(雖然原理我也不太懂就是了)。172.17.0.2是container的IP地址。

為了保證主機(jī)能夠直接通過ssh登錄container,需要修改一下dockerfile文件。最終我的dockerfile文件長(zhǎng)這樣:

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當(dāng)前context下的文件,所以需要先復(fù)制一份公鑰到源碼目錄中。然后CMD中顯式啟動(dòng)ssh service。并且配置git代理(不然有時(shí)候clone github會(huì)失敗)。

docker啟動(dòng)該鏡像的時(shí)候就不要顯式指定命令了,不然這樣會(huì)覆蓋默認(rèn)的CMD指令。

最后還需要改一下.dockerignore文件,原來的.dockerignore文件會(huì)忽略源碼目錄下所有文件,導(dǎo)致COPY命令出錯(cuò)。OK,這樣就可以愉快地在本地vscode下面調(diào)試container里面的程序了。

update:

發(fā)現(xiàn)上面的遠(yuǎn)程調(diào)試的方法挺麻煩的,vscode的docker插件提供了直接把vscode attach到container里的方法,然后直接在vscode里面調(diào)試就行了。這個(gè)方法唯一的弊端是每次開啟容器后,都需要在容器中重新安裝一次vscode的插件

在bustub容器里裝了一波C++的插件,因?yàn)閎ustub的根目錄中已經(jīng)有一個(gè)CmakeLists.txt,自動(dòng)就配置好啦!

在這里插入圖片描述

可以在vscode最下方的狀態(tài)欄中選擇cmake的build參數(shù),比如我希望運(yùn)行buffer_pool_manager_instance_test,選擇相應(yīng)的build對(duì)象,然后點(diǎn)擊圖上的小蟲就可以斷點(diǎn)調(diào)試了。

另外,之前用lldb調(diào)試的時(shí)候有如下報(bào)錯(cuò)

error: 'A' packet returned an error: 8

需要在運(yùn)行容器時(shí)加上--security-opt seccomp=unconfined 參數(shù),允許容器內(nèi)的程序執(zhí)行全部系統(tǒng)調(diào)用。

原文鏈接:https://blog.csdn.net/passenger12234/article/details/123266556

欄目分類
最近更新