Download - 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.
Why do I need debugger?
● Good programmer always use debugger● Are echo() and var_dump() enough capable? ● What about some complex program?
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
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
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
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
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
PHP/Webserver is crashing?
● Really?● When? stack overflow infinite recursion● Need to protect it?
Protection
<?php function myfunction() { myfunction(); } myfunction();
?> xdebug.max_nesting_level = 4
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
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
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()
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.
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
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
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
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
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
Thanks
Happy debugging :)