21
Debugging in PHP PHPCAMP 2008, Pune 20 th Sept. Jignesh Thummar [email protected]

debugging in PHP

Embed Size (px)

Citation preview

Page 1: debugging in PHP

Debugging in PHP

PHPCAMP 2008, Pune20th Sept.

Jignesh [email protected]

Page 2: debugging in PHP

Debugging

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”

- Brian W. Kernighan, father of UNIX.

Page 3: debugging in PHP

Why do I need debugger?

● Good programmer always use debugger● Are echo() and var_dump() enough capable? ● What about some complex program?

Page 4: debugging in PHP

Available debuggers for PHPAPD (Advanced PHP Debugger) - http://pecl.php.net/package/apd - open source, but unmaintained

Zend Debugger - http://www.zend.com/en/products/studio/ - commercial

DBG - http://dd.cron.ru/dbg/ - half commercial

Xdebug

Page 5: debugging in PHP

Introducing Xdebug

● Xdebug – An open source debugging tool● Developed by Derick Rethans – core PHP developer● About 4 years old● Current version 2.0.3● Works on Linux (at least), Windows and Mac

Page 6: debugging in PHP

Installation

with PECL #pecl install xdebug

without PECL #wget http://xdebug.org/files/xdebug-2.0.3.tgz #tar -zxvf xdebug-2.0.3 #cd xdebug-2.0.3 #phpize #./configure #make #make install #cp modules/xdebug.so /path/to/your/ext/directory

Debian users: # sudo apt-get install php5-xdebug

Page 7: debugging in PHP

Enabling Xdebug

in php.ini add: zend_extension=xdebug.so

for Windows users● download .dll from http://pecl4win.php.net ● in php.ini add: zend_extension_ts=C:\php\ext\xdebug.dll

restart apache

Note: Zend's extensions (optimizer, debugger, cache) prohibit Xdebug (and other non-Zend zend-extensions) from loading

Page 8: debugging in PHP

What is there in xdebug?

xdebug provides● stack traces and call traces● profiling information of the script● code coverage analysis● remote debugging● memory allocation, time tracking for function calls● error/warning/notice messages more explainable

Page 9: debugging in PHP

PHP/Webserver is crashing?

● Really?● When? stack overflow infinite recursion● Need to protect it?

Page 10: debugging in PHP

Protection

<?php function myfunction() { myfunction(); } myfunction();

?> xdebug.max_nesting_level = 4

Page 11: debugging in PHP

debug the variables

● xdebug.show_local_vars=on ● Error display

for superglobals ● xdebug.dump.COOKIE=*● xdebug.dump.POST=login,useremail● xdebug.dump.SESSION=sess_id,hash● same for GET, FILES, SERVER, ENV, REQUEST

demo

Page 12: debugging in PHP

color coded variables

override var_dump() by xdebug_var_dump()

xdebug.var_display_max_children xdebug.var_display_max_data xdebug.var_display_max_depth

demo

Page 13: debugging in PHP

few important functions

xdebug_memory_usage( )xdebug_peak_memory_usage( )xdebug_time_index( ) in seconds

xdebug_enable()xdebug_disable()

xdebug_call_class()xdebug_call_function()xdebug_call_file()xdebug_call_line()

Page 14: debugging in PHP

Execution Trace● new to the team?● team member left the project and you don't know what is happening in the script?● want to know the flow of the script● or want to do function traces● show the value passed to functions/methods and also return values

* ini optionsxdebug.auto_trace = 0xdebug.trace_format =0xdebug.trace_output_dir = /tmp/xdebug-tracesxdebug.trace_options = 0xdebug.trace_output_name = trace.%c // crc32, timestamp, pid, %r, scriptname etc.

Page 15: debugging in PHP

more options for trace ● xdebug.collect_params=0 // can make it 1,2,3,4● xdebug.collect_return=1● xdebug.collect_includes=on● xdebug.show_mem_delta=1

functions for trace

xdebug_start_trace(string trace_file [,integer options])xdebug_stop_trace()xdebug_get_tracefile_name( )

demo

Page 16: debugging in PHP

Code Coverage

Which code snippet do I use in my script?

add in php.ini - xdebug.extended_info = 1

xdebug_start_code_coverage([int option]) XDEBUG_CC_UNUSED -

XDEBUG_CC_DEAD_CODE - var_dump(xdebug_start_code_coverage())xdebug_stop_code_coverage()

demo

Page 17: debugging in PHP

Profiler

● functionality to analyze the performance of the script● find out bottleneck in the script● will give output in cachegrind compatible file viewable in KcacheGrind - KDE, Linux webgrind - http://code.google.com/p/webgrind WinCacheGrind – Windows users

demo

Page 18: debugging in PHP

analyze running script

DBGp – debugger protocol

xdebug.remote_enable=1xdebug.remote_handler=dbgpxdebug.remote_mode=reqxdebug.remote_host=127.0.0.1xdebug.remote_port=9000xdebug.idekey=Jigneshxdebug.remote_log = /tmp/xdebug_remote_log

Page 19: debugging in PHP

need to install debugclient

go to xdebug source directory # cd debugclient # ./buildconfig # ./configure --with=libedit # make # make install

Editor supports : PDT(PHP Eclipse), VIM, Komodo, netbeans and many more

demo it

Page 20: debugging in PHP

Resources

www.xdebug.org www.derickrethans.nl

Page 21: debugging in PHP

Thanks

Happy debugging :)