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