How to enable/disable all breakpoints in single keystroke?

9 06 2008

During heavy debugging, we usually have dozens of breakpoints dropped in various locations of source. Some of these breakpoints can also be in the application startup route. While stopping and starting programs during debugging, these breakpoints are really irritating, because they stop always. I’ve to press F5 a couple to times to start the application and run it. If there’s some shortcuts to enable and disable all break points at one single keystroke, it will be nice. isn’t it? But how?

You can use the VisualStudio’s powerful object model to do so. See the macro code for enabling and disabling the breakpoints. In later steps, i’ll explain how to map it to keystroke. The macro code is as follows,

' Macro to Enable all Breakpoints.
Sub EnableBreakPoints()
    Dim myBreakpoint
    For Each myBreakpoint in Debugger.Breakpoints
        ' Enable the break points.
End Sub

' Macro to Disable all Breakpoints.
Sub DisableBreakPoints()
    Dim myBreakpoint
    For Each myBreakpoint in Debugger.Breakpoints
        ' Disable the break points.
End Sub

How to map the macros to Keystroke
1) Take Tools > Macro.
2) Now you’ll see your macro window and i assume no macros are defined in your macro file. For getting the macro editor, enter some dummy macro name and click “edit” button.

3) Now Macro editor will be opened and copy-paste the Enable/Disable macro code in your macro editor and save it.
4) Now take Tools > Macro again and VisualStudio will ask, whether to reload the macros. Press Yes.
5) Now you’ll see the macro dialog, and click “Options” and “Keystrokes“.

6) Now select the macro and press a key combination, then click “Assign”.

7) The greatest difficult that you’ll face is to find a free slot of key combination. Almost all are reserved by visual studio itself. 🙂 After some R&D I’ve found two working key combinationAlt+z and Alt+x. Map your enable/disable macros to those shortcuts.

8 ) Now take visual studio and press the key combination to disable the breakpoints. Note that the breakpoints will not be visually disabled but if you take breakpoint window by Ctrl+B you can see all are disabled. This happens because we’ve modified the internal structure of VisualStudio, but GUI is not refreshed.

The only drawback is the breakpoints will not be visually disabled on keystroke. I’m searching for some solution and hope i’ll be back soon. Till then enjoy this one. 😉

Targeted Audience – Intermediate.

Stringizing Operator, Charizing Operator and Token pasting operator.

29 05 2008

What ever C++ best practices says, its a bit tough to eliminate macros. Especially in extensible frameworks. The best example is MFC. Indeed macros have drawbacks, but its the best wepon to inject code inside client classes which uses the framework.

For instance, the MFC message map itself. The message map support is provided by three macros – DECLARE_MESSAGE_MAP(), BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP(). These macros declare static message map array and some utility functions to class. Indeed, the user is free from all those complications and everything are buried deep inside these macros .

There are some helpful operators which helps you to write powerful marcos. They are #, #@ and ##.

Stringizing Operator( # )
As name says – Stringizing Operator converts the macro parameter to a string. if # is prefixed before the parameter name, and used in the macro body, that parameter will get enclosed in double quotes. For instance see the following macro used for showing messagebox. The message – CDialogDlg::OnButton will get expanded to “CDialogDlg::OnButton”

MESSAGE_BOX( CDialogDlg::OnButton );
// The macro which shows Message.
#define MESSAGE_BOX( Message ) \
    AfxMessageBox( _T( #Message )); \

if you want to see a real world example, check the following line from DAOCORE.CPP of MFC. A DAO trace macro.

#define DAO_TRACE(f) AfxDaoTrace(f, #f, THIS_FILE, __LINE__)

Charizing Operator( #@ )
The Charizing operator is used to convert a macro parameter to a charector constant. If its prefixed in macro parameter, the parameter get enclosed in single quotes to form a char constant. For instance see the e.g. below. Its taken from MSDN.

// Same as a = 'b';
char a = MAKECHAR( b );
#define MAKECHAR( x )  #@x

Token-Pasting Operator (##)
Its also known as “Merging operator”. If its prefixed in macro parameter, that token just get pasted for each occurance. The best example is _T() macro. See its definition below.

// Assume unicode is defined.
// This will expand to csString = L"Hello";
CString csString = _T( "Hello" );
#define _T(x)       __T(x)
#define __T(x)      L ## x

Here the ##x pasts the token directly there. See one more example to see the power of token pasting operator. It can also be used to give unique names to the member variables in the injected code. See the definition of DECLARE_DYNAMIC() macro. Here the class name is pasted to generate unique name for the member variable injected.

#define DECLARE_DYNAMIC(class_name) \
protected: \
    static CRuntimeClass* PASCAL _GetBaseClass(); \
public: \
    static const AFX_DATA CRuntimeClass class##class_name; \
    virtual CRuntimeClass* GetRuntimeClass() const; \

But still keep in mind that macros are twin side sharp swords. If not properly used, … 😉

Targeted Audience – Intermediate.