Vi/vim Use advanced: Use GDB debugging in Vim – use Vimgdb

Source: Internet
Author: User
Tags bz2 netbeans gdb debugger

Vi/vim Use advanced: Use GDB debugging in Vim – use Vimgdb

<< return to VIM using advanced: Directory

The help entry for the commands used in this section:

In the original design of Unix system, there is a very important idea: each program only to achieve a single function, through the pipeline and other ways to connect multiple programs, so that they work together to achieve more powerful functions. The program only implements a single function, on the one hand reduces the complexity of the program, on the other hand, let it focus on this function, the function is the best. Just like building blocks, each building has a simple function, but different blocks can be built together to create complex things like buildings, cars, and so on.

This can be seen from the command line of the UNIX system (and its variants, including Linux), where each command focuses only on a single function, but by combining the commands with pipelines, scripts, and so on, the complex tasks can be accomplished.

Vi/vim's design also follows this idea, which provides only textual editing (just the opposite of Emacs's chatty), and as you can see, it does so well in this area.

Because of this, vim itself does not provide all the functionality needed to integrate the development environment (nor is it ready to do so, vim just wants to be a generic text editor). It puts functions such as compiling and debugging to a more professional tool, and VIM only provides interfaces to those tools.

We have already described the interface between Vim and compilers (i.e., quickfix), and Vim also provides an interface to the debugger, which is NetBeans. In addition, you can also give vim a patch to support the GDB debugger.

Since the NetBeans interface can only be used in Gvim, the vimgdb patch can be debugged, either at the end of vim or gvim. So I prefer the way of patching, I first introduce this method.

Patching the way, need to recompile vim, just take this opportunity to introduce the vim of the compilation method. I only introduce the Linux compilation method, if you want to compile Vim on Windows, you can refer to this document: Vim:compiling howto:for windows.

[Download vim source code]

First of all we need to download vim source code. To the VIM home page download the current VIM 7.1 source code, suppose we put the code in the ~/install/directory, the file name is vim-7.1.tar.bz2.

[Download Vimgdb patch]

Next, we need to download vimgdb patch, download page in:

http://sourceforge.net/project/showfiles.php?group_id=111038&package_id=120238

Here, select the VIM 7.1 patch and save it to ~/install/vimgdb71-1.12.tar.gz.

Vim 7.4 is as follows:

(1) https://github.com/liulong3712/vimgdb-for-vim7.4

(2)

vim7-4.tar.bz2:ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2

Vimgdb-for-7.4:https://github.com/larrupingpig/vimgdb-for-vim7.4/archive/master.zip

  

[Patching]

Run the following command, unzip the source file, and hit the Patch:

[Customize Vim's function]

The default vim configuration is suitable for most people, but sometimes you may need some extra functionality, and you'll need to customize vim yourself. Customizing Vim is simple, go to ~/install/vim71/src file, edit makefile file. This is a well-commented document and is selected according to the comments:

    • If you do not want to compile Gvim, you can open the –disable-gui option;
    • If you want to compile Perl, Python, Tcl, Ruby and other interfaces, open the appropriate options, for example, I opened the –enable-tclinterp option;
    • If you want to use Cscope in vim, open the –enable-cscope option;
    • We just hit the vimgdb patch, automatically added the –enable-gdb option in makefile;
    • If you wish to use Chinese in vim, enable the –enable-multibyte and –enable-xim options;
    • The –with-features=xxx option can be used to select the set of Vim features compiled, the default is –with-features=normal;
    • If you do not have root privileges, you can put vim in your home directory, you need to open the prefix = $ (HOME) option;

Once you have edited this file, you can edit and install vim. If you need more detailed customization of vim, you can modify the Config.h file to turn on/off the features you want.

[Compile and install]

Compiling and installing Vim is simple, using the following two commands:

You do not need to run the./configure command manually, and the make command automatically calls the Configure command.

After the above command has been executed, Vim is installed successfully.

An issue encountered during the make process:

(Missing ncurses installation package

Workaround:

Download and install the appropriate package

First, if your system is the Redhat series:

Yum List|grep ncurses

Yum-y Install Ncurses-devel

Yum Install Ncurses-devel

Second, if your system is Ubuntu or Debian:

Apt-cache Search ncurses

Apt-get install Libncurses5-dev)

I opened the "prefix = $ (HOME)" option at compile time, so my vim was installed in the ~/bin directory. You need to modify the path variable so that it finds the vim I have edited. In the ~/.BASHRC file, add the following two words:

Exit and log back in again, now typing the vim command and discovering that we have already run our compiled vim.

[Install VIMGDB Runtime file]

Run the following command to unzip the VIMGDB runtime file to your ~/.vim/directory:

Now start vim and run the following command in vim to generate the Help file index:

Now you can use the ": Help vimgdb" command to see the vimgdb.

At this point, we recompiled the vim and patched it up with a vimgdb patch. Let me take an example to illustrate how to complete the "encode-compile-debug" one-stop service in Vim.

[Debug in Vim]

First make sure that GDB is installed on your computer and that VIMGDB supports more than 5.3 gdb versions, but it is best to use GDB 6.0 or more.

Let me use this simple example to illustrate how to use GDB debugging in Vim. First look at the sample code:

The file ~/tmp/sample.c content is as follows, this is the main program that calls the function to calculate the factorial of a number and print:

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

The ~/tmp/factor/factor.c contents of the file are as follows, and the child Function factor () is defined. The reason I put it in the subdirectory factor/is to demonstrate that VIMGDB can automatically open the file based on the debug location, regardless of the directory in which it 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;}  

