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