PHP uses xdebug for remote debugging

Source: Internet
Author: User
Tags cdata derick php error

PHP uses xdebug for remote debugging

Translation by MYLXSW posted on 2014/07/14 under Technical articles > programming languages

Xdebug provides an interface for clients to interact with PHP scripts, and this chapter will show you how to get PHP and Xdebug to turn on this feature and introduce some common clients.

Overview

The Xdebug (remote) debugger allows you to test the data structure, step in, and debug your code. Xdebug provides two protocols for interacting with them: implemented in the old and Xdebug2 in Xdebug1.3 and 2 GDB协议 DBGp协议 .

Client

XDEBUG2 provides a simple command-line-based client for the DBGP protocol and, of course, some other client implementations (free and commercial). I am not the author of these clients, so please go to the author's website to find use Help.

  • dev-php (Ide:windows)
  • Eclipse plugin, which has been submitted as a 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; Commercial).
  • Protoeditor (Editor:linux).
  • Tswebeditor (editor:windows).
  • XORed ' s Truestudio IDE (IDE; Commercial).
  • VIM plugin (Tutorial) (Editor plugin).
  • JCX Software ' s vs.php (MS Visual Studio Plugin; Commercial).
  • Xdebugclient-standalone Windows Client.

The Xdebug1.3 also comes with a simple command-line client based on the GDB protocol.

Start the debugger

In order to enable the Xdebug debugger, you need to php.ini do some configuration in the file. These configurations contain the xdebug.remote_enable IP address and port number used to allow the debugger, xdebug.remote_host and xdebug.remote_port to specify which debugger should connect to. If you want the debugger to be able to initialize a session in the event of an error (PHP error or exception), you need to modify the xdebug.remote_mode configuration, which allows the req debugger to initialize the session when all scripts start executing, or jit To initialize a session when an error occurs.

When the above configuration is complete, you will find that Xdebug does not automatically open a debugging session when the script is running. You also need to activate the Xdebug debugger as follows.

  1. When you run the script from the 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_host , xdebug.remote_port xdebug.remote_mode and xdebug.remote_handler these options:

    Export xdebug_config= "Idekey=session_name remote_host=localhost profiler_enable=1"

    All of these configuration items you set here can also be set in the php.ini file.

  2. If you want to debug a script that is accessed through a browser, simply add it as a parameter in the Access parameter XDEBUG_SESSION_START=session_name , and in the next section you will see how a debugging session works from the browser window.

  3. Another way 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 open the debugger by clicking a button. These extensions are as follows:

    • The simplest of xdebug
      This extension is used on Firefox to make it easier to debug with the IDE. You can find this extension on the https://addons.mozilla.org/en-US/firefox/addon/the-easiest-xdebug/.
    • Xdebug Helper for Chrome
      This extension is run on the Chrome browser and it will help you to allow/disable debugging and profiling T by clicking the button. You can find this extension in HTTPS://CHROME.GOOGLE.COM/EXTENSIONS/DETAIL/EADNDFJPLGIELDJBIGJAKMDGKMOAAAOC.
    • Xdebug Toggler for Safari
      This extension is run on safari, allowing you to start the Xdebug debugging process automatically in Safari, and you can find this extension https://github.com/benmatselby/xdebug-toggler on GitHub.
    • Xdebug launcher for Opera
      This extension is run on opera, which allows you to open a Xdebug session on the Xdebug.

Before you start executing the script, you need to tell the client that you can receive the debug connection, and check the documentation for the client you are using to get how to do so. To use a bound client, you first need to install it, and after the installation is complete you can run the command " debugclient " command. If you want to use the GDB command set to debug your script, you need to make sure that you are using a Xdebug1.3 bound client.

When Debugclient starts running, it will display the following information and wait until the debug server connection arrives for initialization:

Xdebug Simple DBGP Client (0.10.0) Copyright 2002-2007 by Derick rethans.-libedit support:enabledwaiting for debug server To connect.

After the connection is complete, the debug server will display 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)

You can then use the set of commands described in the DBGP documentation. When the script executes, the debug server disconnects the client and reverts to the state of waiting for a new connection request.

Connection setup for static IP, single developer

With Xdebug remote debugging, Xdebug acts as a client-side role as a program embedded in PHP, while the IDE acts as a server. The following dynamic diagram shows the process of establishing a connection.

    • The server IP is 10.0.1.2, using the HTTP protocol with Port 80
    • The IDE xdebug.remote_host is set to 10.0.1.42 on a machine with an IP address of 10.0.1.42
    • The IDE listens on port 9000, so xdebug.remote_port set to 9000
    • HTTP requests originate from the server running the IDE
    • Xdebug Connecting to 10.0.1.42:9000
    • Debug start Run, return HTTP response
With non-fixed IP, multiple developers

If the xdebug.remote_connect_back instructions are used, the connection creation process is different:

    • The IP on the server is 10.0.1.2 and the port is 80
    • The IDE is running on a computer with a dynamic IP, so it xdebug.remote_connect_back is set to 1
    • The IDE listens on port 9000, so it xdebug.remote_port is configured to 9000
    • Sends an HTTP request, Xdebug detects the IP address in the HTTP header
    • Debug starts running, resulting in an HTTP response
