Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

break whenever a file (or class) is entered

In Visual Studio, is there any way to make the debugger break whenever a certain file (or class) is entered? Please don't answer "just set a breakpoint at the beginning of every method" :)

I am using C#.

like image 782
JoelFan Avatar asked Oct 30 '08 15:10

JoelFan


3 Answers

Macros can be your friend. Here is a macro that will add a breakpoint to every method in the current class (put the cursor somewhere in the class before running it).

Public Module ClassBreak
    Public Sub BreakOnAnyMember()
        Dim debugger As EnvDTE.Debugger = DTE.Debugger
        Dim sel As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
        Dim editPoint As EnvDTE.EditPoint = sel.ActivePoint.CreateEditPoint()
        Dim classElem As EnvDTE.CodeElement = editPoint.CodeElement(vsCMElement.vsCMElementClass)

        If Not classElem Is Nothing Then
            For Each member As EnvDTE.CodeElement In classElem.Children
                If member.Kind = vsCMElement.vsCMElementFunction Then
                    debugger.Breakpoints.Add(member.FullName)
                End If
            Next
        End If
    End Sub

End Module

Edit: Updated to add breakpoint by function name, rather than file/line number. It 'feels' better and will be easier to recognise in the breakpoints window.

like image 151
Richard Szalay Avatar answered Nov 17 '22 09:11

Richard Szalay


You could start by introducing some sort of Aspect-Oriented Programming - see for instance this explanation - and then put a breakpoint in the single OnEnter method.

Depending on which AOP framework you choose, it'd require a little decoration in your code and introduce a little overhead (that you can remove later) but at least you won't need to set breakpoints everywhere. In some frameworks you might even be able to introduce it with no code change at all, just an XML file on the side?

like image 34
Steve Eisner Avatar answered Nov 17 '22 07:11

Steve Eisner


Maybe you could use an AOP framework such as PostSharp to break into the debugger whenever a method is entered. Have a look at the very short tutorial on this page for an example, how you can log/trace whenever a method is entered.

Instead of logging, in your case you could put the Debugger.Break() statement into the OnEntry-handler. Although, the debugger would not stop in your methods, but in the OnEntry-handler (so I'm not sure if this really helps).

Here's a very basic sample:

The aspect class defines an OnEntry handler, which calls Debugger.Break():

[Serializable]
public sealed class DebugBreakAttribute : PostSharp.Laos.OnMethodBoundaryAspect
{
    public DebugBreakAttribute() {}
    public DebugBreakAttribute(string category) {}
    public string Category { get { return "DebugBreak"; } }

    public override void OnEntry(PostSharp.Laos.MethodExecutionEventArgs eventArgs)
    {
        base.OnEntry(eventArgs);
        // debugger will break here. Press F10 to continue to the "real" method
        System.Diagnostics.Debugger.Break();
    }
}

I can then apply this aspect to my class, where I want the debugger to break whenever a method is called:

[DebugBreak("DebugBreak")]
public class MyClass
{
    public MyClass()
    {
        // ...
    }
    public void Test()
    {
        // ...
    }
}

Now if I build and run the application, the debugger will stop in the OnEntry() handler whenever one of the methods of MyClass is called. All I have to do then, is to press F10, and I'm in the method of MyClass.

like image 10
M4N Avatar answered Nov 17 '22 07:11

M4N