簡單介紹vscode除錯container中的程式的方法步驟

roc_guo發表於2022-04-11

在寫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: 
  "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除錯container中的程式的方法步驟

可以在vscode最下方的狀態列中選擇cmake的build引數,比如我希望執行buffer_pool_manager_instance_test,選擇相應的build物件,然後點選圖上的小蟲就可以斷點除錯了。

另外,之前用lldb除錯的時候有如下報錯

error: 'A' packet returned an error: 8

需要在執行容器時加上--security-opt seccomp=unconfined 引數,允許容器內的程式執行全部系統呼叫。

到此這篇關於vscode除錯container中的程式的方法步驟的文章就介紹到這了。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69901823/viewspace-2886443/,如需轉載,請註明出處,否則將追究法律責任。

相關文章