$30 off During Our Annual Pro Sale. View Details »

The debug dance: An intro to step debugging - ZendCon & OpenEnterprise 2018

The debug dance: An intro to step debugging - ZendCon & OpenEnterprise 2018

Talk given at ZendCon & OpenEnterprise (2018-10-16)

Using var_dump() to debug your app has its benefits but there are more comprehensive and efficient ways to debug those particularly elusive bugs. Enter step debugging. We’ll be using 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

October 16, 2018
Tweet

More Decks by Sammy Kaye Powers

Other Decks in Programming

Transcript

  1. joind.in/talk/5fb02
    @SammyK #zcoe18
    Sammy Kaye Powers
    Dance
    The
    Las Vegas, NV - 2018
    An Intro To
    Step Debugging in

    View Slide

  2. joind.in/talk/5fb02
    @SammyK #zcoe18
    Download the
    slides
    joind.in/talk/5fb02

    View Slide

  3. joind.in/talk/5fb02
    @SammyK #zcoe18
    Does not work for

    View Slide

  4. joind.in/talk/5fb02
    @SammyK #zcoe18
    What is
    Debugging?
    Finding and fixing unexpected behavior in code

    View Slide

  5. So you write
    Some Code

    View Slide

  6. Does not work

    View Slide

  7. joind.in/talk/5fb02
    @SammyK #zcoe18
    You could check the
    Error logs

    View Slide

  8. joind.in/talk/5fb02
    @SammyK #zcoe18
    All the things
    var_dump()

    View Slide

  9. joind.in/talk/5fb02
    @SammyK #zcoe18

    View Slide

  10. joind.in/talk/5fb02
    @SammyK #zcoe18
    OR

    View Slide

  11. joind.in/talk/5fb02
    @SammyK #zcoe18
    Use a
    ‘er

    View Slide

  12. joind.in/talk/5fb02
    @SammyK #zcoe18
    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

  13. joind.in/talk/5fb02
    @SammyK #zcoe18
    …now is the perfect time to learn
    Step Debugging
    BNEW!
    Professional

    View Slide

  14. joind.in/talk/5fb02
    @SammyK #zcoe18
    Some step-debugging
    Jargon

    View Slide

  15. joind.in/talk/5fb02
    @SammyK #zcoe18
    a signal that tells the debugger to pause the execution of your code
    Breakpoint

    View Slide

  16. joind.in/talk/5fb02
    @SammyK #zcoe18
    run to the next breakpoint
    Resume Program

    View Slide

  17. joind.in/talk/5fb02
    @SammyK #zcoe18
    advance to the next line in the same scope
    Step Over

    View Slide

  18. joind.in/talk/5fb02
    @SammyK #zcoe18
    if the next line is a function call, enter the function
    Step Into

    View Slide

  19. joind.in/talk/5fb02
    @SammyK #zcoe18
    run to the end of the current function
    Step Out

    View Slide

  20. joind.in/talk/5fb02
    @SammyK #zcoe18
    shows the execution path to the point where the code was paused
    Frames
    I… don’t have a cool drawing for “frames”…

    View Slide

  21. joind.in/talk/5fb02
    @SammyK #zcoe18
    &
    Environment
    Setup

    View Slide

  22. joind.in/talk/5fb02
    @SammyK #zcoe18

    View Slide

  23. joind.in/talk/5fb02
    @SammyK #zcoe18
    https://xdebug.org/docs/remote
    Debug client (DBGp protocol)

    View Slide

  24. joind.in/talk/5fb02
    @SammyK #zcoe18
    Setup

    View Slide

  25. #1
    Start listening to
    debugging connections

    View Slide

  26. #2
    Set a
    Breakpoint

    View Slide

  27. joind.in/talk/5fb02
    @SammyK #zcoe18
    Done!

    View Slide

  28. joind.in/talk/5fb02
    @SammyK #zcoe18
    Setup

    View Slide

  29. configured
    installed
    installed

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  33. View Slide

  34. joind.in/talk/5fb02
    @SammyK #zcoe18
    Configure

    View Slide

  35. $ php --ini

    View Slide

  36. $ php --ini

    View Slide

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

    View Slide

  38. $ php -i | grep xdebug

    View Slide

  39. joind.in/talk/5fb02
    @SammyK #zcoe18
    Start a session
    or
    CLI

    View Slide

  40. joind.in/talk/5fb02
    @SammyK #zcoe18
    Query Param
    XDEBUG_SESSION_START

    View Slide

  41. joind.in/talk/5fb02
    @SammyK #zcoe18
    Cookie
    XDEBUG_SESSION

    View Slide

  42. joind.in/talk/5fb02
    @SammyK #zcoe18

    View Slide

  43. joind.in/talk/5fb02
    @SammyK #zcoe18

    View Slide

  44. joind.in/talk/5fb02
    @SammyK #zcoe18
    Env Variable
    export XDEBUG_CONFIG="idekey=foo"
    CLI

    View Slide

  45. joind.in/talk/5fb02
    @SammyK #zcoe18
    Env Variable
    set XDEBUG_CONFIG="idekey=foo"
    CLI

    View Slide

  46. joind.in/talk/5fb02
    @SammyK #zcoe18
    Don’t forget to unset
    set XDEBUG_CONFIG=
    unset XDEBUG_CONFIG
    when you’re done

    View Slide

  47. joind.in/talk/5fb02
    @SammyK #zcoe18
    alias debug-on='export
    XDEBUG_CONFIG="ide_key=foo"'
    alias debug-off='unset
    XDEBUG_CONFIG'
    Bash alias

    View Slide

  48. joind.in/talk/5fb02
    @SammyK #zcoe18
    Run some code!
    &
    CLI

    View Slide

  49. joind.in/talk/5fb02
    @SammyK #zcoe18
    PHP’s Built-in
    Web Server
    run locally using

    View Slide

  50. joind.in/talk/5fb02
    @SammyK #zcoe18
    Built right into the CLI SAPI!
    $ php -S 127.0.0.1:8888

    View Slide

  51. joind.in/talk/5fb02
    @SammyK #zcoe18

    View Slide

  52. joind.in/talk/5fb02
    @SammyK #zcoe18
    learn?
    What did we

    View Slide

  53. joind.in/talk/5fb02
    @SammyK #zcoe18
    +

    View Slide

  54. joind.in/talk/5fb02
    @SammyK #zcoe18

    View Slide

  55. joind.in/talk/5fb02
    @SammyK #zcoe18

    View Slide

  56. joind.in/talk/5fb02
    @SammyK #zcoe18
    CLI

    View Slide

  57. joind.in/talk/5fb02
    @SammyK #zcoe18
    Debugging In Action

    View Slide

  58. joind.in/talk/5fb02
    @SammyK #zcoe18

    View Slide

  59. joind.in/talk/5fb02
    @SammyK #zcoe18
    In production

    View Slide

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

    View Slide

  61. Sammy Kaye Powers
    Thanks!
    /talk/5fb02
    @SammyK
    SammyK.me
    Host of @PHPRoundtable

    View Slide