VIM+GDB+DDD+XXGDB Wonderful program debugging

Source: Internet
Author: User
Tags bz2 netbeans gdb debugger

//------------------------------------------------------------------------------------------------------------- -----------------------------------
Graphics Gdb1.sudo apt-get Install Xxgdb
Graphics Gdb2.sudo apt-get Install DDD
Graphics GDB3. Using VIMGDB patches in vim (highly recommended Vimgdb debugger, extremely easy to use gliethttp_20080314)
//------------------------------------------------------------------------------------------------------------- -----------------------------------
Text reproduced from: http://blog.csdn.net/easwy/archive/2007/10/17/1828678.aspx
The help entry for the commands used in this section:
: Help Vimgdb
In the UNIX department
System was originally designed, there is a very important idea: each program only to achieve a single function, through the pipeline and other ways to connect a number of 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, it also allows it to focus on this function, to do the best. Just like building blocks, each building has a simple function, but different blocks are
Together, we can build a building, a car and so on complex things.
This can be seen from the command line of the UNIX system (and its variants), 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've already covered the interface between Vim and compilers (see Quickfix topic), 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. We describe both of these approaches in this and the next article.
Because NetBeans interface can only be used in Gvim, and Vimgdb patch, whether in the terminal vim, or gvim, can be debugged. So I prefer the way of patching, this article first introduced this method.
Patching the way, need to recompile vim, just take this opportunity to introduce the vim of the compilation method. I will only describe 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
http://www.vim.org/sources.php
Download the current VIM 7.1 source code, suppose we put the code into 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.

[Patching]

Run the following command, unzip the source file, and hit the Patch:
CD ~/install/
Tar xjf vim-7.1.tar.bz2
Tar xzf vimgdb71-1.12.tar.gz
patch-d vim71--backup-p0
[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 played 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:
Make
Make install
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.
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:
Path= $HOME/bin: $PATH
Export PATH
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:
CD ~/install/vimgdb/
Tar zxf vimgdb_runtime.tgz–c~/.vim/
Now start vim and run the following command in vim to generate the Help file index:
: Helptags ~/.vim/doc
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
extern int factor (int n, int *rt);
int main (int argc, char **argv)
{
int i;
int result = 1;
for (i = 1; i
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 subdirectory factor/is to demonstrate that VIM can automatically open a 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
The makefile file is used to compile the sample code and the resulting executable file is named Sample.
# ~/tmp/makefile
Sample:sample.c factor/factor.c
Gcc-g-wall-o Sample Sample.c factor/factor.c
Assume that the current working directory of Vim is ~/tmp (using ": CD
~/tmp "command to switch to this directory). After we have edited the above several files, we enter the command ": Make" and vim will compile according to the makefile file. If a compilation error occurs,
Vim jumps to the first place where it went wrong, then skips to the next error with the ": Cnext" command, and so on. This development approach is called Quickfix, which we have in the previous article
Said, 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:
: Run Macros/gdb_mappings.vim
After loading, some keys are bound to debug commands (Vimgdb defined key bindings See ": Help Gdb-mappings"). Press the key to switch between the default definition of the key and the debug command.
Well, now that we press SPACEBAR, a small window (command-line window) opens below the current window, which is the Vimgdb command window where you can enter any
The GDB command of the method, the input command will be sent to GDB for execution. Now, we enter "GDB" in this window, press ENTER after the command-line window automatically closes, and in the current window
A window is opened above it, which is the GDB Output window. Now Vim's window layout is as follows (I opened the command-line window again by a space):


Tips:
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. You're going to repeat a life.
Order, 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:
CD ~/tmp
File sample
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 line 14 of sample.c, press "Ctrl-b" to set breakpoints in both places, and then press "R" to let GdB run to our settings
At the first breakpoint ("Ctrl-b" and "R" are the key bindings defined by the Gdb_mappings.vim, the same as the other debug commands described below). Now vim looks like this:


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:
: Bel 20vsplit 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.


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:
Createvar *r
Now, Vim looks like this:


Now you can continue 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
Do not like vimgdb key mapping (for example, it maps ctrl-b to set breakpoints, and this key is a common paging function), you can press cancel vimgdb The key mapping, or you directly repair
Change the mappings defined in the Gdb_mappings.vim file.
See, with 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 "let S:gdb_k = 0" line:
"Easwy add
if! 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 ()
endif
Call GDB ("file"). G:vimgdb_debug_file)
"Easwy End
Add this section below the "Let S:gdb_k = 1" line:
"Easwy add
Call GDB ("quit")
"End Easwy
Comment out the last line of "Call S:toggle ()".
Then add this section to your VIMRC:
""""""""""""""""""""""""""""""
"Vimgdb setting
""""""""""""""""""""""""""""""
Let G:vimgdb_debug_file = ""
Run Macros/gdb_mappings.vim
Now, after starting vim, press to enter debug mode and define the key mapping for debugging. 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 again to exit Debug mode and cancel the Debug key mapping.
With Vim's key mapping 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:


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.
Finally, let us thank Vimgdb author Xdegaye's hard work, our next article will introduce clewn, this is a different form of vim and GDB, it and vimgdb belong to a project.

[Reference Document]
1. Vim Help file
2.
http://vimcdoc.sourceforge.net/

3.
Http://clewn.sourceforge.net/index.html

[end note]
This article is free to use for non-commercial purposes. Reprint please indicate the source.
Original link:
Http://blog.csdn.net/easwy

Version
15oct07, Easwy, v0.1, initial version

This article comes from Chinaunix blog, if you look at the original point:Http://blog.chinaunix.net/u1/40690/showart_1875942.html

VIM+GDB+DDD+XXGDB Wonderful program debugging

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.