hpr4042 :: Debugging directly in vim
Debug directly in vim using termdebug bundled with vim since vim 8.1
Hosted by crvs on Tuesday, 2024-01-30 is flagged as Explicit and is released under a CC-BY-SA license.
gdb, vim, debug, C.
(Be the first).
The show is available on the Internet Archive at: https://archive.org/details/hpr4042
Listen in ogg,
spx,
or mp3 format. Play now:
Duration: 00:33:18
general.
Debug directly in vim
TL;DL you can use gdb directly from vim without installing any extensions. Just compile your program with debug options and do:
:packadd termdebug
:TermdebugCommand path-to-my-program
Now the actual notes:
If you actually listened to the show, I am sorry for how long it took me to realize that I was compiling without debug flags.
This is not a good overview of gdb
, I don't think, for
that I would recommend episode
415 on Klaatu's Gnu World Order podcast.
The program that we build up to is this:
// file: main.c
#include <stdio.h>
#include <string.h>
void greet(char* greeting)
{
- int end = strlen(greeting);
+ int end = strlen(greeting) - 1;
if (greeting[end] == '
')
printf("%s", greeting);
else
printf("%s
", greeting);
}
int main ()
{
greet( "Hello world!
");
return 0;
}
Where we use gdb to find out that we are checking one character past the end in the if-condition rather than the last character, this then results in a double new-line.
Once the debugger is launched within vim, termdebug offers a few ways to control the debugging, namely:
:Source
- focus on source window. :Gdb
-
focus on debugger window. :Program
- focus on program
output window :Break
- introduce a break point in line
under the cursor (in the source window) :Clear
- clear the
break point in the line under the cursor. :Evaluate
- show
the value of the expression under the cursor.
... And a good deal more, if you're interested just have a gander at
:help termdebug
.
I hope this helps someone make their debugging experience slightly less painful, as it has helped me recently :)