Condividi tramite


Start, Break, Step, Run through Code, and Stop Debugging in Visual Studio

Start debugging a VS project; attach to a process; break into code; step through code; run to the cursor; run to a function on the call stack; set next statement; step through Just My Code; Stop, restart debugging; detach from a debugged process.

Contents

Start debugging a VS project; attach to a process

Break into code, step or run through code, set the next statement to execute

Stop, restart debugging; detach from the debugged process

Start debugging a VS project; attach to a process

  • Start debugging a VS project**|**Attach to a process

Start debugging a VS project

When a solution is open is Visual Studio, you have three choices to start debugging:

Choose Debug, Start Debugging (keyboard: F5).

Your app starts and then runs until:

  • The debugger reaches a breakpoint.

  • You choose Stop Debugging on the Debug menu.

  • An exception is thrown.

  • The application exits.

Choose Debug, Step Into (keyboard: F11) or Debug, Step Over (Keyboard: F10).

Your app starts and then breaks on the first line.

In a source window, select a line of executable code and choose Run to Cursor on the context menu.

Your app starts and runs until it reaches a breakpoint or the cursor location, whichever comes first.

To visually trace your code’s execution on a code map while debugging, see Map methods on the call stack while debugging in Visual Studio.

Back to topContents**|**Start debugging a VS project; attach to a process

Attach to a process

The debugger can also to attach to a program that is running in a process outside of Visual Studio. After you attach to a program, you can use debugger execution commands, inspect the program state, and so on. Your ability to inspect the program might be limited, depending on whether the program was built with debug information and whether you have access to the program's source code, and whether the common language runtime JIT compiler is tracking debug information.

To attach to a process that is running on your local machine

Choose Debug, Attach to Process. On the Attach to Process dialog box, select the process from the Available Processes list, and then choose Attach.

Attach to Process dialog box

See Attach to Running Processes with the Visual Studio Debugger for more information.

Back to topContents**|**Start debugging a VS project; attach to a process

Break into code, step or run through code, set the next statement to execute

  • Break into code by using breakpoints or Break All**|Step into, over, or out of the code|Run to a specified location or function|Set the next statement to execute|Restrict stepping to Just My Code|Step into system calls|**Step into properties and operators in managed code

Break into code by using breakpoints or Break All

When you are debugging an application with the VS debugger, your application is either running (executing) or it is in break mode.

The debugger breaks execution of the app when execution reaches a breakpoint or when an exception occurs. You also can break execution manually at any time.

A breakpoint is a signal that tells the debugger to temporarily suspend execution of your app at a certain point. When execution is suspended at a breakpoint, your program is said to be in break mode. Entering break mode does not stop or end the execution of your program; execution can be resumed at any time.

Most debugger features, such as viewing variable values in the Locals window or evaluating expressions in the Watch window, are available only in break mode. All the elements of your app remain (functions, variables, and objects remain in memory, for example), but their movements and activities are suspended. During break mode, you can examine the elements' positions and states to look for violations or bugs. You can also make adjustments to the app while in break mode

You can configure breakpoints to suspend execution based on a number of conditions. See Breakpoints: Use Hit Counts, Call Stack Functions, and Conditions to Break When and Where You Want in the Visual Studio Debugger. This section describes two basic ways to break into code.

Set breakpoints in the code

To set a simple breakpoint in your code, open the source file in the Visual Studio editor. Set the cursor at the line of code that you want to break at, and then choose Breakpoint, Insert Breakpoint on the context menu (Keyboard: F9. The debugger breaks execution right before the line is executed.

Set a breakpoint

Breakpoints in Visual Studio provide a rich set of additional functionality, such as conditional breakpoints and tracepoints. See Breakpoints: Use Hit Counts, Call Stack Functions, and Conditions to Break When and Where You Want in the Visual Studio Debugger.

Manually break into code

To break into the next available line of code in an executing app, choose Debug, Break All (keyboard: Ctrl+Alt+Break).

  • If you are debugging with the Just My Code option enabled, you break at the next line of code in your project. See Restrict stepping to Just My Code.

  • If you are debugging multiple programs, a breakpoint or Break All command affects all programs being debugged by default. See Configure the execution behavior of multiple processes.

  • If you break while executing code without corresponding source or symbol (.pdf) files), the debugger displays a Source Files Not Found or a Symbols Not Found page that can help you find the appropriate files. See Specify Symbol (.pdb) and Source Files in the Visual Studio Debugger.

    If you can't access the supporting files, you can still debug the assembly instructions in the Disassembly window.

Back to topContents**|**Break into code, step or run through code, set the next statement to execute

Step into, over, or out of the code

One of the most common debugging procedures is stepping. Stepping is executing code one line at a time. When you have halted execution, such as running the debugger to a breakpoint, you can use three Debug menu commands to step through code:

Menu Command

Keyboard Shortcut

Description

Step Into

F11

If the line contains a function call, Step Into executes only the call itself, then halts at the first line of code inside the function. Otherwise, Step Into executes the next statement.

Step Over

F10

If the line contains a function call, Step Over executes the called function, then halts at the first line of code inside the calling function. Otherwise, Step Into executes the next statement.

Step Out

Shift+F11

