Can I set breakpoints to all methods in a class at once in Visual Studio?

29,460

Solution 1

There is an addon-less method described here: How to set a breakpoint on a C++ class in the Visual Studio Debugger

In short, you can bring up the "New Breakpoint" dialog by pressing Ctrl+K, B and type in ClassName::* to the function field. In Visual Studio 2017 you need to include the namespace in the field, as in NamespaceName::ClassName::*. You can then disable some of them in the breakpoints window.

Solution 2

Here's your macro, but it takes a while to set breakpoints on 1000+ functions... and it WILL slow down Visual Studio!

Sub BreakAtEveryFunction()
    For Each project In DTE.Solution.Projects
        SetBreakpointOnEveryFunction(project)
    Next project
End Sub


' Macro editor
Sub SetBreakpointOnEveryFunction(ByVal project As Project)
    Dim cm = project.CodeModel

    ' Look for all the namespaces and classes in the 
    ' project.
    Dim list As List(Of CodeFunction)
    list = New List(Of CodeFunction)
    Dim ce As CodeElement
    For Each ce In cm.CodeElements
        If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then
            ' Determine whether that namespace or class 
            ' contains other classes.
            GetClass(ce, list)
        End If
    Next

    For Each cf As CodeFunction In list

        DTE.Debugger.Breakpoints.Add(cf.FullName)
    Next

End Sub

Sub GetClass(ByVal ct As CodeElement, ByRef list As List(Of CodeFunction))

    ' Determine whether there are nested namespaces or classes that 
    ' might contain other classes.
    Dim aspace As CodeNamespace
    Dim ce As CodeElement
    Dim cn As CodeNamespace
    Dim cc As CodeClass
    Dim elements As CodeElements
    If (TypeOf ct Is CodeNamespace) Then
        cn = CType(ct, CodeNamespace)
        elements = cn.Members
    Else
        cc = CType(ct, CodeClass)
        elements = cc.Members
    End If
    Try
        For Each ce In elements
            If (TypeOf ce Is CodeNamespace) Or (TypeOf ce Is CodeClass) Then
                GetClass(ce, list)
            End If
            If (TypeOf ce Is CodeFunction) Then
                list.Add(ce)
            End If
        Next
    Catch
    End Try
End Sub

Solution 3

The accepted answer didn't work for me for some reason. And I don't think my workaround applies to Visual Studio 2010. But I used the Macros for Visual Studio extension with my Visual Studio 2015 to do this.

Steps:

  1. Find (Ctrl+F) the right indentation for the opening brace of the methods. Typically that is 8 white spaces (or 2 tabs etc. based upon the settings you might've made).
  2. Append this with an opening brace {.
  3. Prepend this with \r\n to make sure it does not match any nested braces. Now it might look like \r\n {. Also, turn on the regular expression search (by pressing the * on the search dialog).
  4. Start recording a macro.
  5. Press F3 and then press F9 to add a breakpoint. This adds a breakpoint to the first method found using the trick.
  6. Stop recording the macro. Play it for the number of method you might have.
  7. Caution: Be aware of when you reach the end. Otherwise it will start again from the top and that start removing the breakpoints you just added.

Let me know it there is any confusion.

Solution 4

There's a class breakpoint add-in you could try, or you could use a replace expression to add a __debugbreak() at the start of each method.

Share:
29,460
Charu
Author by

Charu

Updated on July 09, 2022

Comments

  • Charu
    Charu almost 2 years

    I have 40-50 methods in a class, I want to add breakpoints to all of them. Can I add breakpoints to all of them at once?

  • goamn
    goamn about 10 years
    Can you please elaborate on the second part of your answer?
  • goamn
    goamn about 10 years
    To get this script to work in VS 2012 you can copy in the functions into a new Addin project and call "BreakAtEveryFunction" inside the "OnConnection" method of the Addin project. The script is a bit too comprehensive in that it does .NET methods, also has errors here and there, the Addin project will complain about "DTE.Debugger" and "DTE.Solution.Projects", change "DTE" to "_applicationObject". For more about the Addin project see This answer.
  • cnh003
    cnh003 almost 5 years
    just an update: it does work in vs 2017, I just did that. However, you "might" need to provide the full qualification, including the namespace.
  • cSharpDotNetGuy
    cSharpDotNetGuy about 4 years
    Do you know what syntax use to to set a break point to all functions in a class in VB.net using Visual Studio Professional 2012? I tried NamespaceName.ClassName.* but it didn't work. Thank you.
  • buttonsrtoys
    buttonsrtoys almost 3 years
    @vt. Not working for me with namespaces. What do you mean by "the full qualification"?
  • buttonsrtoys
    buttonsrtoys almost 3 years
    Dammit. According to developercommunity.visualstudio.com/t/… its broken and was fixed in VS2019 but not for 2017?
  • Anirudh Singh Rawat
    Anirudh Singh Rawat over 2 years
    I'm using mvs 2017 and Im writing the excat same command "NamespaceName::ClassName::*" in New Function Breakpoint but nothing is happening.
  • moudrick
    moudrick over 2 years
    Ctrl-K B is for New Function Breakpoint in VS-2019 out-of-box. Ctrl-B is for Build Project. Don't also confuse this with Ctrl-K Ctrl-B which is Code Snippet Manager... Please community check/confirm this and update the answer accordingly.
  • moudrick
    moudrick over 2 years
    The link just does not show any information on the topic