w3resource

What are the basic pdb commands for debugging?

A Comprehensive Guide to Basic 'pdb' Commands for Python Debugging

'pdb' (Python Debugger) provides commands to interactively debug Python code. These commands allow you to inspect variables, step through code, set breakpoints, and more. Here are some of the basic 'pdb' commands and their explanations:

n (next):

  • Execute the current line and stop at the next line in the code.
  • Usage: 'n' or 'next'

s (step into):

  • Execute the current line and stop at the first possible occasion (e.g., the first line of a called function).
  • Usage: 's' or 'step'

c (continue):

  • Continue execution until the next breakpoint is encountered or the program finishes.
  • Usage: 'c' or 'continue'

r (return):

  • Continue execution until the current function returns.
  • Usage: 'r' or 'return'

l (list):

  • Show the source code around the current line.
  • Usage: 'l' or 'list'

p (print):

  • Print the value of a variable or an expression.
  • Usage: 'p variable_name' or 'p expression'

q (quit):

  • Exit the debugger and terminate the program.
  • Usage: 'q' or 'quit'

b (break):

  • Set a breakpoint at a specific line number or function.
  • Usage: 'b' line_number or 'b' function_name

cl (clear):

  • Clear a previously set breakpoint.
  • Usage: 'cl' breakpoint_number

h (help):

  • Display a list of available 'pdb' commands and their descriptions.
  • Usage: 'h' or 'help'

w (where):

  • Print a stack trace with the current line and the chain of function calls.
  • Usage: 'w' or 'where'

u (up):

  • Move the current frame (context) one level up in the stack trace.
  • Usage: 'u' or 'up'

d (down):

  • Move the current frame (context) one level down in the stack trace.
  • Usage: 'd' or 'down'

a (args):

  • Print the current function arguments.
  • Usage: 'a' or 'args'

Example:

Code:

# test.py
def divide(a, b):
    result = a / b
    return result
x = 9
y = 0
z = divide(x, y)
print(z)

Run in the Anaconda open terminal:

(base) C:\Users\ME>python -m pdb test.py
> c:\users\me\test.py(3)<module>()
-> def divide(a, b):
(Pdb) n
> c:\users\me\test.py(6)<module>()
-> x = 9
(Pdb) s
> c:\users\me\test.py(7)<module>()
-> y = 0
(Pdb) c

Traceback (most recent call last):

  File "h:\anaconda3\lib\pdb.py", line 1726, in main
    pdb._runscript(mainpyfile)
  File "h:\anaconda3\lib\pdb.py", line 1586, in _runscript
    self.run(statement)
  File "h:\anaconda3\lib\bdb.py", line 580, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "c:\users\me\test.py", line 7, in <module>
    y = 0
  File "c:\users\me\test.py", line 4, in divide
    result = a / b
ZeroDivisionError: division by zero
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> c:\users\me\test.py(4)divide()
-> result = a / b
(Pdb) r
Post mortem debugger finished. The C:\Users\ME\test.py will be restarted
> c:\users\me\test.py(3)<module>()
-> def divide(a, b):
(Pdb) l
  1     # test.py
  2     #import pdb
  3  -> def divide(a, b):
  4         result = a / b
  5         return result
  6     x = 9
  7     y = 0
  8     # Set a breakpoint here
  9     #pdb.set_trace()
 10     z = divide(x, y)
 11     print(z)
(Pdb) p
*** SyntaxError: unexpected EOF while parsing
(Pdb) b
(Pdb) h

Documented commands (type help <topic>):
========================================
EOF    c          d        h         list      q        rv       undisplay
a      cl         debug    help      ll        quit     s        unt
alias  clear      disable  ignore    longlist  r        source   until
args   commands   display  interact  n         restart  step     up
b      condition  down     j         next      return   tbreak   w
break  cont       enable   jump      p         retval   u        whatis
bt     continue   exit     l         pp        run      unalias  where

Miscellaneous help topics:
==========================
exec  pdb

(Pdb) w
  h:\anaconda3\lib\bdb.py(580)run()
-> exec(cmd, globals, locals)
  <string>(1)<module>()
> c:\users\me\test.py(3)<module>()
-> def divide(a, b):
(Pdb) u
> <string>(1)<module>()
(Pdb) d
> c:\users\me\test.py(3)<module>()
-> def divide(a, b):
(Pdb) a
(Pdb) q
(base) C:\Users\ME>


Follow us on Facebook and Twitter for latest update.