Skip to content

CodeLLDB

Thomas Hahn edited this page Dec 12, 2024 · 3 revisions

In contrast to Microsoft's C/C++ extension, clangd does not come with a debugging feature. Instead, we will use the CodeLLDB extension together with the lldb debugger.

Install the extension

To install/enable the extension, follow the same steps as in the previous sections.

codelldb_ext

Get started

Create a template launch.json

To use the debugger in VS Code, we have to provide a launch.json file. Fortunately, CodeLLDB makes it fairly easy to generate a basic template:

  • Click on Run and Debug in the activity bar
  • Choose create a launch.json file
  • Select LLDB

This creates a launch.json in our project settings folder .vscode:

codelldb_launch_json

Compile with debug flags

Before using the debugger, it is probably a good idea to compile our code in debug mode. If you haven't done so,

  • Run >CMake: Select a Variant and select Debug
  • Run >Cmake: Build

This creates a new build directory (build.Clang-19.1.2-Debug in our case).

Update launch.json to debug the executable ex2

Now open again the launch.json file and apply the following changes:

codelldb_debug_ex2

Run the debugger

Set a breakpoint

To set a breakpoint,

  • Open the source file ./examples/ex2.cpp
  • Click next to the line number 9
codelldb_breakpoint

The red dot indicates that a breakpoint has been set on this specific line.

Start the debugger

  • Click on Run and Debug in the activity bar
  • Click the green play icon next to Debug ex2
  • Start debugging

We can now examine variables, step into functions, etc.

It is also possible to use the usual commands in the Debug Console:

codelldb_debugging

Add another configuration

If we want to debug a different executable or run the same executable with different input arguments, we can simply add a new configuration to the launch.json file and select the configuration from the drop down menu next to the green play icon in the Run and Debug window:

codelldb_add_config codelldb_debug_ex3