Step Out resumes execution of your code until the function returns, then breaks at the return point in the calling function.

  • On a nested function call, Step Into steps into the most deeply nested function. If you use Step Into on a call like Func1(Func2()), the debugger steps into the function Func2.

  • The debugger actually steps through code statements rather than physical lines. For example an if clause can be written on one line:

    int x = 42;
    string s = "Not answered";
    if( int x == 42) s = "Answered!";
    
    Dim x As Integet = 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 in Visual Studio.

Back to topContents**|**Break into code, step or run through code, set the next statement to execute

Run to a specified location or function

Sometimes you will want to execute to a certain point in code, and then halt execution. If you have a breakpoint set at the location where you want to break, choose Debug, Start Debugging if you have not started debugging, or Debug, Continue. (In both cases F5 is the shortcut key). The debugger stops at the next breakpoint in the execution of the code. Choose Debug, Continue until you reach the breakpoint that you want.

You can also run to where you have placed the cursor in the code editor, or run to specified function.

Run to the cursor location

To run to the cursor location, place the cursor on an executable line of code in a source window. On the editor's context menu, choose Run to Cursor.

Run to a function on the call stack

In the Call Stack window, select the function, and choose Run to Cursor from the context menu. To visually trace the call stack, see Map methods on the call stack while debugging in Visual Studio.

Run to a function specified by name

You can command the debugger to run your application until it reaches a specified function. You can specify the function by name or you can choose it from the call stack.

To specify a function by name, choose Debug, New Breakpoint, Break at Function, then enter the name of the function and other identifying information.

New Breakpoint dialog box

If the function is overloaded or is in multiple namespace, you can choose the functions that you want in the Choose Breakpoints dialog box.

Choose Breakpoints dialog box

Back to topContents**|**Break into code, step or run through code, set the next statement to execute

Set the next statement to execute

After you break into the debugger, you can move the execution point 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:

  • In a source window, drag the yellow arrowhead to a location where you want to set the next statement in the same source file

  • In a source window, set the cursor on the line that you want to execute next and choose Set Next Statement on the context menu.

  • In the Disassembly window, set the cursor on the assembly instruction that you want to execute next and choose Set Next Statement on the context menu.

Warning

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

  • Instructions between the old and new execution points are not executed.

  • If you move the execution point backwards, intervening instructions are not undone.

  • Moving the next statement to another function or scope usually results in call-stack corruption, causing a run-time error or exception. If you try moving the next statement to another scope, the debugger opens a dialog box with a warning and gives you a chance to cancel the operation. In Visual Basic, you cannot move the next statement to another scope or function.

  • In native C++, if you have run-time checks enabled, setting the next statement can cause an exception to be thrown when execution reaches the end of the method.

  • When Edit and Continue is enabled, Set Next Statement fails if you have made edits that Edit and Continue cannot remap immediately. This can occur, for example, if you have edited code inside a catch block. When this happens. You’ll see an error message that tells you that the operation is not supported.

Note

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

  • The next statement is in a different method than the current statement.

  • Debugging was started by using Just-In-Time debugging.

  • A callstack unwind is in progress.

  • A System.StackOverflowException or System.Threading.ThreadAbortException exception has been thrown.

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

Back to topContents**|**Break into code, step or run through code, set the next statement to execute

Restrict stepping to Just My Code

Sometimes, while you are debugging, you might want to look at only the code you have written and ignore other code, such as system calls. You can do this with Just My Code debugging. Just My Code hides non-user code so that it does not appear in the debugger windows. When you step, the debugger steps through any non-user code but does not stop in it. See Just My Code

Note

Just My Code is not supported for device projects.

Back to topContents**|**Break into code, step or run through code, set the next statement to execute

Step into system calls

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

To learn how to disable Just My Code, see Restrict stepping to Just My Code

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 in the Visual Studio Debugger 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.

Back to topContents**|**Break into code, step or run through code, set the next statement to execute

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 and Settings. On the Debugging, General page, clear the Step over properties and operators (Managed only) check box

Back to topContents**|**Break into code, step or run through code, set the next statement to execute

Stop, restart debugging; detach from the debugged process

Stopping debugging means terminating the debugging session. Stopping execution means terminating the process you are debugging and ending the debugging session. It should not be confused with breaking execution, which temporarily halts execution of the process you are debugging but leaves the debugging session active. Detaching from a process stops debugging the process but leaves it running.

  • Stop debugging from VS**|Close the app that is being debugged|Restart debugging|**Detach from the debugged app

Stop debugging from VS

Choose Debug, Stop Debugging to terminates the process you are debugging when the program was launched from Visual Studio. If you attached to the process instead of launching it from Visual Studio, the process continues running.

Back to topContents**|**Stop, restart debugging; detach from the debugged process

Close the app that is being debugged

When you exit an app that is being debugged, debugging stops automatically.

Restart debugging

Choose Debug, Restart to stop the current debugging session and restart the startup project in the debugger.

Back to topContents**|**Stop, restart debugging; detach from the debugged process

Detach from the debugged app

Choose Debug, Detach All to stop debugging, but leave the debugged process or processes running.

Tip

See Attach to Running Processes with the Visual Studio Debugger and Debug One or More Processes in Visual Studio to learn more about attaching to and controlling multiple processes with Visual Studio.

Back to topContents**|**Stop, restart debugging; detach from the debugged process