How to connect vim with gdb — using clewn

by admin

screenshot3.png

First, I will show you the screen shot The screen of vim is divide into two sub windows, the top one can display the code and the bottom one shows the variable you want to watch. Well, this can be changed by you, you can split the window any way you like. You can use C-B to make a break, and press R to run your program, and press S to step, and C-J to add the variable you want to watch. You can download clewn here:

http://clewn.sourceforge.net/

How to install: first of all, you should have your gvim installed,

sudo apt-get install vim-gnome

as well as gdb And then download the source file of clewn; note here, not vimgdb. And extract the file, then use your terminal to enter the clewn folder

./configure make sudo make install

Note here, may be some one will be suffered from the dependency problem during their installing. If your machine tell you that you need readline, then install libreadline5-dev

sudo apt-get install libreadline5-dev

And then, copy some files to ~/.vim you can refer my folder:

cp /usr/local/share/vim/vimfiles/clewn.vim ~/.vim/plugin/

cp /usr/local/share/vim/vimfiles/doc/clewn.txt ~/.vim/doc/

cp /usr/local/share/vim/vimfiles/macros/clewn_mappings.vim ~/.vim/macros/

cp /usr/local/share/vim/vimfiles/syntax/gdbvar.vim ~/.vim/syntax/

Then, every thing is done. You can write a small program to test.

#include 

int main(int argc, char *argv[]) {
  int i;
  int s;
  s = 0;
  for (i = 0; i < 10; ++i) {
    s = s + 1;
  }
}

Save it as test.c , for example. And then,

gcc -g -o test test.c

If everything is right during compiling, then

clewn -va test.c

Then, gdb is opened on your terminal, and your gvim is opened at the same time Cool down now; input the following on your terminal(gdb):

file test

Now, you can debug your program on gvim. For example, you can click the line you want to set break point. And then press CTRL+B, then, break point is set. SHIFT+r(that is capital R)It will run and stop at your break point SHIFT+s(that is capital S)It will step And watch your variable, input following on gvim

:split

Then the window will be split, and then click the variable you want to watch, and press C+J, then you can see the variable appears in another window. To do this, you also can input the following on your terminal(gdb)

createvar yourvar

“youvar” represents the variable you want to watch. Following is the default shortcuts

List of default key mappings:

        CTRL-Z  send an interrupt to GDB and the program it is running
        B       info breakpoints
        L       info locals
        A       info args
        S       step
        I       stepi
        CTRL-N  next: next source line, skipping all function calls
        X       nexti
        F       finish
        R       run
        Q       quit
        C       continue
        W       where
        CTRL-U  up: go up one frame
        CTRL-D  down: go down one frame

cursor position: ~
        CTRL-B  set a breakpoint on the line where the cursor is located
        CTRL-E  clear all breakpoints on the line where the cursor is located

mouse pointer position: ~
        CTRL-P  print the value of the variable defined by the mouse pointer
                position
        CTRL-X  print the value that is referenced by the address whose
                value is that of the variable defined by the mouse pointer
                position
        CTRL-K  set a breakpoint at assembly address shown by mouse position
        CTRL-H  clear a breakpoint at assembly address shown by mouse position
        CTRL-J  add the selected variable at mouse position to the watched
                variables window

This is referred the official document, which is available here

http://clewn.sourceforge.net/doc.html

Hope this will help you somewhat. If you have some new or better idea, please contact me. chunhao86@gmail.com Thanks