Automatically output the callstack on a breakpoint in Visual Studio

When you’re dealing with a large program and multiple developers, it’s not always obvious how and when certain things get executed. One of the very useful ways to debug unexpected behaviour is to set a breakpoint on a suspect line of code, and examine the callstack when it gets hit to see the execution path.

For infrequent events, it’s not always desirable to halt the entire program while you do that though. Instead, you can tell Visual Studio to write the callstack to the output when the breakpoint gets hit, and immediately continue execution.

Supported Visual Studio versions

I’m working with C++ in Visual Studio 2010 Professional. I believe the procedure is roughly the same on VS2012. I’m not sure about older versions though, or the free Express Editions.


To set your breakpoint, do the following:

  1. Create a normal breakpoint (if you don’t have one already)
  2. Right-click the breakpoint marker
  3. On the context menu, click “When Hit…”
  4. Tick the checkbox labelled “Print a message”
  5. In the text box below it, enter $CALLSTACK
  6. (You can optionally add some other text to identify the breakpoint)
  7. Make sure “Continue execution” is ticked
  8. Click OK

The “When Hit…” dialog should look like this:

Screenshot of "When Hit..." dialog

Run your program in Debug mode. When the breakpoint is hit, it should output the callstack to the Output pane in Visual Studio. (If the Output pane isn’t visible, you can usually enable it from the View menu.) Below is an example of what your callstack might look like:

MessageServer.exe!Interface::`scalar deleting destructor'() 
MessageServer.exe!std::vector<Interface,std::allocator<Interface> >::pop_back() 
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]

Advanced breakpoints

There’s a lot more you can do with breakpoints, including breaking after a certain number of hits, or when other conditions are met. For more information, check out this article on MSDN:

2 comments on “Automatically output the callstack on a breakpoint in Visual Studio

Leave a Reply

Your email address will not be published. Required fields are marked *