Difference between revisions of "Python Debugger"

From CDOT Wiki
Jump to: navigation, search
(Debugger Commands)
Line 38: Line 38:
 
(Pdb)
 
(Pdb)
 
</source>
 
</source>
 +
= Online help on Debugger commands =
 
There are quite a bit of commands to get familiar with. However, we only need to learn a few to get started. Here are the ones that you should know: l, ll, n, s, and b
 
There are quite a bit of commands to get familiar with. However, we only need to learn a few to get started. Here are the ones that you should know: l, ll, n, s, and b
  

Revision as of 00:24, 3 November 2020

The Python module pdb defines an interactive source code debugger for Python programs. It supports the following features:

  • setting breakpoints,
  • single stepping at the source code line level,
  • inspection of stack frames,
  • source code listing, and
  • evaluation of arbitrary Python code in the context of any stack frame.

You can use the pdb module to run your Python script interactive mode for debugging purpose, for example:

$ python3 -m pdb a1_template.py
>/home/rchan/ops435/a1/a1_template.py(2)<module>()
-> '''template for ops435 assignment 1 script
(Pdb)

Debugger Commands

Now you are in the Python debugging mode with the Python script "a1_template.py" loaded. The (Pdb) prompt shown at the beginning of the line indicates that the Python interpreter is running under the debugging mode. You can type the word "help" without open or close bracket to get a list of debugging commands:

(Pdb) help
(Pdb) help

Documented commands (type help <topic>):
<source>
========================================
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)

Online help on Debugger commands

There are quite a bit of commands to get familiar with. However, we only need to learn a few to get started. Here are the ones that you should know: l, ll, n, s, and b

(Pdb) help l
l(ist) [first [,last] | .]

        List source code for the current file.  Without arguments,
        list 11 lines around the current line or continue the previous
        listing.  With . as argument, list 11 lines around the current
        line.  With one argument, list 11 lines starting at that line.
        With two arguments, list the given range; if the second
        argument is less than the first, it is a count.

        The current line in the current frame is indicated by "->".
        If an exception is being debugged, the line where the
        exception was originally raised or propagated is indicated by
        ">>", if it differs from the current line.
(Pdb) help ll
longlist | ll
        List the whole source code for the current function or frame.
(Pdb) help n
n(ext)
        Continue execution until the next line in the current function
        is reached or it returns.
(Pdb) help s
s(tep)
        Execute the current line, stop at the first possible occasion
        (either in a function that is called or in the current
        function).
(Pdb) help b
b(reak) [ ([filename:]lineno | function) [, condition] ]
        Without argument, list all breaks.

        With a line number argument, set a break at this line in the
        current file.  With a function name, set a break at the first
        executable line of that function.  If a second argument is
        present, it is a string specifying an expression which must
        evaluate to true before the breakpoint is honored.

        The line number may be prefixed with a filename and a colon,
        to specify a breakpoint in another file (probably one that
        hasn't been loaded yet).  The file is searched for on
        sys.path; the .py suffix may be omitted.
(Pdb)