Use DBGPavim to Debug PHP/Python programs in Vim

Source: Internet
Author: User
This article mainly introduces how to use VIM + XDebug to debug the PHP program on the server. although there are many articles about how to use Eclipse + XDebug to Debug PHP on the developer's work machine, however, VIM + XDebug is rarely configured in the system, and an old plug-in is used in current articles on VIM settings. Here we mainly introduce a new plug-in DBGPavim, which has many advantages over some old plug-ins. At the same time, this plug-in can be perfectly used for Python program debugging. In addition, compared with Eclipse +, VIM + XDebug .. this article describes how to Debug the PHP program by using VIM + X # code/8684.html "target =" _ blank "> Debug on the server, although there are many articles about how to use Eclipse + XDebug to Debug PHP on the developer's work machine, there are still few articles about how to configure VIM + XDebug in the system, currently, an old plug-in is used in VIM settings. Here we mainly introduce a new plug-in DBGPavim, which has many advantages over some old plug-ins. At the same time, this plug-in can be perfectly used for Python program debugging. Besides, VIM + XDebug has many advantages over Eclipse + XDebug, which will be described in this article.

Implementation principle

DBGp is a protocol for communication between the debugger background and the Debugger Interface. it is used for debugging in multiple scripting languages. XDebug is used to debug the DBGp implementation of PHP. VIM needs to communicate with XDebug to Debug PHP. it must be able to understand the DBGp protocol and send the DBGp command. DBGPavim is a plug-in that enables VIM to accept DBGp requests and send DBGp commands for debugging purposes. The name of DBGPavim is derived from DBGp @ VIM.

ActiveState provides DBGp for Debugging Python/Ruby to implement Komodo Remote Debugging Package. later, we will talk about how to connect it with VIM to debug Python. The user will be able to debug ruby, nodejs, and other script languages accordingly.

Configure XDebug
    1. For how to install XDebug, see http://xdebug.org/docs/install.

    2. Edit php. ini and add the following two lines:

      zend_extension=/path/to/xdebug.so xdebug.remote_enable=1
    3. Edit httpd. conf and add the following lines:

       php_value xdebug.remote_autostart 1
      1. Note: This line is not required. if this line is not added, you need to add the XDEBUG_SESSION_START = 1 parameter to the URL of the HTTP server, for example, http: // localhost/index. php? XDEBUG_SESSION_START = 1.

If multiple developers need to debug different virtualhosts at the same time, you can add the following lines in your VirtualHost segment:

 php_value xdebug.remote_port 9009

Note: Here, 9009 is the port that VIM should listen to as the DBGp server. different developers use different port numbers in different virtualhosts. This port number must be consistent with the dbgPavimPort mentioned in the next section. If this row is not added, the default port number is 9000.
Finally, you can use phpinfo. php to check whether your XDebug configuration is correct. you must be able to see the values of the following rows (mainly the first two columns ):

xdebug.remote_autostartOnOff xdebug.remote_enableOnOn xdebug.remote_handlerdbgpdbgp xdebug.remote_host127.0.0.1127.0.0.1 xdebug.remote_port90099000

The phpinfo. php file contains the following content:

 
Configure VIM + DBGPavim

The DBGPavim plug-in is implemented in Python, so you need to support Python 2.7 in VIM. Open your VIM and enter the command

:version

If you can see "+ python", your VIM supports Python. If you see "-python", it means that your VIM does not support Python. you can follow the steps below to compile your VIM:

  1. Install Python 2.7

  2. Export path =/path/to/python2.7/bin: $ PATH

  3. Run the following command to compile VIM:

 ./configure --prefix=/opt/vim --enable-pythoninterp --with-python-config-dir=/usr/lib/python2.7/config make make install

Note: The/usr/lib/python2.7/config here depends on where you install Python2.7.

Download DBGPavim from here or here and put it to your ~ /. Vim directory and edit your ~ /. Add the following two lines to vimrc:

let g:dbgPavimPort = 9009let g:dbgPavimBreakAtEntry = 0

Note: The 9009 value here must be the same as the 9009 value in the previous section. if xdebug. remote_port is not configured in the previous section, no configuration is required because the default 9000 value is used. DbgPavimBreakAtEntry = 0 tells VIM not to stop at the entrance. this will only stop at the breakpoint.

You can restart VIM and press F5 to check whether your DBGPavim configuration is correct. If the configuration is successful, you will see the following prompt in the lower right corner of the VIM window:

bap-LISN-9009

It indicates that VIM is currently listening to Port 9009, and bap indicates that it will only stop at the breakpoint. Other prompts are in the following format:

 
  -
  
 

Breakpoint status

Bae Break At Entry, stop At the entrance, bap Break only At breakPoints, and only stop At the breakpoint

Debugger status

The LISN debugger has been started and is listening. The PEND-n debugger has captured connection requests. you can press F5 to enter the debugging mode. Conw.m is in debugging mode. The CLSD debugger has stopped.
Debug PHP in Apache environment
  1. After confirming that the configuration is correct, you can use VIM to open the file you want to debug, jump to the line you want to debug, press F10 to set the current behavior breakpoint, and press F5 to start the debugger.

  2. Accessing through a browser will call the URL of the corresponding php file. you will see the prompt information in the VIM status bar changed:

     bap-PEND-1
  1. It tells you that a connection has been intercepted. you can press F5 to start debugging.

    Advantages of VIM + DBGPavim over Eclipse + XDebug

    Most servers do not start XServer and cannot start Eclipse on the server. If you start Eclipse + XDebug on the developer's work machine, it is equivalent to running the DBGp Server on the work machine. you need to set the path ING, that is, a copy of the code executed by the HTTP Server on the Server, during Eclipse debugging, a copy of code is opened. on the working machine, ensure that the two pieces of code correspond to the ing path. When the program is not large, the problem is not big. when the program is large, it will be troublesome, and the code synchronization should be ensured, otherwise it will be serialized.

    You can also encounter problems such as network firewalls.

    VIM + DBGPavim also supports remote debugging, but cannot avoid path ing settings, as shown below:

    let g:dbgPavimPathMap = [['D:/works/php','/var/www'],]
    1. Note: Port 9009 is equivalent to the xdebug. remote_port set during PHP Debugging. it must be consistent with dbgPavimPort.

    2. Return to your VIM window and you will see the prompt message PEND-1.

    3. Press F5 to enter the debugging mode.

Advantages of DBGPavim over other plug-ins

DBGPavim was originated from a DBGp plug-in http://www.vim.org/scripts/script.php earlier in VIM? Script_id = 1152. some other DBGp plug-ins are derived from this plug-in. However, DBGPavim overrides the DBGp server used as the background of the debugger for asynchronous listening, so that VIM listens to DBGp without interfering with interaction between users and VIM. After you press F5 to start the debugging listener, you can continue to use VIM and press F6 to stop the listener at any time.

DBGPavim listens to all DBGp connections from DBGp clients such as XDebug and pydbgp. unlike other plug-ins, DBGPavim can only capture the first connection. This is required for large-scale WEB programs, because a webpage load usually triggers multiple HTTP requests, and debugging may come from any of them. At the same time, DBGPavim supports only stopping at the breakpoint, and other plug-ins are stopped at the entrance. programmers need to follow up step by step. This saves developers a lot of trouble and avoids re-debugging after errors.

I believe you have also discovered that DBGPavim can work well with GVIM in Windows.

For more information about DBGPavim, see

VIM normal mode

F5 starts the debugging listening, or enters the debugging mode when debugging connections are available. F6 stop debugging listening. Press F8 to switch the value of dbgPavimBreakAtEntry. you can see that the status bar prompts you to switch between bae and bap, that is, whether to stop at the PHP program entrance. F10 can be used to set or delete breakpoints in the current line in debugging mode.

Debugging Mode

F1 open or close the help window F2 step into F3 one step skip F4 one step exit F5 continue to execute until the next breakpoint, if there is no breakpoint in the future exit the debugging mode. F6 stop debugging. this button causes VIM to exit debugging mode and stop debugging listening. Run the php statement during F7 debugging. After Pressing F7, you can enter the php statement in the variable display window and press enter to execute. F9 maximizes a subwindow or resets the window layout. F11: view the values of all variables in the current execution environment. different results are returned at different stack levels. F12: view the variable value under the cursor.

The above function keys are configured by default. if you are used to setting keys in most browsers, you can add the following code to your. vimrc:

let g:dbgPavimKeyRun = '
 
  'let g:dbgPavimKeyStepOver = '
  
   'let g:dbgPavimKeyStepInto = '
   
    'let g:dbgPavimKeyStepOut = '
    
     'let g:dbgPavimKeyPropertyGet = '
     
      'let g:dbgPavimKeyContextGet = '
      
       'let g:dbgPavimKeyToggleBp = '
       
        'let g:dbgPavimKeyToggleBae = '
        
         'let g:dbgPavimKeyRelayout = '
         
          '
         
        
       
      
     
    
   
  
 

VIM command. only the first letter of all commands is in uppercase.

: Bl lists all breakpoints: Bp and F10 have the same function. The p command can be used to quickly debug the current file. it implements the following functions: 1. check whether the XDebug/pydbgp settings in the command line are correct. 2. start the debugger listener. 3. execute the current file g with php/pydbgp
 
  
View the value of a long variable, for example: Pg $ this-> savings [3]: Up call stack to the next n call stack level: wc [$ foo] enables/disables monitoring of the variable $ foo. If no parameter exists, all variables in the current execution environment are monitored. : We
  
   
Enable/disable the foo statement monitoring, that is, the foo statement is automatically executed after each step. : Wl lists all monitored variables or statements. : Children
   
    
This command can be modified to display the first 1024 elements in an array by default. Epth
    
     
For complex variables, only the next layer of members is displayed by default. this command can be set to limit multiple layers. : Length
     
      
For string variables, the first 1024 characters are displayed by default. you can set the display length.
     
    
   
  
 

The above is a detailed description of using DBGPavim to Debug PHP/Python programs in Vim. For more information, see other related articles in the first PHP community!

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.