如何使Visual Studio宏将调试器附加到w3wp.exe的所有实例?

时间:2023-01-14 23:58:40

I'm normally developing web apps, and a surprisingly large amount of my work time is spent doing "Ctrl + Alt + P", sorting by Process Name, and picking w3wp.exe to attach my debugger.

我正在开发网络应用程序,我花费了大量的工作时间来做“Ctrl + Alt + P”,按进程名称排序,并选择w3wp.exe来附加我的调试器。

To make matters worse, I'm working on an app that spans several application pools, so I normally have 2 or 3 instances of w3wp.exe, and it's impossible to know which one to attach to, so I normally end up attaching to all of them, which is overkill but works.

更糟糕的是,我正在开发一个跨越多个应用程序池的应用程序,因此我通常有2或3个w3wp.exe实例,并且无法知道要连接哪个,所以我通常最终会附加到所有他们,这是过度杀伤但有效。

All in all, this is pretty annoying...

总而言之,这非常烦人......

My colleague figured out a way to have a VS Macro to automatically attach to w3wp.exe (he basically recorded this):

我的同事找到了一种方法让VS宏自动附加到w3wp.exe(他基本上记录了这个):

Sub AttachMacro()    
  Try    
    Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger    
    Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")    
    Dim dbgeng(3) As EnvDTE80.Engine    
    dbgeng(0) = trans.Engines.Item("T-SQL")    
    dbgeng(1) = trans.Engines.Item("T-SQL")    
    dbgeng(2) = trans.Engines.Item("Managed")    
    Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "ENIAC").Item("w3wp.exe")    
    proc2.Attach2(dbgeng)    
  Catch ex As System.Exception    
    MsgBox(ex.Message)    
  End Try    
End Sub

I'm not really sure whether all that is necessary, or anything, I have never made a macro for VS, I don't really know where to start.

我不确定是否所有这些都是必要的,或者其他什么,我从来没有为VS做过一个宏,我真的不知道从哪里开始。

Would there be a way to modify this macro so that instead of attaching itself to an instance of w3wp.exe, it will attach itself to all instances of w3wp.exe?

是否有办法修改此宏,以便它不会将自身附加到w3wp.exe的实例,而是将其自身附加到w3wp.exe的所有实例?

4 个解决方案

#1


Sub MacroAttachToAllProcesses()

    Try

        Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
        Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
        Dim dbgeng(3) As EnvDTE80.Engine

        dbgeng(0) = trans.Engines.Item("T-SQL")
        dbgeng(1) = trans.Engines.Item("T-SQL")
        dbgeng(2) = trans.Engines.Item("Managed")

        For Each theProcess As EnvDTE80.Process2 In dbg2.GetProcesses(trans, "COMPUTERNAME")
            If theProcess.Name.Contains("w3wp.exe") Then
                theProcess.Attach2(dbgeng)
            End If

        Next

    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try

End Sub

#2


I know that you are looking for a Macro for this task, and I have similar macros. However I would like to explain a way to attach the debugger to the projects in your solution when you start to debug.

我知道您正在为此任务寻找宏,并且我有类似的宏。但是,我想解释一种在开始调试时将调试器附加到解决方案中的项目的方法。

It's a little known feature - if you right-click on your solution file in the solution browser, Choose properties, you can then define multiple startup projects and their action. Your debugger will attach to the listed projects when you run it.

这是一个鲜为人知的功能 - 如果您在解决方案浏览器中右键单击解决方案文件,选择属性,则可以定义多个启动项目及其操作。运行时,调试器将附加到列出的项目中。

Note: If you have a web-service it will open a browser window, however you can disable that in the properties of the project by telling it not to open a window.

注意:如果您有一个Web服务,它将打开一个浏览器窗口,但您可以通过告诉它不要打开一个窗口来禁用该项目的属性。

#3


This is how I attach to a remote w3wp process. It runs a bit quicker than DanC's solution and has some extra error handling.

这是我附加到远程w3wp进程的方式。它运行速度比DanC的解决方案快一点,并且有一些额外的错误处理。

Private Sub AttachToW3wp(ByVal machineName As String)
    ' In order for this to work, you have to be running the Visual Studio 2010 Remote Debugging Monitor
    ' as your (domain) user.  
    ' It won't work if the remote debugger is running as a service.  I've tried every permutation of 
    ' domain and username in the the transport qualifier, tried the obvious local system username,
    ' even tried looking at the network traffic 
    ' in WireShark, I can't figure it out how to make it work if you are running as a service.  
    ' If you are running the debugger as a service, even running the macro that gets created by VS's 
    ' macro recorder when you attach to a process doesn't work.
    Dim transportQualifier = machineName
    Try
        Dim processToAttachTo As String = "w3wp.exe"
        Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
        Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
        Dim dbgeng(2) As EnvDTE80.Engine
        dbgeng(0) = trans.Engines.Item("T-SQL")
        dbgeng(1) = trans.Engines.Item("Managed (v2.0, v1.1, v1.0)")
        Dim processesRemote = dbg2.GetProcesses(trans, transportQualifier)
        Dim attached As Boolean = False
        Dim processRemote As EnvDTE80.Process2
        For Each processRemote In processesRemote
            ' For some reason it takes a much longer time to get the remote process names then it 
            ' does the user name, so let's skip over all the processes that have the wrong UserName.
            If processRemote.UserName = "NETWORK SERVICE" AndAlso _
               (Right(processRemote.Name, Len(processToAttachTo)) = processToAttachTo) Then
                If processRemote.IsBeingDebugged Then
                    MsgBox(processToAttachTo & " on " & machineName & " is already being debugged")
                Else
                    processRemote.Attach2(dbgeng)
                End If
                attached = True
            End If
        Next
        If Not attached Then
            MsgBox(processToAttachTo & " is not running on " & machineName & ".")
        End If
    Catch ex As System.Exception
        MsgBox("Exception attempting to attach to " & transportQualifier & ": " & ex.Message)
    End Try
