We've recently moved our development from Visual Studio to VS Code. Our code solution contains .NET Core C# which hands calculations off to C++. In Visual Studio, we were able to seamlessly debug between C# and C++.
We initially moved to running the multi-project solution in a VM and using the remote development features of VS Code. We use two Launch configurations:
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "/usr/share/dotnet/dotnet",
"processId": "${command:pickProcess}",
"additionalSOLibSearchPath": "${workspaceFolder}/src/build/Debug/Cpp/",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Run C#",
"type": "coreclr",
"request": "launch",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/AppProject/bin/Debug/netcoreapp2.2/AppProject.dll",
"args": [],
"cwd": "${workspaceFolder}/src/AppProject",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"EnvironmentName": "Development",
"LD_LIBRARY_PATH": "${workspaceFolder}/src/build/Release/Cpp/;$LD_LIBRARY_PATH"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
}
}
First running "Run C#" then running "(gdb) Attach" and selecting the dotnet process to attach to. This worked, allowing us to step down from C# into C++.
We've now moved to a Docker set up, where the source is mounted into a container with the build tools built in (as described in the VS Code docs). This is roughly our devcontainer.json:
{
"image": [custom image in a private code repository],
"forwardPorts": [various ports to serve web interfaces],
"runArgs": [
"--cap-add=SYS_PTRACE",
"--security-opt",
"seccomp=unconfined"
],
"extensions": [
"ms-vscode.cpptools",
"ms-vscode.cmake-tools",
"hbenl.vscode-test-explorer",
"derivitec-ltd.cmake-test-adapter",
"ms-dotnettools.csharp",
"fernandoescolar.vscode-solution-explorer",
"derivitec-ltd.vscode-dotnet-adapter"
],
"mounts": [
"source=${localEnv:HOME}/.gitconfig,target=/root/.gitconfig,type=bind,consistency=cached",
"source=${localEnv:HOME}/.ssh,target=/root/.ssh,type=bind,consistency=cached"
]
}
In this set up, we've been able to successfully debug C# and C++ code separately, but the above flow of running the C# project and attaching GDB after it does not work as expected. The C# process exits, while the "(gdb) Attach" call starts multiple processes which run into various exceptions at launch. I've pasted the full output to Pastebin.
Much of the advice I've found is confused due to:
- legacy methods such as calling docker directly (omnisharp/omnisharp-vscode issue on debugging in docker)
- alternative setups where source code is packaged into a docker container rather than mounted (VS Code docs for debugging .NET Core in docker)
- alternative setups where build tools live in a docker container but VS Code communicates with the docker container via SSH rather than using the dev container approach recommended in the docs (Microsoft blog post on developing C++ with VS Code and docker)
- C++ seemingly having special requirements for debugging, especially where docker is concerned
- The
runArgsdescription in the devcontainer.json reference:For example, "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ] allows ptrace based debuggers like C++ to work in the container.
- The
- VS Code docs suggesting that debugging should work exactly as it does natively when using remote development tools ("Debugging in a container" from VS Code docs)
How should I debug both C# and C++ running within a docker container using VS Code?