Top Banner
Xdebug from A to X Xdebug from A to X By By Gennady Feldman Gennady Feldman Aug 25, 2009 Aug 25, 2009
30

Xdebug PHP Performance Profiling Debugging

Oct 15, 2014

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Xdebug PHP Performance Profiling Debugging

Xdebug from A to XXdebug from A to X

ByBy

Gennady FeldmanGennady Feldman

Aug 25, 2009Aug 25, 2009

Page 2: Xdebug PHP Performance Profiling Debugging

AgendaAgenda

► InstallationInstallation►Error HandlingError Handling►DebuggingDebugging►ProfilingProfiling►Function TracesFunction Traces

Page 3: Xdebug PHP Performance Profiling Debugging

Xdebug Features Xdebug Features

►Variable displayVariable display►Stack TracesStack Traces►Profiling PHP ScriptsProfiling PHP Scripts►Remote DebuggingRemote Debugging►Function TracesFunction Traces►Code Coverage Analysis*Code Coverage Analysis*

* Not covered in this presentation.* Not covered in this presentation.

Page 4: Xdebug PHP Performance Profiling Debugging

InstallingInstalling

►Xdebug is a PHP extension. (written in Xdebug is a PHP extension. (written in C)C)

►Xdebug is Open Source so you could Xdebug is Open Source so you could download and build/install it yourself.download and build/install it yourself.

►Almost every Linux distro today Almost every Linux distro today provides an Xdebug binary package.provides an Xdebug binary package.

►Binaries are also available for Windows Binaries are also available for Windows from http://xdebug.org website.from http://xdebug.org website.

Page 5: Xdebug PHP Performance Profiling Debugging

Installing (Part 2)Installing (Part 2)

►On Ubuntu 9.04:On Ubuntu 9.04: apt-get install php5-xdebugapt-get install php5-xdebug

►On Mandriva:On Mandriva: urpmi php5-xdebugurpmi php5-xdebug

►Using PEAR/PECL:Using PEAR/PECL: pecl install xdebugpecl install xdebug pecl upgrade xdebugpecl upgrade xdebug

Page 6: Xdebug PHP Performance Profiling Debugging

Installing (Part 3)Installing (Part 3)

►To enable xdebug add the fullpath to To enable xdebug add the fullpath to xdebug.so:xdebug.so:zend_extension=/usr/lib/php5/20060613+lfs/xdebug.sozend_extension=/usr/lib/php5/20060613+lfs/xdebug.so

To php.ini orTo php.ini or To conf.d/xdebug.ini fileTo conf.d/xdebug.ini file

►You could also setup system wide You could also setup system wide defaults as well through the ini file.defaults as well through the ini file.

Page 7: Xdebug PHP Performance Profiling Debugging

Installing (Part 4)Installing (Part 4)

►Check that Xdebug is installed via:Check that Xdebug is installed via: php –vphp –v php –mphp –m php –ri xdebugphp –ri xdebug

►Check phpinfo() report and look for Check phpinfo() report and look for xdebug section. xdebug section.

►You might need to restart your web You might need to restart your web server.server.

Page 8: Xdebug PHP Performance Profiling Debugging

Error Handling (display_errors Error Handling (display_errors off)off)

Page 9: Xdebug PHP Performance Profiling Debugging

Error Handling (display_errors Error Handling (display_errors on)on)

Page 10: Xdebug PHP Performance Profiling Debugging

Error Handling (display_errors Error Handling (display_errors On w/ xdebug)On w/ xdebug)

Page 11: Xdebug PHP Performance Profiling Debugging

Error Handling (display_errors Error Handling (display_errors on w/ Xdebug)on w/ Xdebug)

Page 12: Xdebug PHP Performance Profiling Debugging

Error Handling (My .htaccess)Error Handling (My .htaccess)

php_flag xdebug.collect_vars onphp_flag xdebug.collect_vars on

php_value xdebug.collect_params 4php_value xdebug.collect_params 4

