Download pdf - Xdebug

Transcript
Page 1: Xdebug

XdebugAlltagshilfe und professioneller Debugger

TobiasSchlitt <[email protected]>

PHP World Kongress 2009

2009-11-24

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 1 / 41

Page 2: Xdebug

About me

Tobias Schlitt <[email protected]>

PHP since 2001

Freelancing consultant

Qualified IT Specialist

Studying CS at TU Dortmund(finishing mid 2010)

OSS addicted

eZ ComponentsPHPUnitVarious other projects . . .

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 2 / 41

Page 3: Xdebug

Overview

1 About Debugging

2 Installation

3 Development goodies

4 Tracing

5 More features...

6 The end

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 3 / 41

Page 4: Xdebug

Outline

1 About Debugging

2 Installation

3 Development goodies

4 Tracing

5 More features...

6 The end

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 4 / 41

Page 5: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 6: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 7: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 8: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 9: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 10: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 11: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 12: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 13: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 14: Xdebug

Why a debugger?

I don’t need a debugger

No bug free code

Living life the easy way . . .

var dump(), print r() and echo

Sometimes, yes

Slow and a lot of work

Using a debugger = using an IDE

No, but you can

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 5 / 41

Page 15: Xdebug

What is Xdebug?

Open Source debugger for PHP

PHP (Zend Engine) extension

Works (at least) on Linux, Mac and Windows

About 5 years old

Current stable version: 2.0.5

Created and maintained by Derick Rethans

http://xdebug.org

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 6 / 41

Page 16: Xdebug

Xdebug features

Enhance daily work with PHP

Trace PHP program runs

Profile PHP applications

Analyze code coverage (e.g. PHPUnit)

Remote-debugging with an external clientIncluding break-points, stepping, . . .

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 7 / 41

Page 17: Xdebug

Outline

1 About Debugging

2 InstallationInstallationConfiguration

3 Development goodies

4 Tracing

5 More features...

6 The end

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 8 / 41

Page 18: Xdebug

Via PEAR

Easiest way to install

Working PEAR assumed:$ pear install pecl/xdebug

PEAR Installer performs necessary steps:

1 Download source2 Compiling module3 Copying module to destination

After that: Add module to php.ini

Only on *nix systems

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 9 / 41

Page 19: Xdebug

Manual

Compile by hand

Typical PHP module compile process

wget h t t p : / / xdebug . org / l i n k . php ? u r l=xdebug205t a r −x z f xdebug −2 . 0 . 5 . t g zcd xdebug −2.0 .5p h p i z e. / c o n f i g u r e −−enable−xdebug

−−with−php−c o n f i g =/u s r / b i n /php−c o n f i gmakecp modules / xdebug . so /<module path >/xdebug . so

After that: Add module to php.ini

Only on *nix systems

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 10 / 41

Page 20: Xdebug

Windows

Download binary module from http://xdebug.org

Copy to modules directory

After that: Add module to php.ini

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 11 / 41

Page 21: Xdebug

Add module to php.ini

Not a “normal” extension

Add to php.ini:zend extension = ”/path/to/xdebug.so”

For threadsafe builds: zend extension ts

For debug builds zend extension debug

Check $ php −v or phpinfo() ;

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 12 / 41

Page 22: Xdebug

Common pitfalls

extension dir directive does not take effect!

Binary modules (Windows) do not work with PHP debug builds

−−enable−versioning prevents loading

Other zend-extensions interfere with Xdebug

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 13 / 41

Page 23: Xdebug

Configuration

Huge variety of options

Many shown later

Overview: http://xdebug.org/docs/all_settings

Most options adjustably with ini set () at runtime, except for

xdebug.extended info

xdebug. profiler ∗

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 14 / 41

Page 24: Xdebug

Outline

1 About Debugging

2 Installation

3 Development goodiesError messagesDumping variablesInfinityUseful functions

4 Tracing

5 More features...

6 The end

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 15 / 41

Page 25: Xdebug

Getting started

Install module

Switch it on

Tweak some settings

Work as usual

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 16 / 41

Page 26: Xdebug

Error messages

PHP error messages not very useful

Sometimes poor error locationAlmost no info on affected dataNo info on code context

Xdebug enhances that for you!

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 17 / 41

Page 27: Xdebug

Let’s dig into some code

Let’s dig into some code

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 18 / 41

Page 28: Xdebug

var dump()

Using var dump()

Everyone does itThere is nothing bad about it

var dump() in the browser sucks

Xdebug enhances var dump()

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 19 / 41

Page 29: Xdebug

Infinite loops

Endless recursions

Extremely hard to find

Script just times out . . . or worse!

Xdebug can protect you

Note: Not from endless runs of loops!

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 20 / 41

Page 30: Xdebug

Let’s dig into some code

Let’s dig into some code

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 21 / 41

Page 31: Xdebug

Useful functions

xdebug [en/dis ] able ()

Manually switch stack traces on or off

xdebug call [ class /function/ file / line ]()

Get the call point of the currently running function

xdebug dump superglobals()

Dump super-globals as specified by INI setting

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 22 / 41

Page 32: Xdebug

Useful functions

xdebug get declared vars ()

Returns an array containing the names of all variables in the currentscope

xdebug get function stack ()

Returns the function stack trace as an array

xdebug get stack depth()

Get the current depth in the function stackNote: Includes also create a level!

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 22 / 41

Page 33: Xdebug

Let’s dig into some code

Let’s dig into some code

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 23 / 41

Page 34: Xdebug

Declared vars

Getting declared variables

f u n c t i o n f o o ( $a , $b ){

g l o b a l $c ;$d = ’ I am l o c a l ’ ;var dump ( x d e b u g g e t d e c l a r e d v a r s ( ) ) ;

}

$c = ’ I am g l o b a l ’ ;

f o o ( 23 , 42 ) ;

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 24 / 41

Page 35: Xdebug

Let’s dig into some code

Let’s dig into some code

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 25 / 41

Page 36: Xdebug

Stack depth

Retrieving stack level

echo ”<pre>” ;

echo ” Stack depth i n ” . F I L E . ” : ” .x d e b u g g e t s t a c k d e p t h ( ) . ”\n\n” ;

f u n c t i o n f o o ( ){

echo ” Stack depth i n ” . FUNCTION . ” : ” .x d e b u g g e t s t a c k d e p t h ( ) . ”\n\n” ;

bar ( ) ;}

i n c l ude ’ s t a c k d e p t h / d e e p e r s t a c k . php ’ ;

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 26 / 41

Page 37: Xdebug

Let’s dig into some code

Let’s dig into some code

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 27 / 41

Page 38: Xdebug

Outline

1 About Debugging

2 Installation

3 Development goodies

4 Tracing

5 More features...

6 The end

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 28 / 41

Page 39: Xdebug

What is tracing?

Check control flow of application

Correct function callsCorrect data

Does input data produce correct workflow?

Traces always depend on input dataAlmost impossible to create traces for every possible control flow

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 29 / 41

Page 40: Xdebug

Tracing

Some developers trace like this:

echo ” Here I am ! ! ! ” ;// . . .echo ”Now I ’m h e r e ! ! ! Something i s : ” ;var dump ( $someth ing ) ;

Works well in small scripts

Horror in larger apps / libraries

Debugging like this is a sh**load of work

Almost impossible with 3rd party code

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 30 / 41

Page 41: Xdebug

Tracing with Xdebug

By functions:

xdebug start trace ()

xdebug stop trace ()

Automatically (php.ini)

xdebug.auto trace = ”1”

Cannot be set via ini set ()!

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 31 / 41

Page 42: Xdebug

Tracing example

A simple trace

f u n c t i o n d o u b l e F a c u l t y ( $num ){

i f ( $num < 3 ){

r e t u r n $num ;}r e t u r n $num ∗ d o u b l e F a c u l t y ( $num − 2 ) ;

}

x d e b u g s t a r t t r a c e ( ’ t r a c e s /60 s i m p l e t r a c e . x t ’ ) ;

echo d o u b l e F a c u l t y ( 10 ) ;

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 32 / 41

Page 43: Xdebug

Let’s dig into some code

Let’s dig into some code

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 33 / 41

Page 44: Xdebug

Notes on tracing

Never trace in production!Tracing is extremely slow!

grep & friends help a lot

Tuning configuration makes traces more usable

More information through Xdebug mean

Slower performanceHigher memory consumption

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 34 / 41

Page 45: Xdebug

Outline

1 About Debugging

2 Installation

3 Development goodies

4 Tracing

5 More features...

6 The end

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 35 / 41

Page 46: Xdebug

Profiling

Which code takes which time?

Find bottle necks in code

Check your database queries and setup first!

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 36 / 41

Page 47: Xdebug

Code coverage

Mostly used in testing

Which code is not covered by a test?

PHPUnit can utilize thishttp://www.phpunit.de

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 37 / 41

Page 48: Xdebug

Remote debugging

Integrate Xdebug into your IDE

Komodo IDEEclipse

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 38 / 41

Page 49: Xdebug

Outline

1 About Debugging

2 Installation

3 Development goodies

4 Tracing

5 More features...

6 The end

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 39 / 41

Page 50: Xdebug

Questions / Answers

Questions? Feedback? Critics?

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 40 / 41

Page 51: Xdebug

The end

I hope you enjoyed the session

Slides and material

http://schlitt.info/opensourcehttp://www.slideshare.net/tobyS

Contact: Tobias Schlitt <[email protected]>

Tobias Schlitt (PHP World Kongress 2009) Xdebug 2009-11-24 41 / 41