Even better debugging; Equipped yourself with powerful tools.

38
Even Better Debugging Equipped yourself with powerful debug tools Murshed Ahmmad Khan Sunday, November 7, 2010

description

This slide was prepared for the phpxperts seminar 2010. As the presentation time was limited so it only touches few primitive approaches and moves to a mighty debugging extension xdebug. The tag line is "Every job has a unique tool", so spend some time to enrich yourself with powerful debug tools rather than coding all of the time which in turns will save your lot of development time in the future.

Transcript of Even better debugging; Equipped yourself with powerful tools.

Page 1: Even better debugging; Equipped yourself with powerful tools.

Even Better DebuggingEquipped yourself with powerful debug tools

Murshed Ahmmad Khan

Sunday, November 7, 2010

Page 2: Even better debugging; Equipped yourself with powerful tools.

presented at phpxperts seminar 2010

6th November, 2010

Sunday, November 7, 2010

Page 3: Even better debugging; Equipped yourself with powerful tools.

“The defect is the crime; debugging is the punishment”

Cause nobody’s perfect; study says we use 80% of our time to maintain the old codes.

Sunday, November 7, 2010

Page 4: Even better debugging; Equipped yourself with powerful tools.

Frustrating!! blank pages?

Think!! what does a blank page mean? What happened?

Sunday, November 7, 2010

Page 5: Even better debugging; Equipped yourself with powerful tools.

Let’s have a quick recap of the available error settings in PHP

Sunday, November 7, 2010

Page 6: Even better debugging; Equipped yourself with powerful tools.

display_errors = OnThis determines whether errors should be printed to the screen as part of the script output; depending on the error_reporting value.

It is strongly recommended to turn it “Off” in the production server.

Default value is “On”.

Sunday, November 7, 2010

Page 7: Even better debugging; Equipped yourself with powerful tools.

log_errors = OnWrite/log errors into a server side log file; defined by “error_log”

It is strongly recommended to turn log_errors “On” in place of display_errors in the production web sites.

Default value is “Off”.

Sunday, November 7, 2010

Page 8: Even better debugging; Equipped yourself with powerful tools.

error_reporting = E_ALL

What type of errors, notices, warnings need to be notified/write by the php interpreter.

Default value is E_ALL & ~ E_NOTICE

When developing best practice is to use E_ALL. Or even better E_ALL | E_STRICT.

Sunday, November 7, 2010

Page 9: Even better debugging; Equipped yourself with powerful tools.

Error directives... All at oncephp.ini directive default value Purpose Example recommendations

display_errors OnPrint out errors as part of the

outputdisplay_errors = On

strongly recommended to

turn it off in production

log_errors Off log errors into a log file log_errors = On

strongly recommended to turn it on in

production

error_reporting E_ALL & ~E_NOTICE

what type of errors need to be notified and/or

logged

error_reporting = E_ALL

show all errors except notices:

E_ALL & ~ E_NOTICE

for development in php5, show all type of errors:

E_ALL | E_STRICT

*from php6 E_STRICT will also be included in E_ALL

Sunday, November 7, 2010

Page 10: Even better debugging; Equipped yourself with powerful tools.

Revisiting the blank page...

TextText

browser o/p

Sunday, November 7, 2010

Page 11: Even better debugging; Equipped yourself with powerful tools.

Revisiting the blank page...

erroneous script

server error log

Sunday, November 7, 2010

Page 12: Even better debugging; Equipped yourself with powerful tools.

Controlling php error reporting from apache httpd.conf

Sometimes turning error reporting on in php.ini may not work.

It’s good to know how to set these configuration variables from server side.

Setting these in httpd.conf file overrides all php.ini & guarantee to set the error levels.

Sunday, November 7, 2010

Page 13: Even better debugging; Equipped yourself with powerful tools.

Controlling php error reporting from apache(cont...)

[+] Add the below lines in httpd.conf file:

the integer value 2039 stands for E_ALL & ~E_NOTICE

There are different integer values for each error types

[+] php_flag display_errors on[+] php_value error_reporting 2039

Sunday, November 7, 2010

Page 14: Even better debugging; Equipped yourself with powerful tools.

Setting the error directives at runtime from the php scripts

It won’t affect if script has fatal errors cause it might not get executed.

ini_set(‘display_errors’, 1);ini_set(‘error_reporting’, E_ALL);

// error_reporting(E_ALL);

Sunday, November 7, 2010

Page 15: Even better debugging; Equipped yourself with powerful tools.

For variable debugging, use reusable method

