blob: cf8f44730aa59d8276b18b6fb3fc1311fa604996 [file] [log] [blame]
Georg Brandlb15a8df2007-12-02 14:37:29 +00001:mod:`bdb` --- Debugger framework
2=================================
3
4.. module:: bdb
5 :synopsis: Debugger framework.
6
7The :mod:`bdb` module handles basic debugger functions, like setting breakpoints
8or managing execution via the debugger.
9
10The following exception is defined:
11
12.. exception:: BdbQuit
13
14 Exception raised by the :class:`Bdb` class for quitting the debugger.
15
16
17The :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
Benjamin Petersonc7b05922008-04-25 01:29:10 +000034 :class:`Breakpoint` instances have the following methods:
Georg Brandlb15a8df2007-12-02 14:37:29 +000035
Benjamin Petersonc7b05922008-04-25 01:29:10 +000036 .. method:: deleteMe()
Georg Brandlb15a8df2007-12-02 14:37:29 +000037
Benjamin Petersonc7b05922008-04-25 01:29:10 +000038 Delete the breakpoint from the list associated to a file/line. If it is
39 the last breakpoint in that position, it also deletes the entry for the
40 file/line.
Georg Brandlb15a8df2007-12-02 14:37:29 +000041
Georg Brandlb15a8df2007-12-02 14:37:29 +000042
Benjamin Petersonc7b05922008-04-25 01:29:10 +000043 .. method:: enable()
Georg Brandlb15a8df2007-12-02 14:37:29 +000044
Benjamin Petersonc7b05922008-04-25 01:29:10 +000045 Mark the breakpoint as enabled.
Georg Brandlb15a8df2007-12-02 14:37:29 +000046
Georg Brandlb15a8df2007-12-02 14:37:29 +000047
Benjamin Petersonc7b05922008-04-25 01:29:10 +000048 .. method:: disable()
Georg Brandlb15a8df2007-12-02 14:37:29 +000049
Benjamin Petersonc7b05922008-04-25 01:29:10 +000050 Mark the breakpoint as disabled.
Georg Brandlb15a8df2007-12-02 14:37:29 +000051
Benjamin Petersonc7b05922008-04-25 01:29:10 +000052
53 .. method:: pprint([out])
54
55 Print all the information about the breakpoint:
56
57 * The breakpoint number.
58 * If it is temporary or not.
59 * Its file,line position.
60 * The condition that causes a break.
61 * If it must be ignored the next N times.
62 * The breakpoint hit count.
Georg Brandlb15a8df2007-12-02 14:37:29 +000063
64
65.. class:: Bdb()
66
67 The :class:`Bdb` acts as a generic Python debugger base class.
68
69 This class takes care of the details of the trace facility; a derived class
70 should implement user interaction. The standard debugger class
71 (:class:`pdb.Pdb`) is an example.
72
73
Benjamin Petersonc7b05922008-04-25 01:29:10 +000074 The following methods of :class:`Bdb` normally don't need to be overridden.
Georg Brandlb15a8df2007-12-02 14:37:29 +000075
Benjamin Petersonc7b05922008-04-25 01:29:10 +000076 .. method:: canonic(filename)
Georg Brandlb15a8df2007-12-02 14:37:29 +000077
Benjamin Petersonc7b05922008-04-25 01:29:10 +000078 Auxiliary method for getting a filename in a canonical form, that is, as a
79 case-normalized (on case-insensitive filesystems) absolute path, stripped
80 of surrounding angle brackets.
Georg Brandlb15a8df2007-12-02 14:37:29 +000081
Benjamin Petersonc7b05922008-04-25 01:29:10 +000082 .. method:: reset()
Georg Brandlb15a8df2007-12-02 14:37:29 +000083
Benjamin Petersonc7b05922008-04-25 01:29:10 +000084 Set the :attr:`botframe`, :attr:`stopframe`, :attr:`returnframe` and
85 :attr:`quitting` attributes with values ready to start debugging.
Georg Brandlb15a8df2007-12-02 14:37:29 +000086
Benjamin Petersonc7b05922008-04-25 01:29:10 +000087 .. method:: trace_dispatch(frame, event, arg)
Georg Brandlb15a8df2007-12-02 14:37:29 +000088
Benjamin Petersonc7b05922008-04-25 01:29:10 +000089 This function is installed as the trace function of debugged frames. Its
90 return value is the new trace function (in most cases, that is, itself).
Georg Brandlb15a8df2007-12-02 14:37:29 +000091
Benjamin Petersonc7b05922008-04-25 01:29:10 +000092 The default implementation decides how to dispatch a frame, depending on
93 the type of event (passed as a string) that is about to be executed.
94 *event* can be one of the following:
Georg Brandlb15a8df2007-12-02 14:37:29 +000095
Benjamin Petersonc7b05922008-04-25 01:29:10 +000096 * ``"line"``: A new line of code is going to be executed.
97 * ``"call"``: A function is about to be called, or another code block
98 entered.
99 * ``"return"``: A function or other code block is about to return.
100 * ``"exception"``: An exception has occurred.
101 * ``"c_call"``: A C function is about to be called.
102 * ``"c_return"``: A C function has returned.
103 * ``"c_exception"``: A C function has thrown an exception.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000104
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000105 For the Python events, specialized functions (see below) are called. For
106 the C events, no action is taken.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000107
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000108 The *arg* parameter depends on the previous event.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000109
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000110 For more information on trace functions, see :ref:`debugger-hooks`. For
111 more information on code and frame objects, refer to :ref:`types`.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000112
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000113 .. method:: dispatch_line(frame)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000114
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000115 If the debugger should stop on the current line, invoke the
116 :meth:`user_line` method (which should be overridden in subclasses).
117 Raise a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set
118 (which can be set from :meth:`user_line`). Return a reference to the
119 :meth:`trace_dispatch` method for further tracing in that scope.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000120
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000121 .. method:: dispatch_call(frame, arg)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000122
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000123 If the debugger should stop on this function call, invoke the
124 :meth:`user_call` method (which should be overridden in subclasses).
125 Raise a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set
126 (which can be set from :meth:`user_call`). Return a reference to the
127 :meth:`trace_dispatch` method for further tracing in that scope.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000128
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000129 .. method:: dispatch_return(frame, arg)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000130
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000131 If the debugger should stop on this function return, invoke the
132 :meth:`user_return` method (which should be overridden in subclasses).
133 Raise a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set
134 (which can be set from :meth:`user_return`). Return a reference to the
135 :meth:`trace_dispatch` method for further tracing in that scope.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000136
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000137 .. method:: dispatch_exception(frame, arg)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000138
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000139 If the debugger should stop at this exception, invokes the
140 :meth:`user_exception` method (which should be overridden in subclasses).
141 Raise a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set
142 (which can be set from :meth:`user_exception`). Return a reference to the
143 :meth:`trace_dispatch` method for further tracing in that scope.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000144
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000145 Normally derived classes don't override the following methods, but they may
146 if they want to redefine the definition of stopping and breakpoints.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000147
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000148 .. method:: stop_here(frame)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000149
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000150 This method checks if the *frame* is somewhere below :attr:`botframe` in
151 the call stack. :attr:`botframe` is the frame in which debugging started.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000152
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000153 .. method:: break_here(frame)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000154
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000155 This method checks if there is a breakpoint in the filename and line
156 belonging to *frame* or, at least, in the current function. If the
157 breakpoint is a temporary one, this method deletes it.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000158
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000159 .. method:: break_anywhere(frame)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000160
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000161 This method checks if there is a breakpoint in the filename of the current
162 frame.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000163
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000164 Derived classes should override these methods to gain control over debugger
165 operation.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000166
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000167 .. method:: user_call(frame, argument_list)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000168
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000169 This method is called from :meth:`dispatch_call` when there is the
170 possibility that a break might be necessary anywhere inside the called
171 function.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000172
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000173 .. method:: user_line(frame)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000174
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000175 This method is called from :meth:`dispatch_line` when either
176 :meth:`stop_here` or :meth:`break_here` yields True.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000177
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000178 .. method:: user_return(frame, return_value)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000179
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000180 This method is called from :meth:`dispatch_return` when :meth:`stop_here`
181 yields True.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000182
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000183 .. method:: user_exception(frame, exc_info)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000184
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000185 This method is called from :meth:`dispatch_exception` when
186 :meth:`stop_here` yields True.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000187
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000188 .. method:: do_clear(arg)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000189
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000190 Handle how a breakpoint must be removed when it is a temporary one.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000191
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000192 This method must be implemented by derived classes.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000193
194
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000195 Derived classes and clients can call the following methods to affect the
196 stepping state.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000197
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000198 .. method:: set_step()
Georg Brandlb15a8df2007-12-02 14:37:29 +0000199
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000200 Stop after one line of code.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000201
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000202 .. method:: set_next(frame)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000203
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000204 Stop on the next line in or below the given frame.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000205
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000206 .. method:: set_return(frame)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000207
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000208 Stop when returning from the given frame.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000209
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000210 .. method:: set_trace([frame])
Georg Brandlb15a8df2007-12-02 14:37:29 +0000211
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000212 Start debugging from *frame*. If *frame* is not specified, debugging
213 starts from caller's frame.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000214
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000215 .. method:: set_continue()
Georg Brandlb15a8df2007-12-02 14:37:29 +0000216
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000217 Stop only at breakpoints or when finished. If there are no breakpoints,
218 set the system trace function to None.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000219
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000220 .. method:: set_quit()
Georg Brandlb15a8df2007-12-02 14:37:29 +0000221
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000222 Set the :attr:`quitting` attribute to True. This raises :exc:`BdbQuit` in
223 the next call to one of the :meth:`dispatch_\*` methods.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000224
225
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000226 Derived classes and clients can call the following methods to manipulate
227 breakpoints. These methods return a string containing an error message if
228 something went wrong, or ``None`` if all is well.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000229
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000230 .. method:: set_break(filename, lineno[, temporary=0[, cond[, funcname]]])
Georg Brandlb15a8df2007-12-02 14:37:29 +0000231
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000232 Set a new breakpoint. If the *lineno* line doesn't exist for the
233 *filename* passed as argument, return an error message. The *filename*
234 should be in canonical form, as described in the :meth:`canonic` method.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000235
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000236 .. method:: clear_break(filename, lineno)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000237
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000238 Delete the breakpoints in *filename* and *lineno*. If none were set, an
239 error message is returned.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000240
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000241 .. method:: clear_bpbynumber(arg)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000242
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000243 Delete the breakpoint which has the index *arg* in the
244 :attr:`Breakpoint.bpbynumber`. If *arg* is not numeric or out of range,
245 return an error message.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000246
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000247 .. method:: clear_all_file_breaks(filename)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000248
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000249 Delete all breakpoints in *filename*. If none were set, an error message
250 is returned.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000251
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000252 .. method:: clear_all_breaks()
Georg Brandlb15a8df2007-12-02 14:37:29 +0000253
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000254 Delete all existing breakpoints.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000255
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000256 .. method:: get_break(filename, lineno)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000257
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000258 Check if there is a breakpoint for *lineno* of *filename*.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000259
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000260 .. method:: get_breaks(filename, lineno)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000261
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000262 Return all breakpoints for *lineno* in *filename*, or an empty list if
263 none are set.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000264
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000265 .. method:: get_file_breaks(filename)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000266
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000267 Return all breakpoints in *filename*, or an empty list if none are set.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000268
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000269 .. method:: get_all_breaks()
Georg Brandlb15a8df2007-12-02 14:37:29 +0000270
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000271 Return all breakpoints that are set.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000272
273
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000274 Derived classes and clients can call the following methods to get a data
275 structure representing a stack trace.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000276
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000277 .. method:: get_stack(f, t)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000278
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000279 Get a list of records for a frame and all higher (calling) and lower
280 frames, and the size of the higher part.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000281
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000282 .. method:: format_stack_entry(frame_lineno, [lprefix=': '])
Georg Brandlb15a8df2007-12-02 14:37:29 +0000283
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000284 Return a string with information about a stack entry, identified by a
285 ``(frame, lineno)`` tuple:
Georg Brandlb15a8df2007-12-02 14:37:29 +0000286
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000287 * The canonical form of the filename which contains the frame.
288 * The function name, or ``"<lambda>"``.
289 * The input arguments.
290 * The return value.
291 * The line of code (if it exists).
Georg Brandlb15a8df2007-12-02 14:37:29 +0000292
293
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000294 The following two methods can be called by clients to use a debugger to debug
295 a :term:`statement`, given as a string.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000296
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000297 .. method:: run(cmd, [globals, [locals]])
Georg Brandlb15a8df2007-12-02 14:37:29 +0000298
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000299 Debug a statement executed via the :keyword:`exec` statement. *globals*
300 defaults to :attr:`__main__.__dict__`, *locals* defaults to *globals*.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000301
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000302 .. method:: runeval(expr, [globals, [locals]])
Georg Brandlb15a8df2007-12-02 14:37:29 +0000303
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000304 Debug an expression executed via the :func:`eval` function. *globals* and
305 *locals* have the same meaning as in :meth:`run`.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000306
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000307 .. method:: runctx(cmd, globals, locals)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000308
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000309 For backwards compatibility. Calls the :meth:`run` method.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000310
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000311 .. method:: runcall(func, *args, **kwds)
Georg Brandlb15a8df2007-12-02 14:37:29 +0000312
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000313 Debug a single function call, and return its result.
Georg Brandlb15a8df2007-12-02 14:37:29 +0000314
315
316Finally, the module defines the following functions:
317
318.. function:: checkfuncname(b, frame)
319
320 Check whether we should break here, depending on the way the breakpoint *b*
321 was set.
322
323 If it was set via line number, it checks if ``b.line`` is the same as the one
324 in the frame also passed as argument. If the breakpoint was set via function
325 name, we have to check we are in the right frame (the right function) and if
326 we are in its first executable line.
327
328.. function:: effective(file, line, frame)
329
330 Determine if there is an effective (active) breakpoint at this line of code.
331 Return breakpoint number or 0 if none.
332
333 Called only if we know there is a breakpoint at this location. Returns the
334 breakpoint that was triggered and a flag that indicates if it is ok to delete
335 a temporary breakpoint.
336
337.. function:: set_trace()
338
339 Starts debugging with a :class:`Bdb` instance from caller's frame.