"pdb" --- Python デバッガ
*************************

**ソースコード:** Lib/pdb.py

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

モジュール "pdb" は Python プログラム用の対話型ソースコードデバッガを
定義します。 (条件付き)ブレークポイントの設定やソース行レベルでのシン
グルステップ実行、スタックフレームのインスペクション、ソースコードリス
ティングおよびあらゆるスタックフレームのコンテキストにおける任意の
Python コードの評価をサポートしています。事後解析デバッギングもサポー
トし、プログラムの制御下で呼び出すことができます。

デバッガーは拡張可能です -- 実際にはクラス "Pdb" として定義されていま
す。現在これについてのドキュメントはありませんが、ソースを読めば簡単に
理解できます。拡張インターフェースはモジュール "bdb" と "cmd" を使って
います。

参考:

  "faulthandler" モジュール
     Used to dump Python tracebacks explicitly, on a fault, after a
     timeout, or on a user signal.

  "traceback" モジュール
     Standard interface to extract, format and print stack traces of
     Python programs.

The typical usage to break into the debugger is to insert:

   import pdb; pdb.set_trace()

Or:

   breakpoint()

at the location you want to break into the debugger, and then run the
program. You can then step through the code following this statement,
and continue running without the debugger using the "continue"
command.

バージョン 3.7 で変更: The built-in "breakpoint()", when called with
defaults, can be used instead of "import pdb; pdb.set_trace()".

   def double(x):
      breakpoint()
      return x * 2
   val = 3
   print(f"{val} * 2 is {double(val)}")

The debugger's prompt is "(Pdb)", which is the indicator that you are
in debug mode:

   > ...(3)double()
   -> return x * 2
   (Pdb) p x
   3
   (Pdb) continue
   3 * 2 is 6

バージョン 3.3 で変更: "readline" モジュールによるコマンドおよびコマン
ド引数のタブ補完が利用できます。たとえば、"p" コマンドの引数では現在の
グローバルおよびローカル名が候補として表示されます。

You can also invoke "pdb" from the command line to debug other
scripts.  For example:

   python -m pdb myscript.py

モジュールとして pdb を起動すると、デバッグ中のプログラムが異常終了し
たときに pdb が自動的に事後デバッグモードに入ります。事後デバッグ後 (
またはプログラムの正常終了後)、pdb はプログラムを再起動します。自動再
起動を行った場合、pdb の状態 (ブレークポイントなど) はそのまま維持され
るので、たいていの場合、プログラム終了時にデバッガーも終了させるよりも
便利なはずです。

バージョン 3.2 で変更: Added the "-c" option to execute commands as if
given in a ".pdbrc" file; see デバッガコマンド.

バージョン 3.7 で変更: Added the "-m" option to execute modules
similar to the way "python -m" does. As with a script, the debugger
will pause execution just before the first line of the module.

Typical usage to execute a statement under control of the debugger is:

   >>> import pdb
   >>> def f(x):
   ...     print(1 / x)
   >>> pdb.run("f(2)")
   > <string>(1)<module>()
   (Pdb) continue
   0.5
   >>>

クラッシュしたプログラムを調べるための典型的な使い方は以下のようになり
ます:

   >>> import pdb
   >>> def f(x):
   ...     print(1 / x)
   ...
   >>> f(0)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "<stdin>", line 2, in f
   ZeroDivisionError: division by zero
   >>> pdb.pm()
   > <stdin>(2)f()
   (Pdb) p x
   0
   (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(*, header=None)

   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).  If given, *header* is printed to the console just before
   debugging begins.

   バージョン 3.7 で変更: *header* キーワード専用引数。

pdb.post_mortem(traceback=None)

   与えられた *traceback* オブジェクトの事後解析デバッギングに入ります
   。もし *traceback* が与えられなければ、その時点で取り扱っている例外
   のうちのひとつを使います。 (デフォルト動作をさせるには、例外を取り
   扱っている最中である必要があります。)

