Visualize and Debug Code Execution with Call Stacks in Visual Studio
Create a code map to trace the call stack visually while you’re debugging. You can make notes on the map to track what the code is doing so you can focus on finding bugs.
You’ll need:
Code that you can debug, such as Visual C# .NET, Visual Basic .NET, C++, or JavaScript
See: Video: Debug visually with Code Map debugger integration (Channel 9) • Map the call stack • Make notes about the code • Update the map with the next call stack • Add related code to the map • Find bugs using the map • Q & A
Map the call stack
Start debugging.
When your app enters break mode or you step into a function, choose Code Map. (Keyboard: Ctrl + Shift + `)
The current call stack appears in orange on a new code map:
The map might not look interesting at first, but it updates automatically while you continue debugging. See Update the map with the next call stack.
Make notes about the code
Add comments to track what’s happening in the code. To add a new line in a comment, press Shift + Return.
Update the map with the next call stack
Run your app to the next breakpoint or step into a function. The map adds a new call stack.
Add related code to the map
Now you’ve got a map – what next? If you’re working with Visual C# .NET or Visual Basic .NET, add items, such as fields, properties, and other methods, to track what’s happening in the code.
Double-click a method to see its code definition. (Keyboard: Select the method on the map and press F12)
Add the items that you want to track on the map.
Here you can easily see which methods use the same fields. The most recently added items appear in green.
Continue building the map to see more code.
Find bugs using the map
Visualizing your code can help you find bugs faster. For example, suppose you’re investigating a bug in a drawing program. When you draw a line and try to undo it, nothing happens until you draw another line.
So you set breakpoints, start debugging, and build a map like this one:
You notice that all the user gestures on the map call Repaint, except for undo. This might explain why undo doesn’t work immediately.
After you fix the bug and continue running the program, the map adds the new call from undo to Repaint:
Q & A
Not all calls appear on the map. Why?
By default, only your code appears on the map. To see external code, turn it on in the Call Stack window or turn off Enable Just My Code in the Visual Studio debugging options.
Does changing the map affect the code?
Changing the map doesn’t affect the code in any way. Feel free to rename, move, or remove anything on the map.
What does this message mean: “The diagram may be based on an older version of the code”?
The code might have changed after you last updated the map. For example, a call on the map might not exist in code anymore. Close the message, then try rebuilding the solution before updating the map again.
How do I control the map’s layout?
Open the Layout menu on the map toolbar:
Change the default layout.
To stop rearranging the map automatically, turn off Automatic Layout when Debugging.
To rearrange the map as little as possible when you add items, turn off Incremental Layout.
Can I share the map with others?
You can export the map, send it to others if you have Microsoft Outlook, or save it to your solution so you can check it into Team Foundation version control.
How do I stop the map from adding new call stacks automatically?
Choose on the map toolbar. To manually add the current call stack to the map, press Ctrl + Shift + `.
The map will continue highlighting existing call stacks on the map while you’re debugging.
What do the item icons and arrows mean?
To get more info about an item, look at the item’s tooltip. You can also look at the Legend to learn what each icon means.
See: Map the call stack • Make notes about the code • Update the map with the next call stack • Add related code to the map • Find bugs using the map