Auto Debug Procedure

Have you ever need to debug an application but you couldn’t start it from the debugger. An example is a process that gets launched from another application. Lets say you have a Visual Studio solution which handles 2-3 executable projects. You start one with the debugger and you want to break in another process as soon as it starts.

I wrote a small macro for Visual Studio for which you can easily mark an executable projects as to Auto Debug when it gets executed. It used Microsoft jitdebugger registry entry to make this works.

First you need to import this macro into Visual Studio:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports Microsoft.VisualStudio.VCProject
Imports Microsoft.VisualStudio.VCProjectEngine
 
Public Module Quickies
 
    Sub RegKeySave(ByVal i_RegKey As String, _
               ByVal i_Value As String, _
      Optional ByVal i_Type As String = "REG_SZ")
        Dim myWS As Object
 
        'access Windows scripting
        myWS = CreateObject("WScript.Shell")
        'write registry key
        myWS.RegWrite(i_RegKey, i_Value, i_Type)
 
    End Sub
 
    Function GetMiscDumpPane() As OutputWindowPane
 
        Dim ow As OutputWindow = DTE.Windows.Item(Constants.vsWindowKindOutput).Object
        Dim pane As OutputWindowPane
 
        Try
            pane = ow.OutputWindowPanes.Item("MyMacrosPane")
        Catch ex As Exception
            pane = ow.OutputWindowPanes.Add("MyMacrosPane")
        End Try
        Return pane
    End Function
 
    Sub SetAutoDebug(ByVal state As Boolean)
 
        Dim linkerOutput As String
        Dim itemName As String
        Dim projectName As String
        Dim project As EnvDTE.Project
 
        If DTE.ActiveSolutionProjects.Length <> 1 Then
            MsgBox("Select one project within the Solution Explorer, then re-run this macro.")
            Exit Sub
        End If
 
        Dim pane As OutputWindowPane = GetMiscDumpPane()
 
        project = DTE.ActiveSolutionProjects(0)
        itemName = project.FullName()
 
        ' Get linker option
        If project.Kind = vcContextGuids.vcContextGuidVCProject Then
            Dim vcProj As VCProject = project.Object
            Dim configs As IVCCollection = vcProj.Configurations
 
            projectName = vcProj.Name
 
            Dim cfg As VCConfiguration = configs.Item(1)
            If cfg Is Nothing Then
                pane.OutputString("Project " & vcProj.Name & " has no valid configuration" & vbCr)
                Exit Sub
            End If
 
            Dim tools As IVCCollection = cfg.Tools
            Dim linkerTool As VCLinkerTool = tools.Item("VCLinkerTool")
            If linkerTool Is Nothing Then
                pane.OutputString("Project " & vcProj.Name & " has no output" & vbCr)
                Exit Sub
            End If
 
            linkerOutput = linkerTool.OutputFile
            linkerOutput = linkerOutput.Substring(linkerOutput.IndexOf("$(OutDir)") + 9)
 
        End If
 
        If state = True Then
            ' On
            RegKeySave("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\" & linkerOutput & "\debugger", "vsjitdebugger.exe")
 
            pane.OutputString("Project " & linkerOutput & " => Auto debug ON" & vbCr)
        Else
            ' Off
            RegKeySave("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\" & linkerOutput & "\debugger", "")
 
            pane.OutputString("Project " & linkerOutput & " => Auto debug OFF" & vbCr)
        End If
 
    End Sub
 
    Sub SetAutoDebugOn()
        SetAutoDebug(True)
    End Sub
 
    Sub SetAutoDebugOff()
        SetAutoDebug(False)
    End Sub
 
End Module

Then follow the the steps above to install and use the macro:

  • Import the script Quickies.vb into VS2010 in the Macro Explorer

         

  • Then open the customize tool in VS2010 by right clicking on the menu bar and select Customize…
  • Goto the Commands tab
  • Select ‘Toolbar’ radio button
  • Select in the list ‘Context Menus | Project and Solution Context Menus | Project’

         

  • Then do Add Command…
  • Select the category ‘Macros’
  • Select the Macros called ‘Macros.MyMacros.Quickies.SetAutoDebugOn (repeat the same thing for SetAutoDebugOff)
  • Then you can change de label using the ‘Modify Selection’ button
  • Then finally, when you right click on a project you can set it to AutoDebug On or off like this:

         

  • One last thing, in the Macro Explorer under the Reference thing you need to add two references:

         

Then when Auto Debug is on, then and the process gets launched of any manor, then you’ll get the option to debug it.

This entry was posted in Tool and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *