27.3. "pdb" --- Python的调试器
******************************

**源代码：** Lib/pdb.py

======================================================================

"pdb" 模块定义了一个交互式源代码调试器，用于 Python 程序。它支持在源码
行间设置（有条件的）断点和单步执行，检视堆栈帧，列出源码列表，以及在任
何堆栈帧的上下文中运行任意 Python 代码。它还支持事后调试，可以在程序控
制下调用。

调试器是可扩展的——调试器实际被定义为 "Pdb" 类。该类目前没有文档，但通
过阅读源码很容易理解它。扩展接口使用了 "bdb" 和 "cmd" 模块。

调试器的提示符是 "(Pdb)"。在调试器的控制下运行程序的典型用法是:

   >>> import pdb
   >>> import mymodule
   >>> pdb.run('mymodule.test()')
   > <string>(0)?()
   (Pdb) continue
   > <string>(1)?()
   (Pdb) continue
   NameError: 'spam'
   > <string>(1)?()
   (Pdb)

3.3 版更變: 由 "readline" 模块实现的 Tab 补全可用于补全本模块的命令和
命令的参数，例如，"p" 命令的参数可补全为当前的全局变量和局部变量。

也可以将 "pdb.py" 作为脚本调用来调试其他脚本。例如:

   python3 -m pdb myscript.py

当作为脚本调用时，如果要调试的程序异常退出，pdb 调试将自动进入事后调试
。事后调试之后（或程序正常退出之后），pdb 将重新启动程序。自动重启会保
留 pdb 的状态（如断点），在大多数情况下，这比在退出程序的同时退出调试
器更加实用。

3.2 版新加入: "pdb.py" 现在接受 "-c" 选项，可以执行命令，这与将该命令
写入 ".pdbrc" 文件相同，请参阅 调试器命令。

从正在运行的程序进入调试器的典型用法是插入

   import pdb; pdb.set_trace()

到你想进入调试器的位置。然后就可以单步执行上述语句之后的代码，要关闭调
试器继续运行，请使用 "continue" 命令。

检查已崩溃程序的典型用法是:

   >>> import pdb
   >>> import mymodule
   >>> mymodule.test()
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "./mymodule.py", line 4, in test
       test2()
     File "./mymodule.py", line 3, in test2
       print(spam)
   NameError: spam
   >>> pdb.pm()
   > ./mymodule.py(3)test2()
   -> print(spam)
   (Pdb)

本模块定义了下列函数，每个函数进入调试器的方式略有不同：

pdb.run(statement, globals=None, locals=None)

   在调试器控制范围内执行 *statement* （以字符串或代码对象的形式提供）
   。调试器提示符会在执行代码前出现，你可以设置断点并键入 "continue"，
   也可以使用 "step" 或 "next" 逐步执行语句（上述所有命令在后文有说明
   ）。可选参数 *globals* 和 *locals* 指定代码执行环境，默认时使用
   "__main__" 模块的字典。（请参阅内置函数 "exec()" 或 "eval()" 的说明
   。）

pdb.runeval(expression, globals=None, locals=None)

   在调试器控制范围内执行 *expression* （以字符串或代码对象的形式提供
   ）。"runeval()" 返回时将返回表达式的值。本函数在其他方面与 "run()"
   类似。

pdb.runcall(function, *args, **kwds)

   使用给定的参数调用 *function* （以函数或方法对象的形式提供，不能是
   字符串）。"runcall()" 返回的是所调用函数的返回值。调试器提示符将在
   进入函数后立即出现。

pdb.set_trace()

   Enter the debugger at the calling stack frame.  This is useful to
   hard-code a breakpoint at a given point in a program, even if the
   code is not otherwise being debugged (e.g. when an assertion
   fails).

pdb.post_mortem(traceback=None)

   进入 *traceback* 对象的事后调试。如果没有给定 *traceback*，默认使用
   当前正在处理的异常之一（默认时，必须存在正在处理的异常）。

pdb.pm()

   在 "sys.last_traceback" 中查找 traceback，并进入其事后调试。

"run*" 函数和 "set_trace()" 都是别名，用于实例化 "Pdb" 类和调用同名方
法。如果要使用其他功能，则必须自己执行以下操作：

class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=False, readrc=True)

   "Pdb" 是调试器类。

   *completekey*、*stdin* 和 *stdout* 参数都会传递给底层的 "cmd.Cmd"
   类，请参考相应的描述。

   如果给出 *skip* 参数，则它必须是一个迭代器，可以迭代出 glob-style
   样式的模块名称。如果遇到匹配上述样式的模块，调试器将不会进入来自该
   模块的堆栈帧。 [1]

   默认情况下，当发出 "continue" 命令时，Pdb 将为 SIGINT 信号设置一个
   处理程序（SIGINT 信号是用户在控制台按 "Ctrl-C" 时发出的）。这使用户
   可以按 "Ctrl-C" 再次进入调试器。如果希望 Pdb 不要修改 SIGINT 处理程
   序，请将 *nosigint* 设置为 true。

   *readrc* 参数默认为 true，它控制 Pdb 是否从文件系统加载 .pdbrc 文件
   。

   启用跟踪且带有 *skip* 参数的调用示范:

      import pdb; pdb.Pdb(skip=['django.*']).set_trace()

   3.1 版新加入: *skip* 参数。

   3.2 版新加入: *nosigint* 参数。在这之前，Pdb 不为 SIGINT 设置处理程
   序。

   3.6 版更變: *readrc* 参数。

   run(statement, globals=None, locals=None)
   runeval(expression, globals=None, locals=None)
   runcall(function, *args, **kwds)
   set_trace()

      请参阅上文解释同名函数的文档。


