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

The Debug Dance - An Intro To Step Debugging - php[world] 2018

The Debug Dance - An Intro To Step Debugging - php[world] 2018

This talk was given at php[world] 2018.

Using `var_dump()` to debug your app has its benefits, but there are more comprehensive and efficient ways to debug those particularly illusive bugs. Enter step debugging. We'll be using Xdebug & the PhpStorm IDE to step through our PHP apps line by line and see how much more power step debugging gives us over the conventional `var_dump()` technique. We'll also touch on debugging from the command line. Learn to dance in PHP with step debugging.

Sammy Kaye Powers

November 14, 2018
Tweet

More Decks by Sammy Kaye Powers

Other Decks in Programming

Transcript

  1. legacy.joind.in/24817
    @SammyK #phpworld
    Sammy Kaye Powers
    Dance
    The
    Washington, D.C. - 2018
    An Intro To
    Step Debugging in

    View Slide

  2. legacy.joind.in/24817
    @SammyK #phpworld
    CLOSER
    :)
    Get

    View Slide

  3. Thanks to Our
    Sponsors
    2018

    View Slide

  4. legacy.joind.in/24817
    @SammyK #phpworld
    Download the
    slides
    legacy.joind.in/24817

    View Slide

  5. legacy.joind.in/24817
    @SammyK #phpworld
    Does not work for

    View Slide

  6. legacy.joind.in/24817
    @SammyK #phpworld
    What is
    Debugging?
    Finding and fixing unexpected behavior in code

    View Slide

  7. So you write
    Some Code

    View Slide

  8. Does not work

    View Slide

  9. legacy.joind.in/24817
    @SammyK #phpworld
    You could check the
    Error logs

    View Slide

  10. legacy.joind.in/24817
    @SammyK #phpworld
    All the things
    var_dump()

    View Slide

  11. legacy.joind.in/24817
    @SammyK #phpworld

    View Slide

  12. legacy.joind.in/24817
    @SammyK #phpworld
    OR

    View Slide

  13. legacy.joind.in/24817
    @SammyK #phpworld
    Use a
    ‘er

    View Slide

  14. legacy.joind.in/24817
    @SammyK #phpworld
    Kate Gregory “ I'm not saying, ‘don't
    use printf()’, [...] but
    I think it's insane to
    use printf() on day
    one of learning C++.
    [...] I would first say
    use the debugger.
    Episode 30: Stop Teaching C
    (When Teaching C++)

    View Slide

  15. legacy.joind.in/24817
    @SammyK #phpworld
    …now is the perfect time to learn
    Step Debugging
    BNEW!
    Professional

    View Slide

  16. legacy.joind.in/24817
    @SammyK #phpworld
    Some step-debugging
    Jargon

    View Slide

  17. legacy.joind.in/24817
    @SammyK #phpworld
    a signal that tells the debugger to pause the execution of your code
    Breakpoint

    View Slide

  18. legacy.joind.in/24817
    @SammyK #phpworld
    run to the next breakpoint
    Resume Program

    View Slide

  19. legacy.joind.in/24817
    @SammyK #phpworld
    advance to the next line in the same scope
    Step Over

    View Slide

  20. legacy.joind.in/24817
    @SammyK #phpworld
    if the next line is a function call, enter the function
    Step Into

    View Slide

  21. legacy.joind.in/24817
    @SammyK #phpworld
    run to the end of the current function
    Step Out

    View Slide

  22. legacy.joind.in/24817
    @SammyK #phpworld
    shows the execution path to the point where the code was paused
    Frames
    I… don’t have a cool drawing for “frames”…

    View Slide

  23. legacy.joind.in/24817
    @SammyK #phpworld
    &
    Environment
    Setup

    View Slide

  24. legacy.joind.in/24817
    @SammyK #phpworld

    View Slide

  25. legacy.joind.in/24817
    @SammyK #phpworld
    https://xdebug.org/docs/remote
    Debug client (DBGp protocol)

    View Slide

  26. legacy.joind.in/24817
    @SammyK #phpworld
    Setup

    View Slide

  27. #1
    Start listening to
    debugging connections

    View Slide

  28. #2
    Set a
    Breakpoint

    View Slide

  29. legacy.joind.in/24817
    @SammyK #phpworld
    Done!

    View Slide

  30. legacy.joind.in/24817
    @SammyK #phpworld
    Setup

    View Slide

  31. configured
    installed
    installed

    View Slide

  32. on
    Homebrew
    Install +
    $ brew install php
    $ pecl install xdebug

    View Slide

  33. on
    APT
    Install +
    $ add-apt-repository -y \
    ppa:ondrej/php
    $ apt-get update
    $ apt-get install php7.2-xdebug

    View Slide

  34. https://xdebug.org/docs/install
    Install +

    View Slide

  35. View Slide

  36. legacy.joind.in/24817
    @SammyK #phpworld
    Configure

    View Slide

  37. $ php --ini

    View Slide

  38. $ php --ini

    View Slide

  39. $ sudo vi /usr/local/etc/php/
    7.2/conf.d/ext-xdebug.ini

    View Slide

  40. $ php -i | grep xdebug

    View Slide

  41. legacy.joind.in/24817
    @SammyK #phpworld
    Start a session
    or
    CLI

    View Slide

  42. legacy.joind.in/24817
    @SammyK #phpworld
    Query Param
    XDEBUG_SESSION_START

    View Slide

  43. legacy.joind.in/24817
    @SammyK #phpworld
    Cookie
    XDEBUG_SESSION

    View Slide

  44. legacy.joind.in/24817
    @SammyK #phpworld

    View Slide

  45. legacy.joind.in/24817
    @SammyK #phpworld

    View Slide

  46. legacy.joind.in/24817
    @SammyK #phpworld
    Env Variable
    export XDEBUG_CONFIG="idekey=foo"
    CLI

    View Slide

  47. legacy.joind.in/24817
    @SammyK #phpworld
    Env Variable
    set XDEBUG_CONFIG="idekey=foo"
    CLI

    View Slide

  48. legacy.joind.in/24817
    @SammyK #phpworld
    Don’t forget to unset
    set XDEBUG_CONFIG=
    unset XDEBUG_CONFIG
    when you’re done

    View Slide

  49. legacy.joind.in/24817
    @SammyK #phpworld
    alias debug-on='export
    XDEBUG_CONFIG="ide_key=foo"'
    alias debug-off='unset
    XDEBUG_CONFIG'
    Bash alias

    View Slide

  50. legacy.joind.in/24817
    @SammyK #phpworld
    Run some code!
    &
    CLI

    View Slide

  51. legacy.joind.in/24817
    @SammyK #phpworld
    PHP’s Built-in
    Web Server
    run locally using

    View Slide

  52. legacy.joind.in/24817
    @SammyK #phpworld
    Built right into the CLI SAPI!
    $ php -S 127.0.0.1:8888

    View Slide

  53. legacy.joind.in/24817
    @SammyK #phpworld

    View Slide

  54. legacy.joind.in/24817
    @SammyK #phpworld
    learn?
    What did we

    View Slide

  55. legacy.joind.in/24817
    @SammyK #phpworld
    +

    View Slide

  56. legacy.joind.in/24817
    @SammyK #phpworld

    View Slide

  57. legacy.joind.in/24817
    @SammyK #phpworld

    View Slide

  58. legacy.joind.in/24817
    @SammyK #phpworld
    CLI

    View Slide

  59. legacy.joind.in/24817
    @SammyK #phpworld
    Debugging In Action

    View Slide

  60. legacy.joind.in/24817
    @SammyK #phpworld

    View Slide

  61. legacy.joind.in/24817
    @SammyK #phpworld
    In production

    View Slide

  62. But wait!
    * (see me after class for more info)

    View Slide

  63. Sammy Kaye Powers
    Thanks!
    /24817
    @SammyK
    SammyK.me
    Host of @PHPRoundtable
    legacy.

    View Slide