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 |
| 55 | defined there can be overriden by the local file. |
| 56 | |
| 57 | Aside from aliases, the debugger is not directly programmable; but it |
| 58 | is implemented as a class from which you can derive your own debugger |
| 59 | class, which you can make as fancy as you like. |
| 60 | |
| 61 | |
| 62 | Debugger commands |
| 63 | ================= |
| 64 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 65 | """ |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 66 | # NOTE: the actual command documentation is collected from docstrings of the |
| 67 | # 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] | 68 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 69 | import sys |
| 70 | import linecache |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 71 | import cmd |
| 72 | import bdb |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 73 | import os |
Barry Warsaw | 2bee8fe | 1999-09-09 16:32:41 +0000 | [diff] [blame] | 74 | import re |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 75 | import pprint |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 76 | import traceback |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 77 | import inspect |
| 78 | import types |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | class Restart(Exception): |
| 82 | """Causes a debugger to be restarted for the debugged python program.""" |
| 83 | pass |
| 84 | |
Skip Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 85 | __all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace", |
| 86 | "post_mortem", "help"] |
| 87 | |
Barry Warsaw | 2bee8fe | 1999-09-09 16:32:41 +0000 | [diff] [blame] | 88 | def find_function(funcname, filename): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 89 | cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 90 | try: |
| 91 | fp = open(filename) |
| 92 | except IOError: |
| 93 | return None |
| 94 | # consumer of this info expects the first line to be 1 |
| 95 | lineno = 1 |
| 96 | answer = None |
| 97 | while 1: |
| 98 | line = fp.readline() |
| 99 | if line == '': |
| 100 | break |
| 101 | if cre.match(line): |
| 102 | answer = funcname, filename, lineno |
| 103 | break |
| 104 | lineno = lineno + 1 |
| 105 | fp.close() |
| 106 | return answer |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 107 | |
Georg Brandl | 5ed2b5a | 2010-07-30 18:08:12 +0000 | [diff] [blame] | 108 | def getsourcelines(obj): |
| 109 | lines, lineno = inspect.findsource(obj) |
| 110 | if inspect.isframe(obj) and lineno == 0 and \ |
| 111 | obj.f_globals is obj.f_locals: |
| 112 | # must be a module frame: do not try to cut a block out of it |
| 113 | return lines, 0 |
| 114 | elif inspect.ismodule(obj): |
| 115 | return lines, 0 |
| 116 | return inspect.getblock(lines[lineno:]), lineno+1 |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 117 | |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 118 | # Interaction prompt line will separate file and call info from code |
| 119 | # text using value of line_prefix string. A newline and arrow may |
| 120 | # be to your liking. You can set it once pdb is imported using the |
| 121 | # command "pdb.line_prefix = '\n% '". |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 122 | # line_prefix = ': ' # Use this to get the old situation back |
| 123 | line_prefix = '\n-> ' # Probably a better default |
Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 125 | class Pdb(bdb.Bdb, cmd.Cmd): |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 126 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 127 | def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None): |
| 128 | bdb.Bdb.__init__(self, skip=skip) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 129 | cmd.Cmd.__init__(self, completekey, stdin, stdout) |
| 130 | if stdout: |
| 131 | self.use_rawinput = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 132 | self.prompt = '(Pdb) ' |
| 133 | self.aliases = {} |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 134 | self.mainpyfile = '' |
| 135 | self._wait_for_mainpyfile = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 136 | # Try to load readline if it exists |
| 137 | try: |
| 138 | import readline |
| 139 | except ImportError: |
| 140 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 141 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 142 | # Read $HOME/.pdbrc and ./.pdbrc |
| 143 | self.rcLines = [] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 144 | if 'HOME' in os.environ: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 145 | envHome = os.environ['HOME'] |
| 146 | try: |
| 147 | rcFile = open(os.path.join(envHome, ".pdbrc")) |
| 148 | except IOError: |
| 149 | pass |
| 150 | else: |
| 151 | for line in rcFile.readlines(): |
| 152 | self.rcLines.append(line) |
| 153 | rcFile.close() |
| 154 | try: |
| 155 | rcFile = open(".pdbrc") |
| 156 | except IOError: |
| 157 | pass |
| 158 | else: |
| 159 | for line in rcFile.readlines(): |
| 160 | self.rcLines.append(line) |
| 161 | rcFile.close() |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 162 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 163 | self.commands = {} # associates a command list to breakpoint numbers |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 164 | self.commands_doprompt = {} # for each bp num, tells if the prompt |
| 165 | # must be disp. after execing the cmd list |
| 166 | self.commands_silent = {} # for each bp num, tells if the stack trace |
| 167 | # must be disp. after execing the cmd list |
| 168 | self.commands_defining = False # True while in the process of defining |
| 169 | # a command list |
| 170 | self.commands_bnum = None # The breakpoint number for which we are |
| 171 | # defining a list |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 172 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 173 | def reset(self): |
| 174 | bdb.Bdb.reset(self) |
| 175 | self.forget() |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 176 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 177 | def forget(self): |
| 178 | self.lineno = None |
| 179 | self.stack = [] |
| 180 | self.curindex = 0 |
| 181 | self.curframe = None |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 182 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 183 | def setup(self, f, t): |
| 184 | self.forget() |
| 185 | self.stack, self.curindex = self.get_stack(f, t) |
| 186 | self.curframe = self.stack[self.curindex][0] |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 187 | # The f_locals dictionary is updated from the actual frame |
| 188 | # locals whenever the .f_locals accessor is called, so we |
| 189 | # cache it here to ensure that modifications are not overwritten. |
| 190 | self.curframe_locals = self.curframe.f_locals |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 191 | return self.execRcLines() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 192 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 193 | # Can be executed earlier than 'setup' if desired |
| 194 | def execRcLines(self): |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 195 | if not self.rcLines: |
| 196 | return |
| 197 | # local copy because of recursion |
| 198 | rcLines = self.rcLines |
| 199 | rcLines.reverse() |
| 200 | # execute every line only once |
| 201 | self.rcLines = [] |
| 202 | while rcLines: |
| 203 | line = rcLines.pop().strip() |
| 204 | if line and line[0] != '#': |
| 205 | if self.onecmd(line): |
| 206 | # if onecmd returns True, the command wants to exit |
| 207 | # from the interaction, save leftover rc lines |
| 208 | # to execute before next interaction |
| 209 | self.rcLines += reversed(rcLines) |
| 210 | return True |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 211 | |
Tim Peters | 280488b | 2002-08-23 18:19:30 +0000 | [diff] [blame] | 212 | # Override Bdb methods |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 213 | |
| 214 | def user_call(self, frame, argument_list): |
| 215 | """This method is called when there is the remote possibility |
| 216 | that we ever need to stop in this function.""" |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 217 | if self._wait_for_mainpyfile: |
| 218 | return |
Michael W. Hudson | 01eb85c | 2003-01-31 17:48:29 +0000 | [diff] [blame] | 219 | if self.stop_here(frame): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 220 | self.message('--Call--') |
Michael W. Hudson | 01eb85c | 2003-01-31 17:48:29 +0000 | [diff] [blame] | 221 | self.interaction(frame, None) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 222 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 223 | def user_line(self, frame): |
| 224 | """This function is called when we stop or break at this line.""" |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 225 | if self._wait_for_mainpyfile: |
| 226 | if (self.mainpyfile != self.canonic(frame.f_code.co_filename) |
| 227 | or frame.f_lineno<= 0): |
| 228 | return |
| 229 | self._wait_for_mainpyfile = 0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 230 | if self.bp_commands(frame): |
| 231 | self.interaction(frame, None) |
| 232 | |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 233 | def bp_commands(self, frame): |
Georg Brandl | 3078df0 | 2009-05-05 09:11:31 +0000 | [diff] [blame] | 234 | """Call every command that was set for the current active breakpoint |
| 235 | (if there is one). |
| 236 | |
| 237 | Returns True if the normal interaction function must be called, |
| 238 | False otherwise.""" |
| 239 | # self.currentbp is set in bdb in Bdb.break_here if a breakpoint was hit |
| 240 | if getattr(self, "currentbp", False) and \ |
| 241 | self.currentbp in self.commands: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 242 | currentbp = self.currentbp |
| 243 | self.currentbp = 0 |
| 244 | lastcmd_back = self.lastcmd |
| 245 | self.setup(frame, None) |
| 246 | for line in self.commands[currentbp]: |
| 247 | self.onecmd(line) |
| 248 | self.lastcmd = lastcmd_back |
| 249 | if not self.commands_silent[currentbp]: |
| 250 | self.print_stack_entry(self.stack[self.curindex]) |
| 251 | if self.commands_doprompt[currentbp]: |
| 252 | self.cmdloop() |
| 253 | self.forget() |
| 254 | return |
| 255 | return 1 |
Guido van Rossum | 9e1ee97 | 1997-07-11 13:43:53 +0000 | [diff] [blame] | 256 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 257 | def user_return(self, frame, return_value): |
| 258 | """This function is called when a return trap is set here.""" |
Georg Brandl | 34cc0f5 | 2010-07-30 09:43:00 +0000 | [diff] [blame] | 259 | if self._wait_for_mainpyfile: |
| 260 | return |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 261 | frame.f_locals['__return__'] = return_value |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 262 | self.message('--Return--') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 263 | self.interaction(frame, None) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 264 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 265 | def user_exception(self, frame, exc_info): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 266 | """This function is called if an exception occurs, |
| 267 | 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] | 268 | if self._wait_for_mainpyfile: |
| 269 | return |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 270 | exc_type, exc_value, exc_traceback = exc_info |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 271 | frame.f_locals['__exception__'] = exc_type, exc_value |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 272 | self.message(traceback.format_exception_only(exc_type, |
| 273 | exc_value)[-1].strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 274 | self.interaction(frame, exc_traceback) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 275 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 276 | # General interaction function |
| 277 | |
| 278 | def interaction(self, frame, traceback): |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 279 | if self.setup(frame, traceback): |
| 280 | # no interaction desired at this time (happens if .pdbrc contains |
| 281 | # a command like "continue") |
| 282 | self.forget() |
| 283 | return |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 284 | self.print_stack_entry(self.stack[self.curindex]) |
| 285 | self.cmdloop() |
| 286 | self.forget() |
| 287 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 288 | def displayhook(self, obj): |
| 289 | """Custom displayhook for the exec in default(), which prevents |
| 290 | assignment of the _ variable in the builtins. |
| 291 | """ |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 292 | # reproduce the behavior of the standard displayhook, not printing None |
| 293 | if obj is not None: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 294 | self.message(repr(obj)) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 295 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 296 | def default(self, line): |
| 297 | if line[:1] == '!': line = line[1:] |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 298 | locals = self.curframe_locals |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 299 | globals = self.curframe.f_globals |
| 300 | try: |
| 301 | code = compile(line + '\n', '<stdin>', 'single') |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 302 | save_stdout = sys.stdout |
| 303 | save_stdin = sys.stdin |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 304 | save_displayhook = sys.displayhook |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 305 | try: |
| 306 | sys.stdin = self.stdin |
| 307 | sys.stdout = self.stdout |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 308 | sys.displayhook = self.displayhook |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 309 | exec(code, globals, locals) |
| 310 | finally: |
| 311 | sys.stdout = save_stdout |
| 312 | sys.stdin = save_stdin |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 313 | sys.displayhook = save_displayhook |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 314 | except: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 315 | exc_info = sys.exc_info()[:2] |
| 316 | self.error(traceback.format_exception_only(*exc_info)[-1].strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 317 | |
| 318 | def precmd(self, line): |
| 319 | """Handle alias expansion and ';;' separator.""" |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 320 | if not line.strip(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 321 | return line |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 322 | args = line.split() |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 323 | while args[0] in self.aliases: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 324 | line = self.aliases[args[0]] |
| 325 | ii = 1 |
| 326 | for tmpArg in args[1:]: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 327 | line = line.replace("%" + str(ii), |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 328 | tmpArg) |
| 329 | ii = ii + 1 |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 330 | line = line.replace("%*", ' '.join(args[1:])) |
| 331 | args = line.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 332 | # split into ';;' separated commands |
| 333 | # unless it's an alias command |
| 334 | if args[0] != 'alias': |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 335 | marker = line.find(';;') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 336 | if marker >= 0: |
| 337 | # queue up everything after marker |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 338 | next = line[marker+2:].lstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 339 | self.cmdqueue.append(next) |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 340 | line = line[:marker].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 341 | return line |
| 342 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | def onecmd(self, line): |
| 344 | """Interpret the argument as though it had been typed in response |
| 345 | to the prompt. |
| 346 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 347 | Checks whether this line is typed at the normal prompt or in |
| 348 | a breakpoint command list definition. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 349 | """ |
| 350 | if not self.commands_defining: |
| 351 | return cmd.Cmd.onecmd(self, line) |
| 352 | else: |
| 353 | return self.handle_command_def(line) |
| 354 | |
| 355 | def handle_command_def(self,line): |
Georg Brandl | 44f8bf9 | 2010-07-30 08:54:49 +0000 | [diff] [blame] | 356 | """Handles one command line during command list definition.""" |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 357 | cmd, arg, line = self.parseline(line) |
Georg Brandl | 44f8bf9 | 2010-07-30 08:54:49 +0000 | [diff] [blame] | 358 | if not cmd: |
| 359 | return |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | if cmd == 'silent': |
| 361 | self.commands_silent[self.commands_bnum] = True |
| 362 | return # continue to handle other cmd def in the cmd list |
| 363 | elif cmd == 'end': |
| 364 | self.cmdqueue = [] |
| 365 | return 1 # end of cmd list |
| 366 | cmdlist = self.commands[self.commands_bnum] |
Georg Brandl | 44f8bf9 | 2010-07-30 08:54:49 +0000 | [diff] [blame] | 367 | if arg: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 368 | cmdlist.append(cmd+' '+arg) |
| 369 | else: |
| 370 | cmdlist.append(cmd) |
| 371 | # Determine if we must stop |
| 372 | try: |
| 373 | func = getattr(self, 'do_' + cmd) |
| 374 | except AttributeError: |
| 375 | func = self.default |
Georg Brandl | 3078df0 | 2009-05-05 09:11:31 +0000 | [diff] [blame] | 376 | # one of the resuming commands |
| 377 | if func.__name__ in self.commands_resuming: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 378 | self.commands_doprompt[self.commands_bnum] = False |
| 379 | self.cmdqueue = [] |
| 380 | return 1 |
| 381 | return |
| 382 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 383 | # interface abstraction functions |
| 384 | |
| 385 | def message(self, msg): |
| 386 | print(msg, file=self.stdout) |
| 387 | |
| 388 | def error(self, msg): |
| 389 | print('***', msg, file=self.stdout) |
| 390 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 391 | # Command definitions, called by cmdloop() |
| 392 | # The argument is the remaining string on the command line |
| 393 | # Return true to exit from the command loop |
| 394 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 395 | def do_commands(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 396 | """commands [bpnumber] |
| 397 | (com) ... |
| 398 | (com) end |
| 399 | (Pdb) |
Georg Brandl | 3078df0 | 2009-05-05 09:11:31 +0000 | [diff] [blame] | 400 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 401 | Specify a list of commands for breakpoint number bpnumber. |
| 402 | The commands themselves are entered on the following lines. |
| 403 | Type a line containing just 'end' to terminate the commands. |
| 404 | The commands are executed when the breakpoint is hit. |
| 405 | |
| 406 | To remove all commands from a breakpoint, type commands and |
| 407 | follow it immediately with end; that is, give no commands. |
| 408 | |
| 409 | With no bpnumber argument, commands refers to the last |
| 410 | breakpoint set. |
| 411 | |
| 412 | You can use breakpoint commands to start your program up |
| 413 | again. Simply use the continue command, or step, or any other |
| 414 | command that resumes execution. |
| 415 | |
| 416 | Specifying any command resuming execution (currently continue, |
| 417 | step, next, return, jump, quit and their abbreviations) |
| 418 | terminates the command list (as if that command was |
| 419 | immediately followed by end). This is because any time you |
| 420 | resume execution (even with a simple next or step), you may |
| 421 | encounter another breakpoint -- which could have its own |
| 422 | command list, leading to ambiguities about which list to |
| 423 | execute. |
| 424 | |
| 425 | If you use the 'silent' command in the command list, the usual |
| 426 | message about stopping at a breakpoint is not printed. This |
| 427 | may be desirable for breakpoints that are to print a specific |
| 428 | message and then continue. If none of the other commands |
| 429 | print anything, you will see no sign that the breakpoint was |
| 430 | reached. |
| 431 | """ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 432 | if not arg: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 433 | bnum = len(bdb.Breakpoint.bpbynumber) - 1 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 434 | else: |
| 435 | try: |
| 436 | bnum = int(arg) |
| 437 | except: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 438 | self.error("Usage: commands [bnum]\n ...\n end") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 439 | return |
| 440 | self.commands_bnum = bnum |
| 441 | self.commands[bnum] = [] |
| 442 | self.commands_doprompt[bnum] = True |
| 443 | self.commands_silent[bnum] = False |
| 444 | prompt_back = self.prompt |
| 445 | self.prompt = '(com) ' |
| 446 | self.commands_defining = True |
Georg Brandl | 44f8bf9 | 2010-07-30 08:54:49 +0000 | [diff] [blame] | 447 | try: |
| 448 | self.cmdloop() |
| 449 | finally: |
| 450 | self.commands_defining = False |
| 451 | self.prompt = prompt_back |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 452 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 453 | def do_break(self, arg, temporary = 0): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 454 | """b(reak) [ ([filename:]lineno | function) [, condition] ] |
| 455 | Without argument, list all breaks. |
| 456 | |
| 457 | With a line number argument, set a break at this line in the |
| 458 | current file. With a function name, set a break at the first |
| 459 | executable line of that function. If a second argument is |
| 460 | present, it is a string specifying an expression which must |
| 461 | evaluate to true before the breakpoint is honored. |
| 462 | |
| 463 | The line number may be prefixed with a filename and a colon, |
| 464 | to specify a breakpoint in another file (probably one that |
| 465 | hasn't been loaded yet). The file is searched for on |
| 466 | sys.path; the .py suffix may be omitted. |
| 467 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 468 | if not arg: |
| 469 | if self.breaks: # There's at least one |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 470 | self.message("Num Type Disp Enb Where") |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 471 | for bp in bdb.Breakpoint.bpbynumber: |
| 472 | if bp: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 473 | self.message(bp.bpformat()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 474 | return |
| 475 | # parse arguments; comma has lowest precedence |
| 476 | # and cannot occur in filename |
| 477 | filename = None |
| 478 | lineno = None |
| 479 | cond = None |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 480 | comma = arg.find(',') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 481 | if comma > 0: |
| 482 | # parse stuff after comma: "condition" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 483 | cond = arg[comma+1:].lstrip() |
| 484 | arg = arg[:comma].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 485 | # parse stuff before comma: [filename:]lineno | function |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 486 | colon = arg.rfind(':') |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 487 | funcname = None |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 488 | if colon >= 0: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 489 | filename = arg[:colon].rstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 490 | f = self.lookupmodule(filename) |
| 491 | if not f: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 492 | self.error('%r not found from sys.path' % filename) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 493 | return |
| 494 | else: |
| 495 | filename = f |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 496 | arg = arg[colon+1:].lstrip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 497 | try: |
| 498 | lineno = int(arg) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 499 | except ValueError as msg: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 500 | self.error('Bad lineno: %s' % arg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 501 | return |
| 502 | else: |
| 503 | # no colon; can be lineno or function |
| 504 | try: |
| 505 | lineno = int(arg) |
| 506 | except ValueError: |
| 507 | try: |
| 508 | func = eval(arg, |
| 509 | self.curframe.f_globals, |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 510 | self.curframe_locals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 511 | except: |
| 512 | func = arg |
| 513 | try: |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 514 | if hasattr(func, '__func__'): |
| 515 | func = func.__func__ |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 516 | code = func.__code__ |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 517 | #use co_name to identify the bkpt (function names |
| 518 | #could be aliased, but co_name is invariant) |
| 519 | funcname = code.co_name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 520 | lineno = code.co_firstlineno |
| 521 | filename = code.co_filename |
| 522 | except: |
| 523 | # last thing to try |
| 524 | (ok, filename, ln) = self.lineinfo(arg) |
| 525 | if not ok: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 526 | self.error('The specified object %r is not a function ' |
| 527 | 'or was not found along sys.path.' % arg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 528 | return |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 529 | funcname = ok # ok contains a function name |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 530 | lineno = int(ln) |
| 531 | if not filename: |
| 532 | filename = self.defaultFile() |
| 533 | # Check for reasonable breakpoint |
| 534 | line = self.checkline(filename, lineno) |
| 535 | if line: |
| 536 | # now set the break point |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 537 | err = self.set_break(filename, line, temporary, cond, funcname) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 538 | if err: |
| 539 | self.error(err, file=self.stdout) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 540 | else: |
| 541 | bp = self.get_breaks(filename, line)[-1] |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 542 | self.message("Breakpoint %d at %s:%d" % |
| 543 | (bp.number, bp.file, bp.line)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 544 | |
| 545 | # To be overridden in derived debuggers |
| 546 | def defaultFile(self): |
| 547 | """Produce a reasonable default.""" |
| 548 | filename = self.curframe.f_code.co_filename |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 549 | if filename == '<string>' and self.mainpyfile: |
| 550 | filename = self.mainpyfile |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 551 | return filename |
| 552 | |
| 553 | do_b = do_break |
| 554 | |
| 555 | def do_tbreak(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 556 | """tbreak [ ([filename:]lineno | function) [, condition] ] |
| 557 | Same arguments as break, but sets a temporary breakpoint: it |
| 558 | is automatically deleted when first hit. |
| 559 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 560 | self.do_break(arg, 1) |
| 561 | |
| 562 | def lineinfo(self, identifier): |
| 563 | failed = (None, None, None) |
| 564 | # Input is identifier, may be in single quotes |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 565 | idstring = identifier.split("'") |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 566 | if len(idstring) == 1: |
| 567 | # not in single quotes |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 568 | id = idstring[0].strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 569 | elif len(idstring) == 3: |
| 570 | # quoted |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 571 | id = idstring[1].strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 572 | else: |
| 573 | return failed |
| 574 | if id == '': return failed |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 575 | parts = id.split('.') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 576 | # Protection for derived debuggers |
| 577 | if parts[0] == 'self': |
| 578 | del parts[0] |
| 579 | if len(parts) == 0: |
| 580 | return failed |
| 581 | # Best first guess at file to look at |
| 582 | fname = self.defaultFile() |
| 583 | if len(parts) == 1: |
| 584 | item = parts[0] |
| 585 | else: |
| 586 | # More than one part. |
| 587 | # First is module, second is method/class |
| 588 | f = self.lookupmodule(parts[0]) |
| 589 | if f: |
| 590 | fname = f |
| 591 | item = parts[1] |
| 592 | answer = find_function(item, fname) |
| 593 | return answer or failed |
| 594 | |
| 595 | def checkline(self, filename, lineno): |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 596 | """Check whether specified line seems to be executable. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 597 | |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 598 | Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank |
| 599 | line or EOF). Warning: testing is not comprehensive. |
| 600 | """ |
Georg Brandl | 1e30bd3 | 2010-07-30 07:21:26 +0000 | [diff] [blame] | 601 | # this method should be callable before starting debugging, so default |
| 602 | # to "no globals" if there is no current frame |
| 603 | globs = self.curframe.f_globals if hasattr(self, 'curframe') else None |
| 604 | line = linecache.getline(filename, lineno, globs) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 605 | if not line: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 606 | self.message('End of file') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 607 | return 0 |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 608 | line = line.strip() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 609 | # Don't allow setting breakpoint at a blank line |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 610 | if (not line or (line[0] == '#') or |
| 611 | (line[:3] == '"""') or line[:3] == "'''"): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 612 | self.error('Blank or comment') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 613 | return 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 614 | return lineno |
| 615 | |
| 616 | def do_enable(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 617 | """enable bpnumber [bpnumber ...] |
| 618 | Enables the breakpoints given as a space separated list of |
| 619 | breakpoint numbers. |
| 620 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 621 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 622 | for i in args: |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 623 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 624 | bp = self.get_bpbynumber(i) |
| 625 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 626 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 627 | else: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 628 | bp.enable() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 629 | self.message('Enabled %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 630 | |
| 631 | def do_disable(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 632 | """disable bpnumber [bpnumber ...] |
| 633 | Disables the breakpoints given as a space separated list of |
| 634 | breakpoint numbers. Disabling a breakpoint means it cannot |
| 635 | cause the program to stop execution, but unlike clearing a |
| 636 | breakpoint, it remains in the list of breakpoints and can be |
| 637 | (re-)enabled. |
| 638 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 639 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 640 | for i in args: |
Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 641 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 642 | bp = self.get_bpbynumber(i) |
| 643 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 644 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 645 | else: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 646 | bp.disable() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 647 | self.message('Disabled %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 648 | |
| 649 | def do_condition(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 650 | """condition bpnumber [condition] |
| 651 | Set a new condition for the breakpoint, an expression which |
| 652 | must evaluate to true before the breakpoint is honored. If |
| 653 | condition is absent, any existing condition is removed; i.e., |
| 654 | the breakpoint is made unconditional. |
| 655 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 656 | args = arg.split(' ', 1) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 657 | try: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 658 | cond = args[1] |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 659 | except IndexError: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 660 | cond = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 661 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 662 | bp = self.get_bpbynumber(args[0].strip()) |
| 663 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 664 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 665 | else: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 666 | bp.cond = cond |
| 667 | if not cond: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 668 | self.message('Breakpoint %d is now unconditional.' % bp.number) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 669 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 670 | self.message('New condition set for breakpoint %d.' % bp.number) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 671 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 672 | def do_ignore(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 673 | """ignore bpnumber [count] |
| 674 | Set the ignore count for the given breakpoint number. If |
| 675 | count is omitted, the ignore count is set to 0. A breakpoint |
| 676 | becomes active when the ignore count is zero. When non-zero, |
| 677 | the count is decremented each time the breakpoint is reached |
| 678 | and the breakpoint is not disabled and any associated |
| 679 | condition evaluates to true. |
| 680 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 681 | args = arg.split() |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 682 | try: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 683 | count = int(args[1].strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 684 | except: |
| 685 | count = 0 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 686 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 687 | bp = self.get_bpbynumber(args[0].strip()) |
| 688 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 689 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 690 | else: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 691 | bp.ignore = count |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 692 | if count > 0: |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 693 | if count > 1: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 694 | countstr = '%d crossings' % count |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 695 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 696 | countstr = '1 crossing' |
| 697 | self.message('Will ignore next %s of breakpoint %d.' % |
| 698 | (countstr, bp.number)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 699 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 700 | self.message('Will stop next time breakpoint %d is reached.' |
| 701 | % bp.number) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 702 | |
| 703 | def do_clear(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 704 | """cl(ear) filename:lineno\ncl(ear) [bpnumber [bpnumber...]] |
| 705 | With a space separated list of breakpoint numbers, clear |
| 706 | those breakpoints. Without argument, clear all breaks (but |
| 707 | first ask confirmation). With a filename:lineno argument, |
| 708 | clear all breaks at that line in that file. |
| 709 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 710 | if not arg: |
| 711 | try: |
Guido van Rossum | c5b6ab0 | 2007-05-27 09:19:52 +0000 | [diff] [blame] | 712 | reply = input('Clear all breaks? ') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 713 | except EOFError: |
| 714 | reply = 'no' |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 715 | reply = reply.strip().lower() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 716 | if reply in ('y', 'yes'): |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 717 | bplist = [bp for bp in bdb.Breakpoint.bpbynumber if bp] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 718 | self.clear_all_breaks() |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 719 | for bp in bplist: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 720 | self.message('Deleted %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 721 | return |
| 722 | if ':' in arg: |
| 723 | # Make sure it works for "clear C:\foo\bar.py:12" |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 724 | i = arg.rfind(':') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 725 | filename = arg[:i] |
| 726 | arg = arg[i+1:] |
| 727 | try: |
| 728 | lineno = int(arg) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 729 | except ValueError: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 730 | err = "Invalid line number (%s)" % arg |
| 731 | else: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 732 | bplist = self.get_breaks(filename, lineno) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 733 | err = self.clear_break(filename, lineno) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 734 | if err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 735 | self.error(err) |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 736 | else: |
| 737 | for bp in bplist: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 738 | self.message('Deleted %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 739 | return |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 740 | numberlist = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 741 | for i in numberlist: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 742 | try: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 743 | bp = self.get_bpbynumber(i) |
| 744 | except ValueError as err: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 745 | self.error(err) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 746 | else: |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 747 | self.clear_break(bp.file, bp.line) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 748 | self.message('Deleted %s' % bp) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 749 | do_cl = do_clear # 'c' is already an abbreviation for 'continue' |
| 750 | |
| 751 | def do_where(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 752 | """w(here) |
| 753 | Print a stack trace, with the most recent frame at the bottom. |
| 754 | An arrow indicates the "current frame", which determines the |
| 755 | context of most commands. 'bt' is an alias for this command. |
| 756 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 757 | self.print_stack_trace() |
| 758 | do_w = do_where |
Guido van Rossum | 6bd6835 | 2001-01-20 17:57:37 +0000 | [diff] [blame] | 759 | do_bt = do_where |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 760 | |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 761 | def _select_frame(self, number): |
| 762 | assert 0 <= number < len(self.stack) |
| 763 | self.curindex = number |
| 764 | self.curframe = self.stack[self.curindex][0] |
| 765 | self.curframe_locals = self.curframe.f_locals |
| 766 | self.print_stack_entry(self.stack[self.curindex]) |
| 767 | self.lineno = None |
| 768 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 769 | def do_up(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 770 | """u(p) [count] |
| 771 | Move the current frame count (default one) levels up in the |
| 772 | stack trace (to an older frame). |
| 773 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 774 | if self.curindex == 0: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 775 | self.error('Oldest frame') |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 776 | return |
| 777 | try: |
| 778 | count = int(arg or 1) |
| 779 | except ValueError: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 780 | self.error('Invalid frame count (%s)' % arg) |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 781 | return |
| 782 | if count < 0: |
| 783 | newframe = 0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 784 | else: |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 785 | newframe = max(0, self.curindex - count) |
| 786 | self._select_frame(newframe) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 787 | do_u = do_up |
| 788 | |
| 789 | def do_down(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 790 | """d(own) [count] |
| 791 | Move the current frame count (default one) levels down in the |
| 792 | stack trace (to a newer frame). |
| 793 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 794 | if self.curindex + 1 == len(self.stack): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 795 | self.error('Newest frame') |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 796 | return |
| 797 | try: |
| 798 | count = int(arg or 1) |
| 799 | except ValueError: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 800 | self.error('Invalid frame count (%s)' % arg) |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 801 | return |
| 802 | if count < 0: |
| 803 | newframe = len(self.stack) - 1 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 804 | else: |
Georg Brandl | eb1f4aa | 2010-06-27 10:37:48 +0000 | [diff] [blame] | 805 | newframe = min(len(self.stack) - 1, self.curindex + count) |
| 806 | self._select_frame(newframe) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 807 | do_d = do_down |
| 808 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 809 | def do_until(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 810 | """unt(il) [lineno] |
| 811 | Without argument, continue execution until the line with a |
| 812 | number greater than the current one is reached. With a line |
| 813 | number, continue execution until a line with a number greater |
| 814 | or equal to that is reached. In both cases, also stop when |
| 815 | the current frame returns. |
| 816 | """ |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 817 | if arg: |
| 818 | try: |
| 819 | lineno = int(arg) |
| 820 | except ValueError: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 821 | self.error('Error in argument: %r' % arg) |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 822 | return |
| 823 | if lineno <= self.curframe.f_lineno: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 824 | self.error('"until" line number is smaller than current ' |
| 825 | 'line number') |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 826 | return |
| 827 | else: |
| 828 | lineno = None |
| 829 | self.set_until(self.curframe, lineno) |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 830 | return 1 |
| 831 | do_unt = do_until |
| 832 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 833 | def do_step(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 834 | """s(tep) |
| 835 | Execute the current line, stop at the first possible occasion |
| 836 | (either in a function that is called or in the current |
| 837 | function). |
| 838 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 839 | self.set_step() |
| 840 | return 1 |
| 841 | do_s = do_step |
| 842 | |
| 843 | def do_next(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 844 | """n(ext) |
| 845 | Continue execution until the next line in the current function |
| 846 | is reached or it returns. |
| 847 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 848 | self.set_next(self.curframe) |
| 849 | return 1 |
| 850 | do_n = do_next |
| 851 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 852 | def do_run(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 853 | """run [args...] |
| 854 | Restart the debugged python program. If a string is supplied |
| 855 | it is splitted with "shlex", and the result is used as the new |
| 856 | sys.argv. History, breakpoints, actions and debugger options |
| 857 | are preserved. "restart" is an alias for "run". |
| 858 | """ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 859 | if arg: |
| 860 | import shlex |
| 861 | argv0 = sys.argv[0:1] |
| 862 | sys.argv = shlex.split(arg) |
| 863 | sys.argv[:0] = argv0 |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 864 | # this is caught in the main debugger loop |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 865 | raise Restart |
| 866 | |
| 867 | do_restart = do_run |
| 868 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 869 | def do_return(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 870 | """r(eturn) |
| 871 | Continue execution until the current function returns. |
| 872 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 873 | self.set_return(self.curframe) |
| 874 | return 1 |
| 875 | do_r = do_return |
| 876 | |
| 877 | def do_continue(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 878 | """c(ont(inue)) |
| 879 | Continue execution, only stop when a breakpoint is encountered. |
| 880 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 881 | self.set_continue() |
| 882 | return 1 |
| 883 | do_c = do_cont = do_continue |
| 884 | |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 885 | def do_jump(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 886 | """j(ump) lineno |
| 887 | Set the next line that will be executed. Only available in |
| 888 | the bottom-most frame. This lets you jump back and execute |
| 889 | code again, or jump forward to skip code that you don't want |
| 890 | to run. |
| 891 | |
| 892 | It should be noted that not all jumps are allowed -- for |
| 893 | instance it is not possible to jump into the middle of a |
| 894 | for loop or out of a finally clause. |
| 895 | """ |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 896 | if self.curindex + 1 != len(self.stack): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 897 | self.error('You can only jump within the bottom frame') |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 898 | return |
| 899 | try: |
| 900 | arg = int(arg) |
| 901 | except ValueError: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 902 | self.error("The 'jump' command requires a line number") |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 903 | else: |
| 904 | try: |
| 905 | # Do the jump, fix up our copy of the stack, and display the |
| 906 | # new position |
| 907 | self.curframe.f_lineno = arg |
| 908 | self.stack[self.curindex] = self.stack[self.curindex][0], arg |
| 909 | self.print_stack_entry(self.stack[self.curindex]) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 910 | except ValueError as e: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 911 | self.error('Jump failed: %s' % e) |
Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 912 | do_j = do_jump |
| 913 | |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 914 | def do_debug(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 915 | """debug code |
| 916 | Enter a recursive debugger that steps through the code |
| 917 | argument (which is an arbitrary expression or statement to be |
| 918 | executed in the current environment). |
| 919 | """ |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 920 | sys.settrace(None) |
| 921 | globals = self.curframe.f_globals |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 922 | locals = self.curframe_locals |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 923 | p = Pdb(self.completekey, self.stdin, self.stdout) |
Guido van Rossum | ed538d8 | 2003-04-09 19:36:34 +0000 | [diff] [blame] | 924 | p.prompt = "(%s) " % self.prompt.strip() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 925 | self.message("ENTERING RECURSIVE DEBUGGER") |
Guido van Rossum | ed538d8 | 2003-04-09 19:36:34 +0000 | [diff] [blame] | 926 | sys.call_tracing(p.run, (arg, globals, locals)) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 927 | self.message("LEAVING RECURSIVE DEBUGGER") |
Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 928 | sys.settrace(self.trace_dispatch) |
| 929 | self.lastcmd = p.lastcmd |
| 930 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 931 | def do_quit(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 932 | """q(uit)\nexit |
| 933 | Quit from the debugger. The program being executed is aborted. |
| 934 | """ |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 935 | self._user_requested_quit = 1 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 936 | self.set_quit() |
| 937 | return 1 |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 938 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 939 | do_q = do_quit |
Guido van Rossum | d1c08f3 | 2002-04-15 00:48:24 +0000 | [diff] [blame] | 940 | do_exit = do_quit |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 941 | |
Guido van Rossum | eef2607 | 2003-01-13 21:13:55 +0000 | [diff] [blame] | 942 | def do_EOF(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 943 | """EOF |
| 944 | Handles the receipt of EOF as a command. |
| 945 | """ |
| 946 | self.message('') |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 947 | self._user_requested_quit = 1 |
Guido van Rossum | eef2607 | 2003-01-13 21:13:55 +0000 | [diff] [blame] | 948 | self.set_quit() |
| 949 | return 1 |
| 950 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 951 | def do_args(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 952 | """a(rgs) |
| 953 | Print the argument list of the current function. |
| 954 | """ |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 955 | co = self.curframe.f_code |
| 956 | dict = self.curframe_locals |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 957 | n = co.co_argcount |
| 958 | if co.co_flags & 4: n = n+1 |
| 959 | if co.co_flags & 8: n = n+1 |
| 960 | for i in range(n): |
| 961 | name = co.co_varnames[i] |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 962 | if name in dict: |
| 963 | self.message('%s = %r' % (name, dict[name])) |
| 964 | else: |
| 965 | self.message('%s = *** undefined ***' % (name,)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 966 | do_a = do_args |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 967 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 968 | def do_retval(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 969 | """retval |
| 970 | Print the return value for the last return of a function. |
| 971 | """ |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 972 | if '__return__' in self.curframe_locals: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 973 | self.message(repr(self.curframe_locals['__return__'])) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 974 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 975 | self.error('Not yet returned!') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 976 | do_rv = do_retval |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 977 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 978 | def _getval(self, arg): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 979 | try: |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 980 | return eval(arg, self.curframe.f_globals, self.curframe_locals) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 981 | except: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 982 | exc_info = sys.exc_info()[:2] |
| 983 | self.error(traceback.format_exception_only(*exc_info)[-1].strip()) |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 984 | raise |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 985 | |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 986 | def do_p(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 987 | """p(rint) expression |
| 988 | Print the value of the expression. |
| 989 | """ |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 990 | try: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 991 | self.message(repr(self._getval(arg))) |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 992 | except: |
| 993 | pass |
Georg Brandl | c987924 | 2007-09-04 07:07:56 +0000 | [diff] [blame] | 994 | # make "print" an alias of "p" since print isn't a Python statement anymore |
| 995 | do_print = do_p |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 996 | |
| 997 | def do_pp(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 998 | """pp expression |
| 999 | Pretty-print the value of the expression. |
| 1000 | """ |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1001 | try: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1002 | self.message(pprint.pformat(self._getval(arg))) |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1003 | except: |
| 1004 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1005 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1006 | def do_list(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1007 | """l(ist) [first [,last] | .] |
| 1008 | List source code for the current file. |
| 1009 | Without arguments, list 11 lines around the current line |
| 1010 | or continue the previous listing. |
| 1011 | With . as argument, list 11 lines around the current line. |
| 1012 | With one argument, list 11 lines starting at that line. |
| 1013 | With two arguments, list the given range; |
| 1014 | if the second argument is less than the first, it is a count. |
| 1015 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1016 | self.lastcmd = 'list' |
| 1017 | last = None |
Georg Brandl | a91a94b | 2010-07-30 07:14:01 +0000 | [diff] [blame] | 1018 | if arg and arg != '.': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1019 | try: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1020 | if ',' in arg: |
| 1021 | first, last = arg.split(',') |
| 1022 | first = int(first.strip()) |
| 1023 | last = int(last.strip()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1024 | if last < first: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1025 | # assume it's a count |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1026 | last = first + last |
| 1027 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1028 | first = int(arg.strip()) |
| 1029 | first = max(1, first - 5) |
| 1030 | except ValueError: |
| 1031 | self.error('Error in argument: %r' % arg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1032 | return |
Georg Brandl | a91a94b | 2010-07-30 07:14:01 +0000 | [diff] [blame] | 1033 | elif self.lineno is None or arg == '.': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1034 | first = max(1, self.curframe.f_lineno - 5) |
| 1035 | else: |
| 1036 | first = self.lineno + 1 |
| 1037 | if last is None: |
| 1038 | last = first + 10 |
| 1039 | filename = self.curframe.f_code.co_filename |
| 1040 | breaklist = self.get_file_breaks(filename) |
| 1041 | try: |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1042 | # XXX add tb_lineno feature |
| 1043 | lines = linecache.getlines(filename, self.curframe.f_globals) |
| 1044 | self._print_lines(lines[first-1:last], first, breaklist, |
| 1045 | self.curframe.f_lineno, -1) |
| 1046 | self.lineno = min(last, len(lines)) |
| 1047 | if len(lines) < last: |
| 1048 | self.message('[EOF]') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1049 | except KeyboardInterrupt: |
| 1050 | pass |
| 1051 | do_l = do_list |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1052 | |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1053 | def do_longlist(self, arg): |
| 1054 | """longlist | ll |
| 1055 | List the whole source code for the current function or frame. |
| 1056 | """ |
| 1057 | filename = self.curframe.f_code.co_filename |
| 1058 | breaklist = self.get_file_breaks(filename) |
| 1059 | try: |
Georg Brandl | 5ed2b5a | 2010-07-30 18:08:12 +0000 | [diff] [blame] | 1060 | lines, lineno = getsourcelines(self.curframe) |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1061 | except IOError as err: |
| 1062 | self.error(err) |
| 1063 | return |
| 1064 | self._print_lines(lines, lineno, breaklist, self.curframe.f_lineno, -1) |
| 1065 | do_ll = do_longlist |
| 1066 | |
| 1067 | def do_source(self, arg): |
| 1068 | """source expression |
| 1069 | Try to get source code for the given object and display it. |
| 1070 | """ |
| 1071 | try: |
| 1072 | obj = self._getval(arg) |
| 1073 | except: |
| 1074 | return |
| 1075 | try: |
Georg Brandl | 5ed2b5a | 2010-07-30 18:08:12 +0000 | [diff] [blame] | 1076 | lines, lineno = getsourcelines(obj) |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1077 | except (IOError, TypeError) as err: |
| 1078 | self.error(err) |
| 1079 | return |
| 1080 | self._print_lines(lines, lineno, [], -1, -1) |
| 1081 | |
| 1082 | def _print_lines(self, lines, start, breaks, current, special): |
| 1083 | """Print a range of lines.""" |
| 1084 | for lineno, line in enumerate(lines, start): |
| 1085 | s = str(lineno).rjust(3) |
| 1086 | if len(s) < 4: |
| 1087 | s += ' ' |
| 1088 | if lineno in breaks: |
| 1089 | s += 'B' |
| 1090 | else: |
| 1091 | s += ' ' |
| 1092 | if lineno == current: |
| 1093 | s += '->' |
| 1094 | elif lineno == special: |
| 1095 | s += '>>' |
| 1096 | self.message(s + '\t' + line.rstrip()) |
| 1097 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1098 | def do_whatis(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1099 | """whatis arg |
| 1100 | Print the type of the argument. |
| 1101 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1102 | try: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1103 | value = self._getval(arg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1104 | except: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1105 | # _getval() already printed the error |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1106 | return |
| 1107 | code = None |
| 1108 | # Is it a function? |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1109 | try: |
| 1110 | code = value.__code__ |
| 1111 | except Exception: |
| 1112 | pass |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1113 | if code: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1114 | self.message('Function %s' % code.co_name) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1115 | return |
| 1116 | # Is it an instance method? |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1117 | try: |
| 1118 | code = value.__func__.__code__ |
| 1119 | except Exception: |
| 1120 | pass |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1121 | if code: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1122 | self.message('Method %s' % code.co_name) |
| 1123 | return |
| 1124 | # Is it a class? |
| 1125 | if value.__class__ is type: |
| 1126 | self.message('Class %s.%s' % (value.__module__, value.__name__)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1127 | return |
| 1128 | # None of the above... |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1129 | self.message(type(value)) |
Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 1130 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1131 | def do_alias(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1132 | """alias [name [command [parameter parameter ...] ]] |
| 1133 | Create an alias called 'name' that executes 'command'. The |
| 1134 | command must *not* be enclosed in quotes. Replaceable |
| 1135 | parameters can be indicated by %1, %2, and so on, while %* is |
| 1136 | replaced by all the parameters. If no command is given, the |
| 1137 | current alias for name is shown. If no name is given, all |
| 1138 | aliases are listed. |
| 1139 | |
| 1140 | Aliases may be nested and can contain anything that can be |
| 1141 | legally typed at the pdb prompt. Note! You *can* override |
| 1142 | internal pdb commands with aliases! Those internal commands |
| 1143 | are then hidden until the alias is removed. Aliasing is |
| 1144 | recursively applied to the first word of the command line; all |
| 1145 | other words in the line are left alone. |
| 1146 | |
| 1147 | As an example, here are two useful aliases (especially when |
| 1148 | placed in the .pdbrc file): |
| 1149 | |
| 1150 | # Print instance variables (usage "pi classInst") |
| 1151 | alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k] |
| 1152 | # Print instance variables in self |
| 1153 | alias ps pi self |
| 1154 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 1155 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1156 | if len(args) == 0: |
Benjamin Peterson | be74a37 | 2009-09-11 21:17:13 +0000 | [diff] [blame] | 1157 | keys = sorted(self.aliases.keys()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1158 | for alias in keys: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1159 | self.message("%s = %s" % (alias, self.aliases[alias])) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1160 | return |
Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 1161 | if args[0] in self.aliases and len(args) == 1: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1162 | self.message("%s = %s" % (args[0], self.aliases[args[0]])) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1163 | else: |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 1164 | self.aliases[args[0]] = ' '.join(args[1:]) |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 1165 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1166 | def do_unalias(self, arg): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1167 | """unalias name |
| 1168 | Delete the specified alias. |
| 1169 | """ |
Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 1170 | args = arg.split() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1171 | if len(args) == 0: return |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1172 | if args[0] in self.aliases: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1173 | del self.aliases[args[0]] |
Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 1174 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1175 | # List of all the commands making the program resume execution. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1176 | commands_resuming = ['do_continue', 'do_step', 'do_next', 'do_return', |
| 1177 | 'do_quit', 'do_jump'] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1178 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1179 | # Print a traceback starting at the top stack frame. |
| 1180 | # The most recently entered frame is printed last; |
| 1181 | # this is different from dbx and gdb, but consistent with |
| 1182 | # the Python interpreter's stack trace. |
| 1183 | # It is also consistent with the up/down commands (which are |
| 1184 | # compatible with dbx and gdb: up moves towards 'main()' |
| 1185 | # and down moves towards the most recent stack frame). |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1186 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1187 | def print_stack_trace(self): |
| 1188 | try: |
| 1189 | for frame_lineno in self.stack: |
| 1190 | self.print_stack_entry(frame_lineno) |
| 1191 | except KeyboardInterrupt: |
| 1192 | pass |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1193 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1194 | def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix): |
| 1195 | frame, lineno = frame_lineno |
| 1196 | if frame is self.curframe: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1197 | prefix = '> ' |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1198 | else: |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1199 | prefix = ' ' |
| 1200 | self.message(prefix + |
| 1201 | self.format_stack_entry(frame_lineno, prompt_prefix)) |
Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1202 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1203 | # Provide help |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 1204 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1205 | def do_help(self, arg): |
| 1206 | """h(elp) |
| 1207 | Without argument, print the list of available commands. |
| 1208 | With a command name as argument, print help about that command. |
| 1209 | "help pdb" shows the full pdb documentation. |
| 1210 | "help exec" gives help on the ! command. |
| 1211 | """ |
| 1212 | if not arg: |
| 1213 | return cmd.Cmd.do_help(self, arg) |
| 1214 | try: |
| 1215 | try: |
| 1216 | topic = getattr(self, 'help_' + arg) |
| 1217 | return topic() |
| 1218 | except AttributeError: |
| 1219 | command = getattr(self, 'do_' + arg) |
| 1220 | except AttributeError: |
| 1221 | self.error('No help for %r' % arg) |
| 1222 | else: |
| 1223 | self.message(command.__doc__.rstrip()) |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 1224 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1225 | do_h = do_help |
Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1226 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1227 | def help_exec(self): |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1228 | """(!) statement |
| 1229 | Execute the (one-line) statement in the context of the current |
| 1230 | stack frame. The exclamation point can be omitted unless the |
| 1231 | first word of the statement resembles a debugger command. To |
| 1232 | assign to a global variable you must always prefix the command |
| 1233 | with a 'global' command, e.g.: |
| 1234 | (Pdb) global list_options; list_options = ['-l'] |
| 1235 | (Pdb) |
| 1236 | """ |
| 1237 | self.message(self.help_exec.__doc__.strip()) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1238 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1239 | def help_pdb(self): |
| 1240 | help() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1241 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1242 | # other helper functions |
| 1243 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1244 | def lookupmodule(self, filename): |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1245 | """Helper function for break/clear parsing -- may be overridden. |
| 1246 | |
| 1247 | lookupmodule() translates (possibly incomplete) file or module name |
| 1248 | into an absolute file name. |
| 1249 | """ |
| 1250 | if os.path.isabs(filename) and os.path.exists(filename): |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1251 | return filename |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1252 | f = os.path.join(sys.path[0], filename) |
| 1253 | if os.path.exists(f) and self.canonic(f) == self.mainpyfile: |
| 1254 | return f |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1255 | root, ext = os.path.splitext(filename) |
| 1256 | if ext == '': |
| 1257 | filename = filename + '.py' |
| 1258 | if os.path.isabs(filename): |
| 1259 | return filename |
| 1260 | for dirname in sys.path: |
| 1261 | while os.path.islink(dirname): |
| 1262 | dirname = os.readlink(dirname) |
| 1263 | fullname = os.path.join(dirname, filename) |
| 1264 | if os.path.exists(fullname): |
| 1265 | return fullname |
| 1266 | return None |
Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 1267 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1268 | def _runscript(self, filename): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1269 | # The script has to run in __main__ namespace (or imports from |
| 1270 | # __main__ will break). |
| 1271 | # |
| 1272 | # So we clear up the __main__ and set several special variables |
| 1273 | # (this gets rid of pdb's globals and cleans old variables on restarts). |
| 1274 | import __main__ |
| 1275 | __main__.__dict__.clear() |
| 1276 | __main__.__dict__.update({"__name__" : "__main__", |
| 1277 | "__file__" : filename, |
| 1278 | "__builtins__": __builtins__, |
| 1279 | }) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1280 | |
| 1281 | # When bdb sets tracing, a number of call and line events happens |
| 1282 | # BEFORE debugger even reaches user's code (and the exact sequence of |
| 1283 | # events depends on python version). So we take special measures to |
| 1284 | # avoid stopping before we reach the main script (see user_line and |
| 1285 | # user_call for details). |
| 1286 | self._wait_for_mainpyfile = 1 |
| 1287 | self.mainpyfile = self.canonic(filename) |
| 1288 | self._user_requested_quit = 0 |
Georg Brandl | d07ac64 | 2009-08-13 07:50:57 +0000 | [diff] [blame] | 1289 | with open(filename, "rb") as fp: |
| 1290 | statement = "exec(compile(%r, %r, 'exec'))" % \ |
| 1291 | (fp.read(), self.mainpyfile) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1292 | self.run(statement) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1293 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1294 | # Collect all command help into docstring |
| 1295 | |
| 1296 | # unfortunately we can't guess this order from the class definition |
| 1297 | _help_order = [ |
| 1298 | 'help', 'where', 'down', 'up', 'break', 'tbreak', 'clear', 'disable', |
| 1299 | 'enable', 'ignore', 'condition', 'commands', 'step', 'next', 'until', |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1300 | 'jump', 'return', 'retval', 'run', 'continue', 'list', 'longlist', |
| 1301 | 'args', 'print', 'pp', 'whatis', 'source', 'alias', 'unalias', |
| 1302 | 'debug', 'quit', |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1303 | ] |
| 1304 | |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 1305 | docs = set() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1306 | for _command in _help_order: |
| 1307 | __doc__ += getattr(Pdb, 'do_' + _command).__doc__.strip() + '\n\n' |
| 1308 | __doc__ += Pdb.help_exec.__doc__ |
| 1309 | |
| 1310 | del _help_order, _command |
| 1311 | |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1312 | # Simplified interface |
| 1313 | |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 1314 | def run(statement, globals=None, locals=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1315 | Pdb().run(statement, globals, locals) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 1316 | |
| 1317 | def runeval(expression, globals=None, locals=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1318 | return Pdb().runeval(expression, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1319 | |
| 1320 | def runctx(statement, globals, locals): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1321 | # B/W compatibility |
| 1322 | run(statement, globals, locals) |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1323 | |
Raymond Hettinger | 2ef7e6c | 2004-10-24 00:32:24 +0000 | [diff] [blame] | 1324 | def runcall(*args, **kwds): |
| 1325 | return Pdb().runcall(*args, **kwds) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 1326 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1327 | def set_trace(): |
Johannes Gijsbers | 84a6c20 | 2004-11-07 11:35:30 +0000 | [diff] [blame] | 1328 | Pdb().set_trace(sys._getframe().f_back) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1329 | |
| 1330 | # Post-Mortem interface |
| 1331 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1332 | def post_mortem(t=None): |
| 1333 | # handling the default |
| 1334 | if t is None: |
| 1335 | # sys.exc_info() returns (type, value, traceback) if an exception is |
| 1336 | # being handled, otherwise it returns None |
| 1337 | t = sys.exc_info()[2] |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 1338 | if t is None: |
| 1339 | raise ValueError("A valid traceback must be passed if no " |
| 1340 | "exception is being handled") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1341 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1342 | p = Pdb() |
| 1343 | p.reset() |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 1344 | p.interaction(None, t) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1345 | |
| 1346 | def pm(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1347 | post_mortem(sys.last_traceback) |
Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1348 | |
| 1349 | |
| 1350 | # Main program for testing |
| 1351 | |
Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 1352 | TESTCMD = 'import x; x.main()' |
Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1353 | |
Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 1354 | def test(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1355 | run(TESTCMD) |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 1356 | |
| 1357 | # print help |
| 1358 | def help(): |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 1359 | import pydoc |
| 1360 | pydoc.pager(__doc__) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1361 | |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1362 | _usage = """\ |
| 1363 | usage: pdb.py [-c command] ... pyfile [arg] ... |
| 1364 | |
| 1365 | Debug the Python program given by pyfile. |
| 1366 | |
| 1367 | Initial commands are read from .pdbrc files in your home directory |
| 1368 | and in the current directory, if they exist. Commands supplied with |
| 1369 | -c are executed after commands from .pdbrc files. |
| 1370 | |
| 1371 | To let the script run until an exception occurs, use "-c continue". |
Georg Brandl | 2dfec55 | 2010-07-30 08:43:32 +0000 | [diff] [blame] | 1372 | To let the script run up to a given line X in the debugged file, use |
| 1373 | "-c 'until X'".""" |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1374 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1375 | def main(): |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1376 | import getopt |
| 1377 | |
| 1378 | opts, args = getopt.getopt(sys.argv[1:], 'hc:', ['--help', '--command=']) |
| 1379 | |
| 1380 | if not args: |
| 1381 | print(_usage) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1382 | sys.exit(2) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1383 | |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1384 | commands = [] |
| 1385 | for opt, optarg in opts: |
| 1386 | if opt in ['-h', '--help']: |
| 1387 | print(_usage) |
| 1388 | sys.exit() |
| 1389 | elif opt in ['-c', '--command']: |
| 1390 | commands.append(optarg) |
| 1391 | |
| 1392 | mainpyfile = args[0] # Get script filename |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1393 | if not os.path.exists(mainpyfile): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1394 | print('Error:', mainpyfile, 'does not exist') |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1395 | sys.exit(1) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1396 | |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1397 | 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] | 1398 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1399 | # Replace pdb's dir with script's dir in front of module search path. |
| 1400 | sys.path[0] = os.path.dirname(mainpyfile) |
Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1401 | |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1402 | # Note on saving/restoring sys.argv: it's a good idea when sys.argv was |
| 1403 | # 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] | 1404 | # changed by the user from the command line. There is a "restart" command |
| 1405 | # which allows explicit specification of command line arguments. |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1406 | pdb = Pdb() |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1407 | pdb.rcLines.extend(commands) |
Georg Brandl | 1e30bd3 | 2010-07-30 07:21:26 +0000 | [diff] [blame] | 1408 | while True: |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1409 | try: |
| 1410 | pdb._runscript(mainpyfile) |
| 1411 | if pdb._user_requested_quit: |
| 1412 | break |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1413 | print("The program finished and will be restarted") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1414 | except Restart: |
| 1415 | print("Restarting", mainpyfile, "with arguments:") |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1416 | print("\t" + " ".join(args)) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1417 | except SystemExit: |
| 1418 | # In most cases SystemExit does not warrant a post-mortem session. |
Georg Brandl | e023091 | 2010-07-30 08:29:39 +0000 | [diff] [blame] | 1419 | print("The program exited via sys.exit(). Exit status:", end=' ') |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1420 | print(sys.exc_info()[1]) |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1421 | except: |
| 1422 | traceback.print_exc() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 1423 | print("Uncaught exception. Entering post mortem debugging") |
| 1424 | print("Running 'cont' or 'step' will restart the program") |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1425 | t = sys.exc_info()[2] |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 1426 | pdb.interaction(None, t) |
Georg Brandl | 3078df0 | 2009-05-05 09:11:31 +0000 | [diff] [blame] | 1427 | print("Post mortem debugger finished. The " + mainpyfile + |
| 1428 | " will be restarted") |
Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1429 | |
| 1430 | |
| 1431 | # When invoked as main program, invoke the debugger on a script |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1432 | if __name__ == '__main__': |
| 1433 | import pdb |
| 1434 | pdb.main() |