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.

Step-by-step

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!Protocol::clear() 
MessageServer.exe!Protocol::~Protocol() 
MessageServer.exe!Interface::~Interface() 
MessageServer.exe!Interface::`scalar deleting destructor'() 
MessageServer.exe!std::_Destroy<Interface>() 
MessageServer.exe!std::allocator<Interface>::destroy() 
MessageServer.exe!std::_Dest_val<std::allocator<Interface>,Interface>() 
MessageServer.exe!std::vector<Interface,std::allocator<Interface> >::pop_back() 
MessageServer.exe!Connection::negotiateHandler() 
MessageServer.exe!Connection::send() 
MessageServer.exe!ConnectionManager::broadcastToAll() 
MessageServer.exe!main() 
MessageServer.exe!__tmainCRTStartup() 
MessageServer.exe!mainCRTStartup() 
kernel32.dll!7632ed5c() 
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
ntdll.dll!777137eb() 
ntdll.dll!777137be() 

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:

I'm a software engineer at Vertual Ltd., working on virtual reality medical training systems for radiotherapy. I work mainly with C/C++, using OpenGL for graphics. However, I also have experience with C#, Java, Python, PHP, SQL, JavaScript, and HTML/CSS. I have a BSc Hons. (1st class) in Computer Games Technology, and am currently writing-up a PhD thesis in educational technology. In my free time, I'm a Karate Nerd, keen musician, electronics hobbyist, and all-round geek.

Posted in Programming Tagged with: , ,
2 comments on “Automatically output the callstack on a breakpoint in Visual Studio
  1. Minor edit: I just noticed that the angle brackets in the callstack example hadn’t been escaped properly in my original post. I’ve fixed that now.

  2. Shibbu says:

    Very helpful article. Thanks Peter for sharing this information. I needed it much.

Leave a Reply

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

*


eight + 1 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About

Avid Insight is my personal blog about various software, programming, electronics, and occasionally academic things. I also have a few past projects linked in the navigation menu above, so feel free to look around!

Categories

Archives