The makefile file is used to compile the sample code and the resulting executable file is named 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 (switch to this directory using the ": CD ~/tmp" command). After we have edited the above several files, we enter the command ": Make" and vim will compile according to the makefile file. If the compilation error occurs, Vim jumps to the first error location, and after the change, the ": Cnext" command jumps to the next error, and so on. This way of development is called Quickfix, we have been in the sword –quickfix a article, not to repeat.

Now, assuming that the link has been completed and the final sample file is generated, we are ready to debug.

VIMGDB patch has defined some key bindings, we load these bindings first:

After loading, some keys are defined as Debug commands (Vimgdb defined key bindings See ": HelpGdb-mappings"). Press <F7> to toggle between the default definition of the key and the debug command.

OK, we now press SPACEBAR, in the current window will open a small window (command-line window), this is the VIMGDB command window, you can enter any legitimate GDB command in this window, the input command will be sent to gdb execution. Now, when we enter "GDB" in this window,press ENTER, the command-line window closes automatically, and a window opens at the top of the current window, which is the GDB Output window. Now Vim's window layout is as follows (I opened the command-line window again by a space):

Click to view larger image

Tip: The command-line window is a special window in which you can edit the command as you would edit the text, and then press ENTER when you finish editing, and the command executes. To repeat a command, you can move the cursor to the line where the command is located, and then press ENTER, and you can also modify the history command before executing. See ": Help Cmdline-window".

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

These two commands toggle GDB's current working directory and load our compiled sample program to prepare for debugging.

Now using Vim's move command, move the cursor to line 7th and 14 of sample.c, press "Ctrl-b" to set breakpoints in both places, and then press "R" to make GdB run to the first breakpoint we set ("ctrl-b" and "R" Are the key bindings defined by Gdb_mappings.vim, the other debug commands described below are the same). Now vim looks like this:

Click to view larger image

The line where the breakpoint is located is set to blue, and the lines shown in front of 1 and 2 indicate the breakpoint, the line that the program is currently running to is set to yellow, and the line is indicated by "= =" indicating that this is the location of the program execution (the user can adjust the color displayed).

Next, we Press "C", run to the 2nd breakpoint, now, we enter the following VIM command, in the lower right to separate a window named Gdb-variables:

Then use the "V" command to select the variable i, press "ctrl-p" command, add the variable i to the Watch window, the same way the variable result is added to the Watch window, where you can see the value of the variable I and result from the Watch window.

Click to view larger image

Now we Press "S" step into the factor function, VIM will automatically open the Factor/factor.c file and indicate where the program executes. Let's add the variable N in the factor () function to the Watch window, then press the space to open the command-line window, enter the following command, and enter the variable *r into the variable window:

Now, Vim looks like this:

Click to view larger image

Now you cancontinue execution with "S", "ctrl-n" or "C" until the end of the program runs.

If you are stepping to the end of the program, then vim may end up opening a compilation window. Yes, VIMGDB supports assembly-level debugging. Here we do not need to compile-level debugging, ignore can.

If you find that the program has errors, you can press "Q" to exit the debug (GdB will prompt to exit, answer y), and then modify the code, compile, debug, until the final completion. When you modify the code, you may not like the VIMGDB key mapping (for example, it maps ctrl-b to set breakpoints, and this key is a common paging function), you can press <F7> cancel the Vimgdb key mapping, or you directly modify Gdb_ The mappings defined in the Mappings.vim file.

See, Vim + gdb debugging is not very simple?!

We can customize it to make debugging more convenient.

Open the ~/.vim/macros/gdb_mappings.vim file and add this to the "lets:gdb_k = 0" 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")    

Add this section below the "lets:gdb_k = 1" line:

Comment out the last line of "Calls:toggle ()".

Then add this section to your VIMRC:

Now, after starting vim, press <f7>, enter debug mode and set the Debug key mapping. The first time you enter debug mode, you will be prompted to enter the name of the file you want to debug, and you won't have to enter it later. Press <f7> again, and then exit Debug mode to cancel the Debug key mapping.

With Vim's key mapping (map) mechanism, you can map your favorite GDB commands to Vim's keys for more convenience. Examples of mappings can be referred to ~/.vim/macros/gdb_mappings.vim.

Attach a screenshot, which is used putty telnet to Linux and debug in the terminal vim. That's why I like vimgdb because it can be debugged in the terminal vim, and clewn only supports Gvim:

Click to view larger image

Because I don't often use gdb debugging, this article just gives a simple example to give a start. You are welcome to share your experience and experiences.

I am in the article Vimgdb debugging the common problems and solutions listed some common problems and their solutions, I hope to be helpful to everyone.

Finally, let us thank Vimgdb author Xdegaye's hard work, we will introduce the following article pyclewn, this is a combination of vim and gdb another form, it and vimgdb belong to a project.

Vi/vim Use advanced: Use GDB debugging in Vim – use Vimgdb

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: info-contact@alibabacloud.com 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.