VI/Vim advanced: Use GDB for debugging in vim-use vimgdb

Source: Internet
Author: User
Tags netbeans gdb debugger

The help entry for the commands used in this section:

:help vimgdb 

At the beginning of the UNIX system design, there was a very important idea: each program only implements a single function and connects multiple programs through pipelines to make them work together, to provide more powerful functions. The program only implements a single function. On the one hand, it reduces the complexity of the program, and on the other hand, it also enables it to focus on this function and make it the best. Just like building blocks, each block only provides simple functions, but different blocks are built together to build complex things such as buildings and cars.

This can be seen from the command lines of UNIX systems (and its variants, including Linux). Each Command focuses only on a single function, but these commands are combined through pipelines, scripts, and so on, you can complete complex tasks.

The VI/Vim design follows this idea. It only provides the text editing function (the opposite is true for Emacs), and as you can see, it is doing so well in this field.

Because of this, VIM itself does not provide all the functions required for the integrated development environment (it is not prepared to do so, VIM just wants to become a general text editor ). It provides functions such as compilation and debugging to more professional tools for implementation, while Vim only provides interfaces with these tools.

We have already introduced vim and the compiler interface (quickfix). Vim also provides interfaces with the debugger, Which is netbeans. In addition, you can patch Vim to support the gdb debugger.

Because the netbeans interface can only be used in gvim, And the vimgdb patch can be used for debugging, whether in vim or gvim of the terminal. Therefore, I prefer patching. First, I will introduce this method.

We need to re-compile Vim by patching. We just took this opportunity to introduce the vim compilation method. I will only introduce how to compile Vim on Linux. If you want to compile Vim on Windows, refer to this document: VIM: compiling HOWTO: for Windows.

[Download Vim source code]

First, download the vim source code. Go to the vim homepage to download the latest Vim 7.1 source code. Suppose we put the code in ~ /Install/directory. The file name is vim-7.1.tar.bz2.

[Download vimgdb Patch]

Next, we need to download the vimgdb patch. The download page is:

Http:// Group_id = 111038 & package_id = 120238

Here, select the vim 7.1 patch and save it ~ /Install/vimgdb71-1.12.tar.gz.


Run the following command to decompress the source code file and install patches:

cd ~/install/tar xjf vim-7.1.tar.bz2tar xzf vimgdb71-1.12.tar.gzpatch -d vim71 --backup -p0 < vimgdb/vim71.diff 

[Customize Vim functions]

The default Vim configuration is suitable for most people, but sometimes you may need some additional features, and you need to customize Vim yourself. It's easy to customize vim ~ /Install/vim71/src file to edit the MAKEFILE file. This is a well-Noted document. Select it based on the comment:

  • If you do not want to compile gvim, open-Disable-GuiOption;
  • If you want to compile Perl, Python, TCL, Ruby, and other interfaces, open the corresponding options. For example-Enable-tclinterpOption;
  • If you want to use cscope in Vim, open-Enable-cscopeOption;
  • The vimgdb patch we just called is automatically added to the makefile.-Enable-GDBOption;
  • If you want to use Chinese Characters in Vim-Enable-multibyteAnd-Enable-ximOption;
  • You can use-With-features = xxxSelect the compiled Vim feature set. The default value is-With-features = normal;
  • If you do not have the root permission, you can install Vim in your home directory.Prefix = $ (home)Option;

After editing this file, you can edit and install vim. If you need to customize Vim in more detail, you can modify the config. h file to enable/disable the features you want.

[Compile and install]

Compiling and installing Vim is very simple. Use the following two commands:

makemake install 

You do not need to run it manually./ConfigureThe make command automatically calls the configure command.

After the preceding command is executed, VIM is successfully installed.

I opened the "prefix = $ (home)" option during compilation, so my vim is installed in ~ /Bin directory. Modify the PATH variable to find the edited vim. In ~ The following two sentences are added to the/. bashrc file:

PATH=$HOME/bin:$PATHexport PATH 

Log out and log on again. Now, run the vim command and find that the compiled Vim has been run.

[Install the vimgdb runtime file]

Run the following command to decompress the vimgdb runtime file to your ~ /. Vim/directory:

cd ~/install/vimgdb/tar zxf vimgdb_runtime.tgz –C~/.vim/ 

Start vim and run the following command in VIM to generate the Help File index:

:helptags ~/.vim/doc 

Now, you can use": Help vimgdb"Command to view the help of vimgdb.

So far, we have re-compiled vim and installed vimgdb patches for it. The following is an example of how to complete the "encoding-compilation-debugging" one-stop service in vim.

[Debug in VIM]

First, make sure that GDB is installed on your computer. vimgdb supports GDB 5.3 or later versions, but it is best to use GDB 6.0 or later versions.

I will use the following simple example to illustrate how to use GDB for debugging in Vim. Let's take a look at the sample code:

File ~ The content of/tmp/sample. C is as follows. This is the main program. Call the function to calculate the factorial of a certain number and print it:

/* ~/tmp/sample.c */#include <stdio.h>extern int factor(int n, int *rt);int main(int argc, char **argv){    int i;    int result = 1;    for (i = 1; i < 6; i++)    {        factor(i, &result);        printf("%d! = %d/n", i, result);    }    return 0;}  

File ~ The content of/tmp/factor. C is as follows, and the sub-function factor () is defined (). Put it in the sub-directory factor/to demonstrate that vimgdb can automatically open the file according to the debugging location, regardless of the directory where the file is located:

/* ~/tmp/factor/factor.c */int factor(int n, int *r){    if (n <= 1)        *r =  n;    else    {        factor(n - 1, r);        *r *= n;    }    return 0;}  