php_flag xdebug.dump_globals onphp_flag xdebug.dump_globals on

php_value xdebug.dump.SERVER REQUEST_URIphp_value xdebug.dump.SERVER REQUEST_URI

php_value xdebug.dump.GET *php_value xdebug.dump.GET *

php_value xdebug.dump.POST *php_value xdebug.dump.POST *

php_value xdebug.dump.COOKIE *php_value xdebug.dump.COOKIE *

php_flag xdebug.show_local_vars onphp_flag xdebug.show_local_vars on

►Documentation: Documentation: http://www.xdebug.org/doc/http://www.xdebug.org/doc/► Also check phpinfo(), specifically Xdebug Also check phpinfo(), specifically Xdebug

sectionsection

Page 13: Xdebug PHP Performance Profiling Debugging

Remote DebuggingRemote Debugging

►XDebug has Remove Debugging XDebug has Remove Debugging support through GDB (old) and DBGP support through GDB (old) and DBGP (new) protocols.(new) protocols.

►You will need a client that can speak You will need a client that can speak one of these protocols.one of these protocols.

►You will need to adjust Xdebug settings You will need to adjust Xdebug settings on the server to enable remote on the server to enable remote debugging.debugging.

►You can also debug CLI scripts.You can also debug CLI scripts.

Page 14: Xdebug PHP Performance Profiling Debugging

