Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Remote debugging with Xdebug and Eclispe
1. Debugging WP plugins with
Xdebug
and Eclipse.
Cascina Roccafranca – Torino – 16 giugno 2012
Walter Dal Mut – walterdalmut.com – walter.dalmut@corley.it - @walterdalmut
1 di 28
2. Debugging PHP App/Scripts
• Using var_dump or print_r typically in conjunction with die
function.
• Generate tons of logs
• File
• Database
• Etc.
• Using debug support
• Xdebug
• Zend Debugger
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 2 di 28
P.Iva 10669790015
www.corley.it
3. Introduction to xdebug
• What is it?
– A PHP Extension designed by Derick Rethans
• Features
– Stack Traces
– Execution Traces
– Memory Allocation
– Code Profiling
– Code Coverage Analysis
– Interactive Debugging
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 3 di 28
P.Iva 10669790015
www.corley.it
4. Useful information about xdebug
• Main website
– www.xdebug.org
• Source code
– https://github.com/derickr/xdebug
• Bug Tracking
– http://bugs.xdebug.org/my_view_page.php
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 4 di 28
P.Iva 10669790015
www.corley.it
5. Why debugging applications with xdebug
• Works var_dump +/- die well?
– Always debatable, not always true
– Conditional debugging may be complex
– Big scopes could be hard to read and understand
• Xdebug improve var_dump and add useful inspection functions
• xdebug_* functions
• Xdebug
– Step by step interaction
– Inline variable modifications
– Remote debugging
– Conditional debugging
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 5 di 28
P.Iva 10669790015
www.corley.it
6. Obtaining xdebug - Linux
• Use distribution package installer
– Example on Debian - Ubuntu
• Install
– apt-get install php5-xdebug
• Dedicated configuration file
– /etc/php5/conf.d/xdebug.ini
• Install using PECL package manager
– Install
• sudo pecl install xdebug
– Add into your php.ini
• zend_extension=”/path/to/xdebug.so” → complete path
• Compile by your-self
• phpize configure make make install
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 6 di 28
P.Iva 10669790015
www.corley.it
7. Obtaining xdebug - Windows
• Use a self-install package that includes xdebug
– Xampp (Activate it into your php.ini)
– Wamp (Activate it through dedicated menus)
• Download the xdebug library (DLL) and copy/paste into your
PHP extension folder
– Download the library from
• http://www.xdebug.org
• http://pecl4win.php.net
• http://windows.php.net
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 7 di 28
P.Iva 10669790015
www.corley.it
8. Obtaining xdebug – MAC OS
• Use a self-install package that includes xdebug
– MAMP (Activate it into your php.ini)
• Download the xdebug library (.so) and copy/paste into your PHP
extension folder
• Compile xdebug by your-self (phpize)
• Use BREW package manager
• brew install xdebug
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 8 di 28
P.Iva 10669790015
www.corley.it
9. Interactive Debugging
• Enable in php.ini
– zend_extension=/path/to/xdebug.so
– Pay attention on
• Use complete path
• Ignore extension=xdebug.so because it causes problems
– Typical you have to setup only four directives
• xdebug.remote_enable (On) [default as Off]
• xdebug.remote_host (localhost) [default]
• xdebug.remote_port (9000) [default]
• xdebug.remote_handler (dbgp) [default]
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 9 di 28
P.Iva 10669790015
www.corley.it
10. Xdebug and other extensions
Xdebug does not work together with the Zend Optimizer or any other
extension that deals with PHP's internals (DBG, APD, ioncube etc). This
is due to compatibility problems with those modules.
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 10 di 28
P.Iva 10669790015
www.corley.it
11. Strategies of remote debugging
Corley S.r.l. www.corley.it
Piazza Statuto 10, Torino (10122) info@corley.it 11 di 28
P.Iva 10669790015 Tel. (+39) 011 4365278
12. Start with WP project
• Download WP
– http://wordpress.org/download/
• Unzip it into an external eclipse workspace dir
– /home/walter/mywp
– C:Documentswalterslidesmywp
• Install WP normally using web pages
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 12 di 28
P.Iva 10669790015
www.corley.it
13. Plugin source code
• A simple plugin that append twitter news of
wpcon.it
• Fork it on github
– https://github.com/wdalmut/wpcon-plugin
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 13 di 28
P.Iva 10669790015
www.corley.it
14. Plugin Output
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 14 di 28
P.Iva 10669790015
www.corley.it
15. Create PHP project on eclipse
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 15 di 28
P.Iva 10669790015
www.corley.it
16. Setup WPCON dev server
• Configure your local Apache2
– Create vhost for wpcon.local
• Open Eclipse IDE
– Window → Preferences → PHP → PHP Servers
• Add a new server for the wpcon.local configuration
– Name: WPCON (whatever you want, it's just a label)
– Base URL: http://wpcon.local (the base url of local server)
– Local Web Root: C:UsersWalterslideswordpress (Path of wp)
– Window → Preferences → PHP → Debug
• Check that xdebug is installed and configured
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 16 di 28
P.Iva 10669790015
www.corley.it
18. Now we are ready to debug application
• Creates or copy files for this plugin into your project
• If you prefer use git clone
• Activate this plugin using the WP administrator center
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 18 di 28
P.Iva 10669790015
www.corley.it
19. Debugging in Eclipse IDE
• A debug session
– Starts with command “Debug as”
• Debug as → PHP Web Application
– ALT + SHIFT + D, W
– It terminates with the explicit stop command
• Breakpoint
– It is an explicit marker that stop the code execution at a particular
point of your software.
• Inspection windows
– When the debugger waits for a command you can see and
modify variables in the scope.
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 19 di 28
P.Iva 10669790015
www.corley.it
20.
21.
22.
23. Debugging flow control
• Resume → Wake up and run until a debug target
• Pause → Sleep this session
• Stop → Close this debug session
• Disconnect → Close debug but go ahead
• StepIn → Execute a step inside a method/function
• StepOver → Execute a step but don't go into.
• StepReturn → Return from a method which has been stepped into
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 23 di 28
P.Iva 10669790015
www.corley.it
24. Breakpoints and conditions
• You can add breakpoints
– Double-click on left part of eclipse code view
• You can add conditional breakpoints
– Add a normal breakpoint, right-click on this one and select
that you need a condition
– Add a normal PHP condition
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 24 di 28
P.Iva 10669790015
www.corley.it
25. Conditional breakpoints (2)
Conditional breakpoints simplify your life in debugging parts of
code with conditions. A simple recursive example:
function factorial($n) {
if ($n === 0) {
return 1;
} else {
return $n * factorial($n-1);
}
}
echo factorial(12);
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 25 di 28
P.Iva 10669790015
www.corley.it
26. Online updates
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 26 di 28
P.Iva 10669790015
www.corley.it
27. Online updates (2)
Corley S.r.l.
Piazza Statuto 10, Torino (10122) 27 di 28
P.Iva 10669790015
www.corley.it
28. THANK YOU FOR LISTENING
Walter Dal Mut
walterdalmut.com - walter.dalmut@corley.it -@walterdalmut
Cascina Roccafranca – Torino – 16 giugno 2012
28 di 28