27.3.1. 调试器命令
==================

下方列出的是调试器可接受的命令。如下所示，大多数命令可以缩写为一个或两
个字母。如 "h(elp)" 表示可以输入 "h" 或 "help" 来输入帮助命令（但不能
输入 "he" 或 "hel"，也不能是 "H" 或 "Help" 或 "HELP"）。命令的参数必须
用空格（空格符或制表符）分隔。在命令语法中，可选参数括在方括号 ("[]")
中，使用时请勿输入方括号。命令语法中的选择项由竖线 ("|") 分隔。

输入一个空白行将重复最后输入的命令。例外：如果最后一个命令是 "list" 命
令，则会列出接下来的 11 行。

调试器无法识别的命令将被认为是 Python 语句，并在正在调试的程序的上下文
中执行。Python 语句也可以用感叹号 ("!") 作为前缀。这是检查正在调试的程
序的强大方法，甚至可以修改变量或调用函数。当此类语句发生异常，将打印异
常名称，但调试器的状态不会改变。

调试器支持 别名。别名可以有参数，使得调试器对被检查的上下文有一定程度
的适应性。

在一行中可以输入多个命令，以 ";;" 分隔。（不能使用单个 ";"，因为它用于
分隔传递给 Python 解释器的一行中的多条语句。）切分命令很无脑，总是在第
一个 ";;" 对处将输入切分开，即使它位于带引号的字符串之中。

如果文件 ".pdbrc" 存在于用户主目录或当前目录中，则将其读入并执行，等同
于在调试器提示符下键入该文件。这对于别名很有用。若两个文件都存在则首先
读取主目录中的文件，且本地文件可以覆盖其中定义的别名。

3.2 版更變: ".pdbrc" 现在可以包含继续调试的命令，如 "continue" 或
"next"。文件中的这些命令以前是无效的。

h(elp) [command]

   不带参数时，显示可用的命令列表。参数为 *command* 时，打印有关该命令
   的帮助。"help pdb" 显示完整文档（即 "pdb" 模块的文档字符串）。由于
   *command* 参数必须是标识符，因此要获取 "!" 的帮助必须输入 "help
   exec"。

w(here)

   打印堆栈回溯，最新一帧在底部。有一个箭头指向当前帧，该帧决定了大多
   数命令的上下文。

d(own) [count]

   在堆栈回溯中，将当前帧向下移动 *count* 级（默认为 1 级，移向更新的
   帧）。

u(p) [count]

   在堆栈回溯中，将当前帧向上移动 *count* 级（默认为 1 级，移向更老的
   帧）。

b(reak) [([filename:]lineno | function) [, condition]]

   如果带有 *lineno* 参数，则在当前文件相应行处设置一个断点。如果带有
   *function* 参数，则在该函数的第一条可执行语句处设置一个断点。行号可
   以加上文件名和冒号作为前缀，以在另一个文件（可能是尚未加载的文件）
   中设置一个断点。另一个文件将在 "sys.path" 范围内搜索。请注意，每个
   断点都分配有一个编号，其他所有断点命令都引用该编号。

   如果第二个参数存在，它应该是一个表达式，且它的计算值为 true 时断点
   才起作用。

   如果不带参数执行，将列出所有中断，包括每个断点、命中该断点的次数、
   当前的忽略次数以及关联的条件（如果有）。

tbreak [([filename:]lineno | function) [, condition]]

   临时断点，在第一次命中时会自动删除。它的参数与 "break" 相同。

cl(ear) [filename:lineno | bpnumber [bpnumber ...]]

   如果参数是 *filename:lineno*，则清除此行上的所有断点。如果参数是空
   格分隔的断点编号列表，则清除这些断点。如果不带参数，则清除所有断点
   （但会先提示确认）。

disable [bpnumber [bpnumber ...]]

   禁用断点，断点以空格分隔的断点编号列表给出。禁用断点表示它不会导致
   程序停止执行，但是与清除断点不同，禁用的断点将保留在断点列表中并且
   可以（重新）启用。

enable [bpnumber [bpnumber ...]]

   启用指定的断点。

ignore bpnumber [count]

   为指定的断点编号设置忽略次数。如果省略 count，则忽略次数将设置为 0
   。忽略次数为 0 时断点将变为活动状态。如果为非零值，在每次达到断点，
   且断点未禁用，且关联条件计算值为 true 的情况下，该忽略次数会递减。

condition bpnumber [condition]

   Set a new *condition* for the breakpoint, an expression which must
   evaluate to true before the breakpoint is honored.  If *condition*
   is absent, any existing condition is removed; i.e., the breakpoint
   is made unconditional.