var_debug($dataNodes,true);function var_debug( $item, $exit=false ){$op = '<pre>DEBUG INFO</pre>';$op .= '<pre>' . print_r( $item, true ) . '</pre>';if( is_production() ){ $op = ‘<!-- ’ . $op . ‘ -->’; //return false;}echo $op;if( $exit ){ exit(); }}

Sunday, November 7, 2010

Page 16: Even better debugging; Equipped yourself with powerful tools.

Don’t code all of the time! Rather equipped yourself with powerful tools!!

Sunday, November 7, 2010

Page 17: Even better debugging; Equipped yourself with powerful tools.

Xdebug

Sunday, November 7, 2010

Page 18: Even better debugging; Equipped yourself with powerful tools.

What is Xdebug?Xdebug is one of the most popular debugging engines in PHP

A PHP extension about 8 years old

Uses DBGp debugging protocol

Sunday, November 7, 2010

Page 19: Even better debugging; Equipped yourself with powerful tools.

Xdebug featuresstack traces

infinite recursion protection

colored var_dump()

function traces

Sunday, November 7, 2010

Page 20: Even better debugging; Equipped yourself with powerful tools.

Xdebug features(Cont...)

code coverage reports

profiling / performance analysis

remote debugging

a whole lot other features

Sunday, November 7, 2010

Page 21: Even better debugging; Equipped yourself with powerful tools.

Installing Xdebug [by PEAR/PECL]

# pecl install xdebug As easy as one command!

Sunday, November 7, 2010

Page 22: Even better debugging; Equipped yourself with powerful tools.

Installing Xdebug[PEAR/PECL cont...]

Ignore any prompt: you should add “extension=xdebug.so” in php.ini

[+] Add the correct line in your php.ini file:

[+] zend_extension = “/usr/local/php/modules/xdebug.so”

Sunday, November 7, 2010

Page 23: Even better debugging; Equipped yourself with powerful tools.

Installing Xdebug(By Downloading)

Activestate site has binary packages available for all versions of php in all platforms(Windows/MacOSX/Linux)

Sunday, November 7, 2010

Page 24: Even better debugging; Equipped yourself with powerful tools.

Download Xdebug binaries (cont...)

http://code.activestate.com/komodo/remotedebugging/

Sunday, November 7, 2010

Page 25: Even better debugging; Equipped yourself with powerful tools.

Download xDebug...(Grab the extension as per your PHP version

Sunday, November 7, 2010

Page 26: Even better debugging; Equipped yourself with powerful tools.

Xdebug Installation(contd...)paste the xdebug.so/xdebug.dll file into the extension directory

[+]Add the extension path in the php.ini

[xdebug]zend_extension = “/Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so”xdebug.file_link_format = “txmt://open?url=file://%f&line=%1”

Sunday, November 7, 2010

Page 27: Even better debugging; Equipped yourself with powerful tools.

Xdebug in action!restart apache and you’re done!!

xdebug now replaces traditional error messages with more helpful debug information.

Sunday, November 7, 2010

Page 28: Even better debugging; Equipped yourself with powerful tools.

Xdebug: Stack Traces

Sunday, November 7, 2010

Page 29: Even better debugging; Equipped yourself with powerful tools.

Debug Request VariablesXdebug.dump.GET = *

Sunday, November 7, 2010

Page 30: Even better debugging; Equipped yourself with powerful tools.

Debug Request Variables

Xdebug.dump.GET = *

Xdebug.dump.POST = *

Also for: COOKIE, ENV, FILES, REQUEST, SERVER and SESSION

Sunday, November 7, 2010

Page 31: Even better debugging; Equipped yourself with powerful tools.

xdebug: Code Coverage

xdebug_start_code_coverage();var_dump( xdebug_get_code_coverage);xdebug_stop_code_coverage();

Tells you which lines of script have been executed during the request.

Sunday, November 7, 2010

Page 32: Even better debugging; Equipped yourself with powerful tools.

Code Coverage(Contd...)

Sunday, November 7, 2010

Page 33: Even better debugging; Equipped yourself with powerful tools.

Remote Debugging(step by step debugging with IDE or any DbGp

interface)

[+] php.ini settings:

xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_host=localhost xdebug.remote_port=9000 xdebug.extended_info=1

Sunday, November 7, 2010

Page 34: Even better debugging; Equipped yourself with powerful tools.

Powerful code profiling with Xdebug

Sunday, November 7, 2010

Page 35: Even better debugging; Equipped yourself with powerful tools.

Powerful code profiling with Xdebug(Contd...)

WinCacheGrind: for windows

kCacheGrind: for linux

Webgrind: for all platforms

Easy Xdebug: browser extensions(firefox addon)

Sunday, November 7, 2010

Page 37: Even better debugging; Equipped yourself with powerful tools.

who am imurshed ahmmad Khansoftware engineer, somewhere in... also a bug hunter, code ninja

stay tuned for the updated slides at: http://www.usamurai.comtwitter: @usamurai. email: [email protected]

Sunday, November 7, 2010

Page 38: Even better debugging; Equipped yourself with powerful tools.

Thanks!

No Questions?! :)

Sunday, November 7, 2010