pdb.pm()

   Enter post-mortem debugging of the traceback found in
   "sys.last_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 スタイルのモジュール名パターンの
   iterable (イテレート可能オブジェクト) でなければなりません。デバッ
   ガはこのパターンのどれかにマッチするモジュールに属するフレームには
   ステップインしません。 [1]

   デフォルトでは、Pdb は "continue" コマンドが投入されると、(ユーザー
   がコンソールから "Ctrl-C" を押したときに送られる) SIGINT シグナル用
   ハンドラーを設定します。これにより "Ctrl-C" を押すことで再度デバッ
   ガーを起動することができます。Pdb に SIGINT ハンドラーを変更させた
   くない場合は *nosigint* を true に設定してください。

   *readrc* 引数はデフォルトでは真で、 Pdb が .pdbrc ファイルをファイ
   ルシステムから読み込むかどうかを制御します。

   *skip* を使ってトレースする呼び出しの例:

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

   引数無しで 監査イベント "pdb.Pdb" を送出します。

   バージョン 3.1 で変更: *skip* パラメータが追加されました。

   バージョン 3.2 で変更: Added the *nosigint* parameter. Previously,
   a SIGINT handler was never set by Pdb.

   バージョン 3.6 で変更: *readrc* 引数。

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

      前述のこれら関数のドキュメントを参照してください。


デバッガコマンド
================

デバッガーに認識されるコマンドは以下に一覧されています。たいていのコマ
ンドは以下のように 1、2 文字に省略できます。例えば "h(elp)" は "h" か
"help" がで help コマンドを呼び出すことを意味します (ただし "he",
"hel", "H", "Help", "HELP" は使用できません)。コマンドの引数はホワイト
スペース (スペースかタブ) で区切ってください。コマンド構文として任意の
引数は大括弧 ("[]") で括られています (実際に大括弧はタイプしないでくだ
さい)。いくつかから選択できる引数は縦線 ("|") で分割されて記述されてい
ます。

空行を入力すると入力された直前のコマンドを繰り返します。例外: 直前のコ
マンドが "list" コマンドならば、次の 11 行がリストされます。

デバッガが認識しないコマンドは Python 文とみなして、デバッグしているプ
ログラムのコンテキストおいて実行されます。先頭にに感嘆符 ("!") を付け
ることで Python 文であると明示することもできます。これはデバッグ中のプ
ログラムを調査する強力な方法です。変数を変更したり関数を呼び出したりす
ることも可能です。このような文で例外が発生した場合には例外名が出力され
ますが、デバッガの状態は変化しません。

デバッガーは エイリアス をサポートしています。エイリアスはデバッグ中の
コンテキストに適用可能な一定レベルのパラメータを保持することができます
。

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. A
workaround for strings with double semicolons is to use implicit
string concatenation "';'';'" or "";"";"".

If a file ".pdbrc" exists in the user's home directory or in the
current directory, it is read with "'utf-8'" encoding and executed as
if it had been typed at the debugger prompt, with the exception that
empty lines and lines starting with "#" are ignored.  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.

バージョン 3.2 で変更: ".pdbrc" に "continue" や "next" のようなデバッ
グを続行するコマンドが使用できるようになりました。以前はこのようなコマ
ンドは無視されていました。

バージョン 3.11 で変更: ".pdbrc" is now read with "'utf-8'" encoding.
Previously, it was read with the system locale encoding.

h(elp) [command]

   引数を指定しない場合、利用できるコマンドの一覧が表示されます。引数
   として *command* が与えられた場合、そのコマンドのヘルプが表示されま
   す。"help pdb" で完全なドキュメント ("pdb" モジュールの doctring)
   が表示されます。*command* 引数は識別子でなければならないため、"!"
   コマンドのヘルプを表示するには "help exec" と入力します。

w(here)

   スタックの底にある最も新しいフレームと一緒にスタックトレースをプリ
   ントします。矢印 (">") はカレントフレームを指し、それがほとんどのコ
   マンドのコンテキストを決定します。

d(own) [count]

   スタックフレーム内で現在のフレームを *count* レベル (デフォルトは
   1) 新しいフレーム方向に移動します。

u(p) [count]

   スタックフレーム内で現在のフレームを *count* レベル (デフォルトは
   1) 古いフレーム方向に移動します。

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

   With a *lineno* argument, set a break there in the current file.
   With a *function* argument, set a break at the first executable
   statement within that function.  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 on "sys.path".  Note that each breakpoint is assigned a
   number to which all the other breakpoint commands refer.

   第二引数を指定する場合、その値は式で、その評価値が真でなければブレ
   ークポイントは有効になりません。

   引数なしの場合は、それぞれのブレークポイントに対して、そのブレーク
   ポイントに行き当たった回数、現在の通過カウント ( ignore count ) と
   、もしあれば関連条件を含めてすべてのブレークポイントをリストします
   。

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

   一時的なブレークポイントで、最初にそこに達したときに自動的に取り除
   かれます。引数は "break" と同じです。

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

   *filename:lineno* 引数を与えると、その行にある全てのブレークポイン
   トを解除します。スペースで区切られたブレークポイントナンバーのリス
   トを与えると、それらのブレークポイントを解除します。引数なしの場合
   は、すべてのブレークポイントを解除します ( が、はじめに確認します )
   。

