Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Xdebug - Jump Start

Xdebug - Jump Start

Xdebug is an extension that helps you debugging your PHP scripts by providing a lot of valuable debug information.

This talk will be an overview on how to configure and debug your scripts interactively with Xdebug.

This talk was presented @ PHPLX (December 2013)

Jorge Sá Pereira

December 07, 2013
Tweet

Other Decks in Programming

Transcript

  1. What is Xdebug? Xdebug is a PHP extension which provides

    debugging and profiling capabilities.
  2. Install process $sudo pecl install xdebug ! Note: You should

    ignore any prompts to add "extension=xdebug.so" to php.ini — this will cause problems. ! ! ! ! After the installation process you need to add the following line to your php.ini. ! [xdebug] zend_extension="/usr/lib64/php/modules/xdebug.so" ! Note: Don't forget to change the path and filename to the correct one — but make sure you use the full path.
  3. Xdebug: Stack Tracing When Xdebug is activated it will show

    a stack trace whenever PHP decides to show a notice, warning, error etc. ! The information that stack traces display, and the way how they are presented, can be configured to suit your needs.
  4. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages
  5. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP Stack trace:, referer: http://dev1.dri.pt/admin/pages
  6. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP Stack trace:, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ admin/index.php:0, referer: http://dev1.dri.pt/admin/pages
  7. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP Stack trace:, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ admin/index.php:0, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 2. include() /vagrant/ admin/index.php:78, referer: http://dev1.dri.pt/admin/pages
  8. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP Stack trace:, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ admin/index.php:0, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 2. include() /vagrant/ admin/index.php:78, referer: http://dev1.dri.pt/admin/pages
  9. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP Stack trace:, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ admin/index.php:0, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 2. include() /vagrant/ admin/index.php:78, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP Fatal error: Cannot redeclare class Page in /vagrant/system/Page.php on line 18
  10. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP Stack trace:, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ admin/index.php:0, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 2. include() /vagrant/ admin/index.php:78, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP Fatal error: Cannot redeclare class Page in /vagrant/system/Page.php on line 18 [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP Stack trace:
  11. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP Stack trace:, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ admin/index.php:0, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 2. include() /vagrant/ admin/index.php:78, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP Fatal error: Cannot redeclare class Page in /vagrant/system/Page.php on line 18 [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP Stack trace: [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ index.php:0
  12. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP Stack trace:, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ admin/index.php:0, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 2. include() /vagrant/ admin/index.php:78, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP Fatal error: Cannot redeclare class Page in /vagrant/system/Page.php on line 18 [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP Stack trace: [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ index.php:0 [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP 2. require_once() / vagrant/index.php:29
  13. Xdebug: Stack Tracing [Sat Dec 07 00:01:32 2013] [error] [client

    10.0.50.1] PHP Notice: Undefined offset: 100 in /vagrant/admin/create_page.php on line 69, referer: http:// dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP Stack trace:, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ admin/index.php:0, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:01:32 2013] [error] [client 10.0.50.1] PHP 2. include() /vagrant/ admin/index.php:78, referer: http://dev1.dri.pt/admin/pages [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP Fatal error: Cannot redeclare class Page in /vagrant/system/Page.php on line 18 [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP Stack trace: [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP 1. {main}() /vagrant/ index.php:0 [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP 2. require_once() / vagrant/index.php:29 [Sat Dec 07 00:06:44 2013] [error] [client 10.0.50.1] PHP 3. require_once() / vagrant/system/bootstrap.php:44
  14. Xdebug: Stack Tracing Note: display_errors have to be turned On

    in php.ini to display this type of stack traces. In the browser window:
  15. Xdebug: Profiling Xdebug's Profiler is a powerful tool that gives

    you the ability to analyze your PHP code and determine bottlenecks or generally see which parts of your code are slow and could use a speed boost.
  16. Configuration [xdebug] zend_extension = /usr/lib64/php/modules/xdebug.so xdebug.profiler_enable = 1 xdebug.profiler_enable_trigger =

    1 xdebug.profiler_output_dir = /tmp/xdebug xdebug.xdebug.trace_output_name = cachegrind.out.%p
  17. Xdebug: Code coverage stats Code coverage tells you which lines

    of script (or set of scripts) have been executed during a request. ! With this information you can for example find out how good your unit tests are.
  18. Xdebug + PHPUnit - Code coverage These reports can be

    created running: ! $phpunit --coverage-html [reports_output] [tests_to_run]
  19. Xdebug + PHPUnit - Code coverage The reports show the

    lines of code that were executed while running the tests are highlighted green and lines of code that are executable but were not executed are highlighted red.
  20. Xdebug: Debugging Xdebug's (remote) debugger allows you to examine data

    structure, interactively walk through your and debug your code.
  21. Debugging features Xdebug controls the program execution in PHP allowing

    it to pause and resume program execution at any time. ! When paused, xdebug can retrieve information about the current program state, like reading variable values. ! It is also possible for xdebug to change the value of a variable, then continue the script execution with a modified value.
  22. Configuration ! #For Static IP / Single developer ! #

    file: php.ini [xdebug] zend_extension = /usr/lib64/php/modules/xdebug.so xdebug.remote_enable = 1 xdebug.remote_handler = dbgp xdebug.remote_mode = req xdebug.remote_host = 10.0.50.1 xdebug.remote_port = 9000
  23. Configuration #For Unknown IP / Multiple developers ! ! #

    file: php.ini [xdebug] zend_extension = /usr/lib64/php/modules/xdebug.so xdebug.remote_enable = 1 xdebug.remote_handler = dbgp xdebug.remote_mode = req xdebug.remote_connect_back = 1 xdebug.remote_port = 9000
  24. Configuration #For Unknown IP / Multiple developers ! ! #

    file: php.ini [xdebug] zend_extension = /usr/lib64/php/modules/xdebug.so xdebug.remote_enable = 1 xdebug.remote_handler = dbgp xdebug.remote_mode = req xdebug.remote_connect_back = 1 xdebug.remote_port = 9000 More info: http://xdebug.org/docs/remote#communication
  25. Xdebug browser plugins Xdebug Helper https://chrome.google.com/extensions/detail/eadndfjplgieldjbigjakmdgkmoaaaoc easy Xdebug https://addons.mozilla.org/en-US/firefox/addon/58688/ Xdebug

    Toggler https://github.com/benmatselby/xdebug-toggler Xdebug launcher https://addons.opera.com/en/extensions/details/xdebug-launcher/?display=en
  26. Supported IDEs NetBeans Eclipse + PDT PHPStorm Emacs More info:

    http://xdebug.org/docs/remote#clients And many more…
  27. PHPStorm + Xdebug + Vagrant configuration Add your server details

    and map the project directory to the absolute path on the server, in this case was /vagrant
  28. PHPStorm Debug toolbar Resume Resume debugging session Finish Session Finish

    debugging session Pause Suspend debugging session View Breakpoints Opens the breakpoints dialog box Mute Breakpoints Toggle the status of the breakpoints (enabled/disabled)
  29. PHPStorm Stepping toolbar Show execution point Highlights the current execution

    point Step into Step into a function call Step over Step over an execution statement Step out Step out of the current function call Run to cursor Run the execution to the position of the cursor
  30. PHPStorm Debug Window The Variables pane enables you to examine

    the values stored in the objects of your application.
  31. PHPStorm Debug Window In the Watches pane you can evaluate

    any number of variables or expressions in the context of the current stack frame. The values are updated with each step through the application, and become visible every time the application is suspended.