Pablo Viquez Blog

Mi vida y cosas relacionadas

Skip to: Content | Sidebar | Footer

PHP Step by Step Debugging with JMeter, XDebug & Eclipse PDT

5 December, 2012 (18:21) | General | By: Pablo Viquez

In a previous post I wrote about doing step by step debugging using Zend Debugger and Zend Studio making the calls from JMeter, however not everyone has Zend Server or Zend Studio installed, and since XDebug is an awesome project I though that will be awesome to do the same using XDebug.

Basic Intro

To start, lets define the tools we will required

For this tutorial, you required the following awesome projects:

  • Apache Webserver with the XDebug module installed
    • XDebug is a PHP extension which provides debugging and profiling capabilities. It uses the DBGp debugging protocol.
    • http://xdebug.org
  • Eclipse with PHP Development Tools
    • The Eclipse PHP Development Tools project (PDT) is a PHP Integrated Development Environment (IDE). PDT provides all the core capabilities needed in a PHP IDE. Powerful IDE with all the PHP required tools installed. I normally use the All-In-One package, which is the one I used for doing this tutorial
    • http://www.eclipse.org/pdt/downloads/
    • All-In-One download
  • Apache JMeter
    • JMeter is a desktop application designed to load test functional behavior and measure performance. It was originally designed for testing Web Applications but has since expanded to other test functions.
    • JMeter Project

Issue

As explained previously, using the browser for API debugging can be pretty hard or nearly impossible unless you build some sort of UI, which normally I do not have time to do, so JMeter is perfect for the job!

Now, the hard part is to be able to do real debugging using JMeter, which means hook JMeter to start the debugging session and catch the XDebug call with Eclipse, which sounds quite hard but on the contrary, it’s very simple.

Apache Configuration

XDebug requires some small configurations in the php.ini file in order to work, lets look at them:

Open the php.ini file add the following configurations (if you don’t have it already)

[XDebug]

; Load the XDebug module
zend_extension="{FULL_PATH_FOR_XDEBUG_EXTENSION}/xdebug.so"

; This switch controls whether Xdebug should try to contact a debug client, defaults to 0
; http://xdebug.org/docs/all_settings#remote_enable
xdebug.remote_enable=1


; Selects the host where the debug client is running, you can either use a host name or an
; IP address
; http://xdebug.org/docs/all_settings#remote_host
xdebug.remote_host=127.0.0.1


; If enabled, the xdebug.remote_host setting is ignored and Xdebug will try to connect
; to the client that made the HTTP request. It checks the $_SERVER['REMOTE_ADDR']
; variable to find out which IP address to use. Please note that there is no filter
; available, and anybody who can connect to the webserver will then be able to start a
; debugging session, even if their address does not match
; xdebug.remote_host.
; http://xdebug.org/docs/all_settings#remote_connect_back
; xdebug.remote_connect_back=1

; The port to which Xdebug tries to connect on the remote host. Port 9000 is the
; default for both the client and the bundled debugclient.
; http://xdebug.org/docs/all_settings#remote_port
; xdebug.remote_port=9000


; Can be: php3, gdb or dbgp. The DBGp protocol is more widely supported by clients.
; Default value: dbgp
; http://xdebug.org/docs/all_settings#remote_handler
xdebug.remote_handler=dbgp


; If set to a value, it is used as filename to a file to which all remote debugger
; communications are logged. 
; http://xdebug.org/docs/all_settings#remote_log
xdebug.remote_log="/tmp/xdebug/xdebug.log"


; Controls which IDE Key Xdebug should pass on to the DBGp debugger handler.
; http://xdebug.org/docs/all_settings#idekey
xdebug.idekey=ECLIPSE_DBGP

Remote Debugging Gotchas

  1. XDebug requires a local copy of the source code, if you are doing remote debugging, you need to have the project configured locally
  2. You can follow the same steps and do remote debugging, as long as you have the code locally. You don’t require a working instance running, just the code.
  3. Important: For remote debugging make sure your local port 9000 is open and can receive connections from the remote host.

Eclipse PDT Setup

In Eclipse, we need to create a new project, in my case I’ll use the same Apache instance and domain as the one used here

1. Create the project

Right click in the PHP Explorer area and select New > PHP Project

 

In my case, the project source files were already created, so I selected Created project at existing location

Eclipse Project Settings

 

2. Set XDebug as your debugger

Now we need to tell the project to use XDebug. To do this, right click the project name and select Properties

Project Settings

 

In the properties window, select the PHP Debug option and select as the PHP Debugger XDebug. Once you do that, click Configure…

PHP Project Debug

 

Select XDebug and click Configure

PHP Project Debug

 

Now select any for the Accept remote session (JIT) option.

XDebug Settings

 

With the previous steps, you should be set and ready for serious debugging!. NOTE: I had to restart Eclipse for it to work, I guess because of the port binding but this is just a guess. So after doing these steps it does not work, just restart Eclipse.

JMeter Plan

I will be using the exact same plan as my previous post with the small difference of the Cookie Manager. So I won’t be covering the JMeter setup, just the Cookie Manager part.

Download the JMeter plan

Click here to download the JMeter plan (JMeterXdebug.zip)

XDebug is much more simple than Zend Debugger, it just require one cookie called XDEBUG_SESSION as follow:

Cookie name Cookie value Domain Path
XDEBUG_SESSION ECLIPSE_DBGP Host name In our case, the host is a global JMeter variable, so I’ll be using ${HOST} /

 

JMeter Cookie Manager

Execute the JMeter plan

Now let’s test the plan, on the top menu select Run > Start

Execute the JMeter Plan

 

Now Eclipse should start and ask you to switch to the debug perspective, if you click yes, this is the Eclipse view you will have

Eclipse Debugging

Now Eclipse should start and ask you to switch to the debug perspective, if you click yes, this is the Eclipse view you will have

Note that the parameters sent to the request are available in the Variables view

Eclipse Debugging

You’re done!

Congrats you now have a JMeter plan that triggers an XDebug debugging session!