remote debugging with xdebug and eclispe

28
1 di 28 Cascina Roccafranca – Torino – 16 giugno 2012 Walter Dal Mut – walterdalmut.com [email protected] - @walterdalmut Debugging WP plugins with Xdebug and Eclipse.

Upload: corley-srl

Post on 18-May-2015

3.849 views

Category:

Technology


3 download

DESCRIPTION

Debugging di plugin WordPress utilizzando XDebug ed Eclipse. Slide del talk presentato al Wordpress Conference Italia del 16 Giugno 2012 a Torino.

TRANSCRIPT

Page 1: Remote debugging with Xdebug and Eclispe

1 di 28

Cascina Roccafranca – Torino – 16 giugno 2012Walter Dal Mut – walterdalmut.com – [email protected] - @walterdalmut

Debugging WP plugins with Xdebug

and Eclipse.

Page 2: Remote debugging with Xdebug and Eclispe

2 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 3: Remote debugging with Xdebug and Eclispe

3 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 4: Remote debugging with Xdebug and Eclispe

4 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 5: Remote debugging with Xdebug and Eclispe

5 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 6: Remote debugging with Xdebug and Eclispe

6 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 7: Remote debugging with Xdebug and Eclispe

7 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 8: Remote debugging with Xdebug and Eclispe

8 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 9: Remote debugging with Xdebug and Eclispe

9 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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]

Page 10: Remote debugging with Xdebug and Eclispe

10 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

Xdebug and other extensionsXdebug 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.

Page 11: Remote debugging with Xdebug and Eclispe

11 di [email protected]. (+39) 011 4365278

Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015

Strategies of remote debugging

Page 12: Remote debugging with Xdebug and Eclispe

12 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

Start with WP project• Download WP

– http://wordpress.org/download/• Unzip it into an external eclipse workspace dir

– /home/walter/mywp– C:\Documents\walter\slides\mywp

• Install WP normally using web pages

Page 13: Remote debugging with Xdebug and Eclispe

13 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

Plugin source code

•A simple plugin that append twitter news of wpcon.it•Fork it on github– https://github.com/wdalmut/wpcon-plugin

Page 14: Remote debugging with Xdebug and Eclispe

14 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

Plugin Output

Page 15: Remote debugging with Xdebug and Eclispe

15 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

Create PHP project on eclipse

Page 16: Remote debugging with Xdebug and Eclispe

16 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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:\Users\Walter\slides\wordpress (Path of wp)

– Window → Preferences → PHP → Debug• Check that xdebug is installed and configured

Page 17: Remote debugging with Xdebug and Eclispe

17 di 28

Page 18: Remote debugging with Xdebug and Eclispe

18 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 19: Remote debugging with Xdebug and Eclispe

19 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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.

Page 20: Remote debugging with Xdebug and Eclispe
Page 21: Remote debugging with Xdebug and Eclispe
Page 22: Remote debugging with Xdebug and Eclispe
Page 23: Remote debugging with Xdebug and Eclispe

23 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 24: Remote debugging with Xdebug and Eclispe

24 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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

Page 25: Remote debugging with Xdebug and Eclispe

25 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

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);

Page 26: Remote debugging with Xdebug and Eclispe

26 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

Online updates

Page 27: Remote debugging with Xdebug and Eclispe

27 di 28Corley S.r.l.Piazza Statuto 10, Torino (10122)P.Iva 10669790015www.corley.it

Online updates (2)

Page 28: Remote debugging with Xdebug and Eclispe

28 di 28

Cascina Roccafranca – Torino – 16 giugno 2012

THANK YOU FOR LISTENINGWalter Dal Mut

walterdalmut.com - [email protected] -@walterdalmut