Byebug cheatsheet

Updated . Posted . Visible to the public.

Context and further resources

Even though you can get 90% of debugging done with up to 5 basic byebug commands, it comes in handy with it's features for many use cases beyond that to make your life easier.

For this cheatsheat I tried to structure the most useful commands by different use cases, such that a practical oriented overview of all the commands can be gathered by going over this cheatsheet. For some commands I added some tips for their usage and further details on their subcommands

Cheatsheet

General

Navigation within the executed program

Breakpoints

  • continue [n] -- Runs until program ends, hits a breakpoint or reaches line n
  • continue! - execute to end of program by ignoring all breakpoints
  • skip -- Runs until the next breakpoint as long as it is different from the current one

Lines

  • step [n] -- Steps into blocks or methods n times
  • next [n] -- Runs n lines of code within the same frame

Stacktrace

  • frame [n] -- Moves to a frame in the n-th call stack
    • You can use where to find out the numbers
  • finish [n] -- Runs the program until the n-th frame returns
  • up -- Moves to a higher frame in the stack trace
  • down -- Moves to a lower frame in the stack trace

Find out context for the current debugging session (or breakpoint)

  • where -- Displays the backtrace
  • list [[-=]] [ nn-mm] -- Lists coming lines of source code from last breakpoint
    • list- lists previous code lines
    • list= lists code from current line around that line
    • list d-f lists lines from d to f
  • info [subcommand] -- Shows several informations about the program and the current debugging session,
    [subcommand] can be any of:
    • breakpoints
    • display -- displayed variables
    • file, line, program
    • args of the current stack frame

# Show archive.org snapshot Find out variables and methods

  • var [type] [object] -- Shows specified variables and its values,
    [type] (and possibly [object]) can be used as:
    • const <object> -- listing variables and their values in <object>.constant
    • instance <object> -- listing <object>.instance_variables.
    • instance -- instance_variables of self.
    • local -- local variables.
    • global -- global variables
  • method -- shows variables of specified object, class or module
    • method instance <object> -- The same as running <object>.instance_methods(false).
    • method <class-or-module> -- The same as running <class-or-module>.methods.

Commands to change (or add) behavior at breakpoints

  • display / undisplay [<expression>] - display or undisplays an expression at stopped breakpoint
    • enable / disable [id] can be used activate and deactivate displayed values
    • info display can be used to show all id's of displayed variables
  • condition <i>[ <expression>] -- Sets conditions on breakpoint i to stop only when <expression> is true
    • info breakpoints can be used to show all id's of breakpoints
  • catch <exception> -- Catches the specified exception
    • You can use the subcommands to turn off catching all together or only for specific exceptions:
      • catch off
      • catch <exception> off
  • thread -- Commands to manipulate threads
    • If you ever have to deal with threads byebug supports stopping, switching, resuming and listing all current threadsstopped at the breakpoint

Commands add (or delete) breakpoint(s)

  • break [n] - Set breakpoint at line n in the current file
    • Can be used to add breakpoints at any file with break [<file>:]<line>
  • delete-- All breakpoints can be deleted by using the delete command
    • Use info breakpoints to find out the breakpoint's id

Shortcuts

While debugging you don't have to type out all commands presented above. For most of them it is enough to type the first letter, e.g.:

  • c instead of continue
    • You can also use c!
  • s instead of step
  • l= instead of list=
Felix Eschey
Last edit
Dominik Schöler
Keywords
navigate, debugger
License
Source code in this card is licensed under the MIT License.
Posted by Felix Eschey to makandra dev (2023-07-06 11:51)