Termdebug Vim Plugin
Vim's Term-Debug built-in plugin offers a terminal interface for debugging programs with the GNU Debugger (GDB). This integration empowers developers to run their applications, set breakpoints, inspect variable states, and step through code—all within the familiar environment of Vim. This makes it an invaluable tool for those who prefer a keyboard-centric workflow.
In this blog post, we will walk through the process of compiling a simple C++ program, setting breakpoints in the source code, and examining variable values at those breakpoints.
The GDB binary and the GCC compiler, along with the C++ standard library, are available through a local installation of MinGW-W64. To integrate these command-line tools, we will replace the default Vim shell (cmd.exe) with PowerShell. PowerShell streamlines the management of environment variables, enabling efficient configuration of filesystem paths for GDB and the C++ compiler.
In this setup, we have added the path to the MSYS/MinGW installation to PowerShell's $PROFILE as follows:
$env:PATH += ";C\path to\msys2\mingw64\bin"
Let's create a simple C++ program, a Makefile, in order to use termdebug to debug it.
#include <iostream>
int main() {
// Using a for loop to print numbers from 1 to 10 with a step of 2
for (int i = 1; i <= 10; i += 2) {
std::cout << i << std::endl;
}
return 0;
}
A Makefile is a valuable tool that automates the build process, making it easier to compile our program and manage dependencies.
As shown in the video below, we can compile our C++ example program using Make (vim-dispatch), a build automation tool that simplifies the compilation process.
CXX = gcc
CXXFLAGS = -g -Wall
TARGET = main
all: $(TARGET)
$(TARGET): main.cpp
$(CXX) $(CXXFLAGS) -o $(TARGET) main.cpp
clean:
rm -f $(TARGET)
While Term-Debug is a built-in feature of Vim, it requires activation through Vim's package manager, with:
:packadd termdebug
To start termdebug, we can use the now available vim command:
:Termdebug ./main
This will open a new window with the GDB interface. You can set breakpoints and inspect variables easily. GDB is intuitive for stepping through loops. To move to the next line, use the command next (or n), which steps over function calls.
Use continue (or c) to run the program until the next breakpoint. To skip to a specific line, use jump (or j) and the line number:
(gdb) jump 6
These commands help you navigate your code effectively. To set a breakpoint at the main function, type:
(gdb) b 5
This sets a breakpoint at line 5.
Once the program hits the breakpoint, you can inspect variables. For example, to check the value of i, type:
(gdb) p i
|
As a Temu Affiliate I earn from qualifying purchases |
gcc or g++
While you can use gcc to compile C++ code, it is generally more straightforward to use g++ for C++ programs, as it handles linking with the C++ standard library automatically. If you choose to use gcc as shown here, make sure to include the -lstdc++ flag to link against the C++ standard library. The rest of the debugging process with termdebug remains the same.