Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Debug SilverStripe like a Pro

Debug SilverStripe like a Pro

xdebug talk by Lukas Erni and Werner M. Krauß at SilverStripe EU Conference in Ljubljana, Slovenia 2016 www.stripecon.eu

wernerkrauss

October 15, 2016
Tweet

More Decks by wernerkrauss

Other Decks in Programming

Transcript

  1. xdebug
    Debug SilverStripe like a Pro

    View full-size slide

  2. Who we are
    Werner M. Krauß alias wmk
    Hallstatt, Austria
    netwerkstatt.at
    Family, 2 kids
    > 5 guitars
    Lukas Erni alias lerni / munomono
    Ruswil, Switzerland
    kraftausdruck.ch
    Family, 2 kids
    Beekeeping

    View full-size slide

  3. Bugs
    The unknown Beings

    View full-size slide

  4. I could search the bug now. Or I could hammer a nail into my knee.
    Where did I put the nails?

    View full-size slide

  5. The History of Bugs

    View full-size slide

  6. 19th Century Hardware Engineering
    „The first step [in all of my inventions] is an intuition,
    and comes with a burst, then difficulties arise – this thing
    gives out and [it is] then that ‘Bugs’ – as such little
    faults and difficulties are called – show themselves […].“
    Thomas Edison, 1878

    View full-size slide

  7. The first Bug
    ● 09.09.1947, Harvard Faculty
    at the Computation Laboratory
    ● Operators traced an error in
    the Mark II to a moth trapped
    in a relay, coining the term
    bug.
    ● This bug was carefully
    removed and taped to the log
    book.
    ● source: Wikipedia

    View full-size slide

  8. So your Talk is
    about Moths?

    View full-size slide

  9. how this talk
    came to bee

    View full-size slide

  10. How this Talk came to Bee
    I’m personally not much of a PHP-Guy. Once in awhile I hang
    out on IRC or work on a project with Werner and he says
    "!debug lerni", then the IRC-bot says…
    lerni: Maybe it's time to start xdebug and see what’s going
    on... check out
    http://docs.silverstripe.org/en/developer_guides/debugging/
    and http://xdebug.org/docs/

    View full-size slide

  11. !debug WMK - YOU ARE NOT ALONE
    https://silverstripe.logged.nz/search.php?q=wmk+%21debug

    View full-size slide

  12. on stackoverflow, you can find some more ;)
    https://stackoverflow.com/questions/39405731/silverstripe-could-not-update-record
    https://stackoverflow.com/questions/38960516/silverstripe-3-1-site-will-not-output-page-ss-template-content

    View full-size slide

  13. History Of
    Debugging PHP

    View full-size slide

  14. Debug History
    ● echo($var);
    ● print_r($array);
    ● die("I'm here");
    ● debug::show(...);
    ● debug::dump(...);
    ● debug::log(...);

    View full-size slide

  15. debug helpers
    There are Libs to help you show stuff more pretty. Like:
    https://github.com/raveren/kint

    View full-size slide

  16. SilverStripe Debug
    Parameters

    View full-size slide

  17. SilverStripe Debug Parameters
    ● ?isDev=1 Put the site into development mode, enabling
    debugging messages to the browser on a live server. For
    security, you'll be asked to log in with an administrator
    log-in. Will persist for the current browser session.
    ● ?isTest=1 See above.
    ● ?debug=1 Show a collection of debugging information about
    the director / controller operation
    ● ?debug_request=1 Show all steps of the request from
    initial HTTPRequest to Controller to Template Rendering

    View full-size slide

  18. Silverstripe Debug Parameters #2
    ● ?showqueries=1 List all SQL queries executed
    ● ?showtemplate=1 Show the compiled version of all the
    templates used, including line numbers. Good when you
    have a syntax error in a template. Cannot be used on a
    Live site without isDev when logged in as Admin.
    https://docs.silverstripe.org/en/3.4/developer_guides/debugg
    ing/
    https://docs.silverstripe.org/en/3.4/developer_guides/debugg
    ing/url_variable_tools/

    View full-size slide

  19. SilverStripe
    DebugBar Module

    View full-size slide

  20. SilverStripe DebugBar Module
    https://github.com/lekoala/silverstripe-debugbar
    This module shows a nice DebugBar on the bottom of your page
    ● execution time overview.
    ● show messages l($var) to the DebugBar,
    ● trace where queries come from.
    ● ?showqueries=1 and also dump d($obj) look pretty.

    View full-size slide

  21. SilverStripe DebugBar Module

    View full-size slide

  22. SilverStripe DebugBar Module - Dump

    View full-size slide

  23. Debugging
    Templates

    View full-size slide

  24. Template Syntax Highlighting
    ● A killer feature of PHPStorm’s SilverStripe plugin
    ● Helps you avoid template bugs

    View full-size slide

  25. Show File Names in Teplates
    ---
    Only:
    environment: 'dev'
    ---
    SSViewer:
    source_file_comments: true

    View full-size slide

  26. Show File Names and Comments
    Attention: ruins ajax calls that output pure templated JSON

    View full-size slide

  27. so, why xdebug?
    ● On-the-fly debugging
    ● Breakpoints - no “leftovers” in your code
    ● Pause and resume execution
    ● Change the value of a variable while the code is running
    ● Pretty backtrace out of the box
    ● Detect @ in the code

    View full-size slide

  28. How xdebug works
    ● PHP extension (PEAR/PECL)
    ● Installed on server
    ● Uses DBGp debugging protocol
    ● Preinstalled on many setups like XAMP or vagrant boxes

    View full-size slide

  29. Setup MAMP/WAMP
    ● It’s preinstalled
    ● Just enable it

    View full-size slide

  30. How xdebug works

    View full-size slide

  31. php.ini
    ● Xdebug slows down execution, just enable what’s needed.
    [xdebug]
    xdebug.remote_enable=1
    debug.remote_host=localhost
    xdebug.remote_port=9000
    Basic localhost:

    View full-size slide

  32. php.ini
    zend_extension=/path/to/xdebug.so
    xdebug.remote_enable = 1
    xdebug.remote_connect_back = On
    xdebug.idekey = PHPSTORM
    Basic vm / dev server:

    View full-size slide

  33. Useful Setting
    ● Xdebug will disable the
    @ (shut-up) operator
    ● Notices, warnings and errors
    are no longer hidden
    ● https://xdebug.org/docs/all_set
    tings
    xdebug.scream = 1;
    https://www.flickr.com/photos/gcarvalho/142922427

    View full-size slide

  34. ● Configure server address
    ● Configure path mapping for Remote-Debugging
    ● Setup bookmarklets for debugging (and profiling)
    https://www.jetbrains.com/phpstorm/marklets/
    IDE-Setup

    View full-size slide

  35. Debug Config

    View full-size slide

  36. Server Config with Path Mapping

    View full-size slide

  37. Debugger Functions

    View full-size slide

  38. Tools
    ● Breakpoint
    ● Conditional breakpoint
    ● List of all available variables in current scope
    ● Watch
    ● Frames (stack of called functions)

    View full-size slide

  39. Methods
    Step Over => goto next line (F8)
    Step Into => go inside a called function or method (F7)
    Force Step Into => (Shift+Alt+F7 | ⌥⇧F7)
    Step Out => leave the current function (Shift+F8 | ⇧F8)
    Run to Cursor => (Alt+F9 | ⌥F9)

    View full-size slide

  40. More Methods
    Resume Program => goto next breakpoint (F9 | ⌥⌘R)
    Evaluate Expression => (Alt+F8 | ⌥F8)
    Quick Evaluate Expression => without dialog
    (Ctrl+Alt+F8 | ⌥⌘F8)
    Toggle Breakpoint => (Ctrl+F8 | ⌘F8)
    View Breakpoints => (Ctrl+Shift+F8 | ⇧⌘F8)

    View full-size slide

  41. GUI Overview

    View full-size slide

  42. Live Example

    View full-size slide

  43. Debugging a Failing Unit Test
    ● Debug (remote) CLI
    ● PHPStorm has a good documentation for this
    ● How to use sqlite3 while debugging the unit test?
    ● Terribly slow
    ● https://confluence.jetbrains.com/display/PhpStorm/Debuggi
    ng+PHP+CLI+scripts+with+PhpStorm

    View full-size slide

  44. Profile like a Pro

    View full-size slide

  45. Your Site is too slow (it is..)
    ● Faster machine
    ● Opcache
    ● Use lastest and fastest PHP
    ● Partial caching
    ● Static publisher module
    ● ...
    ● Maybe it’s just bad, slow code?

    View full-size slide

  46. Pro-File it with Xdebug!
    ● Helps finding bottlenecks
    ○ useful for guitarists!
    ● Runs on the server
    ● Logs time and memory
    consumption of each function
    call
    ● Outputs a cachegrind file
    (can be very big)

    View full-size slide

  47. ● Use bookmarklets for start and stop
    php.ini
    xdebug.profiler_enable=0
    xdebug.profiler_enable_trigger = 1; #enable by trigger
    xdebug.profiler_output_dir="/path/to/logs/debug"
    profiling:

    View full-size slide

  48. Caching helps…
    Who would have
    known…?

    View full-size slide

  49. Alternatives

    View full-size slide

  50. ● Zend Debugger (comes bundled with a Zend Server)
    ● phpdbg (bundled with 5.6 but not supported by PhpStorm)
    ○ not a real step debugger?
    Ok, we’ve found some - in Theory

    View full-size slide

  51. Profiling Alternatives
    ● XHProof
    ● Blackfire
    ● ...

    View full-size slide

  52. Q&A
    ● Does this all make sense to you?
    ● How do you handle debugging?
    ● Anybody wants to show something?

    View full-size slide

  53. Thanks!
    www.silverstrip.es

    View full-size slide