Attaching to WSL process from a visual studio extension.

Kev Martin 0 Reputation points
2025-06-02T10:10:24.7433333+00:00

I can attach to a WSL process in visual studio by going Debug -> 'Attach To Process' in the menu, then setting the 'Connection type' to 'Windows subsystem for Linux', selecting the process from the list, and clicking the 'attach' button.

How can I do this from inside a VSIX extension? Presumably I use IVsDebugger4.LaunchDebugTargets4(). But How do I configure it to attach to WSL?

Thanks for any help.

Developer technologies | Visual Studio | Extensions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 245 Reputation points Microsoft External Staff
    2025-07-04T10:49:13.5633333+00:00

    To attach to WSL processes programmatically, you can approach this task by leveraging the existing functionality in Visual Studio. Here's a general guideline based on what you shared:

    1. Access the Debugger: You’ll want to work with the IVsDebugger interface, which provides methods to start debugging sessions.
    2. Configure the Connection Type: You need to specify Windows Subsystem for Linux (WSL) as the connection type. This is crucial because it directs the debugger to work with WSL.
    3. Attach to the Process: While LaunchDebugTargets4 is typically used for launching new debugging sessions, you will likely want to look into methods similar to AttachToProcess, making sure you can specify the WSL distribution you need to attach to.
    4. Set up the Configuration: Create a debug launch configuration that matches how you set it up in the Visual Studio UI. This includes specifying any necessary parameters for processes running inside the WSL environment. The exact details may depend on the SDK version and the specifics of the process you're debugging.

  2. Anas Younis 0 Reputation points
    2025-07-04T20:38:11.09+00:00

    Yes, use IVsDebugger4.LaunchDebugTargets4(), and set it up like this:

    dlo = DLO_Attach

    bstrExe = "<your PID or exe>"

    bstrPortName = "SSH:wsl+<DistroName>" (e.g., SSH:wsl+Ubuntu)

    guidLaunchDebugEngine = appropriate engine CLSID (C++, .NET Core, etc.)

    That bstrPortName is the key for WSL. Let me know if you need help with engine GUIDs or getting the distro name!


  3. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 245 Reputation points Microsoft External Staff
    2025-07-07T08:04:41.8566667+00:00

    Hope you're doing well , we're following up on the ticket. Please let us know issue persist are resolved.

    0 comments No comments

  4. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 245 Reputation points Microsoft External Staff
    2025-07-18T10:13:32.5133333+00:00

    To attach to a WSL process in a VSIX extension, don't use LaunchDebugTargets4 directly. Instead, use VsDebugTargetInfo2 and the above error means one of those values are missing or wrong.


  5. Susmitha T (INFOSYS LIMITED) 160 Reputation points Microsoft External Staff
    2025-07-24T11:45:25+00:00

    Thank you for reaching out. Please find the answer below.

    You're absolutely right that VsDebugTargetInfo2 is an older API and not ideal for modern managed code scenarios.
    It also doesn't map directly to LaunchDebugTargets4, and in many cases, LaunchDebugTargets2 is a more appropriate choice.

    To attach to a WSL process from a VSIX extension, the recommended approach is to use IVsDebugger4.LaunchDebugTargets4() with a properly configured VsDebugTargetInfo4 structure.

    Here's how you can set it up for WSL:

    VsDebugTargetInfo4[] targets = new VsDebugTargetInfo4[1];

    targets[0].cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo4));

    targets[0].dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning;

    targets[0].bstrExe = "<your-exe-or-pid>"; // e.g., "464" or "myapp"

    targets[0].bstrPortName = "SSH:wsl+<DistroName>"; // e.g., "SSH:wsl+Ubuntu"

    targets[0].guidLaunchDebugEngine = /* appropriate engine GUID, e.g., NativeOnly_guid */;

    targets[0].dwProcessId = <your-process-id>; // optional

    targets[0].bstrRemoteMachine = null;

    IVsDebugger4 debugger = (IVsDebugger4)Package.GetGlobalService(typeof(SVsShellDebugger));

    int hr = debugger.LaunchDebugTargets4(1, targets);

    The key part for WSL is setting bstrPortName to "SSH:wsl+<DistroName>", which tells Visual Studio to connect to the WSL environment.

    If issue still persist after following all the steps, we’ll be happy to assist further if needed.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.