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