20

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?

Gero
  • 12,993
  • 25
  • 65
  • 106
  • possible duplicate of [Is there a Visual Studio macro to set a breakpoint on the start of every method in a class?](http://stackoverflow.com/questions/4396613/is-there-a-visual-studio-macro-to-set-a-breakpoint-on-the-start-of-every-method) – Sjoerd Feb 07 '13 at 12:21
  • 3
    This is an insane idea, but insanely useful! – Grant Thomas Feb 07 '13 at 12:22
  • 2
    You can set many breakpoint in one step, have a look: http://www.codeproject.com/Articles/139120/Tips-Set-Breakpoint-to-multiple-Functions-at-the-s However, it's not setting a breakpoint for _every_ method. Note that this could [slow down debugging](http://stackoverflow.com/questions/589338/slow-debugging-issue-in-visual-studio) enormously. – Tim Schmelter Feb 07 '13 at 12:24
  • Thx for the link, but that does not solve the problem. I want automatically to set a breakpoint for all possible methods in a Solution without having to specify their names. – Gero Feb 07 '13 at 12:31
  • possible duplicate of [How do I set a breakpoint on every access to a class](http://stackoverflow.com/questions/3565694/how-do-i-set-a-breakpoint-on-every-access-to-a-class) – sloth Oct 02 '13 at 08:32
  • possible duplicate of [Can i set breakpoints to all methods in a class at once in visual studio?](http://stackoverflow.com/questions/11625732/can-i-set-breakpoints-to-all-methods-in-a-class-at-once-in-visual-studio) – John Saunders Oct 12 '13 at 15:18
  • Check out my answer at http://stackoverflow.com/questions/3565694/how-do-i-set-a-breakpoint-on-every-access-to-a-class/23345470#23345470 – Guido Tarsia Apr 28 '14 at 15:45

6 Answers6

7

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.

Vicky Chijwani
  • 10,191
  • 6
  • 56
  • 79
  • Thank you for the link, but I was not able to reproduce the same effect with my c# class. No Breakpoints. Could you please just make a small class with 2 methods and check if this works with c# too? – Gero Aug 28 '13 at 12:08
  • 5
    After some searching I found [this answer](http://stackoverflow.com/a/15028569/504611). One of the comments on it says this method only works with C++. Apologies for the misleading answer. – Vicky Chijwani Aug 28 '13 at 12:23
5

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

Community
  • 1
  • 1
Omer Raviv
  • 11,409
  • 5
  • 43
  • 82
  • @Salim There are many different ones, JetBrains dotTrace and Red Gate ANTS are a few examples. Please note that the technique I described will only work in instrumentation mode (where every single method entry is recorded), and not in the profiler's Sampling Mode, which may not include certain methods if they were only executed for a very short period of time. – Omer Raviv Dec 30 '14 at 11:47
1

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.

user2023861
  • 8,030
  • 9
  • 57
  • 86
0

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

Toad
  • 15,593
  • 16
  • 82
  • 128
0

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

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
-1

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.

LukeHennerley
  • 6,344
  • 1
  • 32
  • 50