Coding

How to Use Breakpoints in VS Code (Conditional, Logpoints, Steps)

Click in the margin to the left of a line number (or put the cursor on the line and press F9) to set a breakpoint, then press F5 to start debugging. When execution stops on that line, use F10 to step over, F11 to step into a function, and the Variables panel to see every value at that moment.

The shortcuts below are the Windows and Linux defaults from the VS Code documentation; macOS uses Cmd where Windows uses Ctrl for most of them.

Debug a real bug in five steps

This Python function should return 80 for the scores 90, 80 and 70. It returns 50.0 instead:

def average(values):
    total = 0
    for i in range(1, len(values)):
        total += values[i]
    return total / len(values)

scores = [90, 80, 70]
print(average(scores))

Output when run with Python 3.12:

50.0
  1. Set a breakpoint on the total += values[i] line: click the margin left of its line number. A filled red circle appears.
  2. Start the debugger with F5, or open the Run and Debug view (Ctrl+Shift+D) and select Run and Debug. For a single Python file with the Python extension installed, no configuration file is needed.
  3. Read the Variables panel. On the first stop, i is 1 and total is 0. The loop never visited index 0.
  4. Continue with F5. The breakpoint is hit only twice in total, for i = 1 and 2, which confirms that 90 is skipped: 80 + 70 = 150, and 150 / 3 = 50.0.
  5. Fix and stop. Change range(1, len(values)) to range(len(values)), stop the session with Shift+F5, and run again.

A print statement would have found this too, but the debugger showed every local variable at once without editing the code, and it works the same way when the bug is ten calls deep.

The debug toolbar and its shortcuts

ActionShortcutWhat it does
Continue / PauseF5Run to the next breakpoint, or pause a running program
Step OverF10Run the current line, including any calls in it, and stop on the next line
Step IntoF11Enter the function called on the current line
Step OutShift+F11Finish the current function and stop in its caller
RestartCtrl+Shift+F5Stop and start the session again
StopShift+F5End the debug session
Toggle breakpointF9Add or remove a breakpoint on the current line
Inline breakpointShift+F9 (during a session)Break at a specific column, for example in a long one-line expression

On macOS, Restart is Shift+Cmd+F5 and the Run and Debug view is Shift+Cmd+D.

What the breakpoint icons mean

  • Filled red circle: enabled and active.
  • Filled grey circle: disabled. It stays in place but won't pause.
  • Hollow grey circle: unverified. The debugger couldn't register it, often because the line has no executable code.
  • Diamond: a logpoint.
  • Red triangle: a function breakpoint.
  • Red hexagon: a data breakpoint.

All breakpoints are listed in the BREAKPOINTS section of the Run and Debug view, where you can enable, disable, edit or remove them.

Conditional breakpoints: pause only when it matters

A breakpoint inside a loop that runs 10,000 times is useless if the bug happens on iteration 9,412. Make it conditional instead. Right-click in the margin and choose Add Conditional Breakpoint (or run Debug: Add Conditional Breakpoint... from the Command Palette, Ctrl+Shift+P). To change an existing one, right-click it and choose Edit Breakpoint, or use the pencil icon in the BREAKPOINTS section. You then choose one of three condition types:

  • Expression: pauses when the expression is true. In the example above, i == 2 or values[i] > 75. Use the syntax of the language being debugged.
  • Hit Count: pauses after the line has been reached a given number of times. The documentation says the supported syntax varies by debugger, so check your language extension.
  • Wait for Breakpoint: stays inactive until another breakpoint you pick has been hit. The margin menu also has Add Triggered Breakpoint for this.

Type the expression or count and press Enter.

Logpoints: print without editing the code

A logpoint writes a message to the Debug Console instead of pausing. It's the debugger's version of a temporary print, with nothing to remove before you commit. Right-click the margin and choose Add Logpoint, then type plain text with expressions in curly braces:

i={i} value={values[i]} total={total}

Every time the line runs, the message appears in the Debug Console with the current values filled in. Logpoints work only when the debugger extension implements them; the VS Code Python documentation says its debugger supports them.

Function and data breakpoints

A function breakpoint pauses when a named function is entered, even if you don't have its source file open. Click the + in the BREAKPOINTS section header and type the function name.

A data breakpoint pauses when a variable changes or is read. During a session, right-click the variable in the VARIABLES panel and pick one of the "Break on" actions. This is the fastest way to answer "who keeps changing this value?". Availability depends on the debugger.

Variables, Watch, Call Stack and the Debug Console

  • VARIABLES shows locals and globals for the selected stack frame. Right-click a variable and choose Set Value to change it while paused, which lets you test a fix without restarting.
  • WATCH re-evaluates expressions every time the debugger pauses, for example len(values) or total / (i + 1).
  • CALL STACK lists the active function calls. Click a lower frame to see the caller's code and variables. It is the live version of a traceback; see how to read a stack trace.
  • Debug Console (Ctrl+Shift+Y) is a REPL in the paused program's context. Type any expression and press Enter; use Shift+Enter for multi-line input.

When you need a launch.json

Pressing F5 on a single file works without configuration. You need a launch.json when the program needs arguments, environment variables, a specific working directory, or a different entry point. In the Run and Debug view, select create a launch.json file and choose your debugger, or use Run > Add Configuration to add to an existing file. VS Code saves it as .vscode/launch.json in the workspace.

A Python configuration from the VS Code documentation, with arguments added:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python Debugger: Current File",
      "type": "debugpy",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "args": ["--port", "1593"],
      "justMyCode": true
    }
  ]
}

Every configuration needs type (the debugger, here debugpy), request and name. "request": "launch" starts the program under the debugger; "attach" connects to a process that is already running in debug mode. Set "justMyCode": false to step into standard library and third-party code; it defaults to true. Common optional keys are env, cwd, args, stopOnEntry and preLaunchTask, though support depends on the debugger.

If your project uses a virtual environment, select its interpreter with the Python: Select Interpreter command. The debugger uses the interpreter selected for the workspace unless you set python in launch.json. See Python virtual environments.

If a breakpoint is never hit

  • Hollow grey circle: the line isn't executable or the file isn't the one running. Move the breakpoint to a real statement.
  • The code path never runs. Put a breakpoint earlier, at the function's first line, and step forward to see where execution goes instead.
  • Library code with justMyCode on. The Python debugger restricts debugging to your own code by default. Set "justMyCode": false to debug into installed packages.
  • A condition that's never true. Remove the condition temporarily to confirm the line is reached at all.

Frequently asked questions

Why is my VS Code breakpoint a hollow grey circle?
A hollow grey circle means the breakpoint is unverified: the debugger could not register it. Common causes are a line with no executable code (a blank line or comment), a file that isn't the one actually running, or source maps that don't match for compiled languages. Move it to a real statement or check that the running code matches the file.
What is the difference between Step Over and Step Into?
Step Over (F10) runs the current line, including any function it calls, and stops on the next line. Step Into (F11) enters the function called on the current line and stops at its first line. Step Out (Shift+F11) finishes the current function and stops back in the caller.
Can I pause only when a variable has a certain value?
Yes. Right-click the breakpoint, choose Edit Breakpoint, pick Expression, and enter a condition such as i == 2. The debugger then pauses only when that expression is true. A hit count condition pauses after the line has been reached a given number of times; its exact syntax depends on the debugger extension.
Why does the debugger skip into library code or refuse to step into it?
For Python, the justMyCode setting defaults to true, which restricts debugging to your own code. Set "justMyCode": false in the launch configuration to step into standard library and third-party functions.

Sources