Wednesday, April 13, 2011

Visual Studio Macro to Break on Every Method in File

While tracking down the source of certain features (read bugs) within overly complex applications, I have sometimes found it useful to break on every call to every method within a file.

The functionality to "Break on Every Method" is not built into Visual Studio, but it is possible to set up the necessary break points through a macro.  I did not originally write this macro, but while trying to find it again at a recent job I couldn't, so I decided to post it here for safe keeping.  It is fairly primitive, but it gets the job done.  Simply place the cursor within the file you are working with right before the first method you want to break on.  The macro will then search through the file for each opening brace "{" and place a break point at each one.  Hope it helps someone.

To "install" this macro simply open up your macro explorer, edit a module and paste this macro/method in.  If you do not have any macros you will need to record an empty macro first to get a default module created for you.

--------

Sub BreakOnAllMethods()
    Dim returnValue As vsIncrementalSearchResult

    Dim findresult As vsFindResult

    Dim safeguard As Integer

    safeguard = 1

    DTE.Find.FindWhat = "{"
    DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
    DTE.Find.MatchCase = False
    DTE.Find.MatchWholeWord = False
    DTE.Find.Backwards = False
    DTE.Find.MatchInHiddenText = True
    DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
    DTE.Find.Action = vsFindAction.vsFindActionFind

    While safeguard < 100

        findresult = DTE.Find.Execute()

        If (findresult = vsFindResult.vsFindResultNotFound) Then
            Return
        End If

        DTE.ExecuteCommand("Debug.ToggleBreakpoint")
        DTE.ExecuteCommand("Edit.GotoBrace")
        DTE.ActiveDocument.Selection.CharRight()

        safeguard = safeguard + 1

    End While

End Sub


--------

Enjoy!
Post a Comment