markdown 在docker容器(VSCode)中调试应用程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 在docker容器(VSCode)中调试应用程序相关的知识,希望对你有一定的参考价值。

# How to debug an app in a contaier
To debug an application running it inside a docker container, you have to include a new configuration block in VSCode. Go to debug area and press 'Add Configuration'.
You also need to have some elements previously installed/configured:

Inside the docker container, you need to have:
  - gdb installed.
  - all the binaries of your app compilled with debug symbols (_-g_)
  - Run the docker container with the entrypoint set as: _/bin/bash_

In your local machine:
  - VSCode installed
  - C++ and Gdb VSCode extensions installed (c/c++ , cmake, cmake tools, Native Debug)
  - The application code folder oppened (in VSCode) choosing the root folder (main CMakeLists.txt file location)
  - Run docker with these parameters:
      * alias docker='sudo docker'
      * sudo code --user-data-dir="~/.vscode-root"      // Run VSCode as root

Here, you have to add a new debug configuration inside the launch.json script. You can open this script from "Debug->Add Configuration...". Use any of the default templates as reference.

This is an example configuration block to cross debuging the app:

```
  ...
    "version": "0.2.0",
    "configurations": [
    {
        "name": "C++ Docker Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "hello",  // binary name inside docker container
        "args": [""],
        "stopAtEntry": false,
        "cwd": "/data/build",   // path to the binary file inside docker container
        "environment": [{"CUDA_VISIBLE_DEVICES":"2"}],    // List of extra environment variables
        "externalConsole": false,
        "logging": {
                "moduleLoad": true,
                "trace": true,
  	    "engineLogging": true
        },
        "pipeTransport": {
            "pipeCwd": "/usr/bin/",   // Local directory of pipe program (docker)
            "pipeProgram": "docker",  // Pipe program
            "pipeArgs": [ "exec","-i", "89739f30ca79", "/bin/bash","-c"],  // 89739f30ca79 container id ('-c' concatenates all the app name and the debugger path in the proper way)
            "debuggerPath": "/usr/bin/gdb" // Debugger location on the remote machine  
            //"debuggerArgs": "handle SIGILL nostop"
        },
        "sourceFileMap": {
            "/data/src":"/home/user/app/src"  // map remote src with local srcs
        } 
    }
    ...
```
**WARNING**: Some implementations have problems to be debugged inside the container. In those cases, add the flag "--cap-add=SYS_PTRACE" to the docker container run call.

**WARNING**: In some cases, you need to add _gosu_ call to the launching pipe parameters. In those cases, add _gosu_ and _user_ in pipeArgs list:
  "pipeArgs": [ "exec","-i", "89739f30ca79", "gosu" , "user", "/bin/bash","-c"],

以上是关于markdown 在docker容器(VSCode)中调试应用程序的主要内容,如果未能解决你的问题,请参考以下文章

使用 VSCode 在 Docker 容器中调试 Typescript 文件

如何在 Docker 容器中运行 VS Code 的扩展开发主机来测试我的 vscode 扩展?

vscode如何直接远程到ubuntu虚拟机docker容器?vscode远程开发

VsCode轻松使用docker容器-Remote Containers

vscode中预览markdown文件

当在docker容器中设置php时,如何在vscode中设置php可执行路径php.validate.executablePath?