commands [bpnumber]

   Specify a list of commands for breakpoint number *bpnumber*.  The
   commands themselves appear on the following lines.  Type a line
   containing just "end" to terminate the commands. An example:

      (Pdb) commands 1
      (com) p some_variable
      (com) end
      (Pdb)

   To remove all commands from a breakpoint, type commands and follow
   it immediately with "end"; that is, give no commands.

   With no *bpnumber* argument, commands refers to the last breakpoint
   set.

   You can use breakpoint commands to start your program up again.
   Simply use the continue command, or step, or any other command that
   resumes execution.

   Specifying any command resuming execution (currently continue,
   step, next, return, jump, quit and their abbreviations) terminates
   the command list (as if that command was immediately followed by
   end). This is because any time you resume execution (even with a
   simple next or step), you may encounter another breakpoint—which
   could have its own command list, leading to ambiguities about which
   list to execute.

   If you use the 'silent' command in the command list, the usual
   message about stopping at a breakpoint is not printed.  This may be
   desirable for breakpoints that are to print a specific message and
   then continue.  If none of the other commands print anything, you
   see no sign that the breakpoint was reached.

s(tep)

   Execute the current line, stop at the first possible occasion
   (either in a function that is called or on the next line in the
   current function).

n(ext)

   Continue execution until the next line in the current function is
   reached or it returns.  (The difference between "next" and "step"
   is that "step" stops inside a called function, while "next"
   executes called functions at (nearly) full speed, only stopping at
   the next line in the current function.)

unt(il) [lineno]

   Without argument, continue execution until the line with a number
   greater than the current one is reached.

   With a line number, continue execution until a line with a number
   greater or equal to that is reached.  In both cases, also stop when
   the current frame returns.

   3.2 版更變: Allow giving an explicit line number.

r(eturn)

   Continue execution until the current function returns.

c(ont(inue))

   Continue execution, only stop when a breakpoint is encountered.

j(ump) lineno

   Set the next line that will be executed.  Only available in the
   bottom-most frame.  This lets you jump back and execute code again,
   or jump forward to skip code that you don't want to run.

   It should be noted that not all jumps are allowed -- for instance
   it is not possible to jump into the middle of a "for" loop or out
   of a "finally" clause.

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 around at that line.  With two
   arguments, list the given range; if the second argument is less
   than the first, it is interpreted as 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.

   3.2 版新加入: The ">>" marker.

ll | longlist

   List all source code for the current function or frame.
   Interesting lines are marked as for "list".

   3.2 版新加入.

a(rgs)

   Print the argument list of the current function.

p expression

   Evaluate the *expression* in the current context and print its
   value.

   備註:

     "print()" can also be used, but is not a debugger command ---
     this executes the Python "print()" function.

pp expression

   Like the "p" command, except the value of the expression is pretty-
   printed using the "pprint" module.

whatis expression

   Print the type of the *expression*.

source expression

   Try to get source code for the given object and display it.

   3.2 版新加入.

display [expression]

   Display the value of the expression if it changed, each time
   execution stops in the current frame.

   Without expression, list all display expressions for the current
   frame.

   3.2 版新加入.

undisplay [expression]

   Do not display the expression any more in the current frame.
   Without expression, clear all display expressions for the current
   frame.

   3.2 版新加入.

interact

   Start an interactive interpreter (using the "code" module) whose
   global namespace contains all the (global and local) names found in
   the current scope.

   3.2 版新加入.

alias [name [command]]

   Create an alias called *name* that executes *command*.  The command
   must *not* be enclosed in quotes.  Replaceable parameters can be
   indicated by "%1", "%2", and so on, while "%*" is replaced by all
   the parameters. If no command is given, the current alias for
   *name* is shown. If no arguments are given, all aliases are listed.

   Aliases may be nested and can contain anything that can be legally
   typed at the pdb prompt.  Note that internal pdb commands *can* be
   overridden by aliases.  Such a command is then hidden until the
   alias is removed.  Aliasing is recursively applied to the first
   word of the command line; all other words in the line are left
   alone.

   As an example, here are two useful aliases (especially when placed
   in the ".pdbrc" file):

      # Print instance variables (usage "pi classInst")
      alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k])
      # Print instance variables in self
      alias ps pi self

unalias name

   Delete the specified alias.

! statement

   Execute the (one-line) *statement* in the context of the current
   stack frame. The exclamation point can be omitted unless the first
   word of the statement resembles a debugger command.  To set a
   global variable, you can prefix the assignment command with a
   "global" statement on the same line, e.g.:

      (Pdb) global list_options; list_options = ['-l']
      (Pdb)

run [args ...]
restart [args ...]

   Restart the debugged Python program.  If an argument is supplied,
   it is split with "shlex" and the result is used as the new
   "sys.argv". History, breakpoints, actions and debugger options are
   preserved. "restart" is an alias for "run".

q(uit)

   Quit from the debugger.  The program being executed is aborted.

-[ 註解 ]-

[1] Whether a frame is considered to originate in a certain module is
    determined by the "__name__" in the frame globals.
