PHP Debugging and Profiling – Xdebug
Join the DZone community and get the full member experience.
Join For Free
php debugging and profiling – xdebug
when we are talking about web development, php surely one of the most popular languages in the world. its popularity can be judged from the fact that all the major cms solutions available are powered by php. however for long php developers faced a problem in absence of a suitable debugger which other programming languages such as java and c# enjoyed. overcoming this meant manually adding debug statements which was quite a challenging task. this problem was solved once xdebug hit the scenes and soon became one of the most popular php extensions.
xdebug is a free and open source and offers php developers much more than just basic debugging support. it can be used for stack traces, profiling, code coverage etc. in this write-up we shall take a look at how we can install and configure xdebug. apart from this we shall also look at how we can debug and read a profiling report.
installation
installing xdebug is extremely easy as it is available as pecl extension. in our example we shall be using debian/ubuntu. this process may be slightly different for other linux distributions. to install a pecl extension we need to install pear first.
to install a pecl extension you need to install pear first. be sure that you have installed pear and continue then with installing the xdebug pecl extension:
install pecl/pear for phpshell
sudo apt-get install php-pear sudo apt-get install php5-dev
install xdebug as pecl extensionshell
sudo pecl install xdebug
configuration
once the installation is over you need to your php.ini and add the following line to the extension section:
php.ini
zend_extension=/usr/lib/php5/{builddate}/xdebug.so
make sure you use ‘zend_extension’ instead of ‘extension’ and replace {builddate} with the date you found in your php5 lib folder.
php.ini
xdebug.remote_enable=1 # enable remote debugging for all hosts, which use this php.ini xdebug.remote_handler=dbgp # debugger protocol, you may change this value to 'gdp' if your ide is supporting only this xdebug.remote_mode=req # xdebug tries to connect to your ide as soon as you start a script, you can choose 'jit' when xdebug shall connect to our ide only when an error condition occurs xdebug.remote_port=9000 # default port for xdebug xdebug.remote_host=aaa.bbb.ccc.ddd # the ip of the remote debugger (your local ip) xdebug.remote_autostart=0 # xdebug does only start when you set get/post/cookie variable, if you set it to
with these steps you have installed and configured xdebug with a basic setup.
debugging
now that we are done with the installation and configuration let us see how the debugging process works. you might be tempted to use the var_dump() and exit/die() combination for debugging but this process will require you to modify the code for debugging. you can bypass this problem in xdebug as it lets you pause your application’s execution where ever you want. you can easily configure netbeans to act as an xdebug client. open the options window (tools > options) and in the php section go to the debugging tab. enter the debugging port given in php.ini and a session id as this allows you to run the debugger by clicking debug in the tools tab.
now press the debug button in the toolbar to start debugging. an application opens in a browser, and php’s execution will pause at the first line if you have enabled “stop at first line” otherwise, it will run until it encounters the first breakpoint. go to the next break point using the continue button. you will notice xdebug_session_start parameter in the browser’s url. to start the debugger you must pass xdebug_session_start as a request parameter (get/post) or xdebug_session as a cookie parameter. further you can easily add breakpoints by clicking the line number in the editor’s margin and then pause execution on them.
profiling
to optimize an application you must profile them. this records important details like the time it takes for statements and functions to execute or the number of times they are called. to make use of xdebug as a profiling tool for php add the following settings to php.ini
xdebug.profiler_enable = 1 xdebug.profiler_output_name = xdebug.out.%t xdebug.profiler_output_dir = /tmp xdebug.profiler_enable_trigger = 1
please note that profiling is disabled by default in xdebug as it can reduce the performance of php. to enable it you will have to use xdebug.profiler_enable. you don’t want to run it all the time and thus make use of xdebug.profiler_enable_trigger to instruct xdebug to perform profiling only when xdebug_profile is passed as a get or post parameter.
to sum up xdebug is one of the most useful extensions for php developers. it controls the execution of php programs in the server. it also allows you a feature to remote debug and inspects values at runtime, without modifying your program. in the installation process we have discussed how to install xdebub for remote debugging.
Published at DZone with permission of Andrey Prikaznov, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments