Search code examples
c#c++linuxvisual-studio

Visual Studio mixed mode debugging with Linux development


Given the following scenario:

  • Visual Studio 2019/2022
  • Solution with two projects (.NET 5 C# console app, native C++ .so) targeting Linux
  • WSL or docker linux container runtime

is it possible to mix-debug the code (as it is when targeting windows with the mixed-mode debugging feature)?

If not, is there any VS extension or different IDE I can use for this purpose?


Solution

  • I had the same problem recently. I have a dotnet core app that calls some C++ code in a .so library. I see a lot of posts from Microsoft claiming you only need to enable Mixed mode debugging and that you can then step into native code. That DID NOT work for me.

    Also, when I debugged .net core apps, output from printf didn't show in neither the Debug window nor in Linux Console.

    But I was able to debug my C++ code using this somewhat cumbersome method:

    1. Added a sleep for 20s to the first C++ function that gets called from C#.
    2. Run the dotnet core app in Visual Studio 2022. You don't need any breakpoints here, since they're useless
    3. Run a second instance of Visual Studio 2022, preferably opening the native project you are debugging first. Set a breakpoint for the first statement following your sleep command.
    4. Now go to Debug->Attach To Process. In the dialog box, make sure set these properly

    4.1: Connection Type = Windows Subsystem for Linux (WSL)

    4.2: Connection Target = Correct Linux subsystem. You might have several, make sure you select the correct one

    4.3: Check "show processes from all users" (otherwise you will not see the 'dotnet' process).

    4.4: Click on dotnet

    4.5: Set Code Type to "Native (GDB) Code"

    1. Click on Attach -> Wait for your breakpoint to be hit