MAKEFILE file is used to compile the sample code. The final executable file name is sample.

# ~/tmp/Makefilesample: sample.c factor/factor.cgcc -g -Wall -o sample sample.c factor/factor.c  

Assume that the current working directory of VIM is ~ /Tmp (use": Cd ~ /Tmp"Command to switch to this directory ). After editing the above files, enter the command": Make", VIM will compile according to the MAKEFILE file. If a compilation error occurs, VIM will jump to the first error location. After modification, use": Cnext"The command jumps to the next error, and so on. This development method is called quickfix, which we have already mentioned in the Article quickfix, and will not repeat it.

Now, if the link has been completed and the final sample file is generated, We can debug it.

The vimgdb patch already defines some key bindings. We first load these bindings:

:run macros/gdb_mappings.vim 

After loading, some buttons are defined as Debugging commands (for details about Key Binding defined by vimgdb, see": Help GDB-mappings"). Press<F7>You can switch between the default definition of the key and the debugging command.

Now, press the Space key to open a small window (command-line window) under the current window. This is the command window of vimgdb, you can enter any valid gdb commands in this window. The entered commands will be sent to GDB for execution. Now, in this window, enter"GDB", Press enter, the command-line window is automatically closed, and a window is opened above the current window, which is the gdb output window. Now the vim window layout is as follows (I opened the command-line window by space ):

Tips: The command-line window is a special window. In this window, you can edit commands like editing text. After editing, press enter to execute this command. To execute a command again, move the cursor to the line where the command is located, and press Enter. You can also modify the history command before executing it. For details, see": Help define line-Window".

Next, enter the following command in the command-line window:

cd ~/tmpfile sample 

These two commands switch the current working directory of GDB and load the compiled sample program for debugging.

Now, use the vim move command to move the cursor to lines 7th and 14 of sample. C, and press"CTRL-B"Set the breakpoint in these two places, and then press"R", Make GDB run to the first breakpoint we set ("CTRL-B"And"R"All are key bindings defined by gdb_mappings.vim. The other Debugging commands described below are the same ). Now Vim looks like this:

The row where the breakpoint is located is set to blue and marked 1 and 2 in front of the line indicates the first breakpoint. the row to which the program is currently running is set to yellow, "=>" indicates the position where the program is executed (the displayed color can be adjusted by the user ).

Next, we press"C", Run to 2nd breakpoints. Now, run the following Vim command to separate a window named GDB-variables in the lower right corner:

:bel 20vsplit gdb-variables 

Then use"V"Command to select variable I, press"CTRL-P"Command, add variable I to the monitoring window, and add the variable result to the monitoring window in the same way. Here we can see the values of variable I and result in the monitoring window.

Now we press"S"Step into the factor function, VIM will automatically open the factor/factor. c file and indicate the position where the program is executed. Add Variable N in the factor () function to the monitoring window, open the command-line window by space, enter the following command, and input variable * r to the variable window:

createvar *r 

Now, VIM looks like this:

Now you can use"S","CTRL-N"Or"C"To continue the execution until the program is running.

If you execute the program in a single step, VIM may open an assembly window. Yes, vimgdb supports Assembly-level debugging. Here we do not need to perform assembly-level debugging, just ignore it.

If you find a program error, you can press"Q"Exit debugging (GDB will prompt whether to exit, and answer y), and then modify the code, compile, and debug until the final completion. When modifying code, you may not like vimgdb's key ing (for exampleCTRL-BIng to set breakpoints, and this key is a common page flip function), you can press<F7>Cancel vimgdb key ing, or you can directly modify the ing defined in the gdb_mappings.vim file.

Check out whether VIM + GDB debugging is simple ?!

We can customize it to make debugging more convenient.

Open ~ /. Vim/macros/gdb_mappings.vim file, in"Let s: gdb_k = 0"Add the following content to this line:

" easwy addif ! exists("g:vimgdb_debug_file")    let g:vimgdb_debug_file = ""elseif g:vimgdb_debug_file == ""    call inputsave()    let g:vimgdb_debug_file = input("File: ", "", "file")    call inputrestore()endifcall gdb("file " . g:vimgdb_debug_file)" easwy end 

In"Let s: gdb_k = 1"Add the following content to this line:

" easwy addcall gdb("quit")" end easwy 

Comment out"Call S: toggle ()".

Then add the following content to your vimrc:

""""""""""""""""""""""""""""""" vimgdb setting""""""""""""""""""""""""""""""let g:vimgdb_debug_file = ""run macros/gdb_mappings.vim 

After Vim is started<F7>To enter the debugging mode and set the debugging key ing. When you enter the debugging mode for the first time, you will be prompted to enter the file name to be debugged, and you will not have to enter the file name in the future. Repress<F7>To cancel the key ing.

Using Vim's map mechanism, you can map your favorite gdb commands to VIM buttons, which is much easier. For examples of ing, refer ~ /. Vim/macros/gdb_mappings.vim.

A screenshot is attached, which uses Putty to remotely log on to Linux and debug it in Vim. This is why I like vimgdb, because it can be debugged in the terminal vim, and clewn only supports gvim:

Because I do not often use GDB debugging, this article only provides a simple example to help you. You are welcome to share your experience and experience.

I listed some common problems and their solutions in the article vimgdb debugging, hoping to help you.

Finally, let's thank vimgdb's author xdegaye for his hard work. We will introduce pyclewn in subsequent articles. This is another form of combining Vim with GDB. It is the same project as vimgdb.


  • Vim Help File
  • Vim Chinese manual
  • Http://

<Return to vim and use advanced: Directory

For Original Articles, please read the license for footer. For more information, see:Reposted from Yishui blog []


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.