pdb.doc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. The Python Debugger Pdb
  2. =======================
  3. To use the debugger in its simplest form:
  4. >>> import pdb
  5. >>> pdb.run('<a statement>')
  6. The debugger's prompt is '(Pdb) '. This will stop in the first
  7. function call in <a statement>.
  8. Alternatively, if a statement terminated with an unhandled exception,
  9. you can use pdb's post-mortem facility to inspect the contents of the
  10. traceback:
  11. >>> <a statement>
  12. <exception traceback>
  13. >>> import pdb
  14. >>> pdb.pm()
  15. The commands recognized by the debugger are listed in the next
  16. section. Most can be abbreviated as indicated; e.g., h(elp) means
  17. that 'help' can be typed as 'h' or 'help' (but not as 'he' or 'hel',
  18. nor as 'H' or 'Help' or 'HELP'). Optional arguments are enclosed in
  19. square brackets.
  20. A blank line repeats the previous command literally, except for
  21. 'list', where it lists the next 11 lines.
  22. Commands that the debugger doesn't recognize are assumed to be Python
  23. statements and are executed in the context of the program being
  24. debugged. Python statements can also be prefixed with an exclamation
  25. point ('!'). This is a powerful way to inspect the program being
  26. debugged; it is even possible to change variables. When an exception
  27. occurs in such a statement, the exception name is printed but the
  28. debugger's state is not changed.
  29. The debugger supports aliases, which can save typing. And aliases can
  30. have parameters (see the alias help entry) which allows one a certain
  31. level of adaptability to the context under examination.
  32. Multiple commands may be entered on a single line, separated by the
  33. pair ';;'. No intelligence is applied to separating the commands; the
  34. input is split at the first ';;', even if it is in the middle of a
  35. quoted string.
  36. If a file ".pdbrc" exists in your home directory or in the current
  37. directory, it is read in and executed as if it had been typed at the
  38. debugger prompt. This is particularly useful for aliases. If both
  39. files exist, the one in the home directory is read first and aliases
  40. defined there can be overridden by the local file.
  41. Aside from aliases, the debugger is not directly programmable; but it
  42. is implemented as a class from which you can derive your own debugger
  43. class, which you can make as fancy as you like.
  44. Debugger commands
  45. =================
  46. h(elp)
  47. Without argument, print the list of available commands. With
  48. a command name as argument, print help about that command
  49. (this is currently not implemented).
  50. w(here)
  51. Print a stack trace, with the most recent frame at the bottom.
  52. An arrow indicates the "current frame", which determines the
  53. context of most commands.
  54. d(own)
  55. Move the current frame one level down in the stack trace
  56. (to a newer frame).
  57. u(p)
  58. Move the current frame one level up in the stack trace
  59. (to an older frame).
  60. b(reak) [ ([filename:]lineno | function) [, condition] ]
  61. With a filename:line number argument, set a break there. If
  62. filename is omitted, use the current file. With a function
  63. name, set a break at the first executable line of that
  64. function. Without argument, list all breaks. Each breakpoint
  65. is assigned a number to which all the other breakpoint
  66. commands refer.
  67. The condition argument, if present, is a string which must
  68. evaluate to true in order for the breakpoint to be honored.
  69. tbreak [ ([filename:]lineno | function) [, condition] ]
  70. Temporary breakpoint, which is removed automatically when it
  71. is first hit. The arguments are the same as break.
  72. cl(ear) [bpnumber [bpnumber ...] ]
  73. With a space separated list of breakpoint numbers, clear those
  74. breakpoints. Without argument, clear all breaks (but first
  75. ask confirmation).
  76. disable bpnumber [bpnumber ...]
  77. Disables the breakpoints given as a space separated list of
  78. breakpoint numbers. Disabling a breakpoint means it cannot
  79. cause the program to stop execution, but unlike clearing a
  80. breakpoint, it remains in the list of breakpoints and can be
  81. (re-)enabled.
  82. enable bpnumber [bpnumber ...]
  83. Enables the breakpoints specified.
  84. ignore bpnumber count
  85. Sets the ignore count for the given breakpoint number. If
  86. count is omitted, the ignore count is set to 0. A breakpoint
  87. becomes active when the ignore count is zero. When non-zero,
  88. the count is decremented each time the breakpoint is reached
  89. and the breakpoint is not disabled and any associated
  90. condition evaluates to true.
  91. condition bpnumber condition
  92. condition is an expression which must evaluate to true before
  93. the breakpoint is honored. If condition is absent, any
  94. existing condition is removed; i.e., the breakpoint is made
  95. unconditional.
  96. s(tep)
  97. Execute the current line, stop at the first possible occasion
  98. (either in a function that is called or in the current function).
  99. n(ext)
  100. Continue execution until the next line in the current function
  101. is reached or it returns.
  102. unt(il)
  103. Continue execution until the line with a number greater than the
  104. current one is reached or until the current frame returns.
  105. r(eturn)
  106. Continue execution until the current function returns.
  107. run [args...]
  108. Restart the debugged python program. If a string is supplied it is
  109. splitted with "shlex", and the result is used as the new sys.argv.
  110. History, breakpoints, actions and debugger options are preserved.
  111. "restart" is an alias for "run".
  112. c(ont(inue))
  113. Continue execution, only stop when a breakpoint is encountered.
  114. l(ist) [first [,last]]
  115. List source code for the current file.
  116. Without arguments, list 11 lines around the current line
  117. or continue the previous listing.
  118. With one argument, list 11 lines starting at that line.
  119. With two arguments, list the given range;
  120. if the second argument is less than the first, it is a count.
  121. a(rgs)
  122. Print the argument list of the current function.
  123. p expression
  124. Print the value of the expression.
  125. (!) statement
  126. Execute the (one-line) statement in the context of the current
  127. stack frame. The exclamation point can be omitted unless the
  128. first word of the statement resembles a debugger command. To
  129. assign to a global variable you must always prefix the command
  130. with a 'global' command, e.g.:
  131. (Pdb) global list_options; list_options = ['-l']
  132. (Pdb)
  133. whatis arg
  134. Prints the type of the argument.
  135. alias [name [command]]
  136. Creates an alias called 'name' that executes 'command'. The
  137. command must *not* be enclosed in quotes. Replaceable
  138. parameters can be indicated by %1, %2, and so on, while %* is
  139. replaced by all the parameters. If no command is given, the
  140. current alias for name is shown. If no name is given, all
  141. aliases are listed.
  142. Aliases may be nested and can contain anything that can be
  143. legally typed at the pdb prompt. Note! You *can* override
  144. internal pdb commands with aliases! Those internal commands
  145. are then hidden until the alias is removed. Aliasing is
  146. recursively applied to the first word of the command line; all
  147. other words in the line are left alone.
  148. As an example, here are two useful aliases (especially when
  149. placed in the .pdbrc file):
  150. #Print instance variables (usage "pi classInst")
  151. alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
  152. #Print instance variables in self
  153. alias ps pi self
  154. unalias name
  155. Deletes the specified alias.
  156. q(uit)
  157. Quit from the debugger.
  158. The program being executed is aborted.