Debugging: Difference between revisions

No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
For now, this subject will focus on PHP.
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].
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].


We did an old deep dive using NetBeans and XDebug [[CiviCRM/debugging|on a complex CiviCRM mailer embedded as a module in Drupal]]
== XDebug ==
XDebug is a tool you can't live without once you've learned how to use it.
 
It offers:
 
*   Step Debugging
*   Improvements to PHP's error reporting
*   Tracing
*   Profiling
*   Code Coverage Analysis
 
XDebug 3.2.0 was [https://xdebug.org/announcements/2022-12-08 released in Dec. 2022] and adds support for '''PHP 8.2''' and drops support for PHP 7.2-7.4
 
More [https://xdebug.org/announcements recent releases of XDebug] are 3.3.2 and all the way up to '''3.4.1''' as of Jan 2025


A more current setup would be [[debugging Semantic MediaWiki in a Docker container using VSCode with PHPDebug]].
=== Installing XDebug ===
Installing XDebug is fairly complicated and only gets more challenging if you are trying to debug sources inside a container or on a remote server. And since the PHP cli is different from the web interpreter, there are differences depending on how you are invoking PHP


== XDebug with VSCode ==
https://xdebug.org/docs/install
Be sure to install the [https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug '''PHP Debug''' extension], and ''read the documentation on that page thoroughly''.


== Install ==
== Install ==
Ensure that XDebug extension is installed for your PHP environment.
Ensure that XDebug PHP extension is installed for your PHP environment. It is typically installed via [[PEAR|PECL]], but can be installed with [[Package management|package managers]].


in the console, you can just type <code>php -v</code> which will tell you not just the version of PHP, but also whether XDebug is enabled. More specifically, you could <code>php -r 'echo phpversion("xdebug");'</code>
in the console, you can just type <code>php -v</code> which will tell you not just the version of PHP, but also whether XDebug is enabled. More specifically, you could <code>php -r 'echo phpversion("xdebug");'</code>
Line 36: Line 51:
echo -e "\n\nxdebug_info();\n" >> $myFile;
echo -e "\n\nxdebug_info();\n" >> $myFile;
</syntaxhighlight>
</syntaxhighlight>
== XDebug with VSCode ==
VSCode only supports debugging JavaScript out of the box (including Node.js and TypeScript). For PHP and other languages from Go to Python, you will need to install a debugger extension in VSCode. XDebug is the debugger for PHP, and you will need the [https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug '''PHP Debug''' extension] to VSCode. ''READ the documentation on that page thoroughly''. The extension integrates your IDE with the PHP environment and the XDebug debugger itself.


== Step Debugging ==
== Step Debugging ==
Covered at https://xdebug.org/docs/step_debug
Covered at https://xdebug.org/docs/step_debug
== Deeper Dive ==
We did an old deep dive using NetBeans and XDebug [[CiviCRM/debugging|on a complex CiviCRM mailer embedded as a module in Drupal]]
A more current setup would be [[Debug Semantic MediaWiki in Docker using VSCode and XDebug]].
[[Category:Debugging]]
[[Category:Development]]