Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 2 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 3 | """ |
| 4 | The Python Debugger Pdb |
| 5 | ======================= |
Guido van Rossum | 92df0c6 | 1992-01-14 18:30:15 +0000 | [diff] [blame] | 6 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 7 | To use the debugger in its simplest form: |
| 8 | |
| 9 | >>> import pdb |
| 10 | >>> pdb.run('<a statement>') |
| 11 | |
| 12 | The debugger's prompt is '(Pdb) '. This will stop in the first |
| 13 | function call in <a statement>. |
| 14 | |
| 15 | Alternatively, if a statement terminated with an unhandled exception, |
| 16 | you can use pdb's post-mortem facility to inspect the contents of the |
| 17 | traceback: |
| 18 | |
| 19 | >>> <a statement> |
| 20 | <exception traceback> |
| 21 | >>> import pdb |
| 22 | >>> pdb.pm() |
| 23 | |
| 24 | The commands recognized by the debugger are listed in the next |
| 25 | section. Most can be abbreviated as indicated; e.g., h(elp) means |
| 26 | that 'help' can be typed as 'h' or 'help' (but not as 'he' or 'hel', |
| 27 | nor as 'H' or 'Help' or 'HELP'). Optional arguments are enclosed in |
| 28 | square brackets. Alternatives in the command syntax are separated |
| 29 | by a vertical bar (|). |
| 30 | |
| 31 | A blank line repeats the previous command literally, except for |
| 32 | 'list', where it lists the next 11 lines. |
| 33 | |
| 34 | Commands that the debugger doesn't recognize are assumed to be Python |
| 35 | statements and are executed in the context of the program being |
| 36 | debugged. Python statements can also be prefixed with an exclamation |
| 37 | point ('!'). This is a powerful way to inspect the program being |
| 38 | debugged; it is even possible to change variables or call functions. |
| 39 | When an exception occurs in such a statement, the exception name is |
| 40 | printed but the debugger's state is not changed. |
| 41 | |
| 42 | The debugger supports aliases, which can save typing. And aliases can |
| 43 | have parameters (see the alias help entry) which allows one a certain |
| 44 | level of adaptability to the context under examination. |
| 45 | |
| 46 | Multiple commands may be entered on a single line, separated by the |
| 47 | pair ';;'. No intelligence is applied to separating the commands; the |
| 48 | input is split at the first ';;', even if it is in the middle of a |
| 49 | quoted string. |
| 50 | |
| 51 | If a file ".pdbrc" exists in your home directory or in the current |
| 52 | directory, it is read in and executed as if it had been typed at the |
| 53 | debugger prompt. This is particularly useful for aliases. If both |
| 54 | files exist, the one in the home directory is read first and aliases |
Ćukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 55 | defined there can be overridden by the local file. This behavior can be |
| 56 | disabled by passing the "readrc=False" argument to the Pdb constructor. |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 57 | |
| 58 | Aside from aliases, the debugger is not directly programmable; but it |
| 59 | is implemented as a class from which you can derive your own debugger |
| 60 | class, which you can make as fancy as you like. |
| 61 | |
| 62 | |
| 63 | Debugger commands |
| 64 | ================= |
| 65 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 66 | """ |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 67 | # NOTE: the actual command documentation is collected from docstrings of the |
| 68 | # commands and is appended to __doc__ after the class has been defined. |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 69 | |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 70 | import os |
| 71 | import re |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 72 | import sys |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 73 | import cmd |
| 74 | import bdb |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 75 | import dis |
Georg Brandl | 1acb746 | 2010-12-04 11:20:26 +0000 | [diff] [blame] | 76 | import code |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 77 | import glob |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 78 | import pprint |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 79 | import signal |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 80 | import inspect |
Georg Brandl | 1acb746 | 2010-12-04 11:20:26 +0000 | [diff] [blame] | 81 | import traceback |
| 82 | import linecache |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | class Restart(Exception): |
| 86 | """Causes a debugger to be restarted for the debugged python program.""" |
| 87 | pass |
| 88 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 89 | __all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace", |
| 90 | "post_mortem", "help"] |
| 91 | |
Barry Warsaw | 2bee8fe | 1999-09-09 16:32:41 +0000 | [diff] [blame] | 92 | def find_function(funcname, filename): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 93 | cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 94 | try: |
| 95 | fp = open(filename) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 96 | except OSError: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 97 | return None |
| 98 | # consumer of this info expects the first line to be 1 |
Georg Brandl | 6e22055 | 2013-10-13 20:51:47 +0200 | [diff] [blame] | 99 | with fp: |
| 100 | for lineno, line in enumerate(fp, start=1): |
| 101 | if cre.match(line): |
| 102 | return funcname, filename, lineno |
| 103 | return None |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 104 | |
Georg Brandl | 5ed2b5a | 2010-07-30 18:08:12 +0000 | [diff] [blame] | 105 | def getsourcelines(obj): |
| 106 | lines, lineno = inspect.findsource(obj) |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 107 | if inspect.isframe(obj) and obj.f_globals is obj.f_locals: |
Georg Brandl | 5ed2b5a | 2010-07-30 18:08:12 +0000 | [diff] [blame] | 108 | # must be a module frame: do not try to cut a block out of it |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 109 | return lines, 1 |
Georg Brandl | 5ed2b5a | 2010-07-30 18:08:12 +0000 | [diff] [blame] | 110 | elif inspect.ismodule(obj): |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 111 | return lines, 1 |
Georg Brandl | 5ed2b5a | 2010-07-30 18:08:12 +0000 | [diff] [blame] | 112 | return inspect.getblock(lines[lineno:]), lineno+1 |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 113 | |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 114 | def lasti2lineno(code, lasti): |
| 115 | linestarts = list(dis.findlinestarts(code)) |
| 116 | linestarts.reverse() |
| 117 | for i, lineno in linestarts: |
| 118 | if lasti >= i: |
| 119 | return lineno |
| 120 | return 0 |
| 121 | |
| 122 | |
Georg Brandl | cbc79c7 | 2010-12-04 16:21:42 +0000 | [diff] [blame] | 123 | class _rstr(str): |
| 124 | """String that doesn't quote its repr.""" |
| 125 | def __repr__(self): |
| 126 | return self |
| 127 | |
| 128 | |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 129 | # Interaction prompt line will separate file and call info from code |
| 130 | # text using value of line_prefix string. A newline and arrow may |
| 131 | # be to your liking. You can set it once pdb is imported using the |
| 132 | # command "pdb.line_prefix = '\n% '". |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 133 | # line_prefix = ': ' # Use this to get the old situation back |
| 134 | line_prefix = '\n-> ' # Probably a better default |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 136 | class Pdb(bdb.Bdb, cmd.Cmd): |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 137 | |
Xavier de Gaye | 10e54ae | 2016-10-12 20:13:24 +0200 | [diff] [blame] | 138 | _previous_sigint_handler = None |
| 139 | |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 140 | def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, |
Ćukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 141 | nosigint=False, readrc=True): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 142 | bdb.Bdb.__init__(self, skip=skip) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 143 | cmd.Cmd.__init__(self, completekey, stdin, stdout) |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame^] | 144 | sys.audit("pdb.Pdb") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 145 | if stdout: |
| 146 | self.use_rawinput = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 147 | self.prompt = '(Pdb) ' |
| 148 | self.aliases = {} |
Georg Brandl | cbc79c7 | 2010-12-04 16:21:42 +0000 | [diff] [blame] | 149 | self.displaying = {} |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 150 | self.mainpyfile = '' |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 151 | self._wait_for_mainpyfile = False |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 152 | self.tb_lineno = {} |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 153 | # Try to load readline if it exists |
| 154 | try: |
| 155 | import readline |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 156 | # remove some common file name delimiters |
| 157 | readline.set_completer_delims(' \t\n`@#$%^&*()=+[{]}\\|;:\'",<>?') |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 158 | except ImportError: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 159 | pass |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 160 | self.allow_kbdint = False |
| 161 | self.nosigint = nosigint |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 162 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 163 | # Read $HOME/.pdbrc and ./.pdbrc |
| 164 | self.rcLines = [] |
Ćukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 165 | if readrc: |
| 166 | if 'HOME' in os.environ: |
| 167 | envHome = os.environ['HOME'] |
| 168 | try: |
| 169 | with open(os.path.join(envHome, ".pdbrc")) as rcFile: |
| 170 | self.rcLines.extend(rcFile) |
| 171 | except OSError: |
| 172 | pass |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 173 | try: |
Ćukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 174 | with open(".pdbrc") as rcFile: |
Florent Xicluna | 7dde792 | 2010-09-03 19:52:03 +0000 | [diff] [blame] | 175 | self.rcLines.extend(rcFile) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 176 | except OSError: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 177 | pass |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 178 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 179 | self.commands = {} # associates a command list to breakpoint numbers |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 180 | self.commands_doprompt = {} # for each bp num, tells if the prompt |
| 181 | # must be disp. after execing the cmd list |
| 182 | self.commands_silent = {} # for each bp num, tells if the stack trace |
| 183 | # must be disp. after execing the cmd list |
| 184 | self.commands_defining = False # True while in the process of defining |
| 185 | # a command list |
| 186 | self.commands_bnum = None # The breakpoint number for which we are |
| 187 | # defining a list |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 188 | |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 189 | def sigint_handler(self, signum, frame): |
| 190 | if self.allow_kbdint: |
| 191 | raise KeyboardInterrupt |
| 192 | self.message("\nProgram interrupted. (Use 'cont' to resume).") |
| 193 | self.set_step() |
| 194 | self.set_trace(frame) |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 195 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 196 | def reset(self): |
| 197 | bdb.Bdb.reset(self) |
| 198 | self.forget() |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 199 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 200 | def forget(self): |
| 201 | self.lineno = None |
| 202 | self.stack = [] |
| 203 | self.curindex = 0 |
| 204 | self.curframe = None |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 205 | self.tb_lineno.clear() |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 206 | |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 207 | def setup(self, f, tb): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 208 | self.forget() |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 209 | self.stack, self.curindex = self.get_stack(f, tb) |
| 210 | while tb: |
| 211 | # when setting up post-mortem debugging with a traceback, save all |
| 212 | # the original line numbers to be displayed along the current line |
| 213 | # numbers (which can be different, e.g. due to finally clauses) |
| 214 | lineno = lasti2lineno(tb.tb_frame.f_code, tb.tb_lasti) |
| 215 | self.tb_lineno[tb.tb_frame] = lineno |
| 216 | tb = tb.tb_next |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 217 | self.curframe = self.stack[self.curindex][0] |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 218 | # The f_locals dictionary is updated from the actual frame |
| 219 | # locals whenever the .f_locals accessor is called, so we |
| 220 | # cache it here to ensure that modifications are not overwritten. |
| 221 | self.curframe_locals = self.curframe.f_locals |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 222 | return self.execRcLines() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 223 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 224 | # Can be executed earlier than 'setup' if desired |
| 225 | def execRcLines(self): |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 226 | if not self.rcLines: |
| 227 | return |
| 228 | # local copy because of recursion |
| 229 | rcLines = self.rcLines |
| 230 | rcLines.reverse() |
| 231 | # execute every line only once |
| 232 | self.rcLines = [] |
| 233 | while rcLines: |
| 234 | line = rcLines.pop().strip() |
| 235 | if line and line[0] != '#': |
| 236 | if self.onecmd(line): |
| 237 | # if onecmd returns True, the command wants to exit |
| 238 | # from the interaction, save leftover rc lines |
| 239 | # to execute before next interaction |
| 240 | self.rcLines += reversed(rcLines) |
| 241 | return True |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 242 | |
Tim Peters | 280488b | 2002-08-23 18:19:30 +0000 | [diff] [blame] | 243 | # Override Bdb methods |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 244 | |
| 245 | def user_call(self, frame, argument_list): |
| 246 | """This method is called when there is the remote possibility |
| 247 | that we ever need to stop in this function.""" |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 248 | if self._wait_for_mainpyfile: |
| 249 | return |
Michael W. Hudson | 01eb85c | 2003-01-31 17:48:29 +0000 | [diff] [blame] | 250 | if self.stop_here(frame): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 251 | self.message('--Call--') |
Michael W. Hudson | 01eb85c | 2003-01-31 17:48:29 +0000 | [diff] [blame] | 252 | self.interaction(frame, None) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 253 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 254 | def user_line(self, frame): |
| 255 | """This function is called when we stop or break at this line.""" |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 256 | if self._wait_for_mainpyfile: |
| 257 | if (self.mainpyfile != self.canonic(frame.f_code.co_filename) |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 258 | or frame.f_lineno <= 0): |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 259 | return |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 260 | self._wait_for_mainpyfile = False |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 261 | if self.bp_commands(frame): |
| 262 | self.interaction(frame, None) |
| 263 | |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 264 | def bp_commands(self, frame): |
Georg Brandl | 3078df0 | 2009-05-05 09:11:31 +0000 | [diff] [blame] | 265 | """Call every command that was set for the current active breakpoint |
| 266 | (if there is one). |
| 267 | |
| 268 | Returns True if the normal interaction function must be called, |
| 269 | False otherwise.""" |
| 270 | # self.currentbp is set in bdb in Bdb.break_here if a breakpoint was hit |
| 271 | if getattr(self, "currentbp", False) and \ |
| 272 | self.currentbp in self.commands: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 273 | currentbp = self.currentbp |
| 274 | self.currentbp = 0 |
| 275 | lastcmd_back = self.lastcmd |
| 276 | self.setup(frame, None) |
| 277 | for line in self.commands[currentbp]: |
| 278 | self.onecmd(line) |
| 279 | self.lastcmd = lastcmd_back |
| 280 | if not self.commands_silent[currentbp]: |
| 281 | self.print_stack_entry(self.stack[self.curindex]) |
| 282 | if self.commands_doprompt[currentbp]: |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 283 | self._cmdloop() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 284 | self.forget() |
| 285 | return |
| 286 | return 1 |
Guido van Rossum | 9e1ee97 | 1997-07-11 13:43:53 +0000 | [diff] [blame] | 287 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 288 | def user_return(self, frame, return_value): |
| 289 | """This function is called when a return trap is set here.""" |
Georg Brandl | 34cc0f5 | 2010-07-30 09:43:00 +0000 | [diff] [blame] | 290 | if self._wait_for_mainpyfile: |
| 291 | return |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 292 | frame.f_locals['__return__'] = return_value |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 293 | self.message('--Return--') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 294 | self.interaction(frame, None) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 296 | def user_exception(self, frame, exc_info): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 297 | """This function is called if an exception occurs, |
| 298 | but only if we are to stop at or just below this level.""" |
Georg Brandl | 34cc0f5 | 2010-07-30 09:43:00 +0000 | [diff] [blame] | 299 | if self._wait_for_mainpyfile: |
| 300 | return |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 301 | exc_type, exc_value, exc_traceback = exc_info |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 302 | frame.f_locals['__exception__'] = exc_type, exc_value |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 303 | |
| 304 | # An 'Internal StopIteration' exception is an exception debug event |
| 305 | # issued by the interpreter when handling a subgenerator run with |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 306 | # 'yield from' or a generator controlled by a for loop. No exception has |
Berker Peksag | f23530f | 2014-10-19 18:04:38 +0300 | [diff] [blame] | 307 | # actually occurred in this case. The debugger uses this debug event to |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 308 | # stop when the debuggee is returning from such generators. |
| 309 | prefix = 'Internal ' if (not exc_traceback |
| 310 | and exc_type is StopIteration) else '' |
| 311 | self.message('%s%s' % (prefix, |
| 312 | traceback.format_exception_only(exc_type, exc_value)[-1].strip())) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 313 | self.interaction(frame, exc_traceback) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 314 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 315 | # General interaction function |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 316 | def _cmdloop(self): |
| 317 | while True: |
| 318 | try: |
| 319 | # keyboard interrupts allow for an easy way to cancel |
| 320 | # the current command, so allow them during interactive input |
| 321 | self.allow_kbdint = True |
| 322 | self.cmdloop() |
| 323 | self.allow_kbdint = False |
| 324 | break |
| 325 | except KeyboardInterrupt: |
| 326 | self.message('--KeyboardInterrupt--') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 327 | |
Georg Brandl | cbc79c7 | 2010-12-04 16:21:42 +0000 | [diff] [blame] | 328 | # Called before loop, handles display expressions |
| 329 | def preloop(self): |
| 330 | displaying = self.displaying.get(self.curframe) |
| 331 | if displaying: |
| 332 | for expr, oldvalue in displaying.items(): |
| 333 | newvalue = self._getval_except(expr) |
| 334 | # check for identity first; this prevents custom __eq__ to |
| 335 | # be called at every loop, and also prevents instances whose |
| 336 | # fields are changed to be displayed |
| 337 | if newvalue is not oldvalue and newvalue != oldvalue: |
| 338 | displaying[expr] = newvalue |
| 339 | self.message('display %s: %r [old: %r]' % |
| 340 | (expr, newvalue, oldvalue)) |
| 341 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 342 | def interaction(self, frame, traceback): |
Xavier de Gaye | 10e54ae | 2016-10-12 20:13:24 +0200 | [diff] [blame] | 343 | # Restore the previous signal handler at the Pdb prompt. |
| 344 | if Pdb._previous_sigint_handler: |
| 345 | signal.signal(signal.SIGINT, Pdb._previous_sigint_handler) |
| 346 | Pdb._previous_sigint_handler = None |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 347 | if self.setup(frame, traceback): |
| 348 | # no interaction desired at this time (happens if .pdbrc contains |
| 349 | # a command like "continue") |
| 350 | self.forget() |
| 351 | return |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 352 | self.print_stack_entry(self.stack[self.curindex]) |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 353 | self._cmdloop() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 354 | self.forget() |
| 355 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 356 | def displayhook(self, obj): |
| 357 | """Custom displayhook for the exec in default(), which prevents |
| 358 | assignment of the _ variable in the builtins. |
| 359 | """ |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 360 | # reproduce the behavior of the standard displayhook, not printing None |
| 361 | if obj is not None: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 362 | self.message(repr(obj)) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 363 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 364 | def default(self, line): |
| 365 | if line[:1] == '!': line = line[1:] |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 366 | locals = self.curframe_locals |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 367 | globals = self.curframe.f_globals |
| 368 | try: |
| 369 | code = compile(line + '\n', '<stdin>', 'single') |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 370 | save_stdout = sys.stdout |
| 371 | save_stdin = sys.stdin |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 372 | save_displayhook = sys.displayhook |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 373 | try: |
| 374 | sys.stdin = self.stdin |
| 375 | sys.stdout = self.stdout |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 376 | sys.displayhook = self.displayhook |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 377 | exec(code, globals, locals) |
| 378 | finally: |
| 379 | sys.stdout = save_stdout |
| 380 | sys.stdin = save_stdin |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 381 | sys.displayhook = save_displayhook |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 382 | except: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 383 | exc_info = sys.exc_info()[:2] |
| 384 | self.error(traceback.format_exception_only(*exc_info)[-1].strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 385 | |
| 386 | def precmd(self, line): |
| 387 | """Handle alias expansion and ';;' separator.""" |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 388 | if not line.strip(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 389 | return line |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 390 | args = line.split() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 391 | while args[0] in self.aliases: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 392 | line = self.aliases[args[0]] |
| 393 | ii = 1 |
| 394 | for tmpArg in args[1:]: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 395 | line = line.replace("%" + str(ii), |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 396 | tmpArg) |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 397 | ii += 1 |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 398 | line = line.replace("%*", ' '.join(args[1:])) |
| 399 | args = line.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 400 | # split into ';;' separated commands |
| 401 | # unless it's an alias command |
| 402 | if args[0] != 'alias': |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 403 | marker = line.find(';;') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 404 | if marker >= 0: |
| 405 | # queue up everything after marker |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 406 | next = line[marker+2:].lstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 407 | self.cmdqueue.append(next) |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 408 | line = line[:marker].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 409 | return line |
| 410 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 411 | def onecmd(self, line): |
| 412 | """Interpret the argument as though it had been typed in response |
| 413 | to the prompt. |
| 414 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 415 | Checks whether this line is typed at the normal prompt or in |
| 416 | a breakpoint command list definition. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 417 | """ |
| 418 | if not self.commands_defining: |
| 419 | return cmd.Cmd.onecmd(self, line) |
| 420 | else: |
| 421 | return self.handle_command_def(line) |
| 422 | |
Georg Brandl | b90ffd8 | 2010-07-30 22:20:16 +0000 | [diff] [blame] | 423 | def handle_command_def(self, line): |
Georg Brandl | 44f8bf9 | 2010-07-30 08:54:49 +0000 | [diff] [blame] | 424 | """Handles one command line during command list definition.""" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 425 | cmd, arg, line = self.parseline(line) |
Georg Brandl | 44f8bf9 | 2010-07-30 08:54:49 +0000 | [diff] [blame] | 426 | if not cmd: |
| 427 | return |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 428 | if cmd == 'silent': |
| 429 | self.commands_silent[self.commands_bnum] = True |
| 430 | return # continue to handle other cmd def in the cmd list |
| 431 | elif cmd == 'end': |
| 432 | self.cmdqueue = [] |
| 433 | return 1 # end of cmd list |
| 434 | cmdlist = self.commands[self.commands_bnum] |
Georg Brandl | 44f8bf9 | 2010-07-30 08:54:49 +0000 | [diff] [blame] | 435 | if arg: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 436 | cmdlist.append(cmd+' '+arg) |
| 437 | else: |
| 438 | cmdlist.append(cmd) |
| 439 | # Determine if we must stop |
| 440 | try: |
| 441 | func = getattr(self, 'do_' + cmd) |
| 442 | except AttributeError: |
| 443 | func = self.default |
Georg Brandl | 3078df0 | 2009-05-05 09:11:31 +0000 | [diff] [blame] | 444 | # one of the resuming commands |
| 445 | if func.__name__ in self.commands_resuming: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 446 | self.commands_doprompt[self.commands_bnum] = False |
| 447 | self.cmdqueue = [] |
| 448 | return 1 |
| 449 | return |
| 450 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 451 | # interface abstraction functions |
| 452 | |
| 453 | def message(self, msg): |
| 454 | print(msg, file=self.stdout) |
| 455 | |
| 456 | def error(self, msg): |
| 457 | print('***', msg, file=self.stdout) |
| 458 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 459 | # Generic completion functions. Individual complete_foo methods can be |
| 460 | # assigned below to one of these functions. |
| 461 | |
| 462 | def _complete_location(self, text, line, begidx, endidx): |
| 463 | # Complete a file/module/function location for break/tbreak/clear. |
| 464 | if line.strip().endswith((':', ',')): |
| 465 | # Here comes a line number or a condition which we can't complete. |
| 466 | return [] |
| 467 | # First, try to find matching functions (i.e. expressions). |
| 468 | try: |
| 469 | ret = self._complete_expression(text, line, begidx, endidx) |
| 470 | except Exception: |
| 471 | ret = [] |
| 472 | # Then, try to complete file names as well. |
| 473 | globs = glob.glob(text + '*') |
| 474 | for fn in globs: |
| 475 | if os.path.isdir(fn): |
| 476 | ret.append(fn + '/') |
| 477 | elif os.path.isfile(fn) and fn.lower().endswith(('.py', '.pyw')): |
| 478 | ret.append(fn + ':') |
| 479 | return ret |
| 480 | |
| 481 | def _complete_bpnumber(self, text, line, begidx, endidx): |
| 482 | # Complete a breakpoint number. (This would be more helpful if we could |
| 483 | # display additional info along with the completions, such as file/line |
| 484 | # of the breakpoint.) |
| 485 | return [str(i) for i, bp in enumerate(bdb.Breakpoint.bpbynumber) |
| 486 | if bp is not None and str(i).startswith(text)] |
| 487 | |
| 488 | def _complete_expression(self, text, line, begidx, endidx): |
| 489 | # Complete an arbitrary expression. |
| 490 | if not self.curframe: |
| 491 | return [] |
| 492 | # Collect globals and locals. It is usually not really sensible to also |
| 493 | # complete builtins, and they clutter the namespace quite heavily, so we |
| 494 | # leave them out. |
Serhiy Storchaka | da08470 | 2019-03-27 08:02:28 +0200 | [diff] [blame] | 495 | ns = {**self.curframe.f_globals, **self.curframe_locals} |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 496 | if '.' in text: |
| 497 | # Walk an attribute chain up to the last part, similar to what |
| 498 | # rlcompleter does. This will bail if any of the parts are not |
| 499 | # simple attribute access, which is what we want. |
| 500 | dotted = text.split('.') |
| 501 | try: |
| 502 | obj = ns[dotted[0]] |
| 503 | for part in dotted[1:-1]: |
| 504 | obj = getattr(obj, part) |
| 505 | except (KeyError, AttributeError): |
| 506 | return [] |
| 507 | prefix = '.'.join(dotted[:-1]) + '.' |
| 508 | return [prefix + n for n in dir(obj) if n.startswith(dotted[-1])] |
| 509 | else: |
| 510 | # Complete a simple name. |
| 511 | return [n for n in ns.keys() if n.startswith(text)] |
| 512 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 513 | # Command definitions, called by cmdloop() |
| 514 | # The argument is the remaining string on the command line |
| 515 | # Return true to exit from the command loop |
| 516 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 517 | def do_commands(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 518 | """commands [bpnumber] |
| 519 | (com) ... |
| 520 | (com) end |
| 521 | (Pdb) |
Georg Brandl | 3078df0 | 2009-05-05 09:11:31 +0000 | [diff] [blame] | 522 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 523 | Specify a list of commands for breakpoint number bpnumber. |
| 524 | The commands themselves are entered on the following lines. |
| 525 | Type a line containing just 'end' to terminate the commands. |
| 526 | The commands are executed when the breakpoint is hit. |
| 527 | |
| 528 | To remove all commands from a breakpoint, type commands and |
| 529 | follow it immediately with end; that is, give no commands. |
| 530 | |
| 531 | With no bpnumber argument, commands refers to the last |
| 532 | breakpoint set. |
| 533 | |
| 534 | You can use breakpoint commands to start your program up |
| 535 | again. Simply use the continue command, or step, or any other |
| 536 | command that resumes execution. |
| 537 | |
| 538 | Specifying any command resuming execution (currently continue, |
| 539 | step, next, return, jump, quit and their abbreviations) |
| 540 | terminates the command list (as if that command was |
| 541 | immediately followed by end). This is because any time you |
| 542 | resume execution (even with a simple next or step), you may |
| 543 | encounter another breakpoint -- which could have its own |
| 544 | command list, leading to ambiguities about which list to |
| 545 | execute. |
| 546 | |
| 547 | If you use the 'silent' command in the command list, the usual |
| 548 | message about stopping at a breakpoint is not printed. This |
| 549 | may be desirable for breakpoints that are to print a specific |
| 550 | message and then continue. If none of the other commands |
| 551 | print anything, you will see no sign that the breakpoint was |
| 552 | reached. |
| 553 | """ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 554 | if not arg: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 555 | bnum = len(bdb.Breakpoint.bpbynumber) - 1 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 556 | else: |
| 557 | try: |
| 558 | bnum = int(arg) |
| 559 | except: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 560 | self.error("Usage: commands [bnum]\n ...\n end") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 561 | return |
| 562 | self.commands_bnum = bnum |
Georg Brandl | b90ffd8 | 2010-07-30 22:20:16 +0000 | [diff] [blame] | 563 | # Save old definitions for the case of a keyboard interrupt. |
| 564 | if bnum in self.commands: |
| 565 | old_command_defs = (self.commands[bnum], |
| 566 | self.commands_doprompt[bnum], |
| 567 | self.commands_silent[bnum]) |
| 568 | else: |
| 569 | old_command_defs = None |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 570 | self.commands[bnum] = [] |
| 571 | self.commands_doprompt[bnum] = True |
| 572 | self.commands_silent[bnum] = False |
Georg Brandl | b90ffd8 | 2010-07-30 22:20:16 +0000 | [diff] [blame] | 573 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 574 | prompt_back = self.prompt |
| 575 | self.prompt = '(com) ' |
| 576 | self.commands_defining = True |
Georg Brandl | 44f8bf9 | 2010-07-30 08:54:49 +0000 | [diff] [blame] | 577 | try: |
| 578 | self.cmdloop() |
Georg Brandl | b90ffd8 | 2010-07-30 22:20:16 +0000 | [diff] [blame] | 579 | except KeyboardInterrupt: |
| 580 | # Restore old definitions. |
| 581 | if old_command_defs: |
| 582 | self.commands[bnum] = old_command_defs[0] |
| 583 | self.commands_doprompt[bnum] = old_command_defs[1] |
| 584 | self.commands_silent[bnum] = old_command_defs[2] |
| 585 | else: |
| 586 | del self.commands[bnum] |
| 587 | del self.commands_doprompt[bnum] |
| 588 | del self.commands_silent[bnum] |
| 589 | self.error('command definition aborted, old commands restored') |
Georg Brandl | 44f8bf9 | 2010-07-30 08:54:49 +0000 | [diff] [blame] | 590 | finally: |
| 591 | self.commands_defining = False |
| 592 | self.prompt = prompt_back |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 593 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 594 | complete_commands = _complete_bpnumber |
| 595 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 596 | def do_break(self, arg, temporary = 0): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 597 | """b(reak) [ ([filename:]lineno | function) [, condition] ] |
| 598 | Without argument, list all breaks. |
| 599 | |
| 600 | With a line number argument, set a break at this line in the |
| 601 | current file. With a function name, set a break at the first |
| 602 | executable line of that function. If a second argument is |
| 603 | present, it is a string specifying an expression which must |
| 604 | evaluate to true before the breakpoint is honored. |
| 605 | |
| 606 | The line number may be prefixed with a filename and a colon, |
| 607 | to specify a breakpoint in another file (probably one that |
| 608 | hasn't been loaded yet). The file is searched for on |
| 609 | sys.path; the .py suffix may be omitted. |
| 610 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 611 | if not arg: |
| 612 | if self.breaks: # There's at least one |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 613 | self.message("Num Type Disp Enb Where") |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 614 | for bp in bdb.Breakpoint.bpbynumber: |
| 615 | if bp: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 616 | self.message(bp.bpformat()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 617 | return |
| 618 | # parse arguments; comma has lowest precedence |
| 619 | # and cannot occur in filename |
| 620 | filename = None |
| 621 | lineno = None |
| 622 | cond = None |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 623 | comma = arg.find(',') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 624 | if comma > 0: |
| 625 | # parse stuff after comma: "condition" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 626 | cond = arg[comma+1:].lstrip() |
| 627 | arg = arg[:comma].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 628 | # parse stuff before comma: [filename:]lineno | function |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 629 | colon = arg.rfind(':') |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 630 | funcname = None |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 631 | if colon >= 0: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 632 | filename = arg[:colon].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 633 | f = self.lookupmodule(filename) |
| 634 | if not f: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 635 | self.error('%r not found from sys.path' % filename) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 636 | return |
| 637 | else: |
| 638 | filename = f |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 639 | arg = arg[colon+1:].lstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 640 | try: |
| 641 | lineno = int(arg) |
Georg Brandl | f93390a | 2010-10-14 07:17:44 +0000 | [diff] [blame] | 642 | except ValueError: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 643 | self.error('Bad lineno: %s' % arg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 644 | return |
| 645 | else: |
| 646 | # no colon; can be lineno or function |
| 647 | try: |
| 648 | lineno = int(arg) |
| 649 | except ValueError: |
| 650 | try: |
| 651 | func = eval(arg, |
| 652 | self.curframe.f_globals, |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 653 | self.curframe_locals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 654 | except: |
| 655 | func = arg |
| 656 | try: |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 657 | if hasattr(func, '__func__'): |
| 658 | func = func.__func__ |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 659 | code = func.__code__ |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 660 | #use co_name to identify the bkpt (function names |
| 661 | #could be aliased, but co_name is invariant) |
| 662 | funcname = code.co_name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 663 | lineno = code.co_firstlineno |
| 664 | filename = code.co_filename |
| 665 | except: |
| 666 | # last thing to try |
| 667 | (ok, filename, ln) = self.lineinfo(arg) |
| 668 | if not ok: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 669 | self.error('The specified object %r is not a function ' |
| 670 | 'or was not found along sys.path.' % arg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 671 | return |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 672 | funcname = ok # ok contains a function name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 673 | lineno = int(ln) |
| 674 | if not filename: |
| 675 | filename = self.defaultFile() |
| 676 | # Check for reasonable breakpoint |
| 677 | line = self.checkline(filename, lineno) |
| 678 | if line: |
| 679 | # now set the break point |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 680 | err = self.set_break(filename, line, temporary, cond, funcname) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 681 | if err: |
Berker Peksag | ad5ffd4 | 2014-07-12 18:24:32 +0300 | [diff] [blame] | 682 | self.error(err) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 683 | else: |
| 684 | bp = self.get_breaks(filename, line)[-1] |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 685 | self.message("Breakpoint %d at %s:%d" % |
| 686 | (bp.number, bp.file, bp.line)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 687 | |
| 688 | # To be overridden in derived debuggers |
| 689 | def defaultFile(self): |
| 690 | """Produce a reasonable default.""" |
| 691 | filename = self.curframe.f_code.co_filename |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 692 | if filename == '<string>' and self.mainpyfile: |
| 693 | filename = self.mainpyfile |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 694 | return filename |
| 695 | |
| 696 | do_b = do_break |
| 697 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 698 | complete_break = _complete_location |
| 699 | complete_b = _complete_location |
| 700 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 701 | def do_tbreak(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 702 | """tbreak [ ([filename:]lineno | function) [, condition] ] |
| 703 | Same arguments as break, but sets a temporary breakpoint: it |
| 704 | is automatically deleted when first hit. |
| 705 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 706 | self.do_break(arg, 1) |
| 707 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 708 | complete_tbreak = _complete_location |
| 709 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 710 | def lineinfo(self, identifier): |
| 711 | failed = (None, None, None) |
| 712 | # Input is identifier, may be in single quotes |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 713 | idstring = identifier.split("'") |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 714 | if len(idstring) == 1: |
| 715 | # not in single quotes |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 716 | id = idstring[0].strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 717 | elif len(idstring) == 3: |
| 718 | # quoted |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 719 | id = idstring[1].strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 720 | else: |
| 721 | return failed |
| 722 | if id == '': return failed |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 723 | parts = id.split('.') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 724 | # Protection for derived debuggers |
| 725 | if parts[0] == 'self': |
| 726 | del parts[0] |
| 727 | if len(parts) == 0: |
| 728 | return failed |
| 729 | # Best first guess at file to look at |
| 730 | fname = self.defaultFile() |
| 731 | if len(parts) == 1: |
| 732 | item = parts[0] |
| 733 | else: |
| 734 | # More than one part. |
| 735 | # First is module, second is method/class |
| 736 | f = self.lookupmodule(parts[0]) |
| 737 | if f: |
| 738 | fname = f |
| 739 | item = parts[1] |
| 740 | answer = find_function(item, fname) |
| 741 | return answer or failed |
| 742 | |
| 743 | def checkline(self, filename, lineno): |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 744 | """Check whether specified line seems to be executable. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 745 | |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 746 | Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank |
| 747 | line or EOF). Warning: testing is not comprehensive. |
| 748 | """ |
Georg Brandl | 1e30bd3 | 2010-07-30 07:21:26 +0000 | [diff] [blame] | 749 | # this method should be callable before starting debugging, so default |
| 750 | # to "no globals" if there is no current frame |
| 751 | globs = self.curframe.f_globals if hasattr(self, 'curframe') else None |
| 752 | line = linecache.getline(filename, lineno, globs) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 753 | if not line: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 754 | self.message('End of file') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 755 | return 0 |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 756 | line = line.strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 757 | # Don't allow setting breakpoint at a blank line |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 758 | if (not line or (line[0] == '#') or |
| 759 | (line[:3] == '"""') or line[:3] == "'''"): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 760 | self.error('Blank or comment') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 761 | return 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 762 | return lineno |
| 763 | |
| 764 | def do_enable(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 765 | """enable bpnumber [bpnumber ...] |
| 766 | Enables the breakpoints given as a space separated list of |
| 767 | breakpoint numbers. |
| 768 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 769 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 770 | for i in args: |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 771 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 772 | bp = self.get_bpbynumber(i) |
| 773 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 774 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 775 | else: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 776 | bp.enable() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 777 | self.message('Enabled %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 778 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 779 | complete_enable = _complete_bpnumber |
| 780 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 781 | def do_disable(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 782 | """disable bpnumber [bpnumber ...] |
| 783 | Disables the breakpoints given as a space separated list of |
| 784 | breakpoint numbers. Disabling a breakpoint means it cannot |
| 785 | cause the program to stop execution, but unlike clearing a |
| 786 | breakpoint, it remains in the list of breakpoints and can be |
| 787 | (re-)enabled. |
| 788 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 789 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 790 | for i in args: |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 791 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 792 | bp = self.get_bpbynumber(i) |
| 793 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 794 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 795 | else: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 796 | bp.disable() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 797 | self.message('Disabled %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 798 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 799 | complete_disable = _complete_bpnumber |
| 800 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 801 | def do_condition(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 802 | """condition bpnumber [condition] |
| 803 | Set a new condition for the breakpoint, an expression which |
| 804 | must evaluate to true before the breakpoint is honored. If |
| 805 | condition is absent, any existing condition is removed; i.e., |
| 806 | the breakpoint is made unconditional. |
| 807 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 808 | args = arg.split(' ', 1) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 809 | try: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 810 | cond = args[1] |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 811 | except IndexError: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 812 | cond = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 813 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 814 | bp = self.get_bpbynumber(args[0].strip()) |
Georg Brandl | 0079ffc | 2013-10-14 16:08:15 +0200 | [diff] [blame] | 815 | except IndexError: |
| 816 | self.error('Breakpoint number expected') |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 817 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 818 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 819 | else: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 820 | bp.cond = cond |
| 821 | if not cond: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 822 | self.message('Breakpoint %d is now unconditional.' % bp.number) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 823 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 824 | self.message('New condition set for breakpoint %d.' % bp.number) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 825 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 826 | complete_condition = _complete_bpnumber |
| 827 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 828 | def do_ignore(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 829 | """ignore bpnumber [count] |
| 830 | Set the ignore count for the given breakpoint number. If |
| 831 | count is omitted, the ignore count is set to 0. A breakpoint |
| 832 | becomes active when the ignore count is zero. When non-zero, |
| 833 | the count is decremented each time the breakpoint is reached |
| 834 | and the breakpoint is not disabled and any associated |
| 835 | condition evaluates to true. |
| 836 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 837 | args = arg.split() |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 838 | try: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 839 | count = int(args[1].strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 840 | except: |
| 841 | count = 0 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 842 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 843 | bp = self.get_bpbynumber(args[0].strip()) |
Georg Brandl | 0079ffc | 2013-10-14 16:08:15 +0200 | [diff] [blame] | 844 | except IndexError: |
| 845 | self.error('Breakpoint number expected') |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 846 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 847 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 848 | else: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 849 | bp.ignore = count |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 850 | if count > 0: |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 851 | if count > 1: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 852 | countstr = '%d crossings' % count |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 853 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 854 | countstr = '1 crossing' |
| 855 | self.message('Will ignore next %s of breakpoint %d.' % |
| 856 | (countstr, bp.number)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 857 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 858 | self.message('Will stop next time breakpoint %d is reached.' |
| 859 | % bp.number) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 860 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 861 | complete_ignore = _complete_bpnumber |
| 862 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 863 | def do_clear(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 864 | """cl(ear) filename:lineno\ncl(ear) [bpnumber [bpnumber...]] |
| 865 | With a space separated list of breakpoint numbers, clear |
| 866 | those breakpoints. Without argument, clear all breaks (but |
| 867 | first ask confirmation). With a filename:lineno argument, |
| 868 | clear all breaks at that line in that file. |
| 869 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 870 | if not arg: |
| 871 | try: |
Guido van Rossum | c5b6ab0 | 2007-05-27 09:19:52 +0000 | [diff] [blame] | 872 | reply = input('Clear all breaks? ') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 873 | except EOFError: |
| 874 | reply = 'no' |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 875 | reply = reply.strip().lower() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 876 | if reply in ('y', 'yes'): |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 877 | bplist = [bp for bp in bdb.Breakpoint.bpbynumber if bp] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 878 | self.clear_all_breaks() |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 879 | for bp in bplist: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 880 | self.message('Deleted %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 881 | return |
| 882 | if ':' in arg: |
| 883 | # Make sure it works for "clear C:\foo\bar.py:12" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 884 | i = arg.rfind(':') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 885 | filename = arg[:i] |
| 886 | arg = arg[i+1:] |
| 887 | try: |
| 888 | lineno = int(arg) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 889 | except ValueError: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 890 | err = "Invalid line number (%s)" % arg |
| 891 | else: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 892 | bplist = self.get_breaks(filename, lineno) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 893 | err = self.clear_break(filename, lineno) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 894 | if err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 895 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 896 | else: |
| 897 | for bp in bplist: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 898 | self.message('Deleted %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 899 | return |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 900 | numberlist = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 901 | for i in numberlist: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 902 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 903 | bp = self.get_bpbynumber(i) |
| 904 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 905 | self.error(err) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 906 | else: |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 907 | self.clear_bpbynumber(i) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 908 | self.message('Deleted %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 909 | do_cl = do_clear # 'c' is already an abbreviation for 'continue' |
| 910 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 911 | complete_clear = _complete_location |
| 912 | complete_cl = _complete_location |
| 913 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 914 | def do_where(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 915 | """w(here) |
| 916 | Print a stack trace, with the most recent frame at the bottom. |
| 917 | An arrow indicates the "current frame", which determines the |
| 918 | context of most commands. 'bt' is an alias for this command. |
| 919 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 920 | self.print_stack_trace() |
| 921 | do_w = do_where |
Guido van Rossum | 6bd6835 | 2001-01-20 17:57:37 +0000 | [diff] [blame] | 922 | do_bt = do_where |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 923 | |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 924 | def _select_frame(self, number): |
| 925 | assert 0 <= number < len(self.stack) |
| 926 | self.curindex = number |
| 927 | self.curframe = self.stack[self.curindex][0] |
| 928 | self.curframe_locals = self.curframe.f_locals |
| 929 | self.print_stack_entry(self.stack[self.curindex]) |
| 930 | self.lineno = None |
| 931 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 932 | def do_up(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 933 | """u(p) [count] |
| 934 | Move the current frame count (default one) levels up in the |
| 935 | stack trace (to an older frame). |
| 936 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 937 | if self.curindex == 0: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 938 | self.error('Oldest frame') |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 939 | return |
| 940 | try: |
| 941 | count = int(arg or 1) |
| 942 | except ValueError: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 943 | self.error('Invalid frame count (%s)' % arg) |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 944 | return |
| 945 | if count < 0: |
| 946 | newframe = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 947 | else: |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 948 | newframe = max(0, self.curindex - count) |
| 949 | self._select_frame(newframe) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 950 | do_u = do_up |
| 951 | |
| 952 | def do_down(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 953 | """d(own) [count] |
| 954 | Move the current frame count (default one) levels down in the |
| 955 | stack trace (to a newer frame). |
| 956 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 957 | if self.curindex + 1 == len(self.stack): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 958 | self.error('Newest frame') |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 959 | return |
| 960 | try: |
| 961 | count = int(arg or 1) |
| 962 | except ValueError: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 963 | self.error('Invalid frame count (%s)' % arg) |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 964 | return |
| 965 | if count < 0: |
| 966 | newframe = len(self.stack) - 1 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 967 | else: |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 968 | newframe = min(len(self.stack) - 1, self.curindex + count) |
| 969 | self._select_frame(newframe) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 970 | do_d = do_down |
| 971 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 972 | def do_until(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 973 | """unt(il) [lineno] |
| 974 | Without argument, continue execution until the line with a |
| 975 | number greater than the current one is reached. With a line |
| 976 | number, continue execution until a line with a number greater |
| 977 | or equal to that is reached. In both cases, also stop when |
| 978 | the current frame returns. |
| 979 | """ |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 980 | if arg: |
| 981 | try: |
| 982 | lineno = int(arg) |
| 983 | except ValueError: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 984 | self.error('Error in argument: %r' % arg) |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 985 | return |
| 986 | if lineno <= self.curframe.f_lineno: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 987 | self.error('"until" line number is smaller than current ' |
| 988 | 'line number') |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 989 | return |
| 990 | else: |
| 991 | lineno = None |
| 992 | self.set_until(self.curframe, lineno) |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 993 | return 1 |
| 994 | do_unt = do_until |
| 995 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 996 | def do_step(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 997 | """s(tep) |
| 998 | Execute the current line, stop at the first possible occasion |
| 999 | (either in a function that is called or in the current |
| 1000 | function). |
| 1001 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1002 | self.set_step() |
| 1003 | return 1 |
| 1004 | do_s = do_step |
| 1005 | |
| 1006 | def do_next(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1007 | """n(ext) |
| 1008 | Continue execution until the next line in the current function |
| 1009 | is reached or it returns. |
| 1010 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1011 | self.set_next(self.curframe) |
| 1012 | return 1 |
| 1013 | do_n = do_next |
| 1014 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1015 | def do_run(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1016 | """run [args...] |
| 1017 | Restart the debugged python program. If a string is supplied |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 1018 | it is split with "shlex", and the result is used as the new |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1019 | sys.argv. History, breakpoints, actions and debugger options |
| 1020 | are preserved. "restart" is an alias for "run". |
| 1021 | """ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1022 | if arg: |
| 1023 | import shlex |
| 1024 | argv0 = sys.argv[0:1] |
| 1025 | sys.argv = shlex.split(arg) |
| 1026 | sys.argv[:0] = argv0 |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1027 | # this is caught in the main debugger loop |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1028 | raise Restart |
| 1029 | |
| 1030 | do_restart = do_run |
| 1031 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1032 | def do_return(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1033 | """r(eturn) |
| 1034 | Continue execution until the current function returns. |
| 1035 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1036 | self.set_return(self.curframe) |
| 1037 | return 1 |
| 1038 | do_r = do_return |
| 1039 | |
| 1040 | def do_continue(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1041 | """c(ont(inue)) |
| 1042 | Continue execution, only stop when a breakpoint is encountered. |
| 1043 | """ |
Georg Brandl | 44f2b64 | 2010-12-04 16:00:47 +0000 | [diff] [blame] | 1044 | if not self.nosigint: |
Andrew Svetlov | 539ee5d | 2012-12-04 21:08:28 +0200 | [diff] [blame] | 1045 | try: |
Xavier de Gaye | 10e54ae | 2016-10-12 20:13:24 +0200 | [diff] [blame] | 1046 | Pdb._previous_sigint_handler = \ |
Andrew Svetlov | 539ee5d | 2012-12-04 21:08:28 +0200 | [diff] [blame] | 1047 | signal.signal(signal.SIGINT, self.sigint_handler) |
| 1048 | except ValueError: |
| 1049 | # ValueError happens when do_continue() is invoked from |
| 1050 | # a non-main thread in which case we just continue without |
| 1051 | # SIGINT set. Would printing a message here (once) make |
| 1052 | # sense? |
| 1053 | pass |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1054 | self.set_continue() |
| 1055 | return 1 |
| 1056 | do_c = do_cont = do_continue |
| 1057 | |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1058 | def do_jump(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1059 | """j(ump) lineno |
| 1060 | Set the next line that will be executed. Only available in |
| 1061 | the bottom-most frame. This lets you jump back and execute |
| 1062 | code again, or jump forward to skip code that you don't want |
| 1063 | to run. |
| 1064 | |
| 1065 | It should be noted that not all jumps are allowed -- for |
| 1066 | instance it is not possible to jump into the middle of a |
| 1067 | for loop or out of a finally clause. |
| 1068 | """ |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1069 | if self.curindex + 1 != len(self.stack): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1070 | self.error('You can only jump within the bottom frame') |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1071 | return |
| 1072 | try: |
| 1073 | arg = int(arg) |
| 1074 | except ValueError: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1075 | self.error("The 'jump' command requires a line number") |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1076 | else: |
| 1077 | try: |
| 1078 | # Do the jump, fix up our copy of the stack, and display the |
| 1079 | # new position |
| 1080 | self.curframe.f_lineno = arg |
| 1081 | self.stack[self.curindex] = self.stack[self.curindex][0], arg |
| 1082 | self.print_stack_entry(self.stack[self.curindex]) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 1083 | except ValueError as e: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1084 | self.error('Jump failed: %s' % e) |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1085 | do_j = do_jump |
| 1086 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1087 | def do_debug(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1088 | """debug code |
| 1089 | Enter a recursive debugger that steps through the code |
| 1090 | argument (which is an arbitrary expression or statement to be |
| 1091 | executed in the current environment). |
| 1092 | """ |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1093 | sys.settrace(None) |
| 1094 | globals = self.curframe.f_globals |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 1095 | locals = self.curframe_locals |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 1096 | p = Pdb(self.completekey, self.stdin, self.stdout) |
Guido van Rossum | ed538d8 | 2003-04-09 19:36:34 +0000 | [diff] [blame] | 1097 | p.prompt = "(%s) " % self.prompt.strip() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1098 | self.message("ENTERING RECURSIVE DEBUGGER") |
Daniel Hahler | 3e93643 | 2019-03-12 04:29:04 +0100 | [diff] [blame] | 1099 | try: |
| 1100 | sys.call_tracing(p.run, (arg, globals, locals)) |
| 1101 | except Exception: |
| 1102 | exc_info = sys.exc_info()[:2] |
| 1103 | self.error(traceback.format_exception_only(*exc_info)[-1].strip()) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1104 | self.message("LEAVING RECURSIVE DEBUGGER") |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1105 | sys.settrace(self.trace_dispatch) |
| 1106 | self.lastcmd = p.lastcmd |
| 1107 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 1108 | complete_debug = _complete_expression |
| 1109 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1110 | def do_quit(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1111 | """q(uit)\nexit |
| 1112 | Quit from the debugger. The program being executed is aborted. |
| 1113 | """ |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 1114 | self._user_requested_quit = True |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1115 | self.set_quit() |
| 1116 | return 1 |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1117 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1118 | do_q = do_quit |
Guido van Rossum | d1c08f3 | 2002-04-15 00:48:24 +0000 | [diff] [blame] | 1119 | do_exit = do_quit |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1120 | |
Guido van Rossum | eef2607 | 2003-01-13 21:13:55 +0000 | [diff] [blame] | 1121 | def do_EOF(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1122 | """EOF |
| 1123 | Handles the receipt of EOF as a command. |
| 1124 | """ |
| 1125 | self.message('') |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 1126 | self._user_requested_quit = True |
Guido van Rossum | eef2607 | 2003-01-13 21:13:55 +0000 | [diff] [blame] | 1127 | self.set_quit() |
| 1128 | return 1 |
| 1129 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1130 | def do_args(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1131 | """a(rgs) |
| 1132 | Print the argument list of the current function. |
| 1133 | """ |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 1134 | co = self.curframe.f_code |
| 1135 | dict = self.curframe_locals |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 1136 | n = co.co_argcount + co.co_kwonlyargcount |
RĂ©mi Lapeyre | bf457c7 | 2019-05-21 00:17:30 +0200 | [diff] [blame] | 1137 | if co.co_flags & inspect.CO_VARARGS: n = n+1 |
| 1138 | if co.co_flags & inspect.CO_VARKEYWORDS: n = n+1 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1139 | for i in range(n): |
| 1140 | name = co.co_varnames[i] |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1141 | if name in dict: |
| 1142 | self.message('%s = %r' % (name, dict[name])) |
| 1143 | else: |
| 1144 | self.message('%s = *** undefined ***' % (name,)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1145 | do_a = do_args |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1146 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1147 | def do_retval(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1148 | """retval |
| 1149 | Print the return value for the last return of a function. |
| 1150 | """ |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 1151 | if '__return__' in self.curframe_locals: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1152 | self.message(repr(self.curframe_locals['__return__'])) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1153 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1154 | self.error('Not yet returned!') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1155 | do_rv = do_retval |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1156 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1157 | def _getval(self, arg): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1158 | try: |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 1159 | return eval(arg, self.curframe.f_globals, self.curframe_locals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1160 | except: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1161 | exc_info = sys.exc_info()[:2] |
| 1162 | self.error(traceback.format_exception_only(*exc_info)[-1].strip()) |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1163 | raise |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1164 | |
Georg Brandl | cbc79c7 | 2010-12-04 16:21:42 +0000 | [diff] [blame] | 1165 | def _getval_except(self, arg, frame=None): |
| 1166 | try: |
| 1167 | if frame is None: |
| 1168 | return eval(arg, self.curframe.f_globals, self.curframe_locals) |
| 1169 | else: |
| 1170 | return eval(arg, frame.f_globals, frame.f_locals) |
| 1171 | except: |
| 1172 | exc_info = sys.exc_info()[:2] |
| 1173 | err = traceback.format_exception_only(*exc_info)[-1].strip() |
| 1174 | return _rstr('** raised %s **' % err) |
| 1175 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1176 | def do_p(self, arg): |
R David Murray | 78d692f | 2013-10-10 17:23:26 -0400 | [diff] [blame] | 1177 | """p expression |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1178 | Print the value of the expression. |
| 1179 | """ |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1180 | try: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1181 | self.message(repr(self._getval(arg))) |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1182 | except: |
| 1183 | pass |
| 1184 | |
| 1185 | def do_pp(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1186 | """pp expression |
| 1187 | Pretty-print the value of the expression. |
| 1188 | """ |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1189 | try: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1190 | self.message(pprint.pformat(self._getval(arg))) |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1191 | except: |
| 1192 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1193 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 1194 | complete_print = _complete_expression |
| 1195 | complete_p = _complete_expression |
| 1196 | complete_pp = _complete_expression |
| 1197 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1198 | def do_list(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1199 | """l(ist) [first [,last] | .] |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 1200 | |
| 1201 | List source code for the current file. Without arguments, |
| 1202 | list 11 lines around the current line or continue the previous |
| 1203 | listing. With . as argument, list 11 lines around the current |
| 1204 | line. With one argument, list 11 lines starting at that line. |
| 1205 | With two arguments, list the given range; if the second |
| 1206 | argument is less than the first, it is a count. |
| 1207 | |
| 1208 | The current line in the current frame is indicated by "->". |
| 1209 | If an exception is being debugged, the line where the |
| 1210 | exception was originally raised or propagated is indicated by |
| 1211 | ">>", if it differs from the current line. |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1212 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1213 | self.lastcmd = 'list' |
| 1214 | last = None |
Georg Brandl | a91a94b | 2010-07-30 07:14:01 +0000 | [diff] [blame] | 1215 | if arg and arg != '.': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1216 | try: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1217 | if ',' in arg: |
| 1218 | first, last = arg.split(',') |
| 1219 | first = int(first.strip()) |
| 1220 | last = int(last.strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1221 | if last < first: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1222 | # assume it's a count |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1223 | last = first + last |
| 1224 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1225 | first = int(arg.strip()) |
| 1226 | first = max(1, first - 5) |
| 1227 | except ValueError: |
| 1228 | self.error('Error in argument: %r' % arg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1229 | return |
Georg Brandl | a91a94b | 2010-07-30 07:14:01 +0000 | [diff] [blame] | 1230 | elif self.lineno is None or arg == '.': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1231 | first = max(1, self.curframe.f_lineno - 5) |
| 1232 | else: |
| 1233 | first = self.lineno + 1 |
| 1234 | if last is None: |
| 1235 | last = first + 10 |
| 1236 | filename = self.curframe.f_code.co_filename |
| 1237 | breaklist = self.get_file_breaks(filename) |
| 1238 | try: |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1239 | lines = linecache.getlines(filename, self.curframe.f_globals) |
| 1240 | self._print_lines(lines[first-1:last], first, breaklist, |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 1241 | self.curframe) |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1242 | self.lineno = min(last, len(lines)) |
| 1243 | if len(lines) < last: |
| 1244 | self.message('[EOF]') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1245 | except KeyboardInterrupt: |
| 1246 | pass |
| 1247 | do_l = do_list |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1248 | |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1249 | def do_longlist(self, arg): |
| 1250 | """longlist | ll |
| 1251 | List the whole source code for the current function or frame. |
| 1252 | """ |
| 1253 | filename = self.curframe.f_code.co_filename |
| 1254 | breaklist = self.get_file_breaks(filename) |
| 1255 | try: |
Georg Brandl | 5ed2b5a | 2010-07-30 18:08:12 +0000 | [diff] [blame] | 1256 | lines, lineno = getsourcelines(self.curframe) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1257 | except OSError as err: |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1258 | self.error(err) |
| 1259 | return |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 1260 | self._print_lines(lines, lineno, breaklist, self.curframe) |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1261 | do_ll = do_longlist |
| 1262 | |
| 1263 | def do_source(self, arg): |
| 1264 | """source expression |
| 1265 | Try to get source code for the given object and display it. |
| 1266 | """ |
| 1267 | try: |
| 1268 | obj = self._getval(arg) |
| 1269 | except: |
| 1270 | return |
| 1271 | try: |
Georg Brandl | 5ed2b5a | 2010-07-30 18:08:12 +0000 | [diff] [blame] | 1272 | lines, lineno = getsourcelines(obj) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1273 | except (OSError, TypeError) as err: |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1274 | self.error(err) |
| 1275 | return |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 1276 | self._print_lines(lines, lineno) |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1277 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 1278 | complete_source = _complete_expression |
| 1279 | |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 1280 | def _print_lines(self, lines, start, breaks=(), frame=None): |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1281 | """Print a range of lines.""" |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 1282 | if frame: |
| 1283 | current_lineno = frame.f_lineno |
| 1284 | exc_lineno = self.tb_lineno.get(frame, -1) |
| 1285 | else: |
| 1286 | current_lineno = exc_lineno = -1 |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1287 | for lineno, line in enumerate(lines, start): |
| 1288 | s = str(lineno).rjust(3) |
| 1289 | if len(s) < 4: |
| 1290 | s += ' ' |
| 1291 | if lineno in breaks: |
| 1292 | s += 'B' |
| 1293 | else: |
| 1294 | s += ' ' |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 1295 | if lineno == current_lineno: |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1296 | s += '->' |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 1297 | elif lineno == exc_lineno: |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1298 | s += '>>' |
| 1299 | self.message(s + '\t' + line.rstrip()) |
| 1300 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1301 | def do_whatis(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1302 | """whatis arg |
| 1303 | Print the type of the argument. |
| 1304 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1305 | try: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1306 | value = self._getval(arg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1307 | except: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1308 | # _getval() already printed the error |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1309 | return |
| 1310 | code = None |
| 1311 | # Is it a function? |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1312 | try: |
| 1313 | code = value.__code__ |
| 1314 | except Exception: |
| 1315 | pass |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1316 | if code: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1317 | self.message('Function %s' % code.co_name) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1318 | return |
| 1319 | # Is it an instance method? |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1320 | try: |
| 1321 | code = value.__func__.__code__ |
| 1322 | except Exception: |
| 1323 | pass |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1324 | if code: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1325 | self.message('Method %s' % code.co_name) |
| 1326 | return |
| 1327 | # Is it a class? |
| 1328 | if value.__class__ is type: |
Serhiy Storchaka | 521e586 | 2014-07-22 15:00:37 +0300 | [diff] [blame] | 1329 | self.message('Class %s.%s' % (value.__module__, value.__qualname__)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1330 | return |
| 1331 | # None of the above... |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1332 | self.message(type(value)) |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 1333 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 1334 | complete_whatis = _complete_expression |
| 1335 | |
Georg Brandl | cbc79c7 | 2010-12-04 16:21:42 +0000 | [diff] [blame] | 1336 | def do_display(self, arg): |
| 1337 | """display [expression] |
| 1338 | |
| 1339 | Display the value of the expression if it changed, each time execution |
| 1340 | stops in the current frame. |
| 1341 | |
| 1342 | Without expression, list all display expressions for the current frame. |
| 1343 | """ |
| 1344 | if not arg: |
| 1345 | self.message('Currently displaying:') |
| 1346 | for item in self.displaying.get(self.curframe, {}).items(): |
| 1347 | self.message('%s: %r' % item) |
| 1348 | else: |
| 1349 | val = self._getval_except(arg) |
| 1350 | self.displaying.setdefault(self.curframe, {})[arg] = val |
| 1351 | self.message('display %s: %r' % (arg, val)) |
| 1352 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 1353 | complete_display = _complete_expression |
| 1354 | |
Georg Brandl | cbc79c7 | 2010-12-04 16:21:42 +0000 | [diff] [blame] | 1355 | def do_undisplay(self, arg): |
| 1356 | """undisplay [expression] |
| 1357 | |
| 1358 | Do not display the expression any more in the current frame. |
| 1359 | |
| 1360 | Without expression, clear all display expressions for the current frame. |
| 1361 | """ |
| 1362 | if arg: |
| 1363 | try: |
| 1364 | del self.displaying.get(self.curframe, {})[arg] |
| 1365 | except KeyError: |
| 1366 | self.error('not displaying %s' % arg) |
| 1367 | else: |
| 1368 | self.displaying.pop(self.curframe, None) |
| 1369 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 1370 | def complete_undisplay(self, text, line, begidx, endidx): |
| 1371 | return [e for e in self.displaying.get(self.curframe, {}) |
| 1372 | if e.startswith(text)] |
| 1373 | |
Georg Brandl | 1acb746 | 2010-12-04 11:20:26 +0000 | [diff] [blame] | 1374 | def do_interact(self, arg): |
| 1375 | """interact |
| 1376 | |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 1377 | Start an interactive interpreter whose global namespace |
Georg Brandl | 1acb746 | 2010-12-04 11:20:26 +0000 | [diff] [blame] | 1378 | contains all the (global and local) names found in the current scope. |
| 1379 | """ |
Serhiy Storchaka | da08470 | 2019-03-27 08:02:28 +0200 | [diff] [blame] | 1380 | ns = {**self.curframe.f_globals, **self.curframe_locals} |
Georg Brandl | 1acb746 | 2010-12-04 11:20:26 +0000 | [diff] [blame] | 1381 | code.interact("*interactive*", local=ns) |
| 1382 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1383 | def do_alias(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1384 | """alias [name [command [parameter parameter ...] ]] |
| 1385 | Create an alias called 'name' that executes 'command'. The |
| 1386 | command must *not* be enclosed in quotes. Replaceable |
| 1387 | parameters can be indicated by %1, %2, and so on, while %* is |
| 1388 | replaced by all the parameters. If no command is given, the |
| 1389 | current alias for name is shown. If no name is given, all |
| 1390 | aliases are listed. |
| 1391 | |
| 1392 | Aliases may be nested and can contain anything that can be |
| 1393 | legally typed at the pdb prompt. Note! You *can* override |
| 1394 | internal pdb commands with aliases! Those internal commands |
| 1395 | are then hidden until the alias is removed. Aliasing is |
| 1396 | recursively applied to the first word of the command line; all |
| 1397 | other words in the line are left alone. |
| 1398 | |
| 1399 | As an example, here are two useful aliases (especially when |
| 1400 | placed in the .pdbrc file): |
| 1401 | |
| 1402 | # Print instance variables (usage "pi classInst") |
R David Murray | 78d692f | 2013-10-10 17:23:26 -0400 | [diff] [blame] | 1403 | alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k]) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1404 | # Print instance variables in self |
| 1405 | alias ps pi self |
| 1406 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 1407 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1408 | if len(args) == 0: |
Benjamin Peterson | be74a37 | 2009-09-11 21:17:13 +0000 | [diff] [blame] | 1409 | keys = sorted(self.aliases.keys()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1410 | for alias in keys: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1411 | self.message("%s = %s" % (alias, self.aliases[alias])) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1412 | return |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 1413 | if args[0] in self.aliases and len(args) == 1: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1414 | self.message("%s = %s" % (args[0], self.aliases[args[0]])) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1415 | else: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 1416 | self.aliases[args[0]] = ' '.join(args[1:]) |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 1417 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1418 | def do_unalias(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1419 | """unalias name |
| 1420 | Delete the specified alias. |
| 1421 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 1422 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1423 | if len(args) == 0: return |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1424 | if args[0] in self.aliases: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1425 | del self.aliases[args[0]] |
Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 1426 | |
Georg Brandl | 4c7c3c5 | 2012-03-10 22:36:48 +0100 | [diff] [blame] | 1427 | def complete_unalias(self, text, line, begidx, endidx): |
| 1428 | return [a for a in self.aliases if a.startswith(text)] |
| 1429 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1430 | # List of all the commands making the program resume execution. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1431 | commands_resuming = ['do_continue', 'do_step', 'do_next', 'do_return', |
| 1432 | 'do_quit', 'do_jump'] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1433 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1434 | # Print a traceback starting at the top stack frame. |
| 1435 | # The most recently entered frame is printed last; |
| 1436 | # this is different from dbx and gdb, but consistent with |
| 1437 | # the Python interpreter's stack trace. |
| 1438 | # It is also consistent with the up/down commands (which are |
| 1439 | # compatible with dbx and gdb: up moves towards 'main()' |
| 1440 | # and down moves towards the most recent stack frame). |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1441 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1442 | def print_stack_trace(self): |
| 1443 | try: |
| 1444 | for frame_lineno in self.stack: |
| 1445 | self.print_stack_entry(frame_lineno) |
| 1446 | except KeyboardInterrupt: |
| 1447 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1448 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1449 | def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix): |
| 1450 | frame, lineno = frame_lineno |
| 1451 | if frame is self.curframe: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1452 | prefix = '> ' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1453 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1454 | prefix = ' ' |
| 1455 | self.message(prefix + |
| 1456 | self.format_stack_entry(frame_lineno, prompt_prefix)) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1457 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1458 | # Provide help |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 1459 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1460 | def do_help(self, arg): |
| 1461 | """h(elp) |
| 1462 | Without argument, print the list of available commands. |
| 1463 | With a command name as argument, print help about that command. |
| 1464 | "help pdb" shows the full pdb documentation. |
| 1465 | "help exec" gives help on the ! command. |
| 1466 | """ |
| 1467 | if not arg: |
| 1468 | return cmd.Cmd.do_help(self, arg) |
| 1469 | try: |
| 1470 | try: |
| 1471 | topic = getattr(self, 'help_' + arg) |
| 1472 | return topic() |
| 1473 | except AttributeError: |
| 1474 | command = getattr(self, 'do_' + arg) |
| 1475 | except AttributeError: |
| 1476 | self.error('No help for %r' % arg) |
| 1477 | else: |
Georg Brandl | 9e7dbc8 | 2010-10-14 07:14:31 +0000 | [diff] [blame] | 1478 | if sys.flags.optimize >= 2: |
| 1479 | self.error('No help for %r; please do not run Python with -OO ' |
| 1480 | 'if you need command help' % arg) |
| 1481 | return |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1482 | self.message(command.__doc__.rstrip()) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 1483 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1484 | do_h = do_help |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1485 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1486 | def help_exec(self): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1487 | """(!) statement |
| 1488 | Execute the (one-line) statement in the context of the current |
| 1489 | stack frame. The exclamation point can be omitted unless the |
| 1490 | first word of the statement resembles a debugger command. To |
| 1491 | assign to a global variable you must always prefix the command |
| 1492 | with a 'global' command, e.g.: |
| 1493 | (Pdb) global list_options; list_options = ['-l'] |
| 1494 | (Pdb) |
| 1495 | """ |
Georg Brandl | 9e7dbc8 | 2010-10-14 07:14:31 +0000 | [diff] [blame] | 1496 | self.message((self.help_exec.__doc__ or '').strip()) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1497 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1498 | def help_pdb(self): |
| 1499 | help() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1500 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1501 | # other helper functions |
| 1502 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1503 | def lookupmodule(self, filename): |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1504 | """Helper function for break/clear parsing -- may be overridden. |
| 1505 | |
| 1506 | lookupmodule() translates (possibly incomplete) file or module name |
| 1507 | into an absolute file name. |
| 1508 | """ |
| 1509 | if os.path.isabs(filename) and os.path.exists(filename): |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1510 | return filename |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1511 | f = os.path.join(sys.path[0], filename) |
| 1512 | if os.path.exists(f) and self.canonic(f) == self.mainpyfile: |
| 1513 | return f |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1514 | root, ext = os.path.splitext(filename) |
| 1515 | if ext == '': |
| 1516 | filename = filename + '.py' |
| 1517 | if os.path.isabs(filename): |
| 1518 | return filename |
| 1519 | for dirname in sys.path: |
| 1520 | while os.path.islink(dirname): |
| 1521 | dirname = os.readlink(dirname) |
| 1522 | fullname = os.path.join(dirname, filename) |
| 1523 | if os.path.exists(fullname): |
| 1524 | return fullname |
| 1525 | return None |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 1526 | |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1527 | def _runmodule(self, module_name): |
| 1528 | self._wait_for_mainpyfile = True |
| 1529 | self._user_requested_quit = False |
| 1530 | import runpy |
| 1531 | mod_name, mod_spec, code = runpy._get_module_details(module_name) |
| 1532 | self.mainpyfile = self.canonic(code.co_filename) |
| 1533 | import __main__ |
| 1534 | __main__.__dict__.clear() |
| 1535 | __main__.__dict__.update({ |
| 1536 | "__name__": "__main__", |
| 1537 | "__file__": self.mainpyfile, |
Mario Corchero | 38bfa84 | 2018-02-03 06:40:11 +0000 | [diff] [blame] | 1538 | "__package__": mod_spec.parent, |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1539 | "__loader__": mod_spec.loader, |
| 1540 | "__spec__": mod_spec, |
| 1541 | "__builtins__": __builtins__, |
| 1542 | }) |
| 1543 | self.run(code) |
| 1544 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1545 | def _runscript(self, filename): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1546 | # The script has to run in __main__ namespace (or imports from |
| 1547 | # __main__ will break). |
| 1548 | # |
| 1549 | # So we clear up the __main__ and set several special variables |
| 1550 | # (this gets rid of pdb's globals and cleans old variables on restarts). |
| 1551 | import __main__ |
| 1552 | __main__.__dict__.clear() |
| 1553 | __main__.__dict__.update({"__name__" : "__main__", |
| 1554 | "__file__" : filename, |
| 1555 | "__builtins__": __builtins__, |
| 1556 | }) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1557 | |
| 1558 | # When bdb sets tracing, a number of call and line events happens |
| 1559 | # BEFORE debugger even reaches user's code (and the exact sequence of |
| 1560 | # events depends on python version). So we take special measures to |
| 1561 | # avoid stopping before we reach the main script (see user_line and |
| 1562 | # user_call for details). |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 1563 | self._wait_for_mainpyfile = True |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1564 | self.mainpyfile = self.canonic(filename) |
Georg Brandl | ac9a2bb | 2010-11-29 20:19:15 +0000 | [diff] [blame] | 1565 | self._user_requested_quit = False |
Georg Brandl | d07ac64 | 2009-08-13 07:50:57 +0000 | [diff] [blame] | 1566 | with open(filename, "rb") as fp: |
| 1567 | statement = "exec(compile(%r, %r, 'exec'))" % \ |
| 1568 | (fp.read(), self.mainpyfile) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1569 | self.run(statement) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1570 | |
Georg Brandl | 9e7dbc8 | 2010-10-14 07:14:31 +0000 | [diff] [blame] | 1571 | # Collect all command help into docstring, if not run with -OO |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1572 | |
Georg Brandl | 9e7dbc8 | 2010-10-14 07:14:31 +0000 | [diff] [blame] | 1573 | if __doc__ is not None: |
| 1574 | # unfortunately we can't guess this order from the class definition |
| 1575 | _help_order = [ |
| 1576 | 'help', 'where', 'down', 'up', 'break', 'tbreak', 'clear', 'disable', |
| 1577 | 'enable', 'ignore', 'condition', 'commands', 'step', 'next', 'until', |
| 1578 | 'jump', 'return', 'retval', 'run', 'continue', 'list', 'longlist', |
R David Murray | 78d692f | 2013-10-10 17:23:26 -0400 | [diff] [blame] | 1579 | 'args', 'p', 'pp', 'whatis', 'source', 'display', 'undisplay', |
Georg Brandl | cbc79c7 | 2010-12-04 16:21:42 +0000 | [diff] [blame] | 1580 | 'interact', 'alias', 'unalias', 'debug', 'quit', |
Georg Brandl | 9e7dbc8 | 2010-10-14 07:14:31 +0000 | [diff] [blame] | 1581 | ] |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1582 | |
Georg Brandl | 9e7dbc8 | 2010-10-14 07:14:31 +0000 | [diff] [blame] | 1583 | for _command in _help_order: |
| 1584 | __doc__ += getattr(Pdb, 'do_' + _command).__doc__.strip() + '\n\n' |
| 1585 | __doc__ += Pdb.help_exec.__doc__ |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1586 | |
Georg Brandl | 9e7dbc8 | 2010-10-14 07:14:31 +0000 | [diff] [blame] | 1587 | del _help_order, _command |
| 1588 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1589 | |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1590 | # Simplified interface |
| 1591 | |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 1592 | def run(statement, globals=None, locals=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1593 | Pdb().run(statement, globals, locals) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 1594 | |
| 1595 | def runeval(expression, globals=None, locals=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1596 | return Pdb().runeval(expression, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1597 | |
| 1598 | def runctx(statement, globals, locals): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1599 | # B/W compatibility |
| 1600 | run(statement, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1601 | |
Raymond Hettinger | 2ef7e6c | 2004-10-24 00:32:24 +0000 | [diff] [blame] | 1602 | def runcall(*args, **kwds): |
| 1603 | return Pdb().runcall(*args, **kwds) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 1604 | |
Barry Warsaw | 35425d6 | 2017-09-22 12:29:42 -0400 | [diff] [blame] | 1605 | def set_trace(*, header=None): |
| 1606 | pdb = Pdb() |
| 1607 | if header is not None: |
| 1608 | pdb.message(header) |
| 1609 | pdb.set_trace(sys._getframe().f_back) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1610 | |
| 1611 | # Post-Mortem interface |
| 1612 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1613 | def post_mortem(t=None): |
| 1614 | # handling the default |
| 1615 | if t is None: |
| 1616 | # sys.exc_info() returns (type, value, traceback) if an exception is |
| 1617 | # being handled, otherwise it returns None |
| 1618 | t = sys.exc_info()[2] |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1619 | if t is None: |
| 1620 | raise ValueError("A valid traceback must be passed if no " |
| 1621 | "exception is being handled") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1622 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1623 | p = Pdb() |
| 1624 | p.reset() |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 1625 | p.interaction(None, t) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1626 | |
| 1627 | def pm(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1628 | post_mortem(sys.last_traceback) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1629 | |
| 1630 | |
| 1631 | # Main program for testing |
| 1632 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 1633 | TESTCMD = 'import x; x.main()' |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1634 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 1635 | def test(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1636 | run(TESTCMD) |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 1637 | |
| 1638 | # print help |
| 1639 | def help(): |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 1640 | import pydoc |
| 1641 | pydoc.pager(__doc__) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1642 | |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1643 | _usage = """\ |
Mario Corchero | fcf8b4c | 2018-01-28 04:58:47 +0000 | [diff] [blame] | 1644 | usage: pdb.py [-c command] ... [-m module | pyfile] [arg] ... |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1645 | |
Mario Corchero | fcf8b4c | 2018-01-28 04:58:47 +0000 | [diff] [blame] | 1646 | Debug the Python program given by pyfile. Alternatively, |
| 1647 | an executable module or package to debug can be specified using |
| 1648 | the -m switch. |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1649 | |
| 1650 | Initial commands are read from .pdbrc files in your home directory |
| 1651 | and in the current directory, if they exist. Commands supplied with |
| 1652 | -c are executed after commands from .pdbrc files. |
| 1653 | |
| 1654 | To let the script run until an exception occurs, use "-c continue". |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 1655 | To let the script run up to a given line X in the debugged file, use |
| 1656 | "-c 'until X'".""" |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1657 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1658 | def main(): |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1659 | import getopt |
| 1660 | |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1661 | opts, args = getopt.getopt(sys.argv[1:], 'mhc:', ['--help', '--command=']) |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1662 | |
| 1663 | if not args: |
| 1664 | print(_usage) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1665 | sys.exit(2) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1666 | |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1667 | commands = [] |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1668 | run_as_module = False |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1669 | for opt, optarg in opts: |
| 1670 | if opt in ['-h', '--help']: |
| 1671 | print(_usage) |
| 1672 | sys.exit() |
| 1673 | elif opt in ['-c', '--command']: |
| 1674 | commands.append(optarg) |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1675 | elif opt in ['-m']: |
| 1676 | run_as_module = True |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1677 | |
| 1678 | mainpyfile = args[0] # Get script filename |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1679 | if not run_as_module and not os.path.exists(mainpyfile): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1680 | print('Error:', mainpyfile, 'does not exist') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1681 | sys.exit(1) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1682 | |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1683 | sys.argv[:] = args # Hide "pdb.py" and pdb options from argument list |
Guido van Rossum | ec577d5 | 1996-09-10 17:39:34 +0000 | [diff] [blame] | 1684 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1685 | # Replace pdb's dir with script's dir in front of module search path. |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1686 | if not run_as_module: |
| 1687 | sys.path[0] = os.path.dirname(mainpyfile) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1688 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1689 | # Note on saving/restoring sys.argv: it's a good idea when sys.argv was |
| 1690 | # modified by the script being debugged. It's a bad idea when it was |
Georg Brandl | 3078df0 | 2009-05-05 09:11:31 +0000 | [diff] [blame] | 1691 | # changed by the user from the command line. There is a "restart" command |
| 1692 | # which allows explicit specification of command line arguments. |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1693 | pdb = Pdb() |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1694 | pdb.rcLines.extend(commands) |
Georg Brandl | 1e30bd3 | 2010-07-30 07:21:26 +0000 | [diff] [blame] | 1695 | while True: |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1696 | try: |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1697 | if run_as_module: |
| 1698 | pdb._runmodule(mainpyfile) |
| 1699 | else: |
| 1700 | pdb._runscript(mainpyfile) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1701 | if pdb._user_requested_quit: |
| 1702 | break |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1703 | print("The program finished and will be restarted") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1704 | except Restart: |
| 1705 | print("Restarting", mainpyfile, "with arguments:") |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1706 | print("\t" + " ".join(args)) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1707 | except SystemExit: |
| 1708 | # In most cases SystemExit does not warrant a post-mortem session. |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1709 | print("The program exited via sys.exit(). Exit status:", end=' ') |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1710 | print(sys.exc_info()[1]) |
Terry Jan Reedy | ca3f435 | 2015-09-05 19:13:26 -0400 | [diff] [blame] | 1711 | except SyntaxError: |
| 1712 | traceback.print_exc() |
| 1713 | sys.exit(1) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1714 | except: |
| 1715 | traceback.print_exc() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1716 | print("Uncaught exception. Entering post mortem debugging") |
| 1717 | print("Running 'cont' or 'step' will restart the program") |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1718 | t = sys.exc_info()[2] |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 1719 | pdb.interaction(None, t) |
Georg Brandl | 3078df0 | 2009-05-05 09:11:31 +0000 | [diff] [blame] | 1720 | print("Post mortem debugger finished. The " + mainpyfile + |
| 1721 | " will be restarted") |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1722 | |
| 1723 | |
| 1724 | # When invoked as main program, invoke the debugger on a script |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1725 | if __name__ == '__main__': |
| 1726 | import pdb |
| 1727 | pdb.main() |