HTTP Debugging session

When using a browser for debug, Xdebug supports the ability to use cookies to track conversations.

    • When the parameter XDEBUG_SESSION_START=name is appended to the URL address, Xdebug will set a cookie named with the XDEBUG_SESSION XDEBUG_SESSION_START name specified by the parameter. The expiration time of this cookie is one hours. The DBGp protocol also passes an identical value in the initialization package so that it can connect to the client that has the idekey property set.
    • When a XDEBUG_SESSION_START cookie named get (POST) variable is set XDEBUG_SESSION , Xdebug will attempt to connect Debugcliet
    • To stop a xdebug session, you only need to pass a XDEBUG_SESSION_STOP parameter, and then Xdebug will not attempt to connect to debugclient.
Multi-User debugging

Xdebug only allows you to use the specified IP address ( xdebug.remote_host ) for remote debugging connections. He will not automatically connect back to the browser to run access to the machine IP unless you are using xdebug.remote_connect_back instructions.

If your developers are developing on different projects on the same server, you can use the features in each of the Apache directories to specify the .htaccess xdebug.remote_host instructions, configure them php_value xdebug.remote_host=10.0.0.5 . However, if multiple developers are developing on the same code, they .htaccess will not be able to complete the function.

There are two solutions to this problem. The first is that you can use the DGBP agent, and for how to use this agent, check out the multi-user debugging article. You can download this agent at ActiveState's site. Here are more documents in the Komodo FAQ.

The second scenario is that you can use a xdebug.remote_connect_back configuration item (introduced after Xdebug 2.1).

Related configuration
  • Xdebug.extended_info
    Type:integer, Default value:1

    Controls whether Xdebug should force the PHP interpreter to use ' extended_info ' mode, which allows Xdebug to set breakpoints on files or rows using the remote debugger. It is often desirable to turn off this option when the script is being stack-traced or performance-tuned, because some of the debugging properties added to PHP will slow down the execution of the script and affect the final result. This property can only be set in a php.ini file and cannot be set through a function in the script ini_set() .

  • Xdebug.idekey
    Type:string, Default value: complex

    Control Xdebug should be passed to DBGp the debug processor that one IDE key. By default, it is based on the environment configuration. First, the environment variables are DBGP_IDEKEY used, followed by the user and username. The default is to use the configuration value found for the first time in the environment variable, and if no configuration is found, use the default '. If this option is set, it overrides the configuration of the environment variable.

  • Xdebug.remote_autostart
    Type:boolean, Default value:0

    Typically, you need to use the specified HTTP get/post variable to activate the Xdebug remote debugging feature. When this setting is 1, Xdebug will always try to connect to the debug client when the script executes, even if the Get/post/cookie variable is not set. =

  • Xdebug.remote_connect_back
    Type:boolean, Default value:0, introduced in Xdebug > 2.1

    If allowed, the xdebug.remote_host settings will be invalidated and Xdebug will attempt to connect to the debug client of the computer sending the HTTP request. He will check the $_SERVER[‘REMOTE_ADDR‘] variables to find the IP address used. Note that there are no filters available here, and anyone connected to webserver can start a debugging session, even if their IP address xdebug.remote_host is not the same.

  • Xdebug.remote_cookie_expire_time
    Type:integer, Default value:3600, introduced in Xdebug > 2.1

    This option is used to control when the debugging session is available.

  • Xdebug.remote_enable
    Type:boolean, Default value:0

    This option controls whether remote debugging is allowed, and if the connection cannot be established, the script will continue to execute as if the value of this configuration is 01.

  • Xdebug.remote_handler
    Type:string, Default VALUE:DBGP

    This value can be used to php3 use the old-fashioned PHP 3-style debug output, which gdb allows the debugger interface or protocol to be used with GDB dbgp . DBGpThe agreement is the only supported protocol.

Note: Xdebug 2.1 and later versions only support the dbgp protocol.

    • Xdebug.remote_host
      Type:string, Default Value:localhost

      Select the debug client to run on that host, this option can use the hostname or IP address. xdebug.remote_connect_backThis option will be ignored if the option specifies a flower.

    • Xdebug.remote_log
      Type:string, Default value:

      If this value is set, it will be used as the file name, and all remote debugging connections will be logged to the log file. This file is always opened in append mode, so do not overwrite it by default. There is no concurrency protection here. The format of the log file is similar to the following:

      Log opened at 2007-05-27 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 the debug connection is established. There are two different values for this option:

req
Xdebug will immediately connect to the debug client when the script execution begins.

jit
Xdebug will only attempt to connect to the debug client in the event of a script error.

    • Xdebug.remote_port
      Type:integer, Default value:9000

Xdebug the port number used to connect to the client. The default port is 9000.

Related functions

BOOL Xdebug_break ()

Sends a breakpoint to the debug client, which allows the debugger to set a breakpoint on the specified line.

PHP uses xdebug for remote debugging

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.