How to set a breakpoint in every method in VS2010

2020-02-26 04:44发布

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 a button in my application or any method gets called, i would like the application in VS2010 to hit that breakpoint. I want to understand the flow/progress of the application.

And since i have many methods i would rather not place manually in every and each of them a breakpoint.

Is there any command or tool to place everywhere in my VS2010 solution a breakpoint?

edit: maybe something like the following addin: http://weblogs.asp.net/uruit/archive/2011/08/04/visual-studio-2010-addin-setting-a-class-breakpoint.aspx

edit2: there are some answers but none of them seems like the straight forward easy solution. Anything else?

6条回答
SAY GOODBYE
2楼-- · 2020-02-26 04:52

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

查看更多
唯我独甜
3楼-- · 2020-02-26 04:57

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.

查看更多
老娘就宠你
4楼-- · 2020-02-26 05:06

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

查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-26 05:08

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

查看更多
6楼-- · 2020-02-26 05:12

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.

查看更多
干净又极端
7楼-- · 2020-02-26 05:13

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.

查看更多
登录 后发表回答