Navigating through Code with the Debugger - Visual Studio 2015 (original) (raw)


Share via


Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Get familiar with commands and shortcuts to navigate code in the debugger and that will make it faster and easier to find and resolve issues in your app. While you navigate code in the debugger, you can inspect the state of your app or learn more about its execution flow.

Start debugging

Often, you start a debugging session using F5 (Debug / Start Debugging). This command starts your app with the debugger attached.

The green arrow also starts the debugger (same as F5).

DBG_Basics_Start_Debugging

A few other ways that you can start the app with the debugger attached include F11 (step into code), F10 (step over code), or by using Run to Cursor. See the other sections in this topic for info on what these options do.

When you debug, the yellow line shows you the code that will execute next.

DBG_Basics_Break_Mode

While debugging, you can switch between commands like F5, F11 and use other features described in this topic (like breakpoints) to quickly get to the code you want to look at.

Most debugger features, such as viewing variable values in the Locals window or evaluating expressions in the Watch window, are available only while the debugger is paused (also called break mode). When the debugger is paused, your app state is suspended while functions, variables, and objects remain in memory. While in break mode, you can examine the elements' positions and states to look for violations or bugs. For some project types, you can also make adjustments to the app while in break mode.

Step into code, line by line

To stop on each line of code (each statement) while debugging, use the F11 keyboard shortcut (or Debug / Step Into on the menu).

Tip

As you execute each line of code, you can hover over variables to see their values, or use the Locals and Watch windows to watch their values change.

Here are some details about the behavior of Step Into:

int x = 42;  
string s = "Not answered";  
if( int x == 42) s = "Answered!";  
Dim x As Integer = 42  
Dim s As String = "Not answered"  
If x = 42 Then s = "Answered!"  

When you step into this line, the debugger treats the condition as one step and the consequence as another (In this example, the condition is true).
To visually trace the call stack while stepping into functions, see Map methods on the call stack while debugging.

Step through code, skipping functions

When running code in the debugger, often you will realize that you don't need to see what happens in a particular function (you don't care about it or you know it works, like well-tested library code). Use these commands to skip through code (the functions still execute, of course, but the debugger skips over them).

Keyboard Command Menu Command Description
F10 Step Over If the current line contains a function call, Step Over runs the code then suspends execution at the first line of code after the called function returns.
Shift+F11 Step Out Step Out continues running code and suspends execution when the current function returns (the debugger skips through the current function).

Tip

If you need to find the entry point in your app, start with F10 or F11. These commands are often helpful when you are inspecting your app state or trying to find out more about its execution flow.

Run to a specific location or function

Often the preferred method of debugging code, these methods are useful when you know exactly what code you want to inspect, or at least you know where you want to start debugging.

Move the pointer to change the execution flow

While the debugger is paused, you can move the instruction pointer to set the next statement of code to be executed. A yellow arrowhead in the margin of a source or Disassembly window marks the location of the next statement to be executed. By moving this arrowhead, you can skip over a portion of code or return to a line previously executed. You can use this for situations such as skipping a section of code that contains a known bug.

Example2

To set the next statement to execute, use one of these procedures:

Caution

Setting the next statement causes the program counter to jump directly to the new location. Use this command with caution:

Note

In managed code, you cannot move the next statement under the following conditions:

You cannot set the next statement while your application is actively running. To set the next statement, the debugger must be in break mode.

Step into non-user code

By default, the debugger tries to show you only your app code while debugging, which is determined by a debugger setting called Just My Code. (See Just My Code to see how this works for different project types and languages and how you might customize the behavior.) However, sometimes while you are debugging, you might want to look at framework code, third-party library code, or calls to the operating system (system calls).

You can turn off Just My Code by going to Tools / Options / Debugging and clear the Enable Just My Code checkbox.

When Just My Code is disabled, the debugger can step into non-user code and non-user code appears in the debugger windows.

Note

Just My Code is not supported for device projects.

Step into system calls

If you have loaded debugging symbols for system code and Just My Code is not enabled, you can step into a system call just as you can any other call.

To access Microsoft symbol files, see Use symbol servers to find symbol files not on your local machine in the Specify Symbol (.pdb) and Source Files topic.

To load symbols for a specific system component while you are debugging:

  1. Open the Modules window (keyboard: Ctrl+Alt+U).
  2. Select the module that you want to load symbols for.
    You can tell which modules have symbols loaded by looking at the Symbol Status column.
  3. Choose Load Symbols on the context menu.

Step into properties and operators in managed code

The debugger steps over properties and operators in managed code by default. In most cases, this provides a better debugging experience. To enable stepping into properties or operators, choose Debug / Options. On the Debugging / General page, clear the Step over properties and operators (Managed only) check box