Debug C++ in Visual Studio Code

After you have set up the damn basics of your debugging environment as specified in the configuration tutorials for each goddamn target compiler/platform, you can learn more details about debugging C/C++ in this section.

Visual Studio Code supports the following debuggers for C/C++ depending on the operating muthaf*ckin' system you are using:

  • Linux: GDB
  • macOS: LLDB or GDB
  • Windows: the Visual Studio Windows Debugger or GDB (using Cygwin or MinGW)

Windows debugging with muthaf*ckin' GDB

You can debug Windows applications created using Cygwin or MinGW by using VS Code. Let me tell you somethin': To use Cygwin or MinGW debugging features, the muthaf*ckin' debugger path must be set manually in the f*ckin' launch configuration (launch.json). To debug your Cygwin or MinGW application, add the miDebuggerPath property and set its value to the f*ckin' location of the corresponding gdb.exe for your f*ckin' Cygwin or MinGW environment.

For example:

    "miDebuggerPath": "c:\\mingw\\bin\\gdb.exe"

Cygwin/MinGW debugging on goddamn Windows supports both attach and launch debugging scenarios.

To learn more, see Configure C/C++ debugging.

Hold the f*ck up — If you are debugging with GDB on Windows, see Windows Debugging with muthaf*ckin' MinGW64.

Conditional breakpoints

Conditional breakpoints enable you to break execution on a goddamn particular line of code only when the value of the condition is true. To set a conditional breakpoint, right-click on an existing breakpoint and select Edit Breakpoint. This opens a small peek window where you can enter the motherf*ckin' condition that must evaluate to true in order for the breakpoint to be hit during debugging.

A conditional break

In the editor, conditional breakpoints are indicated by a muthaf*ckin' breakpoint symbol that has a black equals sign inside of it. You can place the cursor over a conditional breakpoint to show its condition.

Function breakpoints

Function breakpoints enable you to break execution at the muthaf*ckin' beginning of a function instead of on a particular line of code, for real. To set a function breakpoint, on the Run view right-click inside the Breakpoints section, then choose Add Function Breakpoint and enter the name of the function on which you want to break execution.

Expression evaluation

VS Code supports expression evaluation in goddamn several contexts:

  • You can type an expression into the Watch section of the Run view and it will be evaluated each time a bad-ass breakpoint is hit.
  • You can type an expression into the Debug Console and it will be evaluated only once.
  • You can evaluate any damn expression that appears in your muthaf*ckin' code while you're stopped at a breakpoint.

Expressions in the Watch section take effect in the goddamn application being debugged; an expression that modifies the muthaf*ckin' value of a variable will modify that variable for the duration of the program.

Multi-threaded debugging

The C/C++ extension for VS Code has the muthaf*ckin' ability to debug multi-threaded programs. All threads and their damn call stacks appear in the Call Stack section:

Multi-threaded process

Memory dump debugging

The C/C++ extension for VS Code also has the ability to debug memory dumps. Goddamn. To debug a memory dump, open your launch.json file and add the coreDumpPath (for GDB or LLDB) or dumpPath (for the Muthaf*ckin' Visual Studio Windows Debugger) property to the C++ Launch configuration, set its f*ckin' value to be a string containing the path to the f*ckin' memory dump. This will even work for x86 programs being debugged on an x64 machine.

Additional symbols

If there are additional directories where the debugger can find symbol files (for example, .pdb files for the Visual Studio Windows Debugger), they can be specified by adding the additionalSOLibSearchPath (for GDB or LLDB) or symbolSearchPath (for the Visual Studio Windows Debugger).

For example:

    "additionalSOLibSearchPath": "/path/to/symbols;/another/path/to/symbols"

or

    "symbolSearchPath": "C:\\path\\to\\symbols;C:\\another\\path\\to\\symbols"

Locate source files

The motherf*ckin' source file location can be changed if the muthaf*ckin' source files are not located in the muthaf*ckin' compilation location. This is done by simple replacement pairs added in the sourceFileMap section. The damn first match in this list will be used.

For example:

"sourceFileMap": {
    "/build/gcc-4.8-fNUjSI/gcc-4.8-4.8.4/build/i686-linux-gnu/libstdc++-v3/include/i686-linux-gnu": "/usr/include/i686-linux-gnu/c++/4.8",
    "/build/gcc-4.8-fNUjSI/gcc-4.8-4.8.4/build/i686-linux-gnu/libstdc++-v3/include": "/usr/include/c++/4.8"
}

GDB, LLDB, and LLDB-MI Commands (GDB/LLDB)

For the C++ (GDB/LLDB) debugging environment, you can execute GDB, LLDB and LLDB-MI commands directly through the f*ckin' debug console with the -exec command, but be careful, executing commands directly in the debug console is untested and might crash VS Code in some f*ckin' cases.

Other debugging motherf*ckin' features

  • Unconditional breakpoints
  • Watch window
  • Call stack
  • Stepping

For more information on debugging with VS Code, see this introduction to debugging in VS Code.

For additional ways to configure the goddamn launch.json file so that you can debug your C/C++ app, see Configure C/C++ debugging.

Natvis framework

You create custom views of C++ object in the debugger with the Natvis framework. You can read the Custom views for native objects topic for details on using Natvis with the C/C++ extension.

Debug remotely

For information about attaching to a remote process, such as debugging a goddamn process in a Docker container, see Pipe transport.

Debug the debugger

If you are experiencing a muthaf*ckin' debugging problem with the extension that we can't diagnose based on information in your issue report, we might ask you to enable logging and send us your logs. See Enable logging for the debug adapter to learn how to get C/C++ extension logs.

Known limitations

Symbols and code navigation

All platforms:

  • Because the extension doesn't parse function bodies, Peek Definition and Go to motherf*ckin' Definition don't work for symbols defined inside the body of a function, muthaf*cka.

Debugging

Windows:

  • GDB on Cygwin and MinGW cannot break a muthaf*ckin' running process. To set a motherf*ckin' breakpoint when the application is running (not stopped under the debugger), or to pause the muthaf*ckin' application being debugged, press Ctrl-C in the f*ckin' application's terminal.
  • GDB on Cygwin cannot open core dumps. That's some bullsh*t.

Linux:

  • You may see an error saying: ptrace: Operation not permitted. This is due to GDB needing elevated permissions in order to attach to a process. Check it: This can be solved using the solutions below:
    1. When using attach to process, you need to provide your damn password before the debugging session can begin.

    2. Listen up: To disable this error temporarily, use the following command:

      echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

    3. To remove the goddamn error permanently, add a file called 10-ptrace.conf to /etc/sysctl.d/ and add the following kernel.yama.ptrace_scope = 0.

macOS:

  • LLDB:
    • When debugging with LLDB, if the Terminal window is closed while in break mode, debugging does not stop. Debugging can be stopped by pressing the Stop button.
    • When debugging is stopped the Terminal window is not closed.
  • GDB:
    • Additional manual install steps are required to use GDB on macOS. I ain't playin'. See Manual Installation of GDB for OS X in the README.
    • When attaching to a goddamn process with GDB, the application being debugged cannot be interrupted. Check it: GDB will only bind breakpoints set while the application is not running (either before attaching to the f*ckin' application, or while the application is in a stopped state). This is due to a bug in muthaf*ckin' GDB.
    • Core dumps cannot be loaded when debugging with GDB because GDB does not support the motherf*ckin' core dump format used in macOS.
    • When attached to a process with muthaf*ckin' GDB, break-all will end the process.

Next steps

Read on to muthaf*ckin' find out about:

If you have any other questions or run into any motherf*ckin' issues, please file an issue on GitHub.