Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr4042 :: Debugging directly in vim

Debug directly in vim using termdebug bundled with vim since vim 8.1

<< First, < Previous, , Latest >>

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


Comments

Subscribe to the comments RSS feed.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
Who is the host of this show?
What does HPR mean to you?