disable [bpnumber ...]

   ブレークポイント番号 *bpnumber* のブレークポイントを無効にします。
   ブレークポイントを無効にすると、プログラムの実行を止めることができ
   なくなりますが、ブレークポイントの解除と違いブレークポイントのリス
   トに残っており、(再び) 有効にできます。

enable [bpnumber ...]

   指定したブレークポイントを有効にします。

ignore bpnumber [count]

   与えられたブレークポイントナンバーに通過カウントを設定します。count
   が省略されると、通過カウントは 0 に設定されます。通過カウントがゼロ
   になったとき、ブレークポイントが機能する状態になります。ゼロでない
   ときは、そのブレークポイントが無効にされず、どんな関連条件も真に評
   価されていて、ブレークポイントに来るたびに *count* が減らされます。

condition bpnumber [condition]

   ブレークポイントに新しい *condition* を設定します。*condition* はブ
   レークポイントを制御する条件式で、この式が真を返す場合のみブレーク
   ポイントが有効になります。*condition* を指定しないと既存の条件が除
   去されます; ブレークポイントは常に有効になります。

commands [bpnumber]

   ブレークポイントナンバー *bpnumber* にコマンドのリストを指定します
   。コマンドそのものはその後の行に続けます。"end" だけからなる行を入
   力することでコマンド群の終わりを示します。例を挙げます:

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

   ブレークポイントからコマンドを取り除くには、 "commands" のあとに
   "end" だけを続けます。つまり、コマンドを一つも指定しないようにしま
   す。

   *bpnumber* 引数を指定しない場合、"commands" は最後にセットしたブレ
   ークポイントを参照します。

   ブレークポイントコマンドはプログラムを走らせ直すのに使えます。単に
   "continue" コマンドや "step"、その他実行を再開するコマンドを使えば
   良いのです。

   実行を再開するコマンド (現在のところ "continue", "step", "next",
   "return", "jump", "quit" とそれらの省略形) によって、コマンドリスト
   は終了するものと見なされます (コマンドにすぐ end が続いているかのよ
   うに)。というのも実行を再開すれば (それが単純な next や step であっ
   ても) 別のブレークポイントに到達するかもしれないからです。そのブレ
   ークポイントにさらにコマンドリストがあれば、どちらのリストを実行す
   べきか状況が曖昧になります。

   コマンドリストの中で "silent" コマンドを使うと、ブレークポイントで
   停止したという通常のメッセージはプリントされません。この振る舞いは
   特定のメッセージを出して実行を続けるようなブレークポイントでは望ま
   しいものでしょう。他のコマンドが何も画面出力をしなければ、そのブレ
   ークポイントに到達したというサインを見ないことになります。

s(tep)

   現在の行を実行し、最初に実行可能なものがあらわれたときに (呼び出さ
   れた関数の中か、現在の関数の次の行で) 停止します。

n(ext)

   現在の関数の次の行に達するか、あるいは関数が返るまで実行を継続しま
   す。 ("next" と "step" の差は "step" が呼び出された関数の内部で停止
   するのに対し、 "next" は呼び出された関数を (ほぼ) 全速力で実行し、
   現在の関数内の次の行で停止するだけです。)

unt(il) [lineno]

   引数なしだと、現在の行から 1 行先まで実行します。

   *lineno* を指定すると、番号が *lineno* 以上である行に到達するまで実
   行します。どちらにしても現在のフレームが返ってきた時点で停止します
   。

   バージョン 3.2 で変更: 明示的に行数指定ができるようになりました。

r(eturn)

   現在の関数が返るまで実行を継続します。

c(ont(inue))

   ブレークポイントに出会うまで、実行を継続します。

j(ump) lineno

   次に実行する行を指定します。最も底のフレーム中でのみ実行可能です。
   前に戻って実行したり、不要な部分をスキップして先の処理を実行する場
   合に使用します。

   ジャンプには制限があり、例えば "for" ループの中には飛び込めませんし
   、 "finally" 節の外にも飛ぶ事ができません。

