Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Effective debugging

Oh dear, your application has suddenly stopped working as expected. What should you do now?

Using techniques applicable to any php application, we'll go over what to look for and which problems to avoid when trying to determine where the problem lies. We'll show how to correctly identify and deal with problems including:

* network connectivity
* server config issues
* php config
* WSOD
* common CakePHP application errors

Effective debugging

  1. 1. EFFECTIVE DEBUGGING SPEND TIME FIXING PROBLEMS, NOT FINDING THEM /AndyDawson @AD7six
  2. 2. THE WEB IS JUST PLUMBING WITH BYTES Image:Wikipedia
  3. 3. OF COURSE IT'S NOT REALLY THAT SIMPLE Image:Flickr.com ↖ The hardestproblems to fix existhere. In the developer's head.
  4. 4. FIND IT WITH A HAMMER Image:Codinghorror.com //pickone thrownewException('Madeithere!'); print_r(debug_backtrace()); die(__FILE__.':'.__LINE__);
  5. 5. MOST APPS AREN'T THAT SIMPLE Ahammer willwork, butitmighttake awhile. Image:energysystemsgroup.com
  6. 6. HELLO WORLD EXAMPLE "Where's mywebpage"
  7. 7. CHECK THE HTTP RESPONSE CODE Is itactually an error?
  8. 8. READ THE WEBSERVER ERROR LOG Location depends on configuration Typically/var/log/*/ PHPParseerror: [...]in/var/www/example.dev/public/index.phponline4"
  9. 9. IDENTIFY THE PROBLEM $cat/var/www/example.dev/public/index.php <?php echo"helloworld'; Parseerrorsareoftenthelinebefore(orearlier)inafile
  10. 10. NOT AN ERROR EXAMPLE Notan error response code so no (direct) logmessages:"
  11. 11. GREP FOR IT $grep-rl"NotFound"* ... src/Really/Not/Obvious/File.php $grep-r"NotFound"* ... src/Really/Not/Obvious/File.php 404=>'NotFound', $catsrc/Really/Not/Obvious/File.php ... functionerror(){ die($this->_statusCodes['404']); }
  12. 12. CAKEPHP HELLO WORLD EXAMPLE "Whythe Four Oh Four?"
  13. 13. CHECK THE ERROR LOG app/tmp/error.log
  14. 14. IDENTIFY THE PROBLEM $catapp/View/Pages/home.ctp <?php ... if(!Configure::read('debug')): thrownewNotFoundException(); endif;
  15. 15. DEBUG BASICS Configand functions everydeveloper should know about
  16. 16. PHP CONFIG Inifile settings are the defaults if notchanged (duh) display_errors(On/Off) log_errors(On) display_startup_errors(Off) error_reporting(On/Off) Runtime settings override the inifile -no effectif the file theyare in has aparse error (duh) ini_set('display_errors', 0/1)
  17. 17. PHP FUNCTIONS print_r(); debug_backtrace(); get_included_files(); phpinfo();
  18. 18. PHP VARIABLES/CONSTANTS __FILE__ __LINE__ $_SERVER et.al.
  19. 19. A WAY TO REPRODUCE Have awayto consistentlyreproduce the error $curl-Ihttp://example.dev/ HTTP/1.1500InternalServerError Server:nginx Date:Sat,23Aug201410:33:46GMT Content-Type:text/html gitbisect-find regression errors quickly
  20. 20. XDEBUG peclinstallxdebug
  21. 21. WEBGRIND Turn on xdebugprofiling, and look atwhatarequestis doing
  22. 22. CLI DEBUGGING Pause execution with read: print_r($somethingInteresting); `readfoo`; Usefulwhen debuggingaloop.
  23. 23. NOT CAKEPHP CODE? Aghetto debugfunction: functiondebug($var,$showHtml=null){ if(!defined('DEBUG')||!DEBUG){return;} if($showHtml===null){ $showHtml=php_sapi_name()==='cli'?false:true; } $var=var_export($var,true); if(!$showHtml){ echo$var; return; } echo'<pre>'.htmlspecialchars($var).'</pre>'; } createatracefunction(Debugger::trace)tooifneeded
  24. 24. LOGIC AIDES Justvoicingaproblem can find the solution/error Image:Wikipedia
  25. 25. ERROR MESSAGES DON'T LIE PHP Parse error: syntax error, unexpected '$bar'(T_VARIABLE) in parse.php on line 3 $catfoo.php <?php ... if($foo||bar){ There's an accidentalnone-breakingspace on thatline
  26. 26. NETWORKING PROBLEMS If the problem is noton the webserver -where is it?
  27. 27. NAMESERVER PROBLEMS No response from nameservers is the same as adomain not existsing $digcakefest.org ;<<>>DiG9.8.3-P1<<>>cakefest.org ;;globaloptions:+cmd ;;Gotanswer: ;;->>HEADER<<-opcode:QUERY,status:SERVFAIL,id:15266 ;;flags:qrrdra;QUERY:1,ANSWER:0,AUTHORITY:0,ADDITIONAL:0 ;;QUESTIONSECTION: ;cakefest.org. IN A ;;Querytime:4142msec ;;SERVER:8.8.8.8#53(8.8.8.8) ;;WHEN:TueAug1916:26:592014 ;;MSGSIZE rcvd:30 ... ;;ANSWERSECTION: cakefest.org. 1568 IN A 50.56.232.22 ...
  28. 28. DNS PROBLEMS "No route to host"means the ip requested isn'taccessible $traceroutecakefest.org traceroutetocakefest.org(50.56.232.22),64hopsmax,52bytepackets 1 172.26.81.1(172.26.81.1) 1.032ms 0.912ms 0.912ms 2 192.168.0.1(192.168.0.1) 2.777ms 1.267ms 2.338ms ... 12 rackspace-ic-302090-dls-bb1.c.telia.net(62.115.33.78) 152.340ms 15 9.367ms 146.339ms 13 *** 14 *** 15 *** If there are stars -there be problems
  29. 29. BACKEND SERVER IS DOWN 502 Bad Gateway E.g. php-fpm or hhvm is notrunning Willbe in the webserver error log There willnotbe anyapplication logentries
  30. 30. PITFALLS TO AVOID Whatnotto do when debuggingcode
  31. 31. FIX PROBLEMS, NOT SYMPTOMS Don'tignore errors/warnings/notices Fix them in order, some errors willbe the concequence of earlier problems.
  32. 32. COMMON MISTAKES Notlookingfor error logs Readingor focussingon the wrongerror message Notreadingthe error message Notreadingthe error message aloud Misinterprettingthe error message Stoppingdebuggingtoo early. If "the problem"is aclass/function with source code -debug the source code of thatfunction New user: Ifound the problem, it's app/webroot/index.php!
  33. 33. IMPLICIT ASSUMPTIONS Be waryof implictassumptions You're debuggingthe same file the browser is loading You're debuggingthe same application the browser is loading You're debuggingthe same server the browser is loading You're debuggingthe same requestthe browser is loading Conclusions so far are accurate WTF?Backup, and re-verifyeverything.
  34. 34. COMMON PROBLEMS And how to debug/identifythem
  35. 35. NO MODREWRITE Don'tlook atphp files -the error is atthe webserver. Enable mod rewrite Restartthe webserver
  36. 36. CAKEPHP AUTOMODELS $model=ClassRegistry::init('MyModel'); $model->methodName(); SQLError:1064:YouhaveanerrorinyourSQLsyntax;[...] check[...]fortherightsyntaxtousenear'methodName' MyModeldoes nothave the function methodName MyModelhas no behaviour bound implementing methodName $modelis an instance of AppModel
  37. 37. CAKEPHP AUTOMODELS - IDENTIFICATION $model=ClassRegistry::init('MyModel'); debug(get_class($model)); ##########DEBUG########## 'MyModel' ########################### $model=ClassRegistry::init('MyModel'); debug(get_included_files()); ##########DEBUG########## array( ... ...app/Model/MyModel.php ... ) ###########################
  38. 38. CAN'T FIX IT (╯°□°)╯︵ ┻━┻ Can'tfind the problem/solution?-gethelp. Butfirst: Collectthe information you've got Write astandalone, reproducible example if possible Reduce the question to it's simplest, form Don'tover simplifyor make acontrived example Ask colleagues/the internet Brace for impact Stack overflow, the google group and irc as greatplaces to get help
  39. 39. SUMMARY Identifythe rightpartof arequestto debug Fix errors in order Check your assumptions/conclusions ateveryWTF Formulate aquestion, and Ask for help Profit!

    Als Erste(r) kommentieren

    Loggen Sie sich ein, um Kommentare anzuzeigen.

  • MarcelKalveram

    Aug. 23, 2014
  • CoolQoo

    Aug. 24, 2014
  • zoghal

    Aug. 31, 2014
  • ssuser11d5b6

    Jul. 27, 2016
  • jonghyunpark944023

    May. 27, 2017
  • nyamada43

    Jul. 11, 2017
  • jinmanchoi2

    Sep. 11, 2018

Oh dear, your application has suddenly stopped working as expected. What should you do now? Using techniques applicable to any php application, we'll go over what to look for and which problems to avoid when trying to determine where the problem lies. We'll show how to correctly identify and deal with problems including: * network connectivity * server config issues * php config * WSOD * common CakePHP application errors

Aufrufe

Aufrufe insgesamt

2.738

Auf Slideshare

0

Aus Einbettungen

0

Anzahl der Einbettungen

136

Befehle

Downloads

18

Geteilt

0

Kommentare

0

Likes

7

×