End Sub

#4


You may want to check out gflags.exe. One of its options is a debugger to run attached to every invocation of a particular executable.

您可能想要查看gflags.exe。它的一个选项是运行的调试器,附加到特定可执行文件的每次调用。

#1


Sub MacroAttachToAllProcesses()

    Try

        Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
        Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
        Dim dbgeng(3) As EnvDTE80.Engine

        dbgeng(0) = trans.Engines.Item("T-SQL")
        dbgeng(1) = trans.Engines.Item("T-SQL")
        dbgeng(2) = trans.Engines.Item("Managed")

        For Each theProcess As EnvDTE80.Process2 In dbg2.GetProcesses(trans, "COMPUTERNAME")
            If theProcess.Name.Contains("w3wp.exe") Then
                theProcess.Attach2(dbgeng)
            End If

        Next

    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try

End Sub

#2


I know that you are looking for a Macro for this task, and I have similar macros. However I would like to explain a way to attach the debugger to the projects in your solution when you start to debug.

我知道您正在为此任务寻找宏,并且我有类似的宏。但是,我想解释一种在开始调试时将调试器附加到解决方案中的项目的方法。

It's a little known feature - if you right-click on your solution file in the solution browser, Choose properties, you can then define multiple startup projects and their action. Your debugger will attach to the listed projects when you run it.

这是一个鲜为人知的功能 - 如果您在解决方案浏览器中右键单击解决方案文件,选择属性,则可以定义多个启动项目及其操作。运行时,调试器将附加到列出的项目中。

Note: If you have a web-service it will open a browser window, however you can disable that in the properties of the project by telling it not to open a window.

注意:如果您有一个Web服务,它将打开一个浏览器窗口,但您可以通过告诉它不要打开一个窗口来禁用该项目的属性。

#3


This is how I attach to a remote w3wp process. It runs a bit quicker than DanC's solution and has some extra error handling.

这是我附加到远程w3wp进程的方式。它运行速度比DanC的解决方案快一点,并且有一些额外的错误处理。

Private Sub AttachToW3wp(ByVal machineName As String)
    ' In order for this to work, you have to be running the Visual Studio 2010 Remote Debugging Monitor
    ' as your (domain) user.  
    ' It won't work if the remote debugger is running as a service.  I've tried every permutation of 
    ' domain and username in the the transport qualifier, tried the obvious local system username,
    ' even tried looking at the network traffic 
    ' in WireShark, I can't figure it out how to make it work if you are running as a service.  
    ' If you are running the debugger as a service, even running the macro that gets created by VS's 
    ' macro recorder when you attach to a process doesn't work.
    Dim transportQualifier = machineName
    Try
        Dim processToAttachTo As String = "w3wp.exe"
        Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
        Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
        Dim dbgeng(2) As EnvDTE80.Engine
        dbgeng(0) = trans.Engines.Item("T-SQL")
        dbgeng(1) = trans.Engines.Item("Managed (v2.0, v1.1, v1.0)")
        Dim processesRemote = dbg2.GetProcesses(trans, transportQualifier)
        Dim attached As Boolean = False
        Dim processRemote As EnvDTE80.Process2
        For Each processRemote In processesRemote
            ' For some reason it takes a much longer time to get the remote process names then it 
            ' does the user name, so let's skip over all the processes that have the wrong UserName.
            If processRemote.UserName = "NETWORK SERVICE" AndAlso _
               (Right(processRemote.Name, Len(processToAttachTo)) = processToAttachTo) Then
                If processRemote.IsBeingDebugged Then
                    MsgBox(processToAttachTo & " on " & machineName & " is already being debugged")
                Else
                    processRemote.Attach2(dbgeng)
                End If
                attached = True
            End If
        Next
        If Not attached Then
            MsgBox(processToAttachTo & " is not running on " & machineName & ".")
        End If
    Catch ex As System.Exception
        MsgBox("Exception attempting to attach to " & transportQualifier & ": " & ex.Message)
    End Try
End Sub

#4


You may want to check out gflags.exe. One of its options is a debugger to run attached to every invocation of a particular executable.

您可能想要查看gflags.exe。它的一个选项是运行的调试器,附加到特定可执行文件的每次调用。