l(ist) [first[, last]]

   現在のファイルのソースコードを表示します。引数を指定しないと、現在
   の行の前後 11 行分を表示するか、直前の表示を続行します。引数に "."
   を指定すると、現在の行の前後 11 行分を表示します。数値を 1 個指定す
   ると、その行番号の前後 11 行分を表示します。数値を 2 個指定すると、
   開始行と最終行として表示します; 2 個めの引数が 1 個め未満だった場合
   、1 個目を開始行、2 個めを開始行からの行数とみなします。

   現在のフレーム内の現在の行は "->" で表示されます。例外をデバッグ中
   の場合、例外が発生または伝搬した行は、それが現在の行とは異なるとき
   ">>" で表示されます。

   バージョン 3.2 で変更: ">>" マーカーが追加されました。

ll | longlist

   現在の関数またはフレームの全ソースコードを表示します。注目する行は
   "list" と同じようにマーカーがつきます。

   バージョン 3.2 で追加.

a(rgs)

   Print the arguments of the current function and their current
   values.

p expression

   現在のコンテキストにおいて *expression* を評価し、その値をプリント
   します。

   注釈:

     "print()" も使えますが、これはデバッガーコマンドではありません
     --- これは Python の関数 "print()" が実行されます。

pp expression

   "p" コマンドに似ていますが、*expression* の値以外は "pprint" モジュ
   ールを使用して "pretty-print" されます。

whatis expression

   *expression* の型を表示します。

source expression

   *expression* のソースコードの取得を試み、可能であれば表示します。

   バージョン 3.2 で追加.

display [expression]

   *expression* の値が変更されていれば表示します。毎回実行は現在のフレ
   ームで停止します。

   *expression* を指定しない場合、現在のフレームのすべての式を表示しま
   す。

   注釈:

     Display evaluates *expression* and compares to the result of the
     previous evaluation of *expression*, so when the result is
     mutable, display may not be able to pick up the changes.

   以下はプログラム例です:

      lst = []
      breakpoint()
      pass
      lst.append(1)
      print(lst)

   Display won't realize "lst" has been changed because the result of
   evaluation is modified in place by "lst.append(1)" before being
   compared:

      > example.py(3)<module>()
      -> pass
      (Pdb) display lst
      display lst: []
      (Pdb) n
      > example.py(4)<module>()
      -> lst.append(1)
      (Pdb) n
      > example.py(5)<module>()
      -> print(lst)
      (Pdb)

   You can do some tricks with copy mechanism to make it work:

      > example.py(3)<module>()
      -> pass
      (Pdb) display lst[:]
      display lst[:]: []
      (Pdb) n
      > example.py(4)<module>()
      -> lst.append(1)
      (Pdb) n
      > example.py(5)<module>()
      -> print(lst)
      display lst[:]: [1]  [old: []]
      (Pdb)

   バージョン 3.2 で追加.

undisplay [expression]

   現在のフレーム内で *expression* をこれ以上表示しないようにします。
   *expression* を指定しない場合、現在のフレームで display 指定されて
   いる式を全てクリアします。

   バージョン 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 *command* is omitted, the current alias
   for *name* is shown. If no arguments are given, all aliases are
   listed.

   エイリアスは入れ子になってもよく、pdb プロンプトで合法的にタイプで
   きるどんなものでも含めることができます。内部 pdb コマンドをエイリア
   スによって上書きすることが *できます*。そのとき、このようなコマンド
   はエイリアスが取り除かれるまで隠されます。エイリアス化はコマンド行
   の最初の語へ再帰的に適用されます。行の他のすべての語はそのままです
   。

   例として、二つの便利なエイリアスがあります (特に ".pdbrc" ファイル
   に置かれたときに):

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

unalias name

   指定したエイリアス *name* を削除します。

! 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 ...]

   デバッグ中の Python プログラムを再実行します。*args* が与えられると
   、 "shlex" で分割され、結果が新しい "sys.argv" として使われます。ヒ
   ストリー、ブレークポイント、アクション、そして、デバッガーオプショ
   ンは引き継がれます。 "restart" は "run" の別名です。

q(uit)

   デバッガーを終了します。実行しているプログラムは中断されます。

debug code

   Enter a recursive debugger that steps through *code* (which is an
   arbitrary expression or statement to be executed in the current
   environment).

retval

   Print the return value for the last return of the current function.

-[ 脚注 ]-

[1] フレームが属するモジュールは、そのフレームのグローバルの
    "__name__" によって決定されます。
