Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


HPR4042: Debugging directly in vim

Hosted by crvs on 2024-01-30 00:00:00
Download or Listen

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 :)

Comments



More Information...


Copyright Information

Unless otherwise stated, our shows are released under a Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.

The HPR Website Design is released to the Public Domain.