Attaching to a child process automatically in Visual Studio during Debugging

20,170

Solution 1

I would use a macro. I've redefined my F5 function to attach to the asp.net process instead of the long build/validate it usually performs. This works pretty well for me and it's really easy.

    For Each process In DTE.Debugger.LocalProcesses
        If (process.Name.IndexOf("aspnet_wp.exe") <> -1) Then
            process.Attach()
            Exit Sub
        End If
    Next

Solution 2

For VS2012, macros have been dropped, but you can still do it quite quickly with standard keyboard shortcuts. For instance, to attach to iisexpress.exe:

Ctrl + Alt + p - brings up the Attach To Process dialog

i - jumps to the the first process beginning with i in the list (for me this is iisexpress.exe)

Enter - attaches

For super speed, you can also Turn off Visual Studio Attach security warning when debugging IIS.

Solution 3

Check out the VisualStudio plugin that I wrote, named Lazy.

Solution 4

I was debugging a C++ plugin in an externally spawned process that crashed by throwing an exception at startup and this worked perfectly for me:

Add the free Reattach Extension for Visual Studio. Ask it to reattach to the process name before it is launched. It will pop a modal dialog saying it is waiting for the process name to launch.

Now launch the process and the Visual Studio debugger will attach immediately, catching exceptions and hitting breakpoints.

(This was also in a media plugin, the exception was normally caught and rethrown by the host process in a Delphi context so I needed to break before that happened).

Share:
20,170
Sam Saffron
Author by

Sam Saffron

Co-founder: http://www.discourse.org email: [email protected] blog: http://samsaffron.com twitter: @samsaffron Ex Stack Overflow Valued Associate #00008, creator of Stack Exchange Data Explorer, co-creator of Media Browser ( now Emby ) Other Projects: Logster : https://github.com/discourse/logster rack-mini-profiler : https://github.com/MiniProfiler/rack-mini-profiler message_bus : https://github.com/SamSaffron/message_bus memory_profiler: https://github.com/SamSaffron/memory_profiler flamegraph : https://github.com/SamSaffron/flamegraph All original source snippets I post on Stack Overflow are dedicated to the public domain. Do with them as you see fit.

Updated on July 03, 2020

Comments

  • Sam Saffron
    Sam Saffron almost 4 years

    When writing plugins for media center your plugin is hosted in ehexthost.exe this exe gets launched from ehshell.exe and you have no way of launching it directly, instead you pass a special param to ehshell.exe which will launch the plugin in a separate process.

    When we are debugging media browser I find the process of attaching to a second process kind of clunky, I know about Debugger.Attach and also of some special registry entries I can use.

    Both these methods don't exactly fit my bill. What I want is to press F5 and have my current instance of visual studio attach to the child process automatically. Can this be done?

    If there is a plugin for VS that allows me to achieve this functionality I would be happy with it.

    EDIT

    I ended up going with the following macro:

    Public Sub CompileRunAndAttachToEhExtHost()
    
        DTE.Solution.SolutionBuild.Build(True)
        DTE.Solution.SolutionBuild.Debug()
    
        Dim trd As System.Threading.Thread = New System.Threading.Thread(AddressOf AttachToEhExtHost)
        trd.Start()
    
    End Sub
    
    Public Sub AttachToEhExtHost()
        Dim i As Integer = 0
        Do Until i = 50
            i = i + 1
            Try
    
                For Each proc As EnvDTE.Process In DTE.Debugger.LocalProcesses
                    If (proc.Name.IndexOf("ehexthost.exe") <> -1) Then
                        proc.Attach()
                        Exit Sub
                    End If
                Next
            Catch e As Exception
                ' dont care - stuff may be busy 
            End Try
            Threading.Thread.Sleep(100)
        Loop
    End Sub
    

    Also, I outlined the process on how to get this going on my blog.

  • Sam Saffron
    Sam Saffron over 15 years
    Thanks! But this trick does not work for C# projects you do not have an option like that in the debug list, only the option to start an external program
  • n99
    n99 over 15 years
    I think i know what you mean... you can create a "dummy" c++ project and add it to your solution. As long as it attaches top a correct executable, you are good to debug.
  • Pablo Retyk
    Pablo Retyk over 15 years
    thanks, I wrote it when I have a similar scenario like yours, my process was invoked by other process and I needed to debug the initialization of my process so I needed to attach it as soon as it starts. If you have any questions let me know. "It works in my machine"
  • Allen Rice
    Allen Rice almost 15 years
    Thanks man, I used this to attach to WebDev.WebServer and IE when I hit F4
  • Andreas Haferburg
    Andreas Haferburg over 11 years
    After I installed it, it only shows up in VS2008, not VS2010.
  • mutex
    mutex almost 11 years
    any suggestions how to do this in VS2012 now that they've removed macros?
  • Jab
    Jab almost 11 years
    You actually no longer have to! At least not for what I needed it for. They added a new option for web projects to "Don't open a page. Wait for a request from an external application." Under the project -> web settings. I'm not sure how I would do it if I needed to attach to a non-web process though. Sorry!
  • Bora
    Bora almost 10 years
    Hey @pablito, do you have any plans to port your plugin to VS2010/2012+?
  • Pablo Retyk
    Pablo Retyk almost 10 years
    haven't updated it for a long time, but since some people seems interested I will port it soon to vs2010/12.