簡體   English   中英

如何使Visual Studio宏將調試器附加到w3wp.exe的所有實例?

[英]How can I make a Visual Studio Macro to attach the debugger to all instances of w3wp.exe?

我通常正在開發網絡應用程序,我花費了大量的工作時間來做“Ctrl + Alt + P”,按進程名稱排序,並選擇w3wp.exe來附加我的調試器。

更糟糕的是,我正在開發一個跨越多個應用程序池的應用程序,所以我通常有2或3個w3wp.exe實例,並且不可能知道要連接哪個,所以我通常最終會附加到所有他們,這是有點過分但有效。

總而言之,這非常煩人......

我的同事找到了一種讓VS Macro自動附加到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

我不確定是否所有這些都是必要的,或者其他什么,我從來沒有為VS做過一個宏,我真的不知道從哪里開始。

是否有辦法修改此宏,以便它不會將自身附加到w3wp.exe 實例,而是將其自身附加到w3wp.exe的所有實例?

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

這是我附加到遠程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

我知道您正在為此任務尋找宏,並且我有類似的宏。 但是,我想解釋一種在開始調試時將調試器附加到解決方案中的項目的方法。

這是一個鮮為人知的功能 - 如果您在解決方案瀏覽器中右鍵單擊解決方案文件,選擇屬性,則可以定義多個啟動項目及其操作。 運行它時,調試器將附加到列出的項目中。

注意:如果您有一個Web服務,它將打開一個瀏覽器窗口,但您可以通過告訴它不要打開一個窗口來禁用該項目的屬性。

您可能想要查看gflags.exe 它的一個選項是運行的調試器,附加到特定可執行文件的每次調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM