Use Xdebug in PHP for remote error debugging

Source: Internet
Author: User
Tags cdata derick ini php error xmlns port number
Xdebug provides interfaces for interaction between clients and PHP scripts. This chapter describes how to enable this feature for PHP and Xdebug, and describes some common clients.

Overview

The Xdebug (remote) debugger allows you to test the data structure, step by step, and debug your code. Xdebug provides two protocols for interaction: the old one in Xdebug1.3 and 2GDB protocolAnd implemented in Xdebug2DBGp protocol.

Client

Xdebug2 provides a simple command line-based client for the DBGp protocol. Of course, there are also some other client implementations (free and commercial ). I am not the author of these clients, so please go to the author's website for help.

  • Dev-PHP (IDE: Windows)
  • Eclipse plugin, which has been submitted as an enhancement for the PDT (IDE ).
  • Emacs plugin (Editor Plugin ).
  • ActiveState's Komodo (IDE: Windows, Linux, Mac; Commercial ).
  • MacGDBP-Standalone Mac client.
  • NetBeans (IDE: Windows, Linux, Mac OS X and Solaris.
  • Notepad ++ plugin (Editor: Windows ).
  • WaterProof's PHPEdit (IDE, from version 2.10: Windows; Commercial ).
  • Anchor System's Peggy (IDE: Windows, Japanese; Commercial ).
  • MP Software's phpDesigner (IDE: Windows, Commercial ).
  • PHPEclipse (Editor Plugin ).
  • JetBrain 'sphpstorm (IDE; inclucial ).
  • Protoeditor (Editor: Linux ).
  • TsWebeditor (Editor: Windows ).
  • Xored's TrueStudio IDE (IDE; trusted CIAL ).
  • VIM plugin (Tutorial) (Editor Plugin ).
  • Jcx software's VS. Php (MS Visual Studio Plugin; Commercial ).
  • XDebugClient-Standalone Windows client.
Xdebug1.3 also comes with a simple command line client based on the GDB protocol.

Start the debugger

To enable the Xdebug debugger, you mustPhp. iniFile. These configurations includeXdebug. remote_enableUsed to allow the debugger,Xdebug. remote_hostAndXdebug. remote_portSpecify the IP address and port number that the debugger should connect. If you want the debugger to initialize a session when an error occurs (php error or exception), you need to modifyXdebug. remote_modeConfiguration. The values allowed by this configuration item are:Req(Default) let the debugger initialize the session orJitInitialize a session only when an error occurs.

After completing the preceding configuration, you will find that Xdebug does not automatically start a debugging session when the script is running. You also need to follow the methods below to activate the Xdebug debugger.
  1. When running a script from a command line, you need to set an environment variable, as described below
    Export XDEBUG_CONFIG = "idekey = session_name" php myscript. php
    You can also configure Xdebug. remote_hostXdebug. remote_portXdebug. remote_modeAnd Xdebug. remote_handlerThese options:
    Export XDEBUG_CONFIG = "idekey = session_name remote_host = localhost profiler_enable = 1"
    All the configuration items you set can also be set in the php. Ini file.
  2. If you want to debug a script accessed through a browser, you only need to add XDEBUG_SESSION_START = session_namePass the parameters. In the next section, you will see how a debugging session works in a browser window.
  3. Another method to activate xdebug is to install one of the following three browser extensions when the browser is running. Each of the following extensions allows you to enable the debugger by clicking a button. These extensions are as follows:
    • The simplest Xdebug 
      This extension is used on Firefox to make debugging easier with IDE. You can find this extension at https://addons.mozilla.org/en-us/firefox/addon/the-easiest-xdebug.
    • Xdebug Helper for Chrome 
      This extension runs on the Chrome browser. It will help you click a button to allow/disable debugging and performance analysis. You can.
    • Xdebug Toggler for Safari 
      This extension runs on Safari, allowing you to automatically start the Xdebug debugging process in Safari, and you can find this extension https://github.com/benmatselby/xdebug-toggler on Github.
    • Xdebug launcher for Opera 
      This extension runs on Opera and allows you to enable an Xdebug session on Xdebug.
Before starting to execute the script, you must first tell the client to receive the debugging connection. Please check the documentation of the client you are using to obtain how to do this. To use the bound client, first install it. After installation, you can run the command" Debugclient"Command. If you want to use the GDB command set to debug your script, make sure that you are using the client bound to Xdebug1.3.
After the debugclient starts running, it will display the following information and wait for the source to wait until the debug server connection arrives for initialization:
Xdebug Simple DBGp client (0.10.0) Copyright 2002-2007 by Derick Rethans.-libedit support: enabled Waiting for debug server to connect.
After the connection is complete, the debug server displays the following output:
Connect <? Xml version = "1.0" encoding = "iso-8859-1"?> <Init xmlns = "urn: debugger_protocol_v1" xmlns: xdebug = "http://xdebug.org/dbgp/xdebug" fileuri = "file: /// home/httpd/www.xdebug.org/html/docs/index.php "language =" PHP "protocol_version =" 1.0 "appid =" 13202 "idekey =" derick "> <engine version =" 2.0.0RC4-dev "> <! [CDATA [Xdebug]> </engine> <author> <! [CDATA [Derick Rethans]> </author> <url> <! [CDATA [http://xdebug.org]> </url> <opyright> <! [CDATA [Copyright (c) 2002-2007 by Derick Rethans]> </copyright> </init> (cmd)

 

Then you can use the command set described in the DBGp document to perform operations. When the script execution ends, the debugging server will disconnect the client and restore it to the status waiting for new connection requests.

Establish a connection

For a single developer with a static IP address

Xdebug is used for remote debugging. Xdebug acts as a program embedded in PHP and acts as a client, while IDE acts as a server. The following dynamic diagram shows the connection creation process.

  • The IP address of the server is 10.0.1.2. The HTTP protocol is used and the port is 80.
  • IDE is on a machine with the IP address 10.0.1.42,Xdebug. remote_hostSet to 10.0.1.42
  • The IDE listens to Port 9000. Therefore,Xdebug. remote_portSet to 9000
  • The HTTP request is initiated from the server running the IDE.
  • Connect Xdebug to 10.0.1.42: 9000
  • Start Debugging and return an HTTP response

Multiple developers use non-fixed IP addresses

IfXdebug. remote_connect_backThe connection creation process is different for commands:

  • The IP address of the server is 10.0.1.2 and port 80.
  • IDE runs on a computer with a dynamic IP address.Xdebug. remote_connect_back1
  • The IDE listens to Port 9000. Therefore, configureXdebug. remote_portIs 9000
  • Send an HTTP request. Xdebug checks the IP address in the HTTP header.
  • Start Debugging and generate an HTTP response

HTTP debugging session

When using the browser for debugging, Xdebug supports the cookie-based session tracking function.

  • When the parameterXDEBUG_SESSION_START = nameIs appended to the URL address, Xdebug will setXDEBUG_SESSIONThe value is a parameter.XDEBUG_SESSION_STARTCookie of the specified name. The Cookie expires in one hour.DBGpThe protocol will also pass the same value in the initialization package, so that you can connect to the setIdekeyThe client of the property.
  • WhenXDEBUG_SESSION_STARTGET (POST) variable orXDEBUG_SESSIONXdebug will try to connect to debugcliet.
  • To stop the xdebug session, you only need to pass oneXDEBUG_SESSION_STOPThen Xdebug will not attempt to connect to the debugclient.

Multi-user debugging

Xdebug only allows you to use the specified IP address (Xdebug. remote_host) To remotely debug the connection. It will not automatically connect back to the IP address of the machine that runs the access in the browser, unless you useXdebug. remote_connect_backCommand.

If your developers develop on different projects on the same server, you can use . HtaccessFunction Xdebug. remote_hostCommand, configuration Php_value xdebug. remote_host = 10.0.0.5. However, if multiple developers develop on the same code, . HtaccessThis feature cannot be completed.
There are two solutions to this problem. The first one is that you can use the DGBp proxy. For details about how to use this proxy, refer to this article for multi-user debugging. You can download this proxy at the site of ActiveState. Here are more documents in the Komodo FAQ.
The second solution is available Xdebug. remote_connect_backConfiguration item (introduced after Xdebug 2.1 ).

Related configuration

 

Xdebug. extended_info 
Type: integer, Default value: 1
Control whether Xdebug should force the PHP interpreter to use the 'extended _ info' mode. This allows Xdebug to use the remote debugger to set breakpoints for files or rows. This option is usually disabled when stack tracing or performance debugging is performed on the script, because some debugging attributes added to PHP will slow down script execution and affect the final result. This attribute can only be Php. iniFile. Ini_set ()Function.

 

  • Xdebug. idekey 
    Type: string, Default value: Complex
    Control Xdebug should be passed DBGpDebug the IDE key of the processor. Environment-based configuration is used by default. First, DBGP_IDEKEYWill be used, followed by USER and USERNAME. By default, the configuration value found for the first time in the environment variable is used. If the configuration cannot be found, the default ''is used ''. If this option is set, it overwrites the environment variable configuration.
  • Xdebug. remote_autostart 
    Type: boolean, Default value: 0
    Generally, you need to use the specified http get/POST variable to activate the remote debugging function of Xdebug. When this parameter is set to 1, Xdebug always tries to connect to the debugging client during script execution, even if no GET/POST/COOKIE variable is set. =
  • Xdebug. remote_connect_back 
    Type: boolean, Default value: 0, Introduced in Xdebug> 2.1
    If yes, Xdebug. remote_hostThe setting will be invalid, and Xdebug will try to connect to the debugging client of the computer that sends the HTTP request. It will check $ _ SERVER ['remote _ ADDR ']Variable to find the IP address used. Please note that there is no available filter. Anyone connected to webserver can start a debugging session, even if their IP address and Xdebug. remote_hostNot the same.
  • Xdebug. remote_cookie_expire_time 
    Type: integer, Default value: 3600, Introduced in Xdebug> 2.1
    This option is used to control the time available for debugging sessions.
  • Xdebug. remote_enable 
    Type: boolean, Default value: 0
    This option controls whether remote debugging is allowed. If a connection cannot be established, the script will continue to be executed, just as the value of this configuration is 0.
  • Xdebug. remote_handler 
    Type: string, Default value: dbgp
    This value can be Php3Used to debug the output using the old PHP 3 style, GdbThe debugger interface that allows the use of GDB or DbgpProtocol. DBGpThe protocol is the only supported protocol.
The code is as follows: Copy code
Note: Xdebug 2.1 and later versions only support DbgpProtocol.
  • Xdebug. remote_host 
    Type: string, Default value: localhost
    Select the host on which the debugging client runs. This option can use the host name or IP address. If Xdebug. remote_connect_backThis option is ignored.
  • Log opened at 14:28:15-> <init xmlns = "urn: debugger_protocol_v1" xmlns: xdebug = "http://xdebug.org/dbgp/x... ight> </init> <-step_into-I 1-> <response xmlns = "urn: debugger_protocol_v1" xmlns: xdebug = "http://xdebug.org/db...> </response>
  • Xdebug. remote_mode 
    Type: string, Default value: req
Select when to establish the debugging connection. This option has two different values:
Req 
Xdebug connects to the debugging client immediately when the script execution starts.
Jit 
Xdebug only tries to connect to the debugging client when a script error occurs.
  • Xdebug. remote_port 
    Type: integer, Default value: 9000
The port number used by Xdebug to connect to the client. The default port is 9000.
Related functions
Bool xdebug_break ()
Sends a breakpoint to the debugging client. This function allows the debugger to set a breakpoint on the specified line.

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.