Example session ~
*termdebug-example*
Start in the Vim "src" directory and build Vim: >
% make
Start Vim: >
% ./vim
Load the termdebug plugin and start debugging Vim: >vim
:packadd termdebug
:Termdebug vim
You should now have three windows:
source - where you started
gdb - you can type gdb commands here
program - the executed program will use this window
Put focus on the gdb window and type: >
break ex_help
run
Vim will start running in the program window. Put focus there and type: >vim
:help gui
Gdb will run into the ex_help breakpoint. The source window now shows the
ex_cmds.c file. A red "1 " marker will appear in the signcolumn where the
breakpoint was set. The line where the debugger stopped is highlighted. You
can now step through the program. You will see the highlighting move as the
debugger executes a line of source code.
Run ":Next" a few times until the for loop is highlighted. Put the cursor on
the end of "eap->arg", then call ":Eval". You will see this displayed:
"eap->arg": 0x555555e68855 "gui" ~
This way you can inspect the value of local variables. You can also focus the
gdb window and use a "print" command, e.g.: >
print *eap
If mouse pointer movements are working, Vim will also show a balloon when the
mouse rests on text that can be evaluated by gdb.
You can also use the "K" mapping that will either use Nvim floating windows
to show the results.
Now go back to the source window and put the cursor on the first line after
the for loop, then type: >
:Break
You will see a "1" marker appear, this indicates the new breakpoint. Now
run ":Cont" command and the code until the breakpoint will be executed.
You can type more advanced commands in the gdb window. For example, type: >
watch curbuf
Now run ":Cont" (or type "cont" in the gdb window). Execution
will now continue until the value of "curbuf" changes, which is in do_ecmd().
To remove this watchpoint again type in the gdb window: >
delete 3
You can see the stack by typing in the gdb window: >
where
Move through the stack frames, e.g. with: >
frame 3
The source window will show the code, at the point where the call was made to
a deeper level.
Stepping through code ~
*termdebug-stepping*
Put focus on the gdb window to type commands there. Some common ones are:
- CTRL-C interrupt the program
- next execute the current line and stop at the next line
- step execute the current line and stop at the next statement,
entering functions
- until execute until past the current cursor line or past a specified
position or the current stack frame returns
- finish execute until leaving the current function
- where show the stack
- frame N go to the Nth stack frame
- continue continue execution
*:Run* *:Arguments*
In the window showing the source code these commands can be used to control
gdb:
`:Run` [args] run the program with [args] or the previous arguments
`:Arguments` {args} set arguments for the next `:Run`
*:Break* set a breakpoint at the cursor position
:Break {position}
set a breakpoint at the specified position
*:Tbreak* set a temporary breakpoint at the cursor position
:Tbreak {position}
set a temporary breakpoint at the specified position
*:Clear* delete the breakpoint at the cursor position
*:Step* execute the gdb "step" command
*:Over* execute the gdb "next" command (`:Next` is a Vim command)
*:Until* execute the gdb "until" command
*:Finish* execute the gdb "finish" command
*:Continue* execute the gdb "continue" command
*:Stop* interrupt the program
If gdb stops at a source line and there is no window currently showing the
source code, a new window will be created for the source code. This also
happens if the buffer in the source code window has been modified and can't be
abandoned.
Gdb gives each breakpoint a number. In Vim the number shows up in the sign
column, with a red background. You can use these gdb commands:
- info break list breakpoints
- delete N delete