Remote Debugging (Clients)Remote Debugging (Clients)► Dev-PHPDev-PHP (IDE: Windows) (IDE: Windows) ► Eclipse Eclipse pluginplugin, which has been submitted as an enhancement for the , which has been submitted as an enhancement for the PDTPDT (IDE). (IDE). ► Emacs Emacs pluginplugin (Editor Plugin). (Editor Plugin). ► ActiveState's ActiveState's KomodoKomodo (IDE: Windows, Linux, Mac; Commercial). (IDE: Windows, Linux, Mac; Commercial). ► MacGDBPMacGDBP - Standalone Mac client. - Standalone Mac client. ► NetBeansNetBeans (IDE: Windows, Linux, Mac OS X and Solaris. (IDE: Windows, Linux, Mac OS X and Solaris. ► Notepad++Notepad++ pluginplugin (IDE: Windows). (IDE: Windows). ► WaterProof's WaterProof's PHPEditPHPEdit (IDE, from version 2.10: Windows; Commercial). (IDE, from version 2.10: Windows; Commercial). ► Anchor System's Anchor System's PeggyPeggy (IDE: Windows, Japanese; Commercial). (IDE: Windows, Japanese; Commercial). ► MP Software's MP Software's phpDesignerphpDesigner (IDE: Windows, Commercial). (IDE: Windows, Commercial). ► PHPEclipsePHPEclipse (Editor Plugin). (Editor Plugin). ► ProtoeditorProtoeditor (Editor: Linux). (Editor: Linux). ► tsWebeditortsWebeditor (Editor: Windows). (Editor: Windows). ► Xored's Xored's TrueStudioTrueStudio IDE IDE (IDE; Commercial). (IDE; Commercial). ► VIM VIM pluginplugin ( (TutorialTutorial) (Editor Plugin). ) (Editor Plugin). ► jcx software's jcx software's VS.PhpVS.Php (MS Visual Studio Plugin; Commercial). (MS Visual Studio Plugin; Commercial). ► XDebugClientXDebugClient - Standalone Windows client. - Standalone Windows client.

* Source: * Source: http://http://www.xdebug.orgwww.xdebug.org/docs/remote/docs/remote

Page 15: Xdebug PHP Performance Profiling Debugging

Remote Debugging (Server)Remote Debugging (Server)

► Need to enable remote debugging on the server:Need to enable remote debugging on the server: php_value xdebug.remote_enable 1php_value xdebug.remote_enable 1 php_value xdebug.remote_host 192.168.1.105php_value xdebug.remote_host 192.168.1.105 php_value xdebug.remote_port 9000php_value xdebug.remote_port 9000 php_value xdebug.remote_mode reqphp_value xdebug.remote_mode req

► remote_host - takes a hostname or IP address remote_host - takes a hostname or IP address (There’s a patch from Facebook devs to remove this (There’s a patch from Facebook devs to remove this restriction)restriction)

► remote_mode – controls when the remote remote_mode – controls when the remote debugging starts:debugging starts: req - at the beginning of the script req - at the beginning of the script jit - upon an error conditionjit - upon an error condition

Page 16: Xdebug PHP Performance Profiling Debugging

Remote Debugging (Starting)Remote Debugging (Starting)

►xdebug_break( ) xdebug_break( ) ►XDEBUG_SESSION_START variable XDEBUG_SESSION_START variable

passed via GET or POST sets passed via GET or POST sets XDEBUG_SESSION cookie.XDEBUG_SESSION cookie.

►XDebug Helper Firefox Addon: XDebug Helper Firefox Addon: https://addons.mozilla.org/en-US/firefohttps://addons.mozilla.org/en-US/firefox/addon/3960x/addon/3960

Page 17: Xdebug PHP Performance Profiling Debugging

ProfilingProfiling

►Xdebug's built-in profiler generates Xdebug's built-in profiler generates cachegrind files.cachegrind files.

►You will need a tool that can read and You will need a tool that can read and visualize cachegrind files.visualize cachegrind files.

►You will need to adjust some XDebug You will need to adjust some XDebug settings in order to enable profiling on settings in order to enable profiling on the server.the server.

Page 18: Xdebug PHP Performance Profiling Debugging

Profiling (Starting)Profiling (Starting)

► Need to enable profiling on the server:Need to enable profiling on the server:php_value xdebug.profiler_enabled 1php_value xdebug.profiler_enabled 1php_value xdebug.profiler_append 1php_value xdebug.profiler_append 1php_value xdebug.profiler_enable_trigger 1php_value xdebug.profiler_enable_trigger 1php_value xdebug.profiler_output_dir /tmpphp_value xdebug.profiler_output_dir /tmpphp_value xdebug.profiler_output_name cachegrind.out.%pphp_value xdebug.profiler_output_name cachegrind.out.%p

► You need to set You need to set

xdebug.profiler_enable_trigger if you want to xdebug.profiler_enable_trigger if you want to trigger the profiling yourself.trigger the profiling yourself.

► You can trigger profiling via XDEBUG_PROFILE You can trigger profiling via XDEBUG_PROFILE variable passed via GET or POST (or via variable passed via GET or POST (or via XDebug Helper Firefox Addon) sets XDebug Helper Firefox Addon) sets XDEBUG_PROFILE cookie.XDEBUG_PROFILE cookie.

Page 19: Xdebug PHP Performance Profiling Debugging

WebGrindWebGrind

http://code.google.com/p/webgrind/

Page 20: Xdebug PHP Performance Profiling Debugging

WinCacheGrindWinCacheGrind

http://sourceforge.net/projects/wincachegrind/

Page 21: Xdebug PHP Performance Profiling Debugging

MacCallGrindMacCallGrind

http://www.maccallgrind.com/

Page 22: Xdebug PHP Performance Profiling Debugging

KCacheGrindKCacheGrind

http://kcachegrind.sourceforge.net/

Page 23: Xdebug PHP Performance Profiling Debugging

Function TracesFunction Traces

►Xdebug allows you to log all function Xdebug allows you to log all function calls, including parameters and return calls, including parameters and return values to a file in different formats. values to a file in different formats.

►Function trace files contain a timeline Function trace files contain a timeline and record of each function call in PHP and record of each function call in PHP including file and line number.including file and line number.

► It’s a great way to see exactly what’s It’s a great way to see exactly what’s going on. (At times a bit too much going on. (At times a bit too much detail)detail)

Page 24: Xdebug PHP Performance Profiling Debugging

Function TracesFunction Traces

Page 25: Xdebug PHP Performance Profiling Debugging

Function Traces (VIM Syntax)Function Traces (VIM Syntax)

► Xdebug package comes with a VIM syntax Xdebug package comes with a VIM syntax highlight file.highlight file.

► Copy the Copy the xt.vimxt.vim file to file to ~/.vim/syntax~/.vim/syntax ► Edit, or create, Edit, or create, ~/.vim/filetype.vim~/.vim/filetype.vim and add and add

the following lines: the following lines: augroup filetypedetect augroup filetypedetect

au BufNewFile,BufRead *.xt setf xt au BufNewFile,BufRead *.xt setf xt

augroup END augroup END

* Xdebug Documentation: * Xdebug Documentation: http://www.xdebug.org/docs/execution_tracehttp://www.xdebug.org/docs/execution_trace

Page 26: Xdebug PHP Performance Profiling Debugging

Function TracesFunction Traces

►Function trace files come in two Function trace files come in two formats:formats: Human readable (lots of options here)Human readable (lots of options here)

►php_valuephp_value xdebug.show_mem_deltaxdebug.show_mem_delta 11►php_valuephp_value xdebug.collect_returnxdebug.collect_return 11►php_value php_value xdebug.collect_paramsxdebug.collect_params 44

Machine readableMachine readable►php_valuephp_value xdebug.trace_formatxdebug.trace_format 11

HTML format.HTML format.

Page 27: Xdebug PHP Performance Profiling Debugging

XDebug Trace OptionsXDebug Trace Options

php_valuephp_value xdebug.auto_tracexdebug.auto_trace 00

php_valuephp_value xdebug.collect_includesxdebug.collect_includes 11

php_valuephp_value xdebug.collect_returnxdebug.collect_return 11

php_value php_value xdebug.collect_paramsxdebug.collect_params 44

php_valuephp_value xdebug.show_mem_deltaxdebug.show_mem_delta 11

php_valuephp_value xdebug.trace_optionsxdebug.trace_options 00

php_value php_value xdebug.trace_output_dirxdebug.trace_output_dir /tmp/tmp

php_valuephp_value xdebug.trace_output_namexdebug.trace_output_nametrace.%ctrace.%c

Page 28: Xdebug PHP Performance Profiling Debugging

Function Traces (Tips)Function Traces (Tips)

►Be careful when using auto_trace Be careful when using auto_trace option for big projects.option for big projects.

►Files can get very big. I saw cases of Files can get very big. I saw cases of >1 Gig>1 Gig

►Turn Off VIM syntax highlighter for Turn Off VIM syntax highlighter for very big files.very big files.

►You can also trigger start/stop of You can also trigger start/stop of tracing via PHP.tracing via PHP.

Page 29: Xdebug PHP Performance Profiling Debugging

Function Traces (Starting)Function Traces (Starting)

►xdebug_start_trace($filename, xdebug_start_trace($filename, $options);$options); XDEBUG_TRACE_APPEND (1)XDEBUG_TRACE_APPEND (1)

►makes the trace file open in append mode makes the trace file open in append mode rather than overwrite moderather than overwrite mode

XDEBUG_TRACE_COMPUTERIZED (2)XDEBUG_TRACE_COMPUTERIZED (2)►creates a trace file with the format as described creates a trace file with the format as described

under under 11 "xdebug.trace_format". "xdebug.trace_format". XDEBUG_TRACE_HTML (4)XDEBUG_TRACE_HTML (4)

►creates a trace file as an HTML tablecreates a trace file as an HTML table

►xdebug_stop_trace(); xdebug_stop_trace();

Page 30: Xdebug PHP Performance Profiling Debugging

Contact MeContact Me

Gennady FeldmanGennady Feldman

E-mail: E-mail: [email protected]@gena01.com

Website:Website: http://www.gena01.comhttp://www.gena01.com

Blog: Blog: http://www.gena01.com/bloghttp://www.gena01.com/blog

Twitter: Twitter: http://twitter.com/gena01http://twitter.com/gena01