| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 1 | :mod:`bdb` --- Debugger framework |
| 2 | ================================= |
| 3 | |
| 4 | .. module:: bdb |
| 5 | :synopsis: Debugger framework. |
| 6 | |
| Raymond Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 7 | **Source code:** :source:`Lib/bdb.py` |
| 8 | |
| 9 | -------------- |
| 10 | |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 11 | The :mod:`bdb` module handles basic debugger functions, like setting breakpoints |
| 12 | or managing execution via the debugger. |
| 13 | |
| 14 | The following exception is defined: |
| 15 | |
| 16 | .. exception:: BdbQuit |
| 17 | |
| 18 | Exception raised by the :class:`Bdb` class for quitting the debugger. |
| 19 | |
| 20 | |
| 21 | The :mod:`bdb` module also defines two classes: |
| 22 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 23 | .. class:: Breakpoint(self, file, line, temporary=False, cond=None, funcname=None) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 24 | |
| 25 | This class implements temporary breakpoints, ignore counts, disabling and |
| 26 | (re-)enabling, and conditionals. |
| 27 | |
| 28 | Breakpoints are indexed by number through a list called :attr:`bpbynumber` |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 29 | and by ``(file, line)`` pairs through :attr:`bplist`. The former points to |
| 30 | a single instance of class :class:`Breakpoint`. The latter points to a list |
| 31 | of such instances since there may be more than one breakpoint per line. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 32 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 33 | When creating a breakpoint, its associated :attr:`file name <file>` should |
| 34 | be in canonical form. If a :attr:`funcname` is defined, a breakpoint |
| 35 | :attr:`hit <hits>` will be counted when the first line of that function is |
| 36 | executed. A :attr:`conditional <cond>` breakpoint always counts a |
| 37 | :attr:`hit <hits>`. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 38 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 39 | :class:`Breakpoint` instances have the following methods: |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 40 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 41 | .. method:: deleteMe() |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 42 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 43 | Delete the breakpoint from the list associated to a file/line. If it is |
| 44 | the last breakpoint in that position, it also deletes the entry for the |
| 45 | file/line. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 46 | |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 47 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 48 | .. method:: enable() |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 49 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 50 | Mark the breakpoint as enabled. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 51 | |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 52 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 53 | .. method:: disable() |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 54 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 55 | Mark the breakpoint as disabled. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 56 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 57 | |
| Georg Brandl | d2fd4ca | 2010-07-30 15:01:23 +0000 | [diff] [blame] | 58 | .. method:: bpformat() |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 59 | |
| Georg Brandl | d2fd4ca | 2010-07-30 15:01:23 +0000 | [diff] [blame] | 60 | Return a string with all the information about the breakpoint, nicely |
| 61 | formatted: |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 62 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 63 | * Breakpoint number. |
| 64 | * Temporary status (del or keep). |
| 65 | * File/line position. |
| 66 | * Break condition. |
| 67 | * Number of times to ignore. |
| 68 | * Number of times hit. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 69 | |
| Georg Brandl | d2fd4ca | 2010-07-30 15:01:23 +0000 | [diff] [blame] | 70 | .. versionadded:: 3.2 |
| 71 | |
| 72 | .. method:: bpprint(out=None) |
| 73 | |
| 74 | Print the output of :meth:`bpformat` to the file *out*, or if it is |
| 75 | ``None``, to standard output. |
| 76 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 77 | :class:`Breakpoint` instances have the following attributes: |
| 78 | |
| 79 | .. attribute:: file |
| 80 | |
| 81 | File name of the :class:`Breakpoint`. |
| 82 | |
| 83 | .. attribute:: line |
| 84 | |
| 85 | Line number of the :class:`Breakpoint` within :attr:`file`. |
| 86 | |
| 87 | .. attribute:: temporary |
| 88 | |
| 89 | True if a :class:`Breakpoint` at (file, line) is temporary. |
| 90 | |
| 91 | .. attribute:: cond |
| 92 | |
| 93 | Condition for evaluating a :class:`Breakpoint` at (file, line). |
| 94 | |
| 95 | .. attribute:: funcname |
| 96 | |
| 97 | Function name that defines whether a :class:`Breakpoint` is hit upon |
| 98 | entering the function. |
| 99 | |
| 100 | .. attribute:: enabled |
| 101 | |
| 102 | True if :class:`Breakpoint` is enabled. |
| 103 | |
| 104 | .. attribute:: bpbynumber |
| 105 | |
| 106 | Numeric index for a single instance of a :class:`Breakpoint`. |
| 107 | |
| 108 | .. attribute:: bplist |
| 109 | |
| 110 | Dictionary of :class:`Breakpoint` instances indexed by |
| 111 | (:attr:`file`, :attr:`line`) tuples. |
| 112 | |
| 113 | .. attribute:: ignore |
| 114 | |
| 115 | Number of times to ignore a :class:`Breakpoint`. |
| 116 | |
| 117 | .. attribute:: hits |
| 118 | |
| 119 | Count of the number of times a :class:`Breakpoint` has been hit. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 120 | |
| Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 121 | .. class:: Bdb(skip=None) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 122 | |
| Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 123 | The :class:`Bdb` class acts as a generic Python debugger base class. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 124 | |
| 125 | This class takes care of the details of the trace facility; a derived class |
| 126 | should implement user interaction. The standard debugger class |
| 127 | (:class:`pdb.Pdb`) is an example. |
| 128 | |
| Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 129 | The *skip* argument, if given, must be an iterable of glob-style |
| 130 | module name patterns. The debugger will not step into frames that |
| 131 | originate in a module that matches one of these patterns. Whether a |
| 132 | frame is considered to originate in a certain module is determined |
| 133 | by the ``__name__`` in the frame globals. |
| 134 | |
| Ezio Melotti | 83fc6dd | 2010-01-27 22:44:03 +0000 | [diff] [blame] | 135 | .. versionadded:: 3.1 |
| Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 136 | The *skip* argument. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 137 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 138 | The following methods of :class:`Bdb` normally don't need to be overridden. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 139 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 140 | .. method:: canonic(filename) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 141 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 142 | Return canonical form of *filename*. |
| 143 | |
| 144 | For real file names, the canonical form is an operating-system-dependent, |
| 145 | :func:`case-normalized <os.path.normcase>` :func:`absolute path |
| C.A.M. Gerlach | ea19c28 | 2022-10-17 18:49:38 -0500 | [diff] [blame] | 146 | <os.path.abspath>`. A *filename* with angle brackets, such as ``"<stdin>"`` |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 147 | generated in interactive mode, is returned unchanged. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 148 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 149 | .. method:: reset() |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 150 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 151 | Set the :attr:`botframe`, :attr:`stopframe`, :attr:`returnframe` and |
| 152 | :attr:`quitting` attributes with values ready to start debugging. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 153 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 154 | .. method:: trace_dispatch(frame, event, arg) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 155 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 156 | This function is installed as the trace function of debugged frames. Its |
| 157 | return value is the new trace function (in most cases, that is, itself). |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 158 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 159 | The default implementation decides how to dispatch a frame, depending on |
| 160 | the type of event (passed as a string) that is about to be executed. |
| 161 | *event* can be one of the following: |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 162 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 163 | * ``"line"``: A new line of code is going to be executed. |
| 164 | * ``"call"``: A function is about to be called, or another code block |
| 165 | entered. |
| 166 | * ``"return"``: A function or other code block is about to return. |
| 167 | * ``"exception"``: An exception has occurred. |
| 168 | * ``"c_call"``: A C function is about to be called. |
| 169 | * ``"c_return"``: A C function has returned. |
| Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 170 | * ``"c_exception"``: A C function has raised an exception. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 171 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 172 | For the Python events, specialized functions (see below) are called. For |
| 173 | the C events, no action is taken. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 174 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 175 | The *arg* parameter depends on the previous event. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 176 | |
| Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 177 | See the documentation for :func:`sys.settrace` for more information on the |
| 178 | trace function. For more information on code and frame objects, refer to |
| 179 | :ref:`types`. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 180 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 181 | .. method:: dispatch_line(frame) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 182 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 183 | If the debugger should stop on the current line, invoke the |
| 184 | :meth:`user_line` method (which should be overridden in subclasses). |
| 185 | Raise a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set |
| 186 | (which can be set from :meth:`user_line`). Return a reference to the |
| 187 | :meth:`trace_dispatch` method for further tracing in that scope. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 188 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 189 | .. method:: dispatch_call(frame, arg) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 190 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 191 | If the debugger should stop on this function call, invoke the |
| 192 | :meth:`user_call` method (which should be overridden in subclasses). |
| 193 | Raise a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set |
| 194 | (which can be set from :meth:`user_call`). Return a reference to the |
| 195 | :meth:`trace_dispatch` method for further tracing in that scope. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 196 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 197 | .. method:: dispatch_return(frame, arg) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 198 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 199 | If the debugger should stop on this function return, invoke the |
| 200 | :meth:`user_return` method (which should be overridden in subclasses). |
| 201 | Raise a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set |
| 202 | (which can be set from :meth:`user_return`). Return a reference to the |
| 203 | :meth:`trace_dispatch` method for further tracing in that scope. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 204 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 205 | .. method:: dispatch_exception(frame, arg) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 206 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 207 | If the debugger should stop at this exception, invokes the |
| 208 | :meth:`user_exception` method (which should be overridden in subclasses). |
| 209 | Raise a :exc:`BdbQuit` exception if the :attr:`Bdb.quitting` flag is set |
| 210 | (which can be set from :meth:`user_exception`). Return a reference to the |
| 211 | :meth:`trace_dispatch` method for further tracing in that scope. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 212 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 213 | Normally derived classes don't override the following methods, but they may |
| 214 | if they want to redefine the definition of stopping and breakpoints. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 215 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 216 | .. method:: is_skipped_line(module_name) |
| 217 | |
| 218 | Return True if *module_name* matches any skip pattern. |
| 219 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 220 | .. method:: stop_here(frame) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 221 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 222 | Return True if *frame* is below the starting frame in the stack. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 223 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 224 | .. method:: break_here(frame) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 225 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 226 | Return True if there is an effective breakpoint for this line. |
| 227 | |
| 228 | Check whether a line or function breakpoint exists and is in effect. Delete temporary |
| 229 | breakpoints based on information from :func:`effective`. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 230 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 231 | .. method:: break_anywhere(frame) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 232 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 233 | Return True if any breakpoint exists for *frame*'s filename. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 234 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 235 | Derived classes should override these methods to gain control over debugger |
| 236 | operation. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 237 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 238 | .. method:: user_call(frame, argument_list) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 239 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 240 | Called from :meth:`dispatch_call` if a break might stop inside the |
| 241 | called function. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 242 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 243 | .. method:: user_line(frame) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 244 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 245 | Called from :meth:`dispatch_line` when either :meth:`stop_here` or |
| 246 | :meth:`break_here` returns ``True``. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 247 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 248 | .. method:: user_return(frame, return_value) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 249 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 250 | Called from :meth:`dispatch_return` when :meth:`stop_here` returns ``True``. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 251 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 252 | .. method:: user_exception(frame, exc_info) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 253 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 254 | Called from :meth:`dispatch_exception` when :meth:`stop_here` |
| 255 | returns ``True``. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 256 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 257 | .. method:: do_clear(arg) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 258 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 259 | Handle how a breakpoint must be removed when it is a temporary one. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 260 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 261 | This method must be implemented by derived classes. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 262 | |
| 263 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 264 | Derived classes and clients can call the following methods to affect the |
| 265 | stepping state. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 266 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 267 | .. method:: set_step() |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 268 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 269 | Stop after one line of code. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 270 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 271 | .. method:: set_next(frame) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 272 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 273 | Stop on the next line in or below the given frame. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 274 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 275 | .. method:: set_return(frame) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 276 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 277 | Stop when returning from the given frame. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 278 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 279 | .. method:: set_until(frame, lineno=None) |
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 280 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 281 | Stop when the line with the *lineno* greater than the current one is |
| Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 282 | reached or when returning from current frame. |
| Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 283 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 284 | .. method:: set_trace([frame]) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 285 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 286 | Start debugging from *frame*. If *frame* is not specified, debugging |
| 287 | starts from caller's frame. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 288 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 289 | .. method:: set_continue() |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 290 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 291 | Stop only at breakpoints or when finished. If there are no breakpoints, |
| Serhiy Storchaka | ecf41da | 2016-10-19 16:29:26 +0300 | [diff] [blame] | 292 | set the system trace function to ``None``. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 293 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 294 | .. method:: set_quit() |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 295 | |
| Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 296 | Set the :attr:`quitting` attribute to ``True``. This raises :exc:`BdbQuit` in |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 297 | the next call to one of the :meth:`dispatch_\*` methods. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 298 | |
| 299 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 300 | Derived classes and clients can call the following methods to manipulate |
| 301 | breakpoints. These methods return a string containing an error message if |
| 302 | something went wrong, or ``None`` if all is well. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 303 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 304 | .. method:: set_break(filename, lineno, temporary=False, cond=None, funcname=None) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 305 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 306 | Set a new breakpoint. If the *lineno* line doesn't exist for the |
| 307 | *filename* passed as argument, return an error message. The *filename* |
| 308 | should be in canonical form, as described in the :meth:`canonic` method. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 309 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 310 | .. method:: clear_break(filename, lineno) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 311 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 312 | Delete the breakpoints in *filename* and *lineno*. If none were set, |
| 313 | return an error message. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 314 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 315 | .. method:: clear_bpbynumber(arg) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 316 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 317 | Delete the breakpoint which has the index *arg* in the |
| 318 | :attr:`Breakpoint.bpbynumber`. If *arg* is not numeric or out of range, |
| 319 | return an error message. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 320 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 321 | .. method:: clear_all_file_breaks(filename) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 322 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 323 | Delete all breakpoints in *filename*. If none were set, return an error |
| 324 | message. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 325 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 326 | .. method:: clear_all_breaks() |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 327 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 328 | Delete all existing breakpoints. If none were set, return an error |
| 329 | message. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 330 | |
| Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 331 | .. method:: get_bpbynumber(arg) |
| 332 | |
| 333 | Return a breakpoint specified by the given number. If *arg* is a string, |
| 334 | it will be converted to a number. If *arg* is a non-numeric string, if |
| 335 | the given breakpoint never existed or has been deleted, a |
| 336 | :exc:`ValueError` is raised. |
| 337 | |
| 338 | .. versionadded:: 3.2 |
| 339 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 340 | .. method:: get_break(filename, lineno) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 341 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 342 | Return True if there is a breakpoint for *lineno* in *filename*. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 343 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 344 | .. method:: get_breaks(filename, lineno) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 345 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 346 | Return all breakpoints for *lineno* in *filename*, or an empty list if |
| 347 | none are set. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 348 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 349 | .. method:: get_file_breaks(filename) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 350 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 351 | Return all breakpoints in *filename*, or an empty list if none are set. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 352 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 353 | .. method:: get_all_breaks() |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 354 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 355 | Return all breakpoints that are set. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 356 | |
| 357 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 358 | Derived classes and clients can call the following methods to get a data |
| 359 | structure representing a stack trace. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 360 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 361 | .. method:: get_stack(f, t) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 362 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 363 | Return a list of (frame, lineno) tuples in a stack trace, and a size. |
| 364 | |
| 365 | The most recently called frame is last in the list. The size is the number |
| 366 | of frames below the frame where the debugger was invoked. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 367 | |
| Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 368 | .. method:: format_stack_entry(frame_lineno, lprefix=': ') |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 369 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 370 | Return a string with information about a stack entry, which is a |
| 371 | ``(frame, lineno)`` tuple. The return string contains: |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 372 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 373 | * The canonical filename which contains the frame. |
| 374 | * The function name or ``"<lambda>"``. |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 375 | * The input arguments. |
| 376 | * The return value. |
| 377 | * The line of code (if it exists). |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 378 | |
| 379 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 380 | The following two methods can be called by clients to use a debugger to debug |
| 381 | a :term:`statement`, given as a string. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 382 | |
| Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 383 | .. method:: run(cmd, globals=None, locals=None) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 384 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 385 | Debug a statement executed via the :func:`exec` function. *globals* |
| 386 | defaults to :attr:`__main__.__dict__`, *locals* defaults to *globals*. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 387 | |
| Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 388 | .. method:: runeval(expr, globals=None, locals=None) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 389 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 390 | Debug an expression executed via the :func:`eval` function. *globals* and |
| 391 | *locals* have the same meaning as in :meth:`run`. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 392 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 393 | .. method:: runctx(cmd, globals, locals) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 394 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 395 | For backwards compatibility. Calls the :meth:`run` method. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 396 | |
| Serhiy Storchaka | 142566c | 2019-06-05 18:22:31 +0300 | [diff] [blame] | 397 | .. method:: runcall(func, /, *args, **kwds) |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 398 | |
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 399 | Debug a single function call, and return its result. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 400 | |
| 401 | |
| 402 | Finally, the module defines the following functions: |
| 403 | |
| 404 | .. function:: checkfuncname(b, frame) |
| 405 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 406 | Return True if we should break here, depending on the way the |
| 407 | :class:`Breakpoint` *b* was set. |
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 408 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 409 | If it was set via line number, it checks if |
| 410 | :attr:`b.line <bdb.Breakpoint.line>` is the same as the one in *frame*. |
| 411 | If the breakpoint was set via |
| 412 | :attr:`function name <bdb.Breakpoint.funcname>`, we have to check we are in |
| 413 | the right *frame* (the right function) and if we are on its first executable |
| 414 | line. |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 415 | |
| 416 | .. function:: effective(file, line, frame) |
| 417 | |
| Miss Islington (bot) | 02c59be | 2022-09-02 10:48:26 -0700 | [diff] [blame] | 418 | Return ``(active breakpoint, delete temporary flag)`` or ``(None, None)`` as the |
| 419 | breakpoint to act upon. |
| 420 | |
| 421 | The *active breakpoint* is the first entry in |
| 422 | :attr:`bplist <bdb.Breakpoint.bplist>` for the |
| 423 | (:attr:`file <bdb.Breakpoint.file>`, :attr:`line <bdb.Breakpoint.line>`) |
| 424 | (which must exist) that is :attr:`enabled <bdb.Breakpoint.enabled>`, for |
| 425 | which :func:`checkfuncname` is True, and that has neither a False |
| 426 | :attr:`condition <bdb.Breakpoint.cond>` nor positive |
| 427 | :attr:`ignore <bdb.Breakpoint.ignore>` count. The *flag*, meaning that a |
| 428 | temporary breakpoint should be deleted, is False only when the |
| 429 | :attr:`cond <bdb.Breakpoint.cond>` cannot be evaluated (in which case, |
| 430 | :attr:`ignore <bdb.Breakpoint.ignore>` count is ignored). |
| 431 | |
| 432 | If no such entry exists, then (None, None) is returned. |
| 433 | |
| Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 434 | |
| 435 | .. function:: set_trace() |
| 436 | |
| Georg Brandl | f51a6c7 | 2010-11-26 12:05:48 +0000 | [diff] [blame] | 437 | Start debugging with a :class:`Bdb` instance from caller's frame. |