debugging in PHP

5,422 views
5,216 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,422
On SlideShare
0
From Embeds
0
Number of Embeds
44
Actions
Shares
0
Downloads
128
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

debugging in PHP

  1. 1. Debugging in PHP PHPCAMP 2008, Pune 20th Sept. Jignesh Thummar jignesh.thummar@gmail.com
  2. 2. 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.
  3. 3. Why do I need debugger? ● Good programmer always use debugger ● Are echo() and var_dump() enough capable? ● What about some complex program?
  4. 4. Available debuggers for PHP APD (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
  5. 5. 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
  6. 6. 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
  7. 7. 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:phpextxdebug.dll restart apache Note: Zend's extensions (optimizer, debugger, cache) prohibit Xdebug (and other non-Zend zend- extensions) from loading
  8. 8. 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
  9. 9. PHP/Webserver is crashing? ● Really? ● When? stack overflow infinite recursion ● Need to protect it?
  10. 10. Protection <?php function myfunction() { myfunction(); } myfunction(); ?> xdebug.max_nesting_level = 4
  11. 11. 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
  12. 12. 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
  13. 13. 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()
  14. 14. 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 options xdebug.auto_trace = 0 xdebug.trace_format =0 xdebug.trace_output_dir = /tmp/xdebug-traces xdebug.trace_options = 0 xdebug.trace_output_name = trace.%c // crc32, timestamp, pid, %r, scriptname etc.
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. analyze running script DBGp – debugger protocol xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_host=127.0.0.1 xdebug.remote_port=9000 xdebug.idekey=Jignesh xdebug.remote_log = /tmp/xdebug_remote_log
  19. 19. 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
  20. 20. Resources www.xdebug.org www.derickrethans.nl
  21. 21. Thanks Happy debugging :)

×