Difference between revisions of "Debugging"
(temp save) |
(adds link to example) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | Debugging a PHP application can involve quite a bit of machinery, and effort getting that machinery setup. But it's worth it because what alternative is there? <code>echo</code>? Come on! | + | Debugging a PHP application can involve quite a bit of machinery, and effort getting that machinery setup. But it's worth it because what alternative is there? <code>echo</code>? Come on! |
+ | |||
+ | Thanks to [http://derickrethans.nl/who.html Derick Rethans], xdebug can do a ton of cool things for you. For example, it overloads <code>[https://secure.php.net/var_dump var_dump()]</code> and gives '''you''' [http://www.xdebug.org/docs/display control over how you want deeply nested data structures to be displayed]. | ||
+ | |||
+ | Once you have NetBeans and XDebug operating, you can [[CiviCRM/debugging|debug complex applications like CiviCRM]] | ||
== First get Xdebug setup == | == First get Xdebug setup == | ||
Line 5: | Line 9: | ||
=== Summary === | === Summary === | ||
− | '''Xdebug installed:''' 2. | + | Here is what the wizard reported for me AFTER my recent installation of xdebug |
− | '''Server API:''' Apache 2.0 Handler | + | <blockquote> |
− | '''Windows:''' no | + | '''Xdebug installed:''' 2.3.3 <br /> |
− | '''Zend Server:''' no | + | '''Server API:''' Apache 2.0 Handler <br /> |
− | '''PHP Version:''' 5.5.9-1 | + | '''Windows:''' no <br /> |
− | '''Zend API nr:''' 220121212 | + | '''Zend Server:''' no <br /> |
− | '''PHP API nr:''' 20121212 | + | '''PHP Version:''' 5.5.9-1 <br /> |
− | '''Debug Build:''' no | + | '''Zend API nr:''' 220121212 <br /> |
− | '''Thread Safe Build:''' no | + | '''PHP API nr:''' 20121212 <br /> |
− | '''Configuration File Path:''' /etc/php5/apache2 | + | '''Debug Build:''' no <br /> |
− | '''Configuration File:''' /etc/php5/apache2/php.ini | + | '''Thread Safe Build:''' no <br /> |
− | '''Extensions directory:''' /usr/lib/php5/20121212+lfs | + | '''Configuration File Path:''' /etc/php5/apache2 <br /> |
+ | '''Configuration File:''' /etc/php5/apache2/php.ini <br /> | ||
+ | '''Extensions directory:''' /usr/lib/php5/20121212+lfs | ||
+ | </blockquote> | ||
+ | Caveat: the wizard can't really tell where the xdebug configuration lives (in the case of many subsidiary ini files). In Ubuntu, there might be a file in <code>/etc/php5/conf.d</code> In my case I actually did have <code>/etc/php5/conf.d/xdebug.ini</code>, but that file is superfluous because there are symbolic links for both '''apache2''' and '''cli''' that go to | ||
+ | <code>/etc/php5/mods-available/xdebug.ini</code> (which helps PHP configuration to be the same for both runtimes.) Bottom line: run <code>locate xdebug.ini</code> to find out whether you have a duplicate/conflicting file; merge those files down to one; and make any overrides that you need from the default settings which xdebug will use if left unconfigured. Then <code>sudo service apache2 restart</code> and check the output of <code>phpinfo()</code> for correctness. | ||
+ | |||
+ | The defaults are probably all good, but you may want to specify some of the following: | ||
+ | <source lang="ini"> | ||
+ | ; the old one installed by apt-get | ||
+ | ; zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so | ||
+ | ; as of php 5.5 you no longer need to supply a full path to the zend_extension= token | ||
+ | ; but it doesn't hurt, and may be required if you have multiple .so files lying around | ||
+ | zend_extension=/usr/lib/php5/20121212+lfs/xdebug.so | ||
+ | |||
+ | ; enable debugging | ||
+ | xdebug.remote_enable = On | ||
+ | ; working from local machine | ||
+ | ; xdebug.remote_host = "127.0.0.1" | ||
+ | ; debugging from any machine on the local network, as well as through the router | ||
+ | xdebug.remote_host = "192.168.1.0/24,127.0.0.1" | ||
+ | ; set which debug client protocol we want | ||
+ | xdebug.remote_handler = "dbgp" | ||
+ | |||
+ | ; enable anyone with access to the machine to start a debugging session | ||
+ | ; this setting also helps when you're having difficulty with the connection setup | ||
+ | ; xdebug.remote_connect_back 1 | ||
+ | |||
+ | </source> | ||
− | + | Once you see xdebug in your <code>phpinfo()</code> output, you know it's enabled -- which means that you can already get xdebug functionality out of your PHP scripts (e.g. var_dump() is overridden). But how do you get debugging working so that you can use it with your IDE? That is covered in the '[http://www.xdebug.org/docs/remote remote]' section of the manual. | |
+ | |||
+ | {{Highlight | | ||
+ | text= If you want to use [https://secure.php.net/manual/en/opcache.installation.php OPcache] with Xdebug, you must load OPcache before Xdebug. In general, extensions are loaded in the order found in php.ini. See https://wiki.php.net/internals/extensions and [https://wiki.php.net/_detail/internals/extensions_lifetime.png?id=internals%3Aextensions this graphic] for more info}} | ||
Following the sage advice of the [http://wiki.netbeans.org/HowToConfigureXDebug#General_Information Netbeans wiki], you want to get xdebug's '''debugclient''' working on localhost first, then add Netbeans. | Following the sage advice of the [http://wiki.netbeans.org/HowToConfigureXDebug#General_Information Netbeans wiki], you want to get xdebug's '''debugclient''' working on localhost first, then add Netbeans. | ||
+ | |||
+ | xdebug provides several configuration parameters as well as functions that you can use in your debugging code. One important parameter is the <code>[http://www.xdebug.org/docs/stack_trace xdebug.file_link_format]</code> which determines the format of the links that are shown in stack traces. This allows for integration with your IDE so that, for example, Netbeans will find and open the file in your local sources. | ||
+ | |||
+ | [http://www.xdebug.org/docs/profiler Profiling] your PHP code is another feature enabled by xdebug. | ||
+ | |||
+ | == Resources == | ||
+ | * http://www.devside.net/wamp-server/netbeans-waiting-for-connection-netbeans-xdebug-issue |
Latest revision as of 08:38, 23 October 2015
Debugging a PHP application can involve quite a bit of machinery, and effort getting that machinery setup. But it's worth it because what alternative is there? echo
? Come on!
Thanks to Derick Rethans, xdebug can do a ton of cool things for you. For example, it overloads var_dump()
and gives you control over how you want deeply nested data structures to be displayed.
Once you have NetBeans and XDebug operating, you can debug complex applications like CiviCRM
First get Xdebug setup[edit | edit source]
Xdebug is the project for debugging PHP. The wizard will show you how to upgrade your package version. In my case, the Xdebug packaged for Ubuntu was 2.2.3, but the more recent version is 2.3.3
Summary[edit | edit source]
Here is what the wizard reported for me AFTER my recent installation of xdebug
Xdebug installed: 2.3.3
Server API: Apache 2.0 Handler
Windows: no
Zend Server: no
PHP Version: 5.5.9-1
Zend API nr: 220121212
PHP API nr: 20121212
Debug Build: no
Thread Safe Build: no
Configuration File Path: /etc/php5/apache2
Configuration File: /etc/php5/apache2/php.ini
Extensions directory: /usr/lib/php5/20121212+lfs
Caveat: the wizard can't really tell where the xdebug configuration lives (in the case of many subsidiary ini files). In Ubuntu, there might be a file in /etc/php5/conf.d
In my case I actually did have /etc/php5/conf.d/xdebug.ini
, but that file is superfluous because there are symbolic links for both apache2 and cli that go to
/etc/php5/mods-available/xdebug.ini
(which helps PHP configuration to be the same for both runtimes.) Bottom line: run locate xdebug.ini
to find out whether you have a duplicate/conflicting file; merge those files down to one; and make any overrides that you need from the default settings which xdebug will use if left unconfigured. Then sudo service apache2 restart
and check the output of phpinfo()
for correctness.
The defaults are probably all good, but you may want to specify some of the following:
; the old one installed by apt-get
; zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so
; as of php 5.5 you no longer need to supply a full path to the zend_extension= token
; but it doesn't hurt, and may be required if you have multiple .so files lying around
zend_extension=/usr/lib/php5/20121212+lfs/xdebug.so
; enable debugging
xdebug.remote_enable = On
; working from local machine
; xdebug.remote_host = "127.0.0.1"
; debugging from any machine on the local network, as well as through the router
xdebug.remote_host = "192.168.1.0/24,127.0.0.1"
; set which debug client protocol we want
xdebug.remote_handler = "dbgp"
; enable anyone with access to the machine to start a debugging session
; this setting also helps when you're having difficulty with the connection setup
; xdebug.remote_connect_back 1
Once you see xdebug in your phpinfo()
output, you know it's enabled -- which means that you can already get xdebug functionality out of your PHP scripts (e.g. var_dump() is overridden). But how do you get debugging working so that you can use it with your IDE? That is covered in the 'remote' section of the manual.
Following the sage advice of the Netbeans wiki, you want to get xdebug's debugclient working on localhost first, then add Netbeans.
xdebug provides several configuration parameters as well as functions that you can use in your debugging code. One important parameter is the xdebug.file_link_format
which determines the format of the links that are shown in stack traces. This allows for integration with your IDE so that, for example, Netbeans will find and open the file in your local sources.
Profiling your PHP code is another feature enabled by xdebug.