Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | The Python Debugger |
| 2 | =================== |
| 3 | |
| 4 | To use the debugger in its simplest form: |
| 5 | |
| 6 | >>> import pdb |
| 7 | >>> pdb.run('<a statement>') |
| 8 | |
| 9 | The debugger's prompt is '(Pdb) '. This will stop in the first |
| 10 | function call in <a statement>. |
| 11 | |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 12 | Alternatively, if a statement terminated with an unhandled exception, |
| 13 | you can use pdb's post-mortem facility to inspect the contents of the |
| 14 | traceback: |
| 15 | |
| 16 | >>> <a statement> |
| 17 | <exception traceback> |
| 18 | >>> import pdb |
| 19 | >>> pdb.pm() |
| 20 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 21 | The commands recognized by the debugger are listed in the next |
| 22 | section. Most can be abbreviated as indicated; e.g., h(elp) means |
| 23 | that 'help' can be typed as 'h' or 'help' (but not as 'he' or 'hel', |
| 24 | nor as 'H' or 'Help' or 'HELP'). Optional arguments are enclosed in |
| 25 | square brackets. |
| 26 | |
| 27 | A blank line repeats the previous command literally. (Except for |
| 28 | 'list', where it lists the next 11 lines.) |
| 29 | |
| 30 | Commands that the debugger doesn't recognize are assumed to be Python |
| 31 | statements and are executed in the context of the program being |
| 32 | debugged. Python statements can also be prefixed with an exclamation |
| 33 | point ('!'). This is a powerful way to inspect the program being |
| 34 | debugged; it is even possible to change variables. When an exception |
| 35 | occurs in such a statement, the exception name is printed but the |
| 36 | debugger's state is not changed. |
| 37 | |
| 38 | The debugger is not directly programmable; but it is implemented as a |
| 39 | class from which you can derive your own debugger class, so you can |
| 40 | make as fancy as you like. |
| 41 | |
| 42 | |
| 43 | Debugger commands |
| 44 | ================= |
| 45 | |
| 46 | h(elp) |
| 47 | Without argument, print the list of available commands. |
| 48 | With a command name as argument, print help about that command |
| 49 | (this is currently not implemented). |
| 50 | |
| 51 | w(here) |
| 52 | Print a stack trace, with the most recent frame at the bottom. |
| 53 | An arrow indicates the "current frame", which determines the |
| 54 | context of most commands. |
| 55 | |
| 56 | d(own) |
| 57 | Move the current frame one level down in the stack trace |
| 58 | (to an older frame). |
| 59 | |
| 60 | u(p) |
| 61 | Move the current frame one level up in the stack trace |
| 62 | (to a newer frame). |
| 63 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 64 | b(reak) [lineno | function] |
| 65 | With a line number argument, set a break there in the current |
| 66 | file. With a function name, set a break at the entry of that |
| 67 | function. Without argument, list all breaks. |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 68 | |
| 69 | cl(ear) [lineno] |
| 70 | With a line number argument, clear that break in the current file. |
| 71 | Without argument, clear all breaks (but first ask confirmation). |
| 72 | |
| 73 | s(tep) |
| 74 | Execute the current line, stop at the first possible occasion |
| 75 | (either in a function that is called or in the current function). |
| 76 | |
| 77 | n(ext) |
| 78 | Continue execution until the next line in the current function |
| 79 | is reached or it returns. |
| 80 | |
| 81 | r(eturn) |
| 82 | Continue execution until the current function returns. |
| 83 | |
| 84 | c(ont(inue)) |
| 85 | Continue execution, only stop when a breakpoint is encountered. |
| 86 | |
| 87 | l(ist) [first [,last]] |
| 88 | List source code for the current file. |
| 89 | Without arguments, list 11 lines around the current line |
| 90 | or continue the previous listing. |
| 91 | With one argument, list 11 lines starting at that line. |
| 92 | With two arguments, list the given range; |
| 93 | if the second argument is less than the first, it is a count. |
| 94 | |
| 95 | a(rgs) |
| 96 | Print the argument list of the current function. |
| 97 | |
| 98 | p expression |
| 99 | Print the value of the expression. |
| 100 | |
| 101 | (!) statement |
| 102 | Execute the (one-line) statement in the context of |
| 103 | the current stack frame. |
| 104 | The exclamation point can be omitted unless the first word |
| 105 | of the statement resembles a debugger command. |
| 106 | To assign to a global variable you must always prefix the |
| 107 | command with a 'global' command, e.g.: |
| 108 | (Pdb) global list_options; list_options = ['-l'] |
| 109 | (Pdb) |
| 110 | |
| 111 | q(uit) |
| 112 | Quit from the debugger. |
| 113 | The program being executed is aborted. |
| 114 | |
| 115 | |
| 116 | How it works |
| 117 | ============ |
| 118 | |
| 119 | Some changes were made to the interpreter: |
Guido van Rossum | 35dcf45 | 1992-03-27 15:06:53 +0000 | [diff] [blame] | 120 | - sys.settrace(func) sets the global trace function |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 121 | - there can also a local trace function (see later) |
| 122 | |
| 123 | Trace functions have three arguments: (frame, event, arg) |
| 124 | - frame is the current stack frame |
| 125 | - event is a string: 'call', 'line', 'return' or 'exception' |
| 126 | - arg is dependent on the event type |
| 127 | A trace function should return a new trace function or None. |
| 128 | Class methods are accepted (and most useful!) as trace methods. |
| 129 | |
| 130 | The events have the following meaning: |
| 131 | |
| 132 | 'call': A function is called (or some other code block entered). |
| 133 | The global trace function is called; |
| 134 | arg is the argument list to the function; |
| 135 | the return value specifies the local trace function. |
| 136 | |
| 137 | 'line': The interpreter is about to execute a new line of code |
| 138 | (sometimes multiple line events on one line exist). |
| 139 | The local trace function is called; arg in None; |
| 140 | the return value specifies the new local trace function. |
| 141 | |
| 142 | 'return': A function (or other code block) is about to return. |
| 143 | The local trace function is called; |
| 144 | arg is the value that will be returned. |
| 145 | The trace function's return value is ignored. |
| 146 | |
| 147 | 'exception': An exception has occurred. |
| 148 | The local trace function is called; |
| 149 | arg is a triple (exception, value, traceback); |
| 150 | the return value specifies the new local trace function |
| 151 | |
| 152 | Note that as an exception is propagated down the chain of callers, an |
| 153 | 'exception' event is generated at each level. |
| 154 | |
| 155 | Stack frame objects have the following read-only attributes: |
| 156 | f_code: the code object being executed |
| 157 | f_lineno: the current line number (-1 for 'call' events) |
| 158 | f_back: the stack frame of the caller, or None |
| 159 | f_locals: dictionary containing local name bindings |
| 160 | f_globals: dictionary containing global name bindings |
| 161 | |
| 162 | Code objects have the following read-only attributes: |
| 163 | co_code: the code string |
| 164 | co_names: the list of names used by the code |
| 165 | co_consts: the list of (literal) constants used by the code |
| 166 | co_filename: the filename from which the code was compiled |