Php Debugger

1,287 views

Published on

Describes usage of PHPDebugger, a debugging tool for PHP, which runs on OS X and Windows.

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

  • Be the first to like this

No Downloads
Views
Total views
1,287
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
33
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php Debugger

  1. 1. Using PHPDebugger - work in progress / unfinished - - work in progress / unfinished - <ul><li>December 2006 </li></ul><ul><li>Stefan Pantke </li></ul><ul><li>[email_address] </li></ul><ul><li>http://www.turingart.com / </li></ul>
  2. 2. Online resources <ul><li>Project info http://www.turingart.com/phpdebugger_lan__en.htm </li></ul><ul><li>Mac OS X download http://www.turingart.com/downloads/phpDebugger.zip </li></ul><ul><li>Windows download http://www.turingart.com/downloads/phpDebuggerWin.zip </li></ul><ul><li>Sample report http://www.turingart.com/downloads/phpDebugger.pdf </li></ul>
  3. 3. In case you change something <ul><li>If you change ‚TADebugger.inc.php‘ Please forward your changed ‚TADebugger.inc.php‘ Probably add a sample PHP file, which uses your new feature </li></ul><ul><li>If you‘d like to enrich the debugger protocol to post other types of information </li></ul><ul><li>Please describe your change-request, I‘m happy to review </li></ul>
  4. 4. Motivation :: Debugging isn‘t that fun... <ul><li>Debugging applications is a time-consuming task </li></ul><ul><li>Without special debugging tools, this task gets even more time-consuming </li></ul><ul><li>Client-side apps may redirect debug-output to a special log-window </li></ul><ul><li>Web-apps can‘t access a log-window of a web-browser </li></ul><ul><li>Writing log-entries on a web-server isn‘t that user friendly </li></ul>
  5. 5. Debuggers :: We have to so many... <ul><li>Yes, but some debuggers </li></ul><ul><ul><li>can‘t be used without a special IDE </li></ul></ul><ul><ul><li>require a source-level re-compile of PHP </li></ul></ul><ul><ul><li>are only available for Windows </li></ul></ul><ul><ul><li>require a special browser </li></ul></ul>
  6. 6. PHPDebugger :: Main features <ul><li>Two components: TADebugger at the PHP side and PHPDebugger at the client-side </li></ul><ul><li>Interfaces with PHP using a new PHP class ‚TADebugger‘ </li></ul><ul><li>PHP and PHPDebugger communicate using TCP [-> Firewall ] </li></ul><ul><li>Does not require a source-level re-compile of PHP </li></ul><ul><li>Is available for Mac OS X and Windows, might be become available for Linux too </li></ul><ul><li>Supports any browser and ships with a built-in one too [a simple one] </li></ul>
  7. 7. PHPDebugger :: Why use it? <ul><li>Improves quality of code TADebugger switches to the highest available error level and automatically catches as much errors as possible </li></ul><ul><li>Keeps HTML clean while debugging TADebugger redirects all errors or exceptions to PHPDebugger and does not disrupt HTML by messages </li></ul><ul><li>Makes messages persist PHPDebugger keeps messages, even if a new HTML page is loaded </li></ul><ul><li>Prints debugging reports PHPDebugger allows to print well-formated reports for your team leader </li></ul>
  8. 8. For the rest, let‘s assume... <ul><li>192.68.1.100 IP of the web-server your PHP web-app runs on </li></ul><ul><li>192.168.1.42 IP of the system PHPDebugger runs on </li></ul><ul><li>8881 Port, on which PHPDebugger listens [default port] </li></ul><ul><li>/Applications/MAMP/htdocs/phpDebugger/ Web-server root of our PHP web-app </li></ul><ul><li>Instrument one of your web-apps using the following step-by-step guide </li></ul>
  9. 9. PHPDebugger :: Installation <ul><li>Install ‚TADebugger.inc.php‘ somewhere in your web-app‘s folder You might wish to install the file your ‚includes‘ folder [assumed] </li></ul><ul><li>Load the ‚TADebugger‘ class using require_once( ‚includes/TADebugger.inc.php‘ ); as the first statement of your PHP source file </li></ul><ul><li>Now, you may access the debugger using the $sharedDebugger variable </li></ul>
  10. 10. PHPDebugger :: Setting up communication <ul><li>First, define where PHPDebugger runs and where your PHP web-app lives </li></ul><ul><li>Define the IP of the system you launched PHPDebugger $sharedDebugger->myDebugHost = '192.168.1.42'; </li></ul><ul><li>Define the port, which PHPDebugger listens on: $sharedDebugger->myDebugPort = 8881; </li></ul><ul><li>Define the root folder of your web-app [optional] $sharedDebugger->setAppBasePath( '/Applications/MAMP/htdocs/phpDebugger/' ); </li></ul>
  11. 11. PHPDebugger :: Output for free... <ul><li>Now start PHPDebugger and click the ‚Stop/Pause‘ button in the upper left corner of the main window </li></ul><ul><li>PHPDebugger is now prepared to receive messages from PHP </li></ul><ul><li>Finally, call your web-app using some URL like this http://192.168.1.100/ </li></ul><ul><li>Note, that you may already see certain messages, although you didn‘t added special debugging instructions </li></ul><ul><li>These messages are for free, since TADebugger defined the highest possible error reporting level for you. </li></ul>
  12. 12. PHPDebugger :: Posting messages <ul><li>To post an arbitrary message from your web-app to PHPDebugger, add this to your code $sharedDebugger->postMessage( 'My Message' ); </li></ul><ul><li>If you call this from within a function, ensure to declare it as global global $sharedDebugger; </li></ul><ul><li>postMessage() basically post one line messages of arbitrary data </li></ul>
  13. 13. PHPDebugger :: Posting backtraces <ul><li>To post a backtrace - a list of all currently called functions or methods - call this $sharedDebugger->postTrace(); </li></ul><ul><li>PHPDebugger prints the first function/method called and each other one slightly shifted right </li></ul><ul><li>Each argument passed will get printed too </li></ul>
  14. 14. PHPDebugger :: Inspecting variables <ul><li>TADebugger can trace variables using this call $sharedDebugger->traceVariable( $myVar, 'some Info' ); </li></ul><ul><li>All values of traced variable get posted back to PHPDebugger if you call this $sharedDebugger->postState(); </li></ul><ul><li>Select ‚Window -> Data Inspector‘ in PHPDebugger to show inspected variables </li></ul>
  15. 15. PHPDebugger :: Installing sample code <ul><li>Copy the contents of the folder named ‚putContentsInWebRoot‘ in your web-server‘s ‚htdocs‘ folder </li></ul><ul><li>Start PHPDebugger and click the ‚Pause/Start‘ button in the upper left corner once </li></ul><ul><li>Launch the sample web-app using this URL http://192.168.1.100/phpDebugger/?k=23 </li></ul><ul><li>You should see output as on the next slide... </li></ul>
  16. 16. Debug output of sample project of sample project <ul><li>Icons indicate message types </li></ul><ul><li>Color indicates source-code </li></ul>
  17. 17. Security Tips <ul><li>Protect your includes directory using a ‚.htaccess‘ file and add these lines # Reject any request regarding this directory Order deny,allow Deny from all Using this ‚.htaccess‘, your web-server denies any access to the ./includes/ directory </li></ul><ul><li>Use the ‚.inc.php‘ extension for each include/require file and deny access using ‚.htaccess‘ settings in your web-app‘s root folder </li></ul>
  18. 18. General debugging tips <ul><li>Use assert to ensure contracts for functions and methods and check as much parameters as possible function handleRequest( $aRequstID ) { assert( is_integer( $aRequestID) ); // fails, if non string passed } // handleRequest </li></ul><ul><li>Use type-safe comparisons Good: if ( ‚something‘ === $aStringVariable) { ... } Less good: if ( ‚something‘ == $aStringVariable) { ... } </li></ul>
  19. 19. PHPDebugger :: Usage behind firewalls <ul><li>I need to install PHPDebugger behind a firewall. Will this work too? For now, the answer is ‚Probably no‘. Currently the ‚TADebugger‘ class initiates a connection. Thus your firewall is likely to not pass the request to PHPDebugger inside a NAT network If you are able to reconfigure your firewall, you need to open a firewall port and redirect it to the IP, where PHPDebugger is running on I‘d propose to acquire a free http://www.dyndns.com / DNS record for your public IP and configure the ‚TADebugger‘ class to use this registered name </li></ul><ul><li>An upcoming release will allow to initiate the communication from PHPDebugger by means of a server-side message-store </li></ul>
  20. 20. The Author <ul><li>Stefan Pantke </li></ul><ul><li>Dipl.-Informatiker [M. Sc. CS] </li></ul><ul><li>[email_address] </li></ul><ul><li>I might wear polo shirts too ;-) </li></ul>
  21. 21. Thanks for your attention! <ul><li>http://www.turingart.com / </li></ul>

×