Source From Here
Preface
The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.
The debugger is extensible — it is actually defined as the class Pdb. This is currently undocumented but easily understood by reading the source. The extension interface uses the modules bdb and cmd.
The debugger’s prompt is (Pdb). Typical usage to run a program under control of the debugger is:
pdb.py can also be invoked as a script to debug other scripts. For example, if we want to test Mock.py
- Mock.py
Then we can use below command to trigger pdb while exception occurs:
When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. After post-mortem debugging (or after normal exit of the program), pdb will restart the program. Automatic restarting preserves pdb’s state (such as breakpoints) and in most cases is more useful than quitting the debugger upon program’s exit.
The typical usage to break into the debugger from a running program is to insert:
at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the c command.
The typical usage to inspect a crashed program is:
The module defines the following functions; each enters the debugger in a slightly different way:
- pdb.run(statement[, globals[, locals]])
- pdb.runeval(expression[, globals[, locals]])
- pdb.runcall(function[, argument, ...])
- pdb.set_trace()
- pdb.post_mortem([traceback])
- pdb.pm()
The run* functions and set_trace() are aliases for instantiating the Pdb class and calling the method of the same name. If you want to access further features, you have to do this yourself:
- class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None)
Debugger Commands
The debugger recognizes the following commands. Most commands can be abbreviated to one or two letters; e.g. h(elp) means that either h or help can be used to enter the help command (but not he or hel, nor H or Help or HELP). Arguments to commands must be separated by whitespace (spaces or tabs). Optional arguments are enclosed in square brackets ([]) in the command syntax; the square brackets must not be typed. Alternatives in the command syntax are separated by a vertical bar (|).
Entering a blank line repeats the last command entered. Exception: if the last command was a list command, the next 11 lines are listed.
Commands that the debugger doesn’t recognize are assumed to be Python statements and are executed in the context of the program being debugged. Python statements can also be prefixed with an exclamation point (!). This is a powerful way to inspect the program being debugged; it is even possible to change a variable or call a function. When an exception occurs in such a statement, the exception name is printed but the debugger’s state is not changed.
Multiple commands may be entered on a single line, separated by ;;. (A single ; is not used as it is the separator for multiple commands in a line that is passed to the Python parser.) No intelligence is applied to separating the commands; the input is split at the first ;; pair, even if it is in the middle of a quoted string.
The debugger supports aliases. Aliases can have parameters which allows one a certain level of adaptability to the context under examination. For more alias usage, you can refer to Customizing the Debugger with Aliases.
If a file .pdbrc exists in the user’s home directory or in the current directory, it is read in and executed as if it had been typed at the debugger prompt. This is particularly useful for aliases. If both files exist, the one in the home directory is read first and aliases defined there can be overridden by the local file.
- h(elp) [command]
- w(here)
- d(own)
- u(p)
- b(reak) [[filename:]lineno | function[, condition]]
- tbreak [[filename:]lineno | function[, condition]]
- cl(ear) [filename:lineno | bpnumber [bpnumber ...]]
- disable [bpnumber [bpnumber ...]]
- enable [bpnumber [bpnumber ...]]
- ignore bpnumber [count]
- condition bpnumber [condition]
- commands [bpnumber]
- s(tep)
- n(ext)
- unt(il)
- r(eturn)
- c(ont(inue))
- j(ump) lineno
- l(ist) [first[, last]]
- a(rgs)
- p expression
- pp expression
- alias [name [command]]
- unalias name
- [!]statement
- run [args ...]
- q(uit)
Supplement
* pdb – Interactive Debugger
Preface
The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.
The debugger is extensible — it is actually defined as the class Pdb. This is currently undocumented but easily understood by reading the source. The extension interface uses the modules bdb and cmd.
The debugger’s prompt is (Pdb). Typical usage to run a program under control of the debugger is:
pdb.py can also be invoked as a script to debug other scripts. For example, if we want to test Mock.py
- Mock.py
- #!/usr/bin/env python
- import pdb, os
- import unittest
- class John:
- def __init__(self):
- self.name = "John"
- def test(self):
- raise Exception("FET", "Test")
- if (__name__ == '__main__'):
- j = John()
- j.test()
When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. After post-mortem debugging (or after normal exit of the program), pdb will restart the program. Automatic restarting preserves pdb’s state (such as breakpoints) and in most cases is more useful than quitting the debugger upon program’s exit.
The typical usage to break into the debugger from a running program is to insert:
- import pdb; pdb.set_trace()
The typical usage to inspect a crashed program is:
The module defines the following functions; each enters the debugger in a slightly different way:
- pdb.run(statement[, globals[, locals]])
- pdb.runeval(expression[, globals[, locals]])
- pdb.runcall(function[, argument, ...])
- pdb.set_trace()
- pdb.post_mortem([traceback])
- pdb.pm()
The run* functions and set_trace() are aliases for instantiating the Pdb class and calling the method of the same name. If you want to access further features, you have to do this yourself:
- class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None)
Debugger Commands
The debugger recognizes the following commands. Most commands can be abbreviated to one or two letters; e.g. h(elp) means that either h or help can be used to enter the help command (but not he or hel, nor H or Help or HELP). Arguments to commands must be separated by whitespace (spaces or tabs). Optional arguments are enclosed in square brackets ([]) in the command syntax; the square brackets must not be typed. Alternatives in the command syntax are separated by a vertical bar (|).
Entering a blank line repeats the last command entered. Exception: if the last command was a list command, the next 11 lines are listed.
Commands that the debugger doesn’t recognize are assumed to be Python statements and are executed in the context of the program being debugged. Python statements can also be prefixed with an exclamation point (!). This is a powerful way to inspect the program being debugged; it is even possible to change a variable or call a function. When an exception occurs in such a statement, the exception name is printed but the debugger’s state is not changed.
Multiple commands may be entered on a single line, separated by ;;. (A single ; is not used as it is the separator for multiple commands in a line that is passed to the Python parser.) No intelligence is applied to separating the commands; the input is split at the first ;; pair, even if it is in the middle of a quoted string.
The debugger supports aliases. Aliases can have parameters which allows one a certain level of adaptability to the context under examination. For more alias usage, you can refer to Customizing the Debugger with Aliases.
If a file .pdbrc exists in the user’s home directory or in the current directory, it is read in and executed as if it had been typed at the debugger prompt. This is particularly useful for aliases. If both files exist, the one in the home directory is read first and aliases defined there can be overridden by the local file.
- h(elp) [command]
- w(here)
- d(own)
- u(p)
- b(reak) [[filename:]lineno | function[, condition]]
- tbreak [[filename:]lineno | function[, condition]]
- cl(ear) [filename:lineno | bpnumber [bpnumber ...]]
- disable [bpnumber [bpnumber ...]]
- enable [bpnumber [bpnumber ...]]
- ignore bpnumber [count]
- condition bpnumber [condition]
- commands [bpnumber]
- s(tep)
- n(ext)
- unt(il)
- r(eturn)
- c(ont(inue))
- j(ump) lineno
- l(ist) [first[, last]]
- a(rgs)
- p expression
- pp expression
- alias [name [command]]
- unalias name
- [!]statement
- run [args ...]
- q(uit)
Supplement
* pdb – Interactive Debugger
沒有留言:
張貼留言