Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 1 | # Verify that gdb can pretty-print the various PyObject* types |
| 2 | # |
| 3 | # The code for testing gdb was adapted from similar work in Unladen Swallow's |
| 4 | # Lib/test/test_jit_gdb.py |
| 5 | |
| 6 | import os |
| 7 | import re |
| 8 | import subprocess |
| 9 | import sys |
Vinay Sajip | f1b34ee | 2012-05-06 12:03:05 +0100 | [diff] [blame] | 10 | import sysconfig |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 11 | import unittest |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 12 | import locale |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 13 | |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 14 | # Is this Python configured to support threads? |
| 15 | try: |
| 16 | import _thread |
| 17 | except ImportError: |
| 18 | _thread = None |
| 19 | |
Benjamin Peterson | 65c66ab | 2010-10-29 21:31:35 +0000 | [diff] [blame] | 20 | from test.support import run_unittest, findfile, python_is_optimized |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 21 | |
| 22 | try: |
| 23 | gdb_version, _ = subprocess.Popen(["gdb", "--version"], |
| 24 | stdout=subprocess.PIPE).communicate() |
| 25 | except OSError: |
| 26 | # This is what "no gdb" looks like. There may, however, be other |
| 27 | # errors that manifest this way too. |
| 28 | raise unittest.SkipTest("Couldn't find gdb on the path") |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 29 | gdb_version_number = re.search(b"^GNU gdb [^\d]*(\d+)\.(\d)", gdb_version) |
| 30 | gdb_major_version = int(gdb_version_number.group(1)) |
| 31 | gdb_minor_version = int(gdb_version_number.group(2)) |
| 32 | if gdb_major_version < 7: |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 33 | raise unittest.SkipTest("gdb versions before 7.0 didn't support python embedding" |
Antoine Pitrou | 6a45e9d | 2010-04-11 22:47:34 +0000 | [diff] [blame] | 34 | " Saw:\n" + gdb_version.decode('ascii', 'replace')) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 35 | |
Vinay Sajip | f1b34ee | 2012-05-06 12:03:05 +0100 | [diff] [blame] | 36 | if not sysconfig.is_python_build(): |
| 37 | raise unittest.SkipTest("test_gdb only works on source builds at the moment.") |
| 38 | |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 39 | # Location of custom hooks file in a repository checkout. |
| 40 | checkout_hook_path = os.path.join(os.path.dirname(sys.executable), |
| 41 | 'python-gdb.py') |
| 42 | |
| 43 | def run_gdb(*args, **env_vars): |
| 44 | """Runs gdb in --batch mode with the additional arguments given by *args. |
| 45 | |
| 46 | Returns its (stdout, stderr) decoded from utf-8 using the replace handler. |
| 47 | """ |
| 48 | if env_vars: |
| 49 | env = os.environ.copy() |
| 50 | env.update(env_vars) |
| 51 | else: |
| 52 | env = None |
| 53 | base_cmd = ('gdb', '--batch') |
| 54 | if (gdb_major_version, gdb_minor_version) >= (7, 4): |
| 55 | base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path) |
| 56 | out, err = subprocess.Popen(base_cmd + args, |
| 57 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, |
| 58 | ).communicate() |
| 59 | return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace') |
| 60 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 61 | # Verify that "gdb" was built with the embedded python support enabled: |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 62 | gdbpy_version, _ = run_gdb("--eval-command=python import sys; print sys.version_info") |
| 63 | if not gdbpy_version: |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 64 | raise unittest.SkipTest("gdb not built with embedded python support") |
| 65 | |
R David Murray | f358eaf | 2012-10-27 13:26:14 -0400 | [diff] [blame] | 66 | # Verify that "gdb" can load our custom hooks. In theory this should never fail. |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 67 | cmd = ['--args', sys.executable] |
| 68 | _, gdbpy_errors = run_gdb('--args', sys.executable) |
| 69 | if "auto-loading has been declined" in gdbpy_errors: |
| 70 | msg = "gdb security settings prevent use of custom hooks: " |
| 71 | raise unittest.SkipTest(msg + gdbpy_errors.rstrip()) |
Nick Coghlan | be4e4b5 | 2012-06-17 18:57:20 +1000 | [diff] [blame] | 72 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 73 | def gdb_has_frame_select(): |
| 74 | # Does this build of gdb have gdb.Frame.select ? |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 75 | stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))") |
| 76 | m = re.match(r'.*\[(.*)\].*', stdout) |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 77 | if not m: |
| 78 | raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test") |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 79 | gdb_frame_dir = m.group(1).split(', ') |
| 80 | return "'select'" in gdb_frame_dir |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 81 | |
| 82 | HAS_PYUP_PYDOWN = gdb_has_frame_select() |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 83 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 84 | BREAKPOINT_FN='builtin_id' |
| 85 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 86 | class DebuggerTests(unittest.TestCase): |
| 87 | |
| 88 | """Test that the debugger can debug Python.""" |
| 89 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 90 | def get_stack_trace(self, source=None, script=None, |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 91 | breakpoint=BREAKPOINT_FN, |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 92 | cmds_after_breakpoint=None, |
| 93 | import_site=False): |
| 94 | ''' |
| 95 | Run 'python -c SOURCE' under gdb with a breakpoint. |
| 96 | |
| 97 | Support injecting commands after the breakpoint is reached |
| 98 | |
| 99 | Returns the stdout from gdb |
| 100 | |
| 101 | cmds_after_breakpoint: if provided, a list of strings: gdb commands |
| 102 | ''' |
| 103 | # We use "set breakpoint pending yes" to avoid blocking with a: |
| 104 | # Function "foo" not defined. |
| 105 | # Make breakpoint pending on future shared library load? (y or [n]) |
| 106 | # error, which typically happens python is dynamically linked (the |
| 107 | # breakpoints of interest are to be found in the shared library) |
| 108 | # When this happens, we still get: |
Victor Stinner | 67df3a4 | 2010-04-21 13:53:05 +0000 | [diff] [blame] | 109 | # Function "textiowrapper_write" not defined. |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 110 | # emitted to stderr each time, alas. |
| 111 | |
| 112 | # Initially I had "--eval-command=continue" here, but removed it to |
| 113 | # avoid repeated print breakpoints when traversing hierarchical data |
| 114 | # structures |
| 115 | |
| 116 | # Generate a list of commands in gdb's language: |
| 117 | commands = ['set breakpoint pending yes', |
| 118 | 'break %s' % breakpoint, |
| 119 | 'run'] |
| 120 | if cmds_after_breakpoint: |
| 121 | commands += cmds_after_breakpoint |
| 122 | else: |
| 123 | commands += ['backtrace'] |
| 124 | |
| 125 | # print commands |
| 126 | |
| 127 | # Use "commands" to generate the arguments with which to invoke "gdb": |
| 128 | args = ["gdb", "--batch"] |
| 129 | args += ['--eval-command=%s' % cmd for cmd in commands] |
| 130 | args += ["--args", |
| 131 | sys.executable] |
| 132 | |
| 133 | if not import_site: |
| 134 | # -S suppresses the default 'import site' |
| 135 | args += ["-S"] |
| 136 | |
| 137 | if source: |
| 138 | args += ["-c", source] |
| 139 | elif script: |
| 140 | args += [script] |
| 141 | |
| 142 | # print args |
| 143 | # print ' '.join(args) |
| 144 | |
| 145 | # Use "args" to invoke gdb, capturing stdout, stderr: |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 146 | out, err = run_gdb(*args, PYTHONHASHSEED='0') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 147 | |
| 148 | # Ignore some noise on stderr due to the pending breakpoint: |
| 149 | err = err.replace('Function "%s" not defined.\n' % breakpoint, '') |
Antoine Pitrou | a045f19 | 2010-05-05 18:30:22 +0000 | [diff] [blame] | 150 | # Ignore some other noise on stderr (http://bugs.python.org/issue8600) |
| 151 | err = err.replace("warning: Unable to find libthread_db matching" |
| 152 | " inferior's thread library, thread debugging will" |
| 153 | " not be available.\n", |
| 154 | '') |
Jesus Cea | cee3655 | 2011-03-16 01:33:16 +0100 | [diff] [blame] | 155 | err = err.replace("warning: Cannot initialize thread debugging" |
| 156 | " library: Debugger service failed\n", |
| 157 | '') |
Benjamin Peterson | f8a9a83 | 2012-09-20 23:48:23 -0400 | [diff] [blame] | 158 | err = err.replace('warning: Could not load shared library symbols for ' |
| 159 | 'linux-vdso.so.1.\n' |
| 160 | 'Do you need "set solib-search-path" or ' |
| 161 | '"set sysroot"?\n', |
| 162 | '') |
R David Murray | f933302 | 2012-10-27 13:22:41 -0400 | [diff] [blame] | 163 | err = err.replace('warning: Could not load shared library symbols for ' |
| 164 | 'linux-gate.so.1.\n' |
| 165 | 'Do you need "set solib-search-path" or ' |
| 166 | '"set sysroot"?\n', |
| 167 | '') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 168 | |
| 169 | # Ensure no unexpected error messages: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 170 | self.assertEqual(err, '') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 171 | return out |
| 172 | |
| 173 | def get_gdb_repr(self, source, |
| 174 | cmds_after_breakpoint=None, |
| 175 | import_site=False): |
| 176 | # Given an input python source representation of data, |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 177 | # run "python -c'id(DATA)'" under gdb with a breakpoint on |
| 178 | # builtin_id and scrape out gdb's representation of the "op" |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 179 | # parameter, and verify that the gdb displays the same string |
| 180 | # |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 181 | # Verify that the gdb displays the expected string |
| 182 | # |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 183 | # For a nested structure, the first time we hit the breakpoint will |
| 184 | # give us the top-level structure |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 185 | gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN, |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 186 | cmds_after_breakpoint=cmds_after_breakpoint, |
| 187 | import_site=import_site) |
| 188 | # gdb can insert additional '\n' and space characters in various places |
| 189 | # in its output, depending on the width of the terminal it's connected |
| 190 | # to (using its "wrap_here" function) |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 191 | m = re.match('.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+\S*Python/bltinmodule.c.*', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 192 | gdb_output, re.DOTALL) |
| 193 | if not m: |
| 194 | self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output)) |
| 195 | return m.group(1), gdb_output |
| 196 | |
| 197 | def assertEndsWith(self, actual, exp_end): |
| 198 | '''Ensure that the given "actual" string ends with "exp_end"''' |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 199 | self.assertTrue(actual.endswith(exp_end), |
| 200 | msg='%r did not end with %r' % (actual, exp_end)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 201 | |
| 202 | def assertMultilineMatches(self, actual, pattern): |
| 203 | m = re.match(pattern, actual, re.DOTALL) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 204 | if not m: |
| 205 | self.fail(msg='%r did not match %r' % (actual, pattern)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 206 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 207 | def get_sample_script(self): |
| 208 | return findfile('gdb_sample.py') |
| 209 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 210 | class PrettyPrintTests(DebuggerTests): |
| 211 | def test_getting_backtrace(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 212 | gdb_output = self.get_stack_trace('id(42)') |
| 213 | self.assertTrue(BREAKPOINT_FN in gdb_output) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 214 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 215 | def assertGdbRepr(self, val, exp_repr=None, cmds_after_breakpoint=None): |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 216 | # Ensure that gdb's rendering of the value in a debugged process |
| 217 | # matches repr(value) in this process: |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 218 | gdb_repr, gdb_output = self.get_gdb_repr('id(' + ascii(val) + ')', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 219 | cmds_after_breakpoint) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 220 | if not exp_repr: |
| 221 | exp_repr = repr(val) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 222 | self.assertEqual(gdb_repr, exp_repr, |
| 223 | ('%r did not equal expected %r; full output was:\n%s' |
| 224 | % (gdb_repr, exp_repr, gdb_output))) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 225 | |
| 226 | def test_int(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 227 | 'Verify the pretty-printing of various "int"/long values' |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 228 | self.assertGdbRepr(42) |
| 229 | self.assertGdbRepr(0) |
| 230 | self.assertGdbRepr(-7) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 231 | self.assertGdbRepr(1000000000000) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 232 | self.assertGdbRepr(-1000000000000000) |
| 233 | |
| 234 | def test_singletons(self): |
| 235 | 'Verify the pretty-printing of True, False and None' |
| 236 | self.assertGdbRepr(True) |
| 237 | self.assertGdbRepr(False) |
| 238 | self.assertGdbRepr(None) |
| 239 | |
| 240 | def test_dicts(self): |
| 241 | 'Verify the pretty-printing of dictionaries' |
| 242 | self.assertGdbRepr({}) |
| 243 | self.assertGdbRepr({'foo': 'bar'}) |
Georg Brandl | 09a7c72 | 2012-02-20 21:31:46 +0100 | [diff] [blame] | 244 | self.assertGdbRepr({'foo': 'bar', 'douglas': 42}, |
| 245 | "{'foo': 'bar', 'douglas': 42}") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 246 | |
| 247 | def test_lists(self): |
| 248 | 'Verify the pretty-printing of lists' |
| 249 | self.assertGdbRepr([]) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 250 | self.assertGdbRepr(list(range(5))) |
| 251 | |
| 252 | def test_bytes(self): |
| 253 | 'Verify the pretty-printing of bytes' |
| 254 | self.assertGdbRepr(b'') |
| 255 | self.assertGdbRepr(b'And now for something hopefully the same') |
| 256 | self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text') |
| 257 | self.assertGdbRepr(b'this is a tab:\t' |
| 258 | b' this is a slash-N:\n' |
| 259 | b' this is a slash-R:\r' |
| 260 | ) |
| 261 | |
| 262 | self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80') |
| 263 | |
| 264 | self.assertGdbRepr(bytes([b for b in range(255)])) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 265 | |
| 266 | def test_strings(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 267 | 'Verify the pretty-printing of unicode strings' |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 268 | encoding = locale.getpreferredencoding() |
| 269 | def check_repr(text): |
| 270 | try: |
| 271 | text.encode(encoding) |
| 272 | printable = True |
| 273 | except UnicodeEncodeError: |
Antoine Pitrou | 4c7c421 | 2010-09-09 20:40:28 +0000 | [diff] [blame] | 274 | self.assertGdbRepr(text, ascii(text)) |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 275 | else: |
| 276 | self.assertGdbRepr(text) |
| 277 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 278 | self.assertGdbRepr('') |
| 279 | self.assertGdbRepr('And now for something hopefully the same') |
| 280 | self.assertGdbRepr('string with embedded NUL here \0 and then some more text') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 281 | |
| 282 | # Test printing a single character: |
| 283 | # U+2620 SKULL AND CROSSBONES |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 284 | check_repr('\u2620') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 285 | |
| 286 | # Test printing a Japanese unicode string |
| 287 | # (I believe this reads "mojibake", using 3 characters from the CJK |
| 288 | # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE) |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 289 | check_repr('\u6587\u5b57\u5316\u3051') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 290 | |
| 291 | # Test a character outside the BMP: |
| 292 | # U+1D121 MUSICAL SYMBOL C CLEF |
| 293 | # This is: |
| 294 | # UTF-8: 0xF0 0x9D 0x84 0xA1 |
| 295 | # UTF-16: 0xD834 0xDD21 |
Victor Stinner | 150016f | 2010-05-19 23:04:56 +0000 | [diff] [blame] | 296 | check_repr(chr(0x1D121)) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 297 | |
| 298 | def test_tuples(self): |
| 299 | 'Verify the pretty-printing of tuples' |
| 300 | self.assertGdbRepr(tuple()) |
| 301 | self.assertGdbRepr((1,), '(1,)') |
| 302 | self.assertGdbRepr(('foo', 'bar', 'baz')) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 303 | |
| 304 | def test_sets(self): |
| 305 | 'Verify the pretty-printing of sets' |
| 306 | self.assertGdbRepr(set()) |
Georg Brandl | 09a7c72 | 2012-02-20 21:31:46 +0100 | [diff] [blame] | 307 | self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}") |
| 308 | self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 309 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 310 | # Ensure that we handle sets containing the "dummy" key value, |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 311 | # which happens on deletion: |
| 312 | gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b']) |
| 313 | s.pop() |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 314 | id(s)''') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 315 | self.assertEqual(gdb_repr, "{'b'}") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 316 | |
| 317 | def test_frozensets(self): |
| 318 | 'Verify the pretty-printing of frozensets' |
| 319 | self.assertGdbRepr(frozenset()) |
Georg Brandl | 09a7c72 | 2012-02-20 21:31:46 +0100 | [diff] [blame] | 320 | self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})") |
| 321 | self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 322 | |
| 323 | def test_exceptions(self): |
| 324 | # Test a RuntimeError |
| 325 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 326 | try: |
| 327 | raise RuntimeError("I am an error") |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 328 | except RuntimeError as e: |
| 329 | id(e) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 330 | ''') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 331 | self.assertEqual(gdb_repr, |
| 332 | "RuntimeError('I am an error',)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 333 | |
| 334 | |
| 335 | # Test division by zero: |
| 336 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 337 | try: |
| 338 | a = 1 / 0 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 339 | except ZeroDivisionError as e: |
| 340 | id(e) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 341 | ''') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 342 | self.assertEqual(gdb_repr, |
| 343 | "ZeroDivisionError('division by zero',)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 344 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 345 | def test_modern_class(self): |
| 346 | 'Verify the pretty-printing of new-style class instances' |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 347 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 348 | class Foo: |
| 349 | pass |
| 350 | foo = Foo() |
| 351 | foo.an_int = 42 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 352 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 353 | m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 354 | self.assertTrue(m, |
| 355 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 356 | |
| 357 | def test_subclassing_list(self): |
| 358 | 'Verify the pretty-printing of an instance of a list subclass' |
| 359 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 360 | class Foo(list): |
| 361 | pass |
| 362 | foo = Foo() |
| 363 | foo += [1, 2, 3] |
| 364 | foo.an_int = 42 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 365 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 366 | m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 367 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 368 | self.assertTrue(m, |
| 369 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 370 | |
| 371 | def test_subclassing_tuple(self): |
| 372 | 'Verify the pretty-printing of an instance of a tuple subclass' |
| 373 | # This should exercise the negative tp_dictoffset code in the |
| 374 | # new-style class support |
| 375 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 376 | class Foo(tuple): |
| 377 | pass |
| 378 | foo = Foo((1, 2, 3)) |
| 379 | foo.an_int = 42 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 380 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 381 | m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 382 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 383 | self.assertTrue(m, |
| 384 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 385 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 386 | def assertSane(self, source, corruption, exprepr=None): |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 387 | '''Run Python under gdb, corrupting variables in the inferior process |
| 388 | immediately before taking a backtrace. |
| 389 | |
| 390 | Verify that the variable's representation is the expected failsafe |
| 391 | representation''' |
| 392 | if corruption: |
| 393 | cmds_after_breakpoint=[corruption, 'backtrace'] |
| 394 | else: |
| 395 | cmds_after_breakpoint=['backtrace'] |
| 396 | |
| 397 | gdb_repr, gdb_output = \ |
| 398 | self.get_gdb_repr(source, |
| 399 | cmds_after_breakpoint=cmds_after_breakpoint) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 400 | if exprepr: |
| 401 | if gdb_repr == exprepr: |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 402 | # gdb managed to print the value in spite of the corruption; |
| 403 | # this is good (see http://bugs.python.org/issue8330) |
| 404 | return |
| 405 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 406 | # Match anything for the type name; 0xDEADBEEF could point to |
| 407 | # something arbitrary (see http://bugs.python.org/issue8330) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 408 | pattern = '<.* at remote 0x-?[0-9a-f]+>' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 409 | |
| 410 | m = re.match(pattern, gdb_repr) |
| 411 | if not m: |
| 412 | self.fail('Unexpected gdb representation: %r\n%s' % \ |
| 413 | (gdb_repr, gdb_output)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 414 | |
| 415 | def test_NULL_ptr(self): |
| 416 | 'Ensure that a NULL PyObject* is handled gracefully' |
| 417 | gdb_repr, gdb_output = ( |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 418 | self.get_gdb_repr('id(42)', |
| 419 | cmds_after_breakpoint=['set variable v=0', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 420 | 'backtrace']) |
| 421 | ) |
| 422 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 423 | self.assertEqual(gdb_repr, '0x0') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 424 | |
| 425 | def test_NULL_ob_type(self): |
| 426 | 'Ensure that a PyObject* with NULL ob_type is handled gracefully' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 427 | self.assertSane('id(42)', |
| 428 | 'set v->ob_type=0') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 429 | |
| 430 | def test_corrupt_ob_type(self): |
| 431 | 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 432 | self.assertSane('id(42)', |
| 433 | 'set v->ob_type=0xDEADBEEF', |
| 434 | exprepr='42') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 435 | |
| 436 | def test_corrupt_tp_flags(self): |
| 437 | 'Ensure that a PyObject* with a type with corrupt tp_flags is handled' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 438 | self.assertSane('id(42)', |
| 439 | 'set v->ob_type->tp_flags=0x0', |
| 440 | exprepr='42') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 441 | |
| 442 | def test_corrupt_tp_name(self): |
| 443 | 'Ensure that a PyObject* with a type with corrupt tp_name is handled' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 444 | self.assertSane('id(42)', |
| 445 | 'set v->ob_type->tp_name=0xDEADBEEF', |
| 446 | exprepr='42') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 447 | |
| 448 | def test_builtins_help(self): |
| 449 | 'Ensure that the new-style class _Helper in site.py can be handled' |
| 450 | # (this was the issue causing tracebacks in |
| 451 | # http://bugs.python.org/issue8032#msg100537 ) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 452 | gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 453 | |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 454 | m = re.match(r'<_Helper at remote 0x-?[0-9a-f]+>', gdb_repr) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 455 | self.assertTrue(m, |
| 456 | msg='Unexpected rendering %r' % gdb_repr) |
| 457 | |
| 458 | def test_selfreferential_list(self): |
| 459 | '''Ensure that a reference loop involving a list doesn't lead proxyval |
| 460 | into an infinite loop:''' |
| 461 | gdb_repr, gdb_output = \ |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 462 | self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 463 | self.assertEqual(gdb_repr, '[3, 4, 5, [...]]') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 464 | |
| 465 | gdb_repr, gdb_output = \ |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 466 | self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 467 | self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 468 | |
| 469 | def test_selfreferential_dict(self): |
| 470 | '''Ensure that a reference loop involving a dict doesn't lead proxyval |
| 471 | into an infinite loop:''' |
| 472 | gdb_repr, gdb_output = \ |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 473 | self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 474 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 475 | self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 476 | |
| 477 | def test_selfreferential_old_style_instance(self): |
| 478 | gdb_repr, gdb_output = \ |
| 479 | self.get_gdb_repr(''' |
| 480 | class Foo: |
| 481 | pass |
| 482 | foo = Foo() |
| 483 | foo.an_attr = foo |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 484 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 485 | self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 486 | gdb_repr), |
| 487 | 'Unexpected gdb representation: %r\n%s' % \ |
| 488 | (gdb_repr, gdb_output)) |
| 489 | |
| 490 | def test_selfreferential_new_style_instance(self): |
| 491 | gdb_repr, gdb_output = \ |
| 492 | self.get_gdb_repr(''' |
| 493 | class Foo(object): |
| 494 | pass |
| 495 | foo = Foo() |
| 496 | foo.an_attr = foo |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 497 | id(foo)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 498 | self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 499 | gdb_repr), |
| 500 | 'Unexpected gdb representation: %r\n%s' % \ |
| 501 | (gdb_repr, gdb_output)) |
| 502 | |
| 503 | gdb_repr, gdb_output = \ |
| 504 | self.get_gdb_repr(''' |
| 505 | class Foo(object): |
| 506 | pass |
| 507 | a = Foo() |
| 508 | b = Foo() |
| 509 | a.an_attr = b |
| 510 | b.an_attr = a |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 511 | id(a)''') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 512 | self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>\) at remote 0x-?[0-9a-f]+>', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 513 | gdb_repr), |
| 514 | 'Unexpected gdb representation: %r\n%s' % \ |
| 515 | (gdb_repr, gdb_output)) |
| 516 | |
| 517 | def test_truncation(self): |
| 518 | 'Verify that very long output is truncated' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 519 | gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 520 | self.assertEqual(gdb_repr, |
| 521 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, " |
| 522 | "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, " |
| 523 | "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, " |
| 524 | "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, " |
| 525 | "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, " |
| 526 | "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, " |
| 527 | "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, " |
| 528 | "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, " |
| 529 | "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, " |
| 530 | "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, " |
| 531 | "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, " |
| 532 | "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, " |
| 533 | "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, " |
| 534 | "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, " |
| 535 | "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, " |
| 536 | "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, " |
| 537 | "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, " |
| 538 | "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, " |
| 539 | "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, " |
| 540 | "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, " |
| 541 | "224, 225, 226...(truncated)") |
| 542 | self.assertEqual(len(gdb_repr), |
| 543 | 1024 + len('...(truncated)')) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 544 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 545 | def test_builtin_method(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 546 | gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)') |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 547 | self.assertTrue(re.match('<built-in method readlines of _io.TextIOWrapper object at remote 0x-?[0-9a-f]+>', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 548 | gdb_repr), |
| 549 | 'Unexpected gdb representation: %r\n%s' % \ |
| 550 | (gdb_repr, gdb_output)) |
| 551 | |
| 552 | def test_frames(self): |
| 553 | gdb_output = self.get_stack_trace(''' |
| 554 | def foo(a, b, c): |
| 555 | pass |
| 556 | |
| 557 | foo(3, 4, 5) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 558 | id(foo.__code__)''', |
| 559 | breakpoint='builtin_id', |
| 560 | cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)'] |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 561 | ) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 562 | self.assertTrue(re.match('.*\s+\$1 =\s+Frame 0x-?[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 563 | gdb_output, |
| 564 | re.DOTALL), |
| 565 | 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output)) |
| 566 | |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 567 | @unittest.skipIf(python_is_optimized(), |
| 568 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 569 | class PyListTests(DebuggerTests): |
| 570 | def assertListing(self, expected, actual): |
| 571 | self.assertEndsWith(actual, expected) |
| 572 | |
| 573 | def test_basic_command(self): |
| 574 | 'Verify that the "py-list" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 575 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 576 | cmds_after_breakpoint=['py-list']) |
| 577 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 578 | self.assertListing(' 5 \n' |
| 579 | ' 6 def bar(a, b, c):\n' |
| 580 | ' 7 baz(a, b, c)\n' |
| 581 | ' 8 \n' |
| 582 | ' 9 def baz(*args):\n' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 583 | ' >10 id(42)\n' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 584 | ' 11 \n' |
| 585 | ' 12 foo(1, 2, 3)\n', |
| 586 | bt) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 587 | |
| 588 | def test_one_abs_arg(self): |
| 589 | 'Verify the "py-list" command with one absolute argument' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 590 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 591 | cmds_after_breakpoint=['py-list 9']) |
| 592 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 593 | self.assertListing(' 9 def baz(*args):\n' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 594 | ' >10 id(42)\n' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 595 | ' 11 \n' |
| 596 | ' 12 foo(1, 2, 3)\n', |
| 597 | bt) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 598 | |
| 599 | def test_two_abs_args(self): |
| 600 | 'Verify the "py-list" command with two absolute arguments' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 601 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 602 | cmds_after_breakpoint=['py-list 1,3']) |
| 603 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 604 | self.assertListing(' 1 # Sample script for use by test_gdb.py\n' |
| 605 | ' 2 \n' |
| 606 | ' 3 def foo(a, b, c):\n', |
| 607 | bt) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 608 | |
| 609 | class StackNavigationTests(DebuggerTests): |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 610 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 611 | @unittest.skipIf(python_is_optimized(), |
| 612 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 613 | def test_pyup_command(self): |
| 614 | 'Verify that the "py-up" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 615 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 616 | cmds_after_breakpoint=['py-up']) |
| 617 | self.assertMultilineMatches(bt, |
| 618 | r'''^.* |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 619 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 620 | baz\(a, b, c\) |
| 621 | $''') |
| 622 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 623 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 624 | def test_down_at_bottom(self): |
| 625 | 'Verify handling of "py-down" at the bottom of the stack' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 626 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 627 | cmds_after_breakpoint=['py-down']) |
| 628 | self.assertEndsWith(bt, |
| 629 | 'Unable to find a newer python frame\n') |
| 630 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 631 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 632 | def test_up_at_top(self): |
| 633 | 'Verify handling of "py-up" at the top of the stack' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 634 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 635 | cmds_after_breakpoint=['py-up'] * 4) |
| 636 | self.assertEndsWith(bt, |
| 637 | 'Unable to find an older python frame\n') |
| 638 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 639 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 640 | @unittest.skipIf(python_is_optimized(), |
| 641 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 642 | def test_up_then_down(self): |
| 643 | 'Verify "py-up" followed by "py-down"' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 644 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 645 | cmds_after_breakpoint=['py-up', 'py-down']) |
| 646 | self.assertMultilineMatches(bt, |
| 647 | r'''^.* |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 648 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 649 | baz\(a, b, c\) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 650 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame] | 651 | id\(42\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 652 | $''') |
| 653 | |
| 654 | class PyBtTests(DebuggerTests): |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 655 | @unittest.skipIf(python_is_optimized(), |
| 656 | "Python was compiled with optimizations") |
Victor Stinner | e670c88 | 2011-05-13 17:40:15 +0200 | [diff] [blame] | 657 | def test_bt(self): |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 658 | 'Verify that the "py-bt" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 659 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 660 | cmds_after_breakpoint=['py-bt']) |
| 661 | self.assertMultilineMatches(bt, |
| 662 | r'''^.* |
Victor Stinner | e670c88 | 2011-05-13 17:40:15 +0200 | [diff] [blame] | 663 | Traceback \(most recent call first\): |
| 664 | File ".*gdb_sample.py", line 10, in baz |
| 665 | id\(42\) |
| 666 | File ".*gdb_sample.py", line 7, in bar |
| 667 | baz\(a, b, c\) |
| 668 | File ".*gdb_sample.py", line 4, in foo |
| 669 | bar\(a, b, c\) |
| 670 | File ".*gdb_sample.py", line 12, in <module> |
| 671 | foo\(1, 2, 3\) |
| 672 | ''') |
| 673 | |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 674 | @unittest.skipIf(python_is_optimized(), |
| 675 | "Python was compiled with optimizations") |
Victor Stinner | e670c88 | 2011-05-13 17:40:15 +0200 | [diff] [blame] | 676 | def test_bt_full(self): |
| 677 | 'Verify that the "py-bt-full" command works' |
| 678 | bt = self.get_stack_trace(script=self.get_sample_script(), |
| 679 | cmds_after_breakpoint=['py-bt-full']) |
| 680 | self.assertMultilineMatches(bt, |
| 681 | r'''^.* |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 682 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 683 | baz\(a, b, c\) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 684 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 685 | bar\(a, b, c\) |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 686 | #[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\) |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 687 | foo\(1, 2, 3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 688 | ''') |
| 689 | |
David Malcolm | 8d37ffa | 2012-06-27 14:15:34 -0400 | [diff] [blame] | 690 | @unittest.skipUnless(_thread, |
| 691 | "Python was compiled without thread support") |
| 692 | def test_threads(self): |
| 693 | 'Verify that "py-bt" indicates threads that are waiting for the GIL' |
| 694 | cmd = ''' |
| 695 | from threading import Thread |
| 696 | |
| 697 | class TestThread(Thread): |
| 698 | # These threads would run forever, but we'll interrupt things with the |
| 699 | # debugger |
| 700 | def run(self): |
| 701 | i = 0 |
| 702 | while 1: |
| 703 | i += 1 |
| 704 | |
| 705 | t = {} |
| 706 | for i in range(4): |
| 707 | t[i] = TestThread() |
| 708 | t[i].start() |
| 709 | |
| 710 | # Trigger a breakpoint on the main thread |
| 711 | id(42) |
| 712 | |
| 713 | ''' |
| 714 | # Verify with "py-bt": |
| 715 | gdb_output = self.get_stack_trace(cmd, |
| 716 | cmds_after_breakpoint=['thread apply all py-bt']) |
| 717 | self.assertIn('Waiting for the GIL', gdb_output) |
| 718 | |
| 719 | # Verify with "py-bt-full": |
| 720 | gdb_output = self.get_stack_trace(cmd, |
| 721 | cmds_after_breakpoint=['thread apply all py-bt-full']) |
| 722 | self.assertIn('Waiting for the GIL', gdb_output) |
| 723 | |
| 724 | @unittest.skipIf(python_is_optimized(), |
| 725 | "Python was compiled with optimizations") |
| 726 | # Some older versions of gdb will fail with |
| 727 | # "Cannot find new threads: generic error" |
| 728 | # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround |
| 729 | @unittest.skipUnless(_thread, |
| 730 | "Python was compiled without thread support") |
| 731 | def test_gc(self): |
| 732 | 'Verify that "py-bt" indicates if a thread is garbage-collecting' |
| 733 | cmd = ('from gc import collect\n' |
| 734 | 'id(42)\n' |
| 735 | 'def foo():\n' |
| 736 | ' collect()\n' |
| 737 | 'def bar():\n' |
| 738 | ' foo()\n' |
| 739 | 'bar()\n') |
| 740 | # Verify with "py-bt": |
| 741 | gdb_output = self.get_stack_trace(cmd, |
| 742 | cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'], |
| 743 | ) |
| 744 | self.assertIn('Garbage-collecting', gdb_output) |
| 745 | |
| 746 | # Verify with "py-bt-full": |
| 747 | gdb_output = self.get_stack_trace(cmd, |
| 748 | cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'], |
| 749 | ) |
| 750 | self.assertIn('Garbage-collecting', gdb_output) |
| 751 | |
| 752 | @unittest.skipIf(python_is_optimized(), |
| 753 | "Python was compiled with optimizations") |
| 754 | # Some older versions of gdb will fail with |
| 755 | # "Cannot find new threads: generic error" |
| 756 | # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround |
| 757 | @unittest.skipUnless(_thread, |
| 758 | "Python was compiled without thread support") |
| 759 | def test_pycfunction(self): |
| 760 | 'Verify that "py-bt" displays invocations of PyCFunction instances' |
| 761 | cmd = ('from time import sleep\n' |
| 762 | 'def foo():\n' |
| 763 | ' sleep(1)\n' |
| 764 | 'def bar():\n' |
| 765 | ' foo()\n' |
| 766 | 'bar()\n') |
| 767 | # Verify with "py-bt": |
| 768 | gdb_output = self.get_stack_trace(cmd, |
| 769 | breakpoint='time_sleep', |
| 770 | cmds_after_breakpoint=['bt', 'py-bt'], |
| 771 | ) |
| 772 | self.assertIn('<built-in method sleep', gdb_output) |
| 773 | |
| 774 | # Verify with "py-bt-full": |
| 775 | gdb_output = self.get_stack_trace(cmd, |
| 776 | breakpoint='time_sleep', |
| 777 | cmds_after_breakpoint=['py-bt-full'], |
| 778 | ) |
| 779 | self.assertIn('#0 <built-in method sleep', gdb_output) |
| 780 | |
| 781 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 782 | class PyPrintTests(DebuggerTests): |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 783 | @unittest.skipIf(python_is_optimized(), |
| 784 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 785 | def test_basic_command(self): |
| 786 | 'Verify that the "py-print" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 787 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 788 | cmds_after_breakpoint=['py-print args']) |
| 789 | self.assertMultilineMatches(bt, |
| 790 | r".*\nlocal 'args' = \(1, 2, 3\)\n.*") |
| 791 | |
Vinay Sajip | 2549f87 | 2012-01-04 12:07:30 +0000 | [diff] [blame] | 792 | @unittest.skipIf(python_is_optimized(), |
| 793 | "Python was compiled with optimizations") |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 794 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 795 | def test_print_after_up(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 796 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 797 | cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a']) |
| 798 | self.assertMultilineMatches(bt, |
| 799 | r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*") |
| 800 | |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 801 | @unittest.skipIf(python_is_optimized(), |
| 802 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 803 | def test_printing_global(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 804 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 805 | cmds_after_breakpoint=['py-print __name__']) |
| 806 | self.assertMultilineMatches(bt, |
| 807 | r".*\nglobal '__name__' = '__main__'\n.*") |
| 808 | |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 809 | @unittest.skipIf(python_is_optimized(), |
| 810 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 811 | def test_printing_builtin(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 812 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 813 | cmds_after_breakpoint=['py-print len']) |
| 814 | self.assertMultilineMatches(bt, |
Antoine Pitrou | 4d09873 | 2011-11-26 01:42:03 +0100 | [diff] [blame] | 815 | r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 816 | |
| 817 | class PyLocalsTests(DebuggerTests): |
Victor Stinner | d208416 | 2011-12-19 13:42:24 +0100 | [diff] [blame] | 818 | @unittest.skipIf(python_is_optimized(), |
| 819 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 820 | def test_basic_command(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 821 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 822 | cmds_after_breakpoint=['py-locals']) |
| 823 | self.assertMultilineMatches(bt, |
| 824 | r".*\nargs = \(1, 2, 3\)\n.*") |
| 825 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 826 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Vinay Sajip | 2549f87 | 2012-01-04 12:07:30 +0000 | [diff] [blame] | 827 | @unittest.skipIf(python_is_optimized(), |
| 828 | "Python was compiled with optimizations") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 829 | def test_locals_after_up(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 830 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 831 | cmds_after_breakpoint=['py-up', 'py-locals']) |
| 832 | self.assertMultilineMatches(bt, |
| 833 | r".*\na = 1\nb = 2\nc = 3\n.*") |
| 834 | |
| 835 | def test_main(): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 836 | run_unittest(PrettyPrintTests, |
| 837 | PyListTests, |
| 838 | StackNavigationTests, |
| 839 | PyBtTests, |
| 840 | PyPrintTests, |
| 841 | PyLocalsTests |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 842 | ) |
| 843 | |
| 844 | if __name__ == "__main__": |
| 845 | test_main() |