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