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:
Develop with pleasure!
– JetBrains PhpStorm Team
Subscribe to Blog updates
Thanks, we've got you!
Qodana Clone Finder: Early Access Program
In December 2020, we announced the EAP for Qodana, which is rapidly evolving into a comprehensive platform that allows companies to perform multi-level evaluations of the quality of code they own, contract, or purchase. Qodana helps you detect bugs without relying on an IDE, either on a local machi…
PhpStorm 2020.2 EAP #3
The Early Access Program for PhpStorm 2020.2 is in full swing and today we’ve got the third build of the 2020.2 EAP for you. In this blog post, you can read about improvements for Git installed in WSL2 and Search Everywhere. Download PhpStorm 2020.2 EAP (more…)…
PhpStorm 2019.3.3 Preview
We’ve just rolled out a preview for the third minor update for PhpStorm 2019.3. Please give the PhpStorm 2019.3.3 Preview build 193.6494.5 a try and share your feedback with us. (more…)…
How to Upgrade to PHP 7.4 with PhpStorm
Check out our fresh video with a few nice tips and tricks on how to update your codebase to PHP 7.4. It combines materials from the PhpStorm 2019.2 and PhpStorm 2019.3 releases, with a few cool bonuses to boot. Nikita drew our attention to the fact that setters are slower than assigning prop…