From Lyceum
Jump to: navigation, search

XDebug Allows for Local and Remote debugging. Kcachegrind is a graphical interface to profile information generated with Valgrind.

These two tools together are a formidable force to leverage against any code you are working on to debug, analyze and profile it.

  • Remote compute, database and I/O resources may be different
  • PHP compiled differently on server, different modules
  • Database access
  • Debug and profile tools available on the server
  • It's just better this way

http://www.xdebug.org/

Install Xdebug

Install Xdebug on remote server:

http://www.xdebug.org/docs/install

With pear: (Yum install php-pear and php-devel)

pecl install xdebug
pecl config-show
pecl info xdebug
[root@cobbler ~]# locate xdebug.so
/usr/lib64/php/modules/xdebug.so

Configure PHP

Edit /etc/php.ini

zend_extension="/usr/lib64/php/modules/xdebug.so"
(Note for php 5.3+ beware using other declarations to load the module)
xdebug.remote_enable=On
xdebug.remote_handler="dbgp"
xdebug.remote_mode="req"
xdebug.remote_port=9000
xdebug.remote_host="160.91.29.78"
xdebug.remote_log=/var/log/xdebug_remote_log

NOTE: Don't forget - restart apache after any changes!

Verify Xdebug working via phpinfo.

Firewall

Don't forget - open port 9000 on both ends (configurable of course)

Check out / Rsync Code:

You need a local copy of the code. Check it out of SVN, or just rsync it with something like:

user@yourbox:~/joe> rsync -rva -e ssh joe@remoteserver:/var/www/vhosts/drupal localCopy/

Firefox extension:

Install Easy Xdebug https://addons.mozilla.org/en-US/firefox/addon/easy-xdebug/

Or use the manual method and add this to the page to be debugged:

?XDEBUG_SESSION_START=default

Debugging:

Configure Eclipse

  • Preferences --> PHP --> Debugger --> Installed Debugger --> XDebug --> Configure

Set "Accept Remote Sessions (jit)" to "any"

  • Preference --> General --> Web Browser

Create new PHP Project from the source code directory your created above.


Debug it!

  • If using the Easy Debug Firefox module, click the little green bug to enable, then simple refresh the webpage. This should automatically start the debugging session in Eclipse.

Or you can . . .

  • Run "Debug as.." --> PHP Web Page
  • Right click "Debug as.." --> PHP Web Page
  • Or use "Debug Configuration"

Or, start it from the site too!

Browser will launch, and send necessary session ID to debugger Eclipse Debugger should start automatically


Profiler

http://www.xdebug.org/docs/profiler

(KDE packages: kdesdk kcachegrind)

You start and stop the collection of profile information using the same Firefox Easy Xdebug plugin, simply click the small green box to enable profile data collection. By default this is saved in the server /tmp directory named similar to: cachegrind.out.24878, this is the file you open with kcachegrind.

For non-Linux users, I'll install this on a development box. You can then SSH in and X forward this tool back to your desktop. Once you run it you can open profile files by using the KDE fish protocol. Just click on Open and for the location type:

fish://user@server:/tmp

Also, to view the source code in the profiler, you have to add the directory containing it in kcachegrind:

"Settings" --> "Configure Kcachegrind" -- "Annotations" and add the source folder.

Source code will be displayed with runtime data collection information next to each function. Very cool. See the crash course at:

https://lbtwiki.cern.ch/bin/view/Online/Kcachegrind

The official Kcachegrind documentation can be found on the projects site, and here is a PDF.

The information this provides is brilliant, not that you can display the function stack with various visual tools - check out the call graph and caller map tabs near the bottom.

See the above link and the manual (under help menu) for a good introduction.

Resources:

Additional Resources:

http://www.eclipse.org/pdt/documents/XDebugGuideForPDT2.0.pdf

http://kcachegrind.sourceforge.net/html/Download.html

http://www.xdebug.org/docs/remote

http://bogdan-albei.blogspot.com/2010/06/php-remote-debugging-with-xdebug-and.html

http://devzone.zend.com/article/2930

Sample php.ini

;XDebug Settings
zend_extension="/usr/lib64/php/modules/xdebug.so"

xdebug.remote_enable=On
xdebug.profiler_output_dir=/tmp
xdebug.remote_handler="dbgp"
xdebug.remote_mode="req"
xdebug.remote_port=9000
;xdebug.remote_host="160.91.29.78"
xdebug.remote_host="callandor.ornl.gov"
xdebug.remote_log=/var/log/xdebug_remote_log

;PHP Profiling
xdebug.profiler_enable=1
xdebug.profiler_enable_trigger=1
;Must be writable by Web server / PHP user.
;Hint: if using suPHP this could be the owner of the script.
xdebug.profiler_output_dir=/tmp