Skip to content

Instantly share code, notes, and snippets.

@dgkim5360
Created October 13, 2017 07:52
Show Gist options
  • Save dgkim5360/f7df20691000329de2dc97dafda3c782 to your computer and use it in GitHub Desktop.
Save dgkim5360/f7df20691000329de2dc97dafda3c782 to your computer and use it in GitHub Desktop.
DIY stuffs for the first steps of Python pdb, summarized from https://pymotw.com/3/pdb

Starting the Debugger

From the command line

$ python3 -m pdb myscript.py  

Within the interpreter

$ python3
>>> import pdb
>>> import myscript
>>> pdb.run('myscript.MyObj(5).mymethod()')

From within a program

Add pdb.set_trace() anywhere you want to start debugging and then run it.

$ python3 myscript.py

After a failure

Run pdb.pm() after an error raised

Controlling the Debugger

Navigating the execution stack

  • where or w: shows the current line and tree
  • list or l: shows more context around the current location
  • longlist or ll: shows the entire function or frame which contains the current location
  • source OBJ: shows the source of OBJ
  • up and down: moves between frames towards older and newer stack, respectively

Examining variables on the stack

  • Just type existing variables to check it out.
  • print(VAR) or p VAR: print a variable VAR
  • prettyprint or pp: pretty print
  • !{expression}: run expression within the interpreter (it can change the value of a variable)
  • interact: dive into "interactive mode" and <CTRL-D> for exiting the interactive prompt

Stepping through a program

  • step: execute the line and step into if there is, otherwise move next
  • next: execute the line and move next
  • until: execute until it progresses to the next line (e.g. run through a loop)
  • return: execute until the return line of a function

Breakpoints

  • break #LINENO: set a breakpoint to #LINENO line
  • continue or c: execute until the next breakpoint
  • break FILENAME:LINENO: set a breakpoint to LINENO of another FILENAME
  • break: list current breakpoints set

Managing breakpoints

  • break OBJNAME: set a breakpoint to OBJNAME
  • disable #BREAKPOINT: disable the breakpoint #BREAKPOINT
  • enable #BREAKPOINT: enable the breakpoint #BREAKPOINT
  • clear #BREAKPOINT: delete the breakpoint #BREAKPOINT

Temporary breakpoints

  • tbreak: it hits only once

Conditional breakpoints

  • break #LINENO, {conditional expression}
  • `condition #BREAKPOINT {conditional expression}': gives the condition to the specified breakpoint

Ignoring breakpoints

  • ignore #BREAKPOINT #HITNUMBER: the specified breakpoint will only hit #HITNUMBER times and then be ignored
  • ignore #BREAKPOINT 0: re-activate

Triggering actions on a breakpoint

  • command #BREAKPOINT: register a custom command for the specified breakpoint (debugger prompt changes from (Pdb) to (com)

Watching data change

  • display VAR
  • display: list current variables to be displayed
  • undisplay VAR

Changing Execution Flow

Jump ahead

  • jump #LINENO: skip to #LINENO line if #CURRENT < #LINENO, otherwise jump back

Restarting a program

$ python3 -m pdb myscript.py
# works are done
(pdb)
  • run ARGS: restart myscript.py with arguments ARGS(space-separated text)

Customizing the Debugger with Aliases

  • alias ALIAS COMMAND: ALIAS <- COMMAND
  • alias: list all aliases
  • alias ALIAS: show information of ALIAS
  • COMMAND can take numbered arguments as format %n, %* consumes all arguments
  • unalias ALIAS: delete the alias

Saving Configuration Settings

Write your .pdbrc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment