Slide 1

Slide 1 text

Xdebug Jump Start Jorge Sá Pereira December 7, 2013

Slide 2

Slide 2 text

About me Software Engineer @ jorgesapereira @xorti

Slide 3

Slide 3 text

! What is Xdebug?

Slide 4

Slide 4 text

What is Xdebug? Xdebug is a PHP extension which provides debugging and profiling capabilities.

Slide 5

Slide 5 text

! What for?

Slide 6

Slide 6 text

What for?

Slide 7

Slide 7 text

What for? Stack Tracing

Slide 8

Slide 8 text

What for? Stack Tracing Execution Tracing

Slide 9

Slide 9 text

What for? Stack Tracing Execution Tracing Memory Allocation

Slide 10

Slide 10 text

What for? Stack Tracing Execution Tracing Memory Allocation Code Profiling

Slide 11

Slide 11 text

What for? Stack Tracing Execution Tracing Memory Allocation Code Profiling Interactive Debugging

Slide 12

Slide 12 text

What for? Stack Tracing Execution Tracing Memory Allocation Code Profiling Interactive Debugging Code coverage stats

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

Xdebug: Tracing

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

Xdebug: Stack Tracing

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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:

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Xdebug: Stack Tracing In the browser window:

Slide 28

Slide 28 text

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:

Slide 29

Slide 29 text

Xdebug: Profiling

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Xdebug: Profiling

Slide 33

Slide 33 text

Xdebug: Profiling

Slide 34

Slide 34 text

Xdebug Code coverage stats

Slide 35

Slide 35 text

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.

Slide 36

Slide 36 text

Xdebug + PHPUnit - Code coverage

Slide 37

Slide 37 text

Xdebug + PHPUnit - Code coverage These reports can be created running: ! $phpunit --coverage-html [reports_output] [tests_to_run]

Slide 38

Slide 38 text

Xdebug + PHPUnit - Code coverage

Slide 39

Slide 39 text

Xdebug + PHPUnit - Code coverage

Slide 40

Slide 40 text

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.

Slide 41

Slide 41 text

Xdebug: Debugging

Slide 42

Slide 42 text

Xdebug: Debugging Xdebug's (remote) debugger allows you to examine data structure, interactively walk through your and debug your code.

Slide 43

Slide 43 text

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.

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

Supported IDEs NetBeans Eclipse + PDT PHPStorm Emacs And many more…

Slide 49

Slide 49 text

Supported IDEs NetBeans Eclipse + PDT PHPStorm Emacs More info: http://xdebug.org/docs/remote#clients And many more…

Slide 50

Slide 50 text

PHPStorm + Xdebug + Vagrant configuration

Slide 51

Slide 51 text

PHPStorm + Xdebug + Vagrant configuration

Slide 52

Slide 52 text

PHPStorm + Xdebug + Vagrant configuration Or you can select the button in the toolbar

Slide 53

Slide 53 text

PHPStorm + Xdebug + Vagrant configuration Or you can select the button in the toolbar

Slide 54

Slide 54 text

PHPStorm + Xdebug + Vagrant configuration

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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)

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

PHPStorm Debug Window

Slide 59

Slide 59 text

PHPStorm Debug Window

Slide 60

Slide 60 text

PHPStorm Debug Window The Variables pane enables you to examine the values stored in the objects of your application.

Slide 61

Slide 61 text

PHPStorm Debug Window

Slide 62

Slide 62 text

PHPStorm Debug Window

Slide 63

Slide 63 text

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.

Slide 64

Slide 64 text

PHPStorm Debug Window

Slide 65

Slide 65 text

PHPStorm Debug Window

Slide 66

Slide 66 text

PHPStorm Debug Window More info: https://www.jetbrains.com/phpstorm/webhelp/debug-tool-window.html

Slide 67

Slide 67 text

Live Demo

Slide 68

Slide 68 text

Q & A

Slide 69

Slide 69 text

Thank you! Please leave feedback https://joind.in/10301 Photo by Rafael Dohms © flickr.com/photos/rdohms/4102269071