47

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?

user2771704
  • 5,994
  • 6
  • 37
  • 38
Charu
  • 2,679
  • 6
  • 35
  • 52

6 Answers6

27

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.

vt.
  • 1,325
  • 12
  • 27
  • 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. – cnh003 Sep 03 '19 at 13:56
  • 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. – cSharpDotNetGuy May 01 '20 at 17:25
  • @vt. Not working for me with namespaces. What do you mean by "the full qualification"? – buttonsrtoys Jun 27 '21 at 22:07
  • Dammit. According to https://developercommunity.visualstudio.com/t/cannot-use-classname-in-function-breakpoint-to-set/982182 its broken and was fixed in VS2019 but not for 2017? – buttonsrtoys Jun 27 '21 at 22:09
  • 3
    I'm using mvs 2017 and Im writing the excat same command "NamespaceName::ClassName::*" in New Function Breakpoint but nothing is happening. – Anirudh Singh Rawat Oct 08 '21 at 12:49
  • 6
    **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 Oct 26 '21 at 10:59
  • 1
    The link just does not show any information on the topic – moudrick Oct 26 '21 at 12:03
5

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
alexkovelsky
  • 3,880
  • 1
  • 27
  • 21
  • 2
    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.](http://stackoverflow.com/questions/12027485/alternative-to-macros-in-visual-studio-2012/12394986#12394986) – goamn May 19 '14 at 21:28
1

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.

PhilMY
  • 2,621
  • 21
  • 29
1

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.

Community
  • 1
  • 1
Nikhil Girraj
  • 1,135
  • 1
  • 15
  • 33
0

If you use vim (vsvim) you can manipulate breakpoints fairly easily. Here are some examples.

break on every line:{Escape}qq:vsc Debug.ToggleBreakpoint{Enter}jq100@q

break on every method:

{Escape}qq:vsc Edit.NextMethod{Enter}:vsc Debug.ToggleBreakpoint{Enter}jq100@q

replace the 100 with the appropriate number of lines/methods.

example: https://i.stack.imgur.com/rqdRc.jpg

dog
  • 1,307
  • 13
  • 10
-3

Click on Debug tab and select "Debug All". Then call the method/controller/function that you want debugging and step through it with debuger (not continue, unless you have breakpoints set).

Elvis Skensberg
  • 151
  • 1
  • 9