Just-In-Time debugging and PHP Exception Breakpoints with PhpStorm and Xdebug

In every project comes a moment where code stabilizes and we don’t want to keep the debugger attached to our code all the time. Or maybe we just want to run our code and only attach the debugger when an error occurs or an exception is thrown. Meet Xdebug’s just-in-time (jit) mode and PHP Exception Breakpoints in PhpStorm!

By default, Xdebug will only connect to our IDE after we enable remote debugging and use a specific HTTP GET/POST variable to start the debugger. However, we can instruct Xdebug to connect only as soon as an error condition occurs, by using the xdebug.remote_mode setting. Setting it to jit instead of the default (req) will do. It can be done in php.ini or dynamically, per script: ini_set(‘xdebug.remote_mode’, ‘jit’);

The remote_mode setting for Xdebug specifies when the debug session will be initiated. req, the default one, will start a debugging session at the script start. jit will start debugging only after an error or Exception occurs.

Let’s run some code. After kindly asking PhpStorm to listen for incoming debugger connections (from the toolbar or the Run | Start Listen for PHP Debug Connections menu), Xdebug will only connect to our IDE when an error occurs, such as this nice Division by zero which seems to have slipped into our code base:

From the above screenshot we can also see that PhpStorm halts execution on the line right after the error condition. The reason for that is PHP first has to run the erroneous code before it knows something is wrong.

Here’s another example to illustrate where PhpStorm will break when an Exception has been thrown. If it is handled, the debugger will break at the first line of the catch block if there is one, or the finally block when using PHP 5.5:

Note that when jit debugging is enabled in php.ini, traditional debugging options using HTTP GET/POST variables will not work. For that, Xdebug’s remote_mode setting has to be reverted to “req”.

PHP Exception Breakpoints

Next to jit debugging, the latest PhpStorm 7.1 features PHP Exception Breakpoints. With them we can initiate the debugger at the start of the script and break on our own breakpoints or whenever an error or Exception of a given type occurs. PHP Exception breakpoints do not require the Xdebug remote_mode to be set to jit.

We can open the Breakpoints window (Run | View Breakpoints…) and add the errors or Exceptions we want the debugger to break on. In this window we can specify custom Exception types we want to break on or make use of Warning, Notice or Deprecated to break on PHP error conditions instead. Do note that E_ERROR, E_PARSE and E_COMPILE_ERROR can’t be handled as they halt execution of the PHP engine:

When the debugger breaks on an error or Exception, PhpStorm can give us some additional information about the actual error. To see the type, simply hover over the breakpoint which PhpStorm adds in the editor:

More information on PHP errors can be retrieved by adding a watch for the error_get_last() function:

Give it a try and let us hear your thoughts in the issue tracker, through the comments below or in our forums!

Develop with pleasure!
– JetBrains PhpStorm Team

This entry was posted in Cool Feature and tagged , , , , . Bookmark the permalink.
  • Pingback: JetBrains.com: Just-In-Time debugging and PHP Exception Breakpoints with PhpStorm and Xdebug | htaccess

  • http://eriz.pcinside.pl/weblog eRIZ

    Is it possible with Zend Debugger? Or do I have to change my environment to XDebug?

    • Mikhail Vink

      Works for Xdebug only – Zend Debugger doesn’t support exception breakpoints and jit option is Xdebug-specific. So please try using Xdebug.

  • http://derickrethans.nl Derick Rethans

    Hmm, instead of having to call error_get_last() I suppose Xdebug can just stick that in the breakpoint structure after a warning or exception has occurred…

  • http://carstenwindler.de Carsten Windler

    Great feature, thanks! It’s really good to know that you guys keep improving the Debugging in phpStorm. A big thanks to Derick at this point for all his great work on xdebug, too!

    One question please: Say e.g. I have MyException and MyOtherException which both extend Exception, can the debugger break on all three by just defining one breakpoint for Exception, or do I need a setup breakpoint for each or the three?

    I tried it, but it didn’t seem to work. Maybe I did something wrong?

  • Shan

    Is it possible to turn off break_on_exception feature until my custom break point?
    I have a long script to load and its keep breaking everywhere. so i need to sending commands to step out.. step out step out… (whew)
    Please help

    • Liubov Melnikova

      You can remove these breakpoints here: Run | View breakpoints.

  • Eldan

    Hey can you please tell me how can I go to the end of the loop in xDebug?