Error analysis using Xdebug in PHPStorm

XDebug is great php-module for application debug «the right way», which has been in «older» languages (read - not interpreted) for decades, because of compilter validation. The need to fully debug is obvious in complex applications where error reproduction takes way too much time, and data size makes use of print_r() pretty much unuseable, though this module can doo it tooNormal stack trace in browser

Because xdebug is a module, not every shared hosting has it installed, so everyone assumes that developer will install php+xdebug by himself. After that, you need to edit php.ini. Note that remote_host property limits number of developers that can create session to xdebug by IP.

extension=C:\Program Files\php\ext\php_xdebug-2.1.0-5.3-vc9.dll
xdebug.profiler_enable = 1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.idekey=

To start debugging, you need to connect IDE and Xdebug module, so that it would stop php process and send state dump of all variables to PHPStorm:

  1. Turn port 9000 listening on in PHPStorm and then start debug mode using Shift+F9 or Run → Debug menu.
    listen_off.png

  2. Write debug mode in your cookies for Xdebug on remote host to understand when it should work. Jetbrains have created marklet generator to make it as easy as possible. Browser plugins would be another solution. Ide key parameter is entered with same value as it was entered in IDE and php.ini

Now we can set breakpoint on any line. The problems start when data actually starts flowing - if remote server runs on linux and you have windows so naturally IDE will not be able to understand linux paths in trace hierarchy. Good thing this is easily solved by path mapping. Another problem is encoded files. XDebug will keep telling you paths, but IDE will not be able to check any of it.

Debug example without break point

And the biggest problem is showing stack trace when error/exception occurs without setting breakpoint. I've solved this by using error reporting with xdebug_break() which triggers IDE panel, the only problem remaining is that fatal errors do not show last method in stack trace.

function ErrorHandler($errno, $errstr, $errfile, $errline) { if (!in_array($errno,array(E_NOTICE,2048))) { xdebug_break(); restore_error_handler(); trigger_error($errno.$errstr." in ".$errfile." on line ".$errline."; showed by error handler "); } } function shutDownFunction() { if(!is_null($e = error_get_last())) xdebug_break();} function exceptionHandler($exception) { xdebug_break(); restore_exception_handler();} set_error_handler('ErrorHandler'); register_shutdown_function('shutdownFunction'); set_exception_handler('exceptionHandler');
RSS