Monday, April 8, 2013

Xdebug

a free and open source  - a swiss army knife tool for PHP developers.
xdebug is a PHP extension created by Derick Rethans, one of the PHP core developers.
We will have a closer look at one of xdebug’s main features, namely tracing, profiling, debugging, and code coverage.

Installing the xdebug extension

First of all, we need to install xdebug. As I write this article, the current version is 2.0.1.
Since the internal PHP APIs may change between different PHP releases, you must make sure
that the version of xdebug you are installing matches the PHP version you are using.
xdebug does not work with any PHP versions before 4.3, and will probably not yet work with PHP 6.
This is not a real problem, however, since PHP 4 will reach its end of life in 2008,
and PHP 6 will probably not be available before the end of 2008. This gives you enough
time to get used to xdebug so you can use it as a helpful tool when it comes to migrating your PHP code
to work with the next major or minor PHP release

Installing on Unix

Before we dig into xdebug’s features, let us get the installation done.
On Unix, you can try installing xdebug through PECL, the PHP extension community library.
The PECL installation does not work on all systems, though. If the PECL installation does not
work out for you, you must compile xdebug from source. But first, try the PECL installation:
pecl install xdebug
If the PECL installation does not work for you, you need to compile xdebug from source.
In addition to a C compiler, you will need appropriate versions of the
usual build tools (Autoconf, Automake and Libtool). If they are not already installed
on your system, you can usually install them by running apt-get install build-essential

Furthermore, two helper programs, phpize and php-config that are a part of PHP, are required.
If you have not compiled PHP from source, you will probably have to install
the developer packages using your distribution’s package manager.
On Ubuntu or Debian, you can install the PHP development tools using a command like
apt-get install php5-dev.
Please note that phpize and php-config must match the PHP version you are using,
so do not just copy them to your system from some other PHP installation. When your development tools are in place, you can download and compile xdebug:

wget http://xdebug.org/link.php?url=xdebug201
tar -xzf xdebug-2.0.1.tgz
cd xdebug-2.0.1
phpize
./configure --enable-xdebug --with-php-config=/usr/bin/php-config
make
cp modules/xdebug.so /usr/lib/apache2/modules/xdebug.so
The php-config path may be different on your system,
and depending on the your Apache installation directory,
you may need to copy xdebug.so to another directory.
Instead of copying the file, you can of course create a link as well.

Installing on Windows

If you are a Windows user, you can download a compiled DLL from xdebug.org.
Select the PHP version you are using and click the appropriate link in the Windows modules section in the right column of the page.
You must use the non-debug version of PHP with xdebug. If you have downloaded PHP from php.net,
debugging should not be enabled. When in doubt, check the Debug Build entry in the phpinfo() output.
I would recommend putting the downloaded DLL into PHP’s extension directory ext, which should be a subdirectory of your PHP directory. You can put the DLL to any directory, provided that you state the full path to the DLL in php.ini.

Activating the xdebug extension

Now you have the xdebug extension ready, either as a shared object on Unix or a DLL on Windows.
To activate xdebug, you must add an entry to php.ini. On Windows, use:
zend_extension_ts="c:\php\ext\php_xdebug-2.0.1-5.2.1.dll"
On Unix, use:
zend_extension="/usr/lib/apache2/modules/xdebug.so"
instead. The path to PHP’s extension directory or Apache’s module directory may differ on your system. Make
sure you specify the full absolute path, not a relative path.
Please note that on Windows, we use zend_extension_ts, which means that a
thread-safe extension is loaded, whereas on Unix, we a non-threadsafe extension is loaded.
Depending on your system setup,
you must decide for yourself wether you need a thread safe or non-thread-safe extension.
If you are not sure wether your PHP installation is thread-safe,
check the Thread Safety entry in the phpinfo() output.
You should not load any other Zend extensions while working with xdebug,
because they would probably use the same internal mechanisms in the Zend engine,
which usually calls for trouble. Not all Zend extensions work together with xdebug.
Since you will probably use xdebug on a development system rather than a live system, this is
no serious limitation. The most important rule is to not mix xdebug with other PHP debugger extensions.
Having restarted your web server because we changed php.ini, you can check
the output of phpinfo() or run php -m at the command line.
In each case, xdebug must be listed twice, once as a PHP extension, and as a Zend extension as well.

Take care when you are updating PHP with xdebug installed. If the internal APIs change between the PHP
versions (which does not happen on every new version, but will certainly happen when you are close to a project deadline), the new version of PHP will probably not start or at least give you funny errors.
If need be, you can always get away with disabling xdebug, at least temporarily, to get an updated
PHP version to work and re-enable xdebug as soon as a new version is released that works with the newer API version.
There are quite some php.ini switches to configure xdebug,
but most of them have sensible deafult values,
so we can start using xdebug without worrying about configuration settings right now.
We will take a look at the most important configuration settings as we need to.

 



No comments:

Post a Comment