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

Source: Internet
Author: User
Tags gdb debugger

From http://easwy.com/blog/archives/advanced-vim-skills-vim-gdb-pyclewn/

========================================================== ==============

The help entry for the commands used in this section:

:help pyclewn 

In the previous article, we introduced how to use vimgdb to debug programs in Vim. This method allows you to debug your programs in vim and gvim in the terminal. However, its limitations are also obvious: First, it needs to patch the vim source code and re-compile Vim. In addition, it can only be used in Linux and cannot be debugged in Windows by GDB.

To debug in Vim, in addition to vimgdb, you can also use clewn and pyclewn. These two tools are also provided by the author of vimgdb, and their usage is basically the same. The difference is that clewn is written in C language, while pyclewn is written in Python language and has better cross-platform capabilities. This document describes the pyclewn tool.

To use pyclewn, install Python 2.4 (or later) and gvim 7.0 or later on the computer. Note that pyclewn can only be used with gvim. It does not support Vim (as does clewn) in the terminal (update 2011/12/27: in VIM 7.3, the latest pyclew supports Vim debugging in the terminal ). In addition, gvim needs to enableNetbeans_intgFeatures andAutocmdFeatures.

If all the preceding conditions have been met, download pyclewnat http://sourceforge.net/projects/pyclewn/and decompress the package. Installing pyclewn is simple. on my computer, gvim installation directory is in my home directory. Therefore, install pyclewn in the home directory by running the following command:

cd /home/easwy/download/pyclewn-0.7/vimdir=$HOME/.vim python setup.py install --home=$HOME 

In the pyclewn source code directory, there isInstallThis document describes how to install pyclewn on UNIX and windows. You can decide how to install pyclewn based on your environment.

After installing pyclewn, start gvim and execute: Help pyclewnCommand to view the pyclewn help file, which contains a lot of useful information.

The following example shows how to use pyclewn for debugging. Download the program debugged in this example: Download the debugging program

Start pyclewn first. The method to start pyclewn is simple. You can execute the following command in a terminal:

pyclewn 

When pyclewn is started, it will start a gvim window, and our debugging will be carried out in this gvim window. The terminal running pyclewn will be used as the gdb control terminal. input and output of the program to be debugged will be carried out through this terminal (of course, you can also change the control terminal through the TTY command in GDB ).

Next, we enter the following command in the gvim window opened by pyclewn to compile the program, open the file to be debugged, and set the breakpoint in the file:

:cd pyclewn-ex:make:e main.c:Cfile factorial:Cbreak 14 

The first three commands are standard Vim commands. Switch to the directory where the sample program is located, compile the program, and open the file main. C. The following two commands start with the uppercase letter C, which is a custom command of pyclewn. pyclewn will pass the command content after the letter C to the gdb debugger. Therefore, the above two commands are equivalent to running in GDB.File factorAndBreak 14Load factor as the program to be debugged, and set a breakpoint in line 14th of Main. C.

When running the custom C command of pyclewn, pyclewn separates a window in gvim to display the input and output of the gdb debugger. Therefore, after executing the preceding command, the screen looks like this:

Click to view the chart

Now that you have set the breakpoint, you can start debugging. Pyclewn has defined some debugging-related key mappings. We can use the following command to load these key mappings:

:Cmapkeys 

After executing this command, the defined key ing will be printed in the clewn_console. I will not repeat it here. In the following debugging process, I will also use several of the most common mappings.

We continue debugging. I have defined a breakpoint above, so pressSHIFT-RKey to run the program. When the program runs to the breakpoint, it stops, and we pressCTRL-NRun the three statements. Now switch to the control terminal. You can see that the program is waiting for input. Enter a number and press Enter. The current screen is as follows:

Click to view the chart

In the above screenshot, we move the mouse over Variable N. A small bubble is displayed, showing that the value of Variable N is 4, which is consistent with the number we entered.

Next, pressCTRL-NExecute a statement, and then pressSHIFT-SStep into the factorial () function. Now gvim will open the file util/factorial. C. Then, run the following command to add the variables T and I to the watch window:

:Cdbgvar t:Cdbgvar i 

The above command will create(Clewn) _ dbgvarBut the buffer is not displayed. Run the following command to create a new window to display the buffer:

:sb (clewn)_dbgvar 

The screen now looks like this:

Click to view the chart

Per timeSHIFT-CSo that the program runs to the end, our debugging will come to an end.

From the preceding debugging process, we can see that pyclewn debugging is very convenient. In addition, pyclewn can be used for debugging on windows, but I have not tried it. If you are interested, try it. For Windows debugging, you may need to use GDB in cygwin.

If you think that the default key ing set by pyclewn cannot meet your needs, you can also define more GDB debugging key ing by setting. Clewn_keys.gdbCopy the file to your home directory and modify the file to customize your own key binding.

cd /home/easwy/download/pyclewn-0.7/cp runtime/.clewn_keys.gdb $HOME 

To learn more about the use of pyclewn, read the pyclewn help manual:: Help pyclewn.

[Reference]

  • Vim Help File
  • Vim Chinese manual
  • Pyclewn
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.