How to set a breakpoint in every method in VS2010

后端 未结 6 1145
小蘑菇
小蘑菇 2020-12-31 02:33

I have a bigger (c#) WPF application with n-classes and m-methods. I would like to place in every single method a breakpoint, so everytime i press

相关标签:
6条回答
  • 2020-12-31 02:37

    You can use System.Diagnostics.Debugger.Break() on entry to your method.

    Something like this perhaps with a bool that you set at the scope?

    #if DEBUG
      if (BreakPointEveryMethod)
        System.Diagnostics.Debugger.Break();
    #endif
    

    There will be a quick way too add this for sure in notepad++ but I am not sure there is a quick and easy way for you to achieve this through a simple command line.

    0 讨论(0)
  • 2020-12-31 02:42

    Here's a quick and dirty way to do it using a simple text replace:

    1. Format your C# file so that all of the indentations are lined up. You can do this in Edit > Advanced > Format Document
    2. Open up text replace with Ctrl+H
    3. Set the "Text to Find" field this "^ {".
    4. Set the "Replace" field to this " {System.Diagnostics.Debugger.Break();"
    5. Click the little "Use Regular Expressions" button in the window
    6. Click "Replace All" or hit Alt+A
    7. If your file has any classes with nested enums, classes, or structs, you'll have some compiler errors. Remove the Debug calls from them until your code compiles. If your nested classes have their own methods, you'll have to run this process again with more tabs in the replace strings.

    How this works: This uses the Visual Studio document formatter and assumes that all methods in a file start with two tabs and then a "{". So any line that starts with two tabs and a "{" will get replaced with the same two tabs, the same "{", and a call to the Debugger.

    If your file has nested enums etc., you'll get compiler errors because the text replace doesn't discriminate between methods and enums. For example, you'll see this:

    enum MyColors
    { System.Diagnostics.Debugger.Break(); //error here
        Red,
        Green,
        Blue,
    }
    

    If you want the ability to disable these breakpoints, the best way I can think of is a simple bool. Somewhere in your code, insert this:

    #if DEBUG
            private static bool _ignoreDebug = false;
    #endif
    

    (I put the #if DEBUG in there as a flag that this code is only for debugging. It's not necessary) Then in step #4 above, use this replace string instead:

    "        {if(!_ignoreDebug){System.Diagnostics.Debugger.Break();}"
    

    Then when you hit a breakpoint and don't want to hit any more, in the watch window type this and hit enter _ignoreDebug = true. To turn it back on you'll need to insert a manual breakpoint somewhere that has access to the _ignoreDebug bool.


    To remove all of this from your code, either do another text replace, or just edit undo everything.

    0 讨论(0)
  • 2020-12-31 02:42

    You can use my Runtime Flow extension to see all methods called after press of a button without setting breakpoints.

    0 讨论(0)
  • 2020-12-31 02:58

    EDIT: tested only with C++

    I came across this article that shows how to set a breakpoint at the beginning of every method in a class. I've tested it with VS 2010. The basic process (when using Visual C++) is:

    1. Go to Debug > New Breakpoint > Breakpoint at Function (Ctrl + B).
    2. In the Function field, insert MyClass::*
    3. This will show up as a single breakpoint in the Breakpoints window, but as soon as one of MyClass's methods is hit, you'll see a breakpoint at the beginning of every function in MyClass, and all of these will be "children" of the original breakpoint in the Breakpoints window.

    I imagine this works with C# as well.

    0 讨论(0)
  • 2020-12-31 02:59

    This answer suggests a macro that will do as you ask, but my personal recommendation would be to use a profiler instead - one that lets you pause and resume profiling on the fly (nearly all of the commercial profilers do), and then hit the "Start Profiling" button just before you do your button click. Viewing the call tree in the profiler is often a very convenient way of gaining insight into what an application is doing, much more than stepping through in the debugger.

    UPDATE: This feature exists in a Visual Studio extension that I'm working on called OzCode. With OzCode, when you click on the icon next to the class definition, you'll see the QuickAction:

    Add Breakpoint to every member in class

    0 讨论(0)
  • 2020-12-31 03:03

    I think you create an 'aspect' for it using a tool like: postsharp

    Aspect oriented programming allows you to add code to the start or end of every method (through a postprocessing step). So it's trivial to add the line:

     System.Diagnostics.Debugger.Break()
    

    to every method (without actually editing all your sourcecode). More typically it is used to add log statements to the beginning of every method like: "Entering method DrawLine(x=30,y=80,z=12)" and at the end of a method: "Leaving method DrawLine(x,y,z)". Which makes following the flow of your program easy

    0 讨论(0)
提交回复
热议问题