Yossi Sternlicht
Yossi Sternlicht

Reputation: 1040

How to debug dotnet core source code using Visual Studio Code

I would like to explore the source code of dotnet when debugging my application, for example. I would like to hit F11 on the Where Linq method and see step by step what is happening at runtime.

I am aware that this is possible using Visual Studio (https://github.com/dotnet/core/issues/897), and in Rider it is enabled by default, and stepping into it is as easy as debugging my own code.

What it the easiest way to set this up in visual studio code and is it even possible?

Please note, this is NOT a duplicate of Is there anyway to debugging .NET Core source code by Visual Studio Code? as that is regarding simple debugging in VSCode, not stepping into the BCL.

Upvotes: 5

Views: 4291

Answers (1)

Lucky
Lucky

Reputation: 56

Yes, it's possible.
Two links that were already mentioned by you are valuable but there is one more link https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md

First, check 'launch.json' configuration file

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "example name",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/src/app/bin/Debug/net6.0/app.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopAtEntry": false,
            "justMyCode": false, // should be false, as we want to debug 3rd party source code
            "requireExactSource": false, // https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#require-exact-source
            "suppressJITOptimizations": true, // it's better to set true for local debugging
            "enableStepFiltering": false, // to step into properties
            "symbolOptions": {
                "searchMicrosoftSymbolServer": true, // get pdb files from ms symbol server
                "searchNuGetOrgSymbolServer": true,
                "moduleFilter": {
                    "mode": "loadAllButExcluded",
                    "excludedModules": []
                }
            },
            "logging": { // you can delete it if all is ok
                "moduleLoad": true,
                "engineLogging": true,
                "trace": true
            }
        }
    ]
}

After you start debugging the 'DEBUG CONSOLE' will contain log like this

Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.10\System.Private.CoreLib.dll'. Symbols loaded.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.10\System.Console.dll'. Symbols loaded.

If there was a problem with loading some pdbs you can use dotPeek, as local symbol server.

After that you can use 'Step Into (F11)' button in debug panel at vscode to debug .net source code.

Upvotes: 4

Related Questions