Remote debugging with Xdebug and Eclispe

Post on 18-May-2015

3.850 views 3 download

Tags:

description

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

Transcript of Remote debugging with Xdebug and Eclispe

1 di 28

Cascina Roccafranca – Torino – 16 giugno 2012Walter Dal Mut – walterdalmut.com – walter.dalmut@corley.it - @walterdalmut

Debugging WP plugins with Xdebug

and Eclipse.

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

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

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

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

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

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

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

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]

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.

11 di 28www.corley.itinfo@corley.itTel. (+39) 011 4365278

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

Strategies of remote debugging

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

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

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

Plugin Output

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

Create PHP project on eclipse

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

17 di 28

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

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.

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

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

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

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

Online updates

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

Online updates (2)

28 di 28

Cascina Roccafranca – Torino – 16 giugno 2012

THANK YOU FOR LISTENINGWalter Dal Mut

walterdalmut.com - walter.dalmut@corley.it -@walterdalmut