Georg Brandl | 0001422 | 2007-09-12 18:03:51 +0000 | [diff] [blame] | 1 | :mod:`bdb` --- Debugger framework
|
| 2 | =================================
|
| 3 |
|
| 4 | .. module:: bdb
|
| 5 | :synopsis: Debugger framework.
|
| 6 |
|
| 7 | The :mod:`bdb` module handles basic debugger functions, like setting breakpoints
|
| 8 | or managing execution via the debugger.
|
| 9 |
|
| 10 | The following exception is defined:
|
| 11 |
|
| 12 | .. exception:: BdbQuit
|
| 13 |
|
| 14 | Exception raised by the :class:`Bdb` class for quitting the debugger.
|
| 15 |
|
| 16 |
|
| 17 | The :mod:`bdb` module also defines two classes:
|
| 18 |
|
| 19 | .. class:: Breakpoint(self, file, line[, temporary=0[, cond=None [, funcname=None]]])
|
| 20 |
|
| 21 | This class implements temporary breakpoints, ignore counts, disabling and
|
| 22 | (re-)enabling, and conditionals.
|
| 23 |
|
| 24 | Breakpoints are indexed by number through a list called :attr:`bpbynumber`
|
| 25 | and by ``(file, line)`` pairs through :attr:`bplist`. The former points to a
|
| 26 | single instance of class :class:`Breakpoint`. The latter points to a list of
|
| 27 | such instances since there may be more than one breakpoint per line.
|
| 28 |
|
| 29 | When creating a breakpoint, its associated filename should be in canonical
|
| 30 | form. If a *funcname* is defined, a breakpoint hit will be counted when the
|
| 31 | first line of that function is executed. A conditional breakpoint always
|
| 32 | counts a hit.
|
| 33 |
|
| 34 | :class:`Breakpoint` instances have the following methods:
|
| 35 |
|
| 36 | .. method:: Breakpoint.deleteMe()
|
| 37 |
|
| 38 | Delete the breakpoint from the list associated to a file/line. If it is the
|
| 39 | last breakpoint in that position, it also deletes the entry for the
|
| 40 | file/line.
|
| 41 |
|
| 42 | .. method:: Breakpoint.enable()
|
| 43 |
|
| 44 | Mark the breakpoint as enabled.
|
| 45 |
|
| 46 | .. method:: Breakpoint.disable()
|
| 47 |
|
| 48 | Mark the breakpoint as disabled.
|
| 49 |
|
| 50 | .. method:: Breakpoint.bpprint([out])
|
| 51 |
|
| 52 | Print all the information about the breakpoint:
|
| 53 |
|
| 54 | * The breakpoint number.
|
| 55 | * If it is temporary or not.
|
| 56 | * Its file,line position.
|
| 57 | * The condition that causes a break.
|
| 58 | * If it must be ignored the next N times.
|
| 59 | * The breakpoint hit count.
|
| 60 |
|
| 61 |
|
| 62 | .. class:: Bdb()
|
| 63 |
|
| 64 | The :class:`Bdb` acts as a generic Python debugger base class.
|
| 65 |
|
| 66 | This class takes care of the details of the trace facility; a derived class
|
| 67 | should implement user interaction. The standard debugger class
|
| 68 | (:class:`pdb.Pdb`) is an example.
|
| 69 |
|
| 70 |
|
| 71 | The following methods of :class:`Bdb` normally don't need to be overridden.
|
| 72 |
|
| 73 | .. method:: Bdb.canonic(filename)
|
| 74 |
|
| 75 | Auxiliary method for getting a filename in a canonical form, that is, as a
|
| 76 | case-normalized (on case-insensitive filesystems) absolute path, stripped
|
| 77 | of surrounding angle brackets.
|
| 78 |
|
| 79 | .. method:: Bdb.reset()
|
| 80 |
|
| 81 | Set the :attr:`botframe`, :attr:`stopframe`, :attr:`returnframe` and
|
| 82 | :attr:`quitting` attributes with values ready to start debugging.
|
| 83 |
|
| 84 |
|
| 85 | .. method:: Bdb.trace_dispatch(frame, event, arg)
|
| 86 |
|
| 87 | This function is installed as the trace function of debugged frames. Its
|
| 88 | return value is the new trace function (in most cases, that is, itself).
|
| 89 |
|
| 90 | The default implementation decides how to dispatch a frame, depending on the
|
| 91 | type of event (passed as a string) that is about to be executed. *event* can
|
| 92 | be one of the following:
|
| 93 |
|
| 94 | * ``"line"``: A new line of code is going to be executed.
|
| 95 | * ``"call"``: A function is about to be called, or another code block
|
| 96 | entered.
|
| 97 | * ``"return"``: A function or other code block is about to return.
|
| 98 | * ``"exception"``: An exception has occurred.
|
| 99 | * ``"c_call"``: A C function is about to be called.
|
| 100 | * ``"c_return"``: A C function has returned.
|
| 101 | * ``"c_exception"``: A C function has thrown an exception.
|
| 102 |
|
| 103 | For the Python events, specialized functions (see below) are called. For the
|
| 104 | C events, no action is taken.
|
| 105 |
|
| 106 | The *arg* parameter depends on the previous event.
|
| 107 |
|
| 108 | For more information on trace functions, see :ref:`debugger-hooks`. For more
|
| 109 | information on code and frame objects, refer to :ref:`types`.
|
| 110 |
|
| 111 | .. method:: Bdb.dispatch_line(frame)
|
| 112 |
|
| 113 | If the debugger should stop on the current line, invoke the :meth:`user_line`
|
| 114 | method (which should be overridden in subclasses). Raise a :exc:`BdbQuit`
|
| 115 | exception if the :attr:`Bdb.quitting` flag is set (which can be set from
|
| 116 | :meth:`user_line`). Return a reference to the :meth:`trace_dispatch` method
|
| 117 | for further tracing in that scope.
|
| 118 |
|
| 119 | .. method:: Bdb.dispatch_call(frame, arg)
|
| 120 |
|
| 121 | If the debugger should stop on this function call, invoke the
|
| 122 | :meth:`user_call` method (which should be overridden in subclasses). Raise a
|
| 123 | :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set (which can
|
| 124 | be set from :meth:`user_call`). Return a reference to the
|
| 125 | :meth:`trace_dispatch` method for further tracing in that scope.
|
| 126 |
|
| 127 | .. method:: Bdb.dispatch_return(frame, arg)
|
| 128 |
|
| 129 | If the debugger should stop on this function return, invoke the
|
| 130 | :meth:`user_return` method (which should be overridden in subclasses). Raise
|
| 131 | a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set (which can
|
| 132 | be set from :meth:`user_return`). Return a reference to the
|
| 133 | :meth:`trace_dispatch` method for further tracing in that scope.
|
| 134 |
|
| 135 | .. method:: Bdb.dispatch_exception(frame, arg)
|
| 136 |
|
| 137 | If the debugger should stop at this exception, invokes the
|
| 138 | :meth:`user_exception` method (which should be overridden in subclasses).
|
| 139 | Raise a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set
|
| 140 | (which can be set from :meth:`user_exception`). Return a reference to the
|
| 141 | :meth:`trace_dispatch` method for further tracing in that scope.
|
| 142 |
|
| 143 | Normally derived classes don't override the following methods, but they may if
|
| 144 | they want to redefine the definition of stopping and breakpoints.
|
| 145 |
|
| 146 | .. method:: Bdb.stop_here(frame)
|
| 147 |
|
| 148 | This method checks if the *frame* is somewhere below :attr:`botframe` in the
|
| 149 | call stack. :attr:`botframe` is the frame in which debugging started.
|
| 150 |
|
| 151 | .. method:: Bdb.break_here(frame)
|
| 152 |
|
| 153 | This method checks if there is a breakpoint in the filename and line
|
| 154 | belonging to *frame* or, at least, in the current function. If the
|
| 155 | breakpoint is a temporary one, this method deletes it.
|
| 156 |
|
| 157 | .. method:: Bdb.break_anywhere(frame)
|
| 158 |
|
| 159 | This method checks if there is a breakpoint in the filename of the current
|
| 160 | frame.
|
| 161 |
|
| 162 | Derived classes should override these methods to gain control over debugger
|
| 163 | operation.
|
| 164 |
|
| 165 | .. method:: Bdb.user_call(frame, argument_list)
|
| 166 |
|
| 167 | This method is called from :meth:`dispatch_call` when there is the
|
| 168 | possibility that a break might be necessary anywhere inside the called
|
| 169 | function.
|
| 170 |
|
| 171 | .. method:: Bdb.user_line(frame)
|
| 172 |
|
| 173 | This method is called from :meth:`dispatch_line` when either
|
| 174 | :meth:`stop_here` or :meth:`break_here` yields True.
|
| 175 |
|
| 176 | .. method:: Bdb.user_return(frame, return_value)
|
| 177 |
|
| 178 | This method is called from :meth:`dispatch_return` when :meth:`stop_here`
|
| 179 | yields True.
|
| 180 |
|
| 181 | .. method:: Bdb.user_exception(frame, exc_info)
|
| 182 |
|
| 183 | This method is called from :meth:`dispatch_exception` when :meth:`stop_here`
|
| 184 | yields True.
|
| 185 |
|
| 186 | .. method:: Bdb.do_clear(arg)
|
| 187 |
|
| 188 | Handle how a breakpoint must be removed when it is a temporary one.
|
| 189 |
|
| 190 | This method must be implemented by derived classes.
|
| 191 |
|
| 192 |
|
| 193 | Derived classes and clients can call the following methods to affect the
|
| 194 | stepping state.
|
| 195 |
|
| 196 | .. method:: Bdb.set_step()
|
| 197 |
|
| 198 | Stop after one line of code.
|
| 199 |
|
| 200 | .. method:: Bdb.set_next(frame)
|
| 201 |
|
| 202 | Stop on the next line in or below the given frame.
|
| 203 |
|
| 204 | .. method:: Bdb.set_return(frame)
|
| 205 |
|
| 206 | Stop when returning from the given frame.
|
| 207 |
|
| 208 | .. method:: Bdb.set_trace([frame])
|
| 209 |
|
| 210 | Start debugging from *frame*. If *frame* is not specified, debugging starts
|
| 211 | from caller's frame.
|
| 212 |
|
| 213 | .. method:: Bdb.set_continue()
|
| 214 |
|
| 215 | Stop only at breakpoints or when finished. If there are no breakpoints, set
|
| 216 | the system trace function to None.
|
| 217 |
|
| 218 | .. method:: Bdb.set_quit()
|
| 219 |
|
| 220 | Set the :attr:`quitting` attribute to True. This raises :exc:`BdbQuit` in
|
| 221 | the next call to one of the :meth:`dispatch_\*` methods.
|
| 222 |
|
| 223 |
|
| 224 | Derived classes and clients can call the following methods to manipulate
|
| 225 | breakpoints. These methods return a string containing an error message if
|
| 226 | something went wrong, or ``None`` if all is well.
|
| 227 |
|
| 228 | .. method:: Bdb.set_break(filename, lineno[, temporary=0[, cond[, funcname]]])
|
| 229 |
|
| 230 | Set a new breakpoint. If the *lineno* line doesn't exist for the *filename*
|
| 231 | passed as argument, return an error message. The *filename* should be in
|
| 232 | canonical form, as described in the :meth:`canonic` method.
|
| 233 |
|
| 234 | .. method:: Bdb.clear_break(filename, lineno)
|
| 235 |
|
| 236 | Delete the breakpoints in *filename* and *lineno*. If none were set, an
|
| 237 | error message is returned.
|
| 238 |
|
| 239 | .. method:: Bdb.clear_bpbynumber(arg)
|
| 240 |
|
| 241 | Delete the breakpoint which has the index *arg* in the
|
| 242 | :attr:`Breakpoint.bpbynumber`. If `arg` is not numeric or out of range,
|
| 243 | return an error message.
|
| 244 |
|
| 245 | .. method:: Bdb.clear_all_file_breaks(filename)
|
| 246 |
|
| 247 | Delete all breakpoints in *filename*. If none were set, an error message is
|
| 248 | returned.
|
| 249 |
|
| 250 | .. method:: Bdb.clear_all_breaks()
|
| 251 |
|
| 252 | Delete all existing breakpoints.
|
| 253 |
|
| 254 | .. method:: Bdb.get_break(filename, lineno)
|
| 255 |
|
| 256 | Check if there is a breakpoint for *lineno* of *filename*.
|
| 257 |
|
| 258 | .. method:: Bdb.get_breaks(filename, lineno)
|
| 259 |
|
| 260 | Return all breakpoints for *lineno* in *filename*, or an empty list if none
|
| 261 | are set.
|
| 262 |
|
| 263 | .. method:: Bdb.get_file_breaks(filename)
|
| 264 |
|
| 265 | Return all breakpoints in *filename*, or an empty list if none are set.
|
| 266 |
|
| 267 | .. method:: Bdb.get_all_breaks()
|
| 268 |
|
| 269 | Return all breakpoints that are set.
|
| 270 |
|
| 271 |
|
| 272 | Derived classes and clients can call the following methods to get a data
|
| 273 | structure representing a stack trace.
|
| 274 |
|
| 275 | .. method:: Bdb.get_stack(f, t)
|
| 276 |
|
| 277 | Get a list of records for a frame and all higher (calling) and lower frames,
|
| 278 | and the size of the higher part.
|
| 279 |
|
| 280 | .. method:: Bdb.format_stack_entry(frame_lineno, [lprefix=': '])
|
| 281 |
|
| 282 | Return a string with information about a stack entry, identified by a
|
| 283 | ``(frame, lineno)`` tuple:
|
| 284 |
|
| 285 | * The canonical form of the filename which contains the frame.
|
| 286 | * The function name, or ``"<lambda>"``.
|
| 287 | * The input arguments.
|
| 288 | * The return value.
|
| 289 | * The line of code (if it exists).
|
| 290 |
|
| 291 |
|
| 292 | The following two methods can be called by clients to use a debugger to debug a
|
| 293 | statement, given as a string.
|
| 294 |
|
| 295 | .. method:: Bdb.run(cmd, [globals, [locals]])
|
| 296 |
|
| 297 | Debug a statement executed via the :keyword:`exec` statement. *globals*
|
| 298 | defaults to :attr:`__main__.__dict__`, *locals* defaults to *globals*.
|
| 299 |
|
| 300 | .. method:: Bdb.runeval(expr, [globals, [locals]])
|
| 301 |
|
| 302 | Debug an expression executed via the :func:`eval` function. *globals* and
|
| 303 | *locals* have the same meaning as in :meth:`run`.
|
| 304 |
|
| 305 | .. method:: Bdb.runctx(cmd, globals, locals)
|
| 306 |
|
| 307 | For backwards compatibility. Calls the :meth:`run` method.
|
| 308 |
|
| 309 | .. method:: Bdb.runcall(func, *args, **kwds)
|
| 310 |
|
| 311 | Debug a single function call, and return its result.
|
| 312 |
|
| 313 |
|
| 314 | Finally, the module defines the following functions:
|
| 315 |
|
| 316 | .. function:: checkfuncname(b, frame)
|
| 317 |
|
| 318 | Check whether we should break here, depending on the way the breakpoint *b*
|
| 319 | was set.
|
| 320 |
|
| 321 | If it was set via line number, it checks if ``b.line`` is the same as the one
|
| 322 | in the frame also passed as argument. If the breakpoint was set via function
|
| 323 | name, we have to check we are in the right frame (the right function) and if
|
| 324 | we are in its first executable line.
|
| 325 |
|
| 326 | .. function:: effective(file, line, frame)
|
| 327 |
|
| 328 | Determine if there is an effective (active) breakpoint at this line of code.
|
| 329 | Return breakpoint number or 0 if none.
|
| 330 |
|
| 331 | Called only if we know there is a breakpoint at this location. Returns the
|
| 332 | breakpoint that was triggered and a flag that indicates if it is ok to delete
|
| 333 | a temporary breakpoint.
|
| 334 |
|
| 335 | .. function:: set_trace()
|
| 336 |
|
| 337 | Starts debugging with a :class:`Bdb` instance from caller's frame.
|