Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Debugging using XDebug"

 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
= Debugging using XDebug<br>  =
+
== About Xdebug <br>  ==
  
XDebug is an opensource Debugger and Profiler for PHP. PDT has built in support for Xdebug, which allows you to step-debug through your PHP files.<br>  
+
Xdebug is an opensource Debugger and Profiler for PHP. PDT has built in support for Xdebug, which allows you to step-debug through your PHP projects.<br>  
  
 
<br>  
 
<br>  
Line 11: Line 11:
 
=== Installation<br>  ===
 
=== Installation<br>  ===
  
The first step is to install XDebug and verify that XDebug is running. See the [http://xdebug.org/docs/install Installation] section of the XDebug manual for how to obtain the extension.<br>  
+
The first step is to install Xdebug and verify that Xdebug is running. See the [http://xdebug.org/docs/install Installation] section of the Xdebug manual for how to obtain the extension.<br>  
  
To setup XDebug as your default debugger in PDT, simply configure the '''Default Settings''' in the '''Debug''' eclipse preference page of PHP  
+
To setup Xdebug as your default debugger in PDT, simply configure the '''Default Settings''' in the '''Debug''' eclipse preference page of PHP  
  
[[Image:Debug_Settings.png]]<br>  
+
[[Image:Debug Settings.png]]<br>  
  
 
=== Configuration<br>  ===
 
=== Configuration<br>  ===
  
The most important setting for xdebug to work with PDT is '''xdebug.remote_enable''' = 1. Typically you set this value in your php.ini. To verify that XDebug
+
The most important setting for Xdebug to work with PDT is '''xdebug.remote_enable''' = 1. Typically you set this value in your php.ini. To verify that Xdebug
  
is loaded by your server, use the phpinfo() method and check if you can find an XDebug configuration section.  
+
is loaded by your server, use the phpinfo() method and check if you can find an Xdebug configuration section.  
  
[[Image:PHP_INI1.png]]
+
[[Image:PHP INI1.png]] [[Image:PHP INI2.png]]  
[[Image:PHP_INI2.png]]
+
  
 
<br>  
 
<br>  
Line 38: Line 37:
 
==== Debugging using the default DocumentRoot (http://localhost)  ====
 
==== Debugging using the default DocumentRoot (http://localhost)  ====
  
PDT provides a preference dialog to configure different PHP servers.
+
PDT provides a preference dialog to configure different PHP servers.  
  
[[Image:PHP_Servers.png]]
+
[[Image:PHP Servers.png]]  
  
 
Each server has has a '''Base URL '''and a '''Local Web Root '''property.<br>  
 
Each server has has a '''Base URL '''and a '''Local Web Root '''property.<br>  
  
[[Image:Server_Settings.png]]
+
[[Image:Server Settings.png]]  
  
When you start a debug session, the ''Base URL'' is used in conjunction with the script to debug to determine the absolute URL to open. For example, when you debug<br>  
+
When you start a debug session, the ''Base URL'' is used in conjunction with the path of the script you're debugging to determine the absolute URL to open. For example, when you debug<br>  
  
 
the script ''index.php'' in the root of your Project with the default PHP Server, PDT will launch a debug session in a browser with the URL '''http://localhost/index.php.'''<br>  
 
the script ''index.php'' in the root of your Project with the default PHP Server, PDT will launch a debug session in a browser with the URL '''http://localhost/index.php.'''<br>  
  
If the '''Local Web Root''' property of a server is set, you can choose the Server as the base-directory in the ''New PHP Project'' dialog<br>  
+
If the '''Local Web Root''' property of a server is set, you can choose the Server as the base-directory in the ''New PHP Project'' dialog (see screenshot below).<br>  
  
 
<br>  
 
<br>  
Line 59: Line 58:
  
 
of your webserver (e.g. /var/www/htdocs).localhost<br>  
 
of your webserver (e.g. /var/www/htdocs).localhost<br>  
 +
 +
[[Image:Web Root.png]]
  
 
<br>  
 
<br>  
Line 64: Line 65:
 
When creating a new PHP project, you will now see a 3rd option named '''Create project on local server.''' The new project will be created in a subfolder of the Local Web Root of the selected server, not  
 
When creating a new PHP project, you will now see a 3rd option named '''Create project on local server.''' The new project will be created in a subfolder of the Local Web Root of the selected server, not  
  
in the default workspace path.  
+
in the default workspace path:
 +
 
 +
[[Image:New Project.png]]
  
 
<br>  
 
<br>  
  
Now let's create a new empty PHP project named "MyProject" in the default PHP server and a simple''index.php'' in the root of the project. To debug your script, all you need to do is right-clicking the index.php<br>
+
Now let's create a new empty PHP project named "MyProject" in the default PHP server and a simple ''index.php'' in the root of the project. To debug your script, all you need to do is right-clicking the index.php file and select Debug as -&gt; PHP Web Application.
  
file and select Debug as -&gt; PHP Web Application.  
+
[[Image:Simple Project.png]]
  
This will launch the default workbench browser with the URL '''http://localhost/MyProject/index.php''' and stop at the first line index.php (this is configurable via the Debug configuration).  
+
This will launch the default workbench browser with the URL '''http://localhost/MyProject/index.php''' and stop at the first line index.php (this is configurable via the Debug configuration):
 +
 
 +
[[Image:Debugger In Action.png]]
  
 
<br>  
 
<br>  
Line 89: Line 94:
  
 
==== Debugging using VirtualHosts (http://myProject.local) ====
 
==== Debugging using VirtualHosts (http://myProject.local) ====
 +
 +
If you're using VirtualHosts for your local project, you need to configure a PHP Server first. This server simply needs to have the correct URL to your project,
 +
and the path to the Document root (Preferences -> PHP -> PHP Servers):
 +
 +
<br/>
 +
[[Image:Php_server.png]]
 +
 +
<br/>
 +
 +
After you've setup the PHP server, you can create a new debug configuration (Run -> Debug Configurations -> PHP Web Application) using the newly created server:
 +
 +
<br/>
 +
 +
[[Image:Debug_configuration.png]]
 +
 +
<br/>
 +
 +
Now you can simply click "Debug" and the debug session should open in your browser.
 +
 +
 +
==== Choosing the browser to use for debugging ====
 +
 +
By default, eclipse launches the debug session inside the internal browser. It is recommended to use an external browser though. You can specify the browser to use in the general preferences:
 +
 +
[[Image:Browser_selection.png]]
 +
 +
==== Perspectives ====
 +
Eclipse may be configured to open the Debug perspective automatically or to get back to the PHP perspective automatically, but not both (see [https://bugs.eclipse.org/bugs/show_bug.cgi?id=46336 bug 46336]).

Latest revision as of 13:34, 2 November 2019

About Xdebug

Xdebug is an opensource Debugger and Profiler for PHP. PDT has built in support for Xdebug, which allows you to step-debug through your PHP projects.


Setup


Installation

The first step is to install Xdebug and verify that Xdebug is running. See the Installation section of the Xdebug manual for how to obtain the extension.

To setup Xdebug as your default debugger in PDT, simply configure the Default Settings in the Debug eclipse preference page of PHP

Debug Settings.png

Configuration

The most important setting for Xdebug to work with PDT is xdebug.remote_enable = 1. Typically you set this value in your php.ini. To verify that Xdebug

is loaded by your server, use the phpinfo() method and check if you can find an Xdebug configuration section.

PHP INI1.png PHP INI2.png


Usage


The following examples assume you're debugging on a local mashine, not a remote server.


Debugging using the default DocumentRoot (http://localhost)

PDT provides a preference dialog to configure different PHP servers.

PHP Servers.png

Each server has has a Base URL and a Local Web Root property.

Server Settings.png

When you start a debug session, the Base URL is used in conjunction with the path of the script you're debugging to determine the absolute URL to open. For example, when you debug

the script index.php in the root of your Project with the default PHP Server, PDT will launch a debug session in a browser with the URL http://localhost/index.php.

If the Local Web Root property of a server is set, you can choose the Server as the base-directory in the New PHP Project dialog (see screenshot below).


Typically you will set the Local Web Root of the default PHP server (http://localhost) to the DocumentRoot of your local Web server (e.g. apache).

The default PHP server is http://localhost and has no Local Web Root set. So the first thing to do is to open the PHP Servers preference page, and set the Local Web Root to the DocumentRoot

of your webserver (e.g. /var/www/htdocs).localhost

Web Root.png


When creating a new PHP project, you will now see a 3rd option named Create project on local server. The new project will be created in a subfolder of the Local Web Root of the selected server, not

in the default workspace path:

New Project.png


Now let's create a new empty PHP project named "MyProject" in the default PHP server and a simple index.php in the root of the project. To debug your script, all you need to do is right-clicking the index.php file and select Debug as -> PHP Web Application.

Simple Project.png

This will launch the default workbench browser with the URL http://localhost/MyProject/index.php and stop at the first line index.php (this is configurable via the Debug configuration):

Debugger In Action.png


You can set a breakpoint in the editor on any line by double clicking on a line number on the left.


Use Step Into (F5), Step Over (F6), Step Out (F7) or Resume (F8) to go through your scripts step by step.


Notice that the debugging session will still be open after pressing F8 (Resum), so you simply need to refresh the browser to restart debugging your script.


Debugging using VirtualHosts (http://myProject.local)

If you're using VirtualHosts for your local project, you need to configure a PHP Server first. This server simply needs to have the correct URL to your project, and the path to the Document root (Preferences -> PHP -> PHP Servers):


Php server.png


After you've setup the PHP server, you can create a new debug configuration (Run -> Debug Configurations -> PHP Web Application) using the newly created server:


Debug configuration.png


Now you can simply click "Debug" and the debug session should open in your browser.


Choosing the browser to use for debugging

By default, eclipse launches the debug session inside the internal browser. It is recommended to use an external browser though. You can specify the browser to use in the general preferences:

Browser selection.png

Perspectives

Eclipse may be configured to open the Debug perspective automatically or to get back to the PHP perspective automatically, but not both (see bug 46336).

Back to the top