Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +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 |
| 10 | import unittest |
| 11 | |
| 12 | from test.test_support import run_unittest |
| 13 | |
| 14 | try: |
| 15 | gdb_version, _ = subprocess.Popen(["gdb", "--version"], |
| 16 | stdout=subprocess.PIPE).communicate() |
| 17 | except OSError: |
| 18 | # This is what "no gdb" looks like. There may, however, be other |
| 19 | # errors that manifest this way too. |
| 20 | raise unittest.SkipTest("Couldn't find gdb on the path") |
| 21 | gdb_version_number = re.search(r"^GNU gdb [^\d]*(\d+)\.", gdb_version) |
| 22 | if int(gdb_version_number.group(1)) < 7: |
| 23 | raise unittest.SkipTest("gdb versions before 7.0 didn't support python embedding" |
| 24 | " Saw:\n" + gdb_version) |
| 25 | |
| 26 | # Verify that "gdb" was built with the embedded python support enabled: |
| 27 | cmd = "--eval-command=python import sys; print sys.version_info" |
| 28 | p = subprocess.Popen(["gdb", "--batch", cmd], |
| 29 | stdout=subprocess.PIPE) |
| 30 | gdbpy_version, _ = p.communicate() |
| 31 | if gdbpy_version == '': |
| 32 | raise unittest.SkipTest("gdb not built with embedded python support") |
| 33 | |
| 34 | |
| 35 | class DebuggerTests(unittest.TestCase): |
| 36 | |
| 37 | """Test that the debugger can debug Python.""" |
| 38 | |
| 39 | def run_gdb(self, *args): |
| 40 | """Runs gdb with the command line given by *args. |
| 41 | |
| 42 | Returns its stdout, stderr |
| 43 | """ |
| 44 | out, err = subprocess.Popen( |
| 45 | args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 46 | ).communicate() |
| 47 | return out, err |
| 48 | |
| 49 | def get_stack_trace(self, source=None, script=None, |
| 50 | breakpoint='PyObject_Print', |
| 51 | cmds_after_breakpoint=None, |
| 52 | import_site=False): |
| 53 | ''' |
| 54 | Run 'python -c SOURCE' under gdb with a breakpoint. |
| 55 | |
| 56 | Support injecting commands after the breakpoint is reached |
| 57 | |
| 58 | Returns the stdout from gdb |
| 59 | |
| 60 | cmds_after_breakpoint: if provided, a list of strings: gdb commands |
| 61 | ''' |
| 62 | # We use "set breakpoint pending yes" to avoid blocking with a: |
| 63 | # Function "foo" not defined. |
| 64 | # Make breakpoint pending on future shared library load? (y or [n]) |
| 65 | # error, which typically happens python is dynamically linked (the |
| 66 | # breakpoints of interest are to be found in the shared library) |
| 67 | # When this happens, we still get: |
| 68 | # Function "PyObject_Print" not defined. |
| 69 | # emitted to stderr each time, alas. |
| 70 | |
| 71 | # Initially I had "--eval-command=continue" here, but removed it to |
| 72 | # avoid repeated print breakpoints when traversing hierarchical data |
| 73 | # structures |
| 74 | |
| 75 | # Generate a list of commands in gdb's language: |
| 76 | commands = ['set breakpoint pending yes', |
| 77 | 'break %s' % breakpoint, |
| 78 | 'run'] |
| 79 | if cmds_after_breakpoint: |
| 80 | commands += cmds_after_breakpoint |
| 81 | else: |
| 82 | commands += ['backtrace'] |
| 83 | |
| 84 | # print commands |
| 85 | |
| 86 | # Use "commands" to generate the arguments with which to invoke "gdb": |
| 87 | args = ["gdb", "--batch"] |
| 88 | args += ['--eval-command=%s' % cmd for cmd in commands] |
| 89 | args += ["--args", |
| 90 | sys.executable] |
| 91 | |
| 92 | if not import_site: |
| 93 | # -S suppresses the default 'import site' |
| 94 | args += ["-S"] |
| 95 | |
| 96 | if source: |
| 97 | args += ["-c", source] |
| 98 | elif script: |
| 99 | args += [script] |
| 100 | |
| 101 | # print args |
| 102 | # print ' '.join(args) |
| 103 | |
| 104 | # Use "args" to invoke gdb, capturing stdout, stderr: |
| 105 | out, err = self.run_gdb(*args) |
| 106 | |
| 107 | # Ignore some noise on stderr due to the pending breakpoint: |
| 108 | err = err.replace('Function "%s" not defined.\n' % breakpoint, '') |
| 109 | |
| 110 | # Ensure no unexpected error messages: |
| 111 | self.assertEquals(err, '') |
| 112 | |
| 113 | return out |
| 114 | |
| 115 | def get_gdb_repr(self, source, |
| 116 | cmds_after_breakpoint=None, |
| 117 | import_site=False): |
| 118 | # Given an input python source representation of data, |
| 119 | # run "python -c'print DATA'" under gdb with a breakpoint on |
| 120 | # PyObject_Print and scrape out gdb's representation of the "op" |
| 121 | # parameter, and verify that the gdb displays the same string |
| 122 | # |
| 123 | # For a nested structure, the first time we hit the breakpoint will |
| 124 | # give us the top-level structure |
| 125 | gdb_output = self.get_stack_trace(source, breakpoint='PyObject_Print', |
| 126 | cmds_after_breakpoint=cmds_after_breakpoint, |
| 127 | import_site=import_site) |
R. David Murray | 0c08009 | 2010-04-05 16:28:49 +0000 | [diff] [blame] | 128 | # gdb can insert additional '\n' and space characters in various places |
| 129 | # in its output, depending on the width of the terminal it's connected |
| 130 | # to (using its "wrap_here" function) |
| 131 | m = re.match('.*#0\s+PyObject_Print\s+\(\s*op\=\s*(.*?),\s+fp=.*\).*', |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 132 | gdb_output, re.DOTALL) |
R. David Murray | 0c08009 | 2010-04-05 16:28:49 +0000 | [diff] [blame] | 133 | if not m: |
| 134 | self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output)) |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 135 | return m.group(1), gdb_output |
| 136 | |
| 137 | def assertEndsWith(self, actual, exp_end): |
| 138 | '''Ensure that the given "actual" string ends with "exp_end"''' |
| 139 | self.assert_(actual.endswith(exp_end), |
| 140 | msg='%r did not end with %r' % (actual, exp_end)) |
| 141 | |
| 142 | def assertMultilineMatches(self, actual, pattern): |
| 143 | m = re.match(pattern, actual, re.DOTALL) |
| 144 | self.assert_(m, |
| 145 | msg='%r did not match %r' % (actual, pattern)) |
| 146 | |
| 147 | class PrettyPrintTests(DebuggerTests): |
| 148 | def test_getting_backtrace(self): |
| 149 | gdb_output = self.get_stack_trace('print 42') |
| 150 | self.assertTrue('PyObject_Print' in gdb_output) |
| 151 | |
| 152 | def assertGdbRepr(self, val, cmds_after_breakpoint=None): |
| 153 | # Ensure that gdb's rendering of the value in a debugged process |
| 154 | # matches repr(value) in this process: |
| 155 | gdb_repr, gdb_output = self.get_gdb_repr('print ' + repr(val), |
| 156 | cmds_after_breakpoint) |
| 157 | self.assertEquals(gdb_repr, repr(val), gdb_output) |
| 158 | |
| 159 | def test_int(self): |
| 160 | 'Verify the pretty-printing of various "int" values' |
| 161 | self.assertGdbRepr(42) |
| 162 | self.assertGdbRepr(0) |
| 163 | self.assertGdbRepr(-7) |
| 164 | self.assertGdbRepr(sys.maxint) |
| 165 | self.assertGdbRepr(-sys.maxint) |
| 166 | |
| 167 | def test_long(self): |
| 168 | 'Verify the pretty-printing of various "long" values' |
| 169 | self.assertGdbRepr(0L) |
| 170 | self.assertGdbRepr(1000000000000L) |
| 171 | self.assertGdbRepr(-1L) |
| 172 | self.assertGdbRepr(-1000000000000000L) |
| 173 | |
| 174 | def test_singletons(self): |
| 175 | 'Verify the pretty-printing of True, False and None' |
| 176 | self.assertGdbRepr(True) |
| 177 | self.assertGdbRepr(False) |
| 178 | self.assertGdbRepr(None) |
| 179 | |
| 180 | def test_dicts(self): |
| 181 | 'Verify the pretty-printing of dictionaries' |
| 182 | self.assertGdbRepr({}) |
| 183 | self.assertGdbRepr({'foo': 'bar'}) |
| 184 | self.assertGdbRepr({'foo': 'bar', 'douglas':42}) |
| 185 | |
| 186 | def test_lists(self): |
| 187 | 'Verify the pretty-printing of lists' |
| 188 | self.assertGdbRepr([]) |
| 189 | self.assertGdbRepr(range(5)) |
| 190 | |
| 191 | def test_strings(self): |
| 192 | 'Verify the pretty-printing of strings' |
| 193 | self.assertGdbRepr('') |
| 194 | self.assertGdbRepr('And now for something hopefully the same') |
| 195 | self.assertGdbRepr('string with embedded NUL here \0 and then some more text') |
| 196 | self.assertGdbRepr('this is byte 255:\xff and byte 128:\x80') |
| 197 | |
| 198 | def test_tuples(self): |
| 199 | 'Verify the pretty-printing of tuples' |
| 200 | self.assertGdbRepr(tuple()) |
| 201 | self.assertGdbRepr((1,)) |
| 202 | self.assertGdbRepr(('foo', 'bar', 'baz')) |
| 203 | |
| 204 | def test_unicode(self): |
| 205 | 'Verify the pretty-printing of unicode values' |
| 206 | # Test the empty unicode string: |
| 207 | self.assertGdbRepr(u'') |
| 208 | |
| 209 | self.assertGdbRepr(u'hello world') |
| 210 | |
| 211 | # Test printing a single character: |
| 212 | # U+2620 SKULL AND CROSSBONES |
| 213 | self.assertGdbRepr(u'\u2620') |
| 214 | |
| 215 | # Test printing a Japanese unicode string |
| 216 | # (I believe this reads "mojibake", using 3 characters from the CJK |
| 217 | # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE) |
| 218 | self.assertGdbRepr(u'\u6587\u5b57\u5316\u3051') |
| 219 | |
| 220 | # Test a character outside the BMP: |
| 221 | # U+1D121 MUSICAL SYMBOL C CLEF |
| 222 | # This is: |
| 223 | # UTF-8: 0xF0 0x9D 0x84 0xA1 |
| 224 | # UTF-16: 0xD834 0xDD21 |
| 225 | try: |
| 226 | # This will only work on wide-unicode builds: |
| 227 | self.assertGdbRepr(unichr(0x1D121)) |
| 228 | except ValueError, e: |
Martin v. Löwis | 7f7765c | 2010-04-12 05:18:16 +0000 | [diff] [blame] | 229 | # We're probably on a narrow-unicode build; if we're seeing a |
| 230 | # different problem, then re-raise it: |
| 231 | if e.args != ('unichr() arg not in range(0x10000) (narrow Python build)',): |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 232 | raise e |
| 233 | |
| 234 | def test_sets(self): |
| 235 | 'Verify the pretty-printing of sets' |
| 236 | self.assertGdbRepr(set()) |
| 237 | self.assertGdbRepr(set(['a', 'b'])) |
| 238 | self.assertGdbRepr(set([4, 5, 6])) |
| 239 | |
| 240 | # Ensure that we handled sets containing the "dummy" key value, |
| 241 | # which happens on deletion: |
| 242 | gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b']) |
| 243 | s.pop() |
| 244 | print s''') |
| 245 | self.assertEquals(gdb_repr, "set(['b'])") |
| 246 | |
| 247 | def test_frozensets(self): |
| 248 | 'Verify the pretty-printing of frozensets' |
| 249 | self.assertGdbRepr(frozenset()) |
| 250 | self.assertGdbRepr(frozenset(['a', 'b'])) |
| 251 | self.assertGdbRepr(frozenset([4, 5, 6])) |
| 252 | |
| 253 | def test_exceptions(self): |
| 254 | # Test a RuntimeError |
| 255 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 256 | try: |
| 257 | raise RuntimeError("I am an error") |
| 258 | except RuntimeError, e: |
| 259 | print e |
| 260 | ''') |
| 261 | self.assertEquals(gdb_repr, |
| 262 | "exceptions.RuntimeError('I am an error',)") |
| 263 | |
| 264 | |
| 265 | # Test division by zero: |
| 266 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 267 | try: |
| 268 | a = 1 / 0 |
| 269 | except ZeroDivisionError, e: |
| 270 | print e |
| 271 | ''') |
| 272 | self.assertEquals(gdb_repr, |
| 273 | "exceptions.ZeroDivisionError('integer division or modulo by zero',)") |
| 274 | |
| 275 | def test_classic_class(self): |
| 276 | 'Verify the pretty-printing of classic class instances' |
| 277 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 278 | class Foo: |
| 279 | pass |
| 280 | foo = Foo() |
| 281 | foo.an_int = 42 |
| 282 | print foo''') |
| 283 | m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
| 284 | self.assertTrue(m, |
| 285 | msg='Unexpected classic-class rendering %r' % gdb_repr) |
| 286 | |
| 287 | def test_modern_class(self): |
| 288 | 'Verify the pretty-printing of new-style class instances' |
| 289 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 290 | class Foo(object): |
| 291 | pass |
| 292 | foo = Foo() |
| 293 | foo.an_int = 42 |
| 294 | print foo''') |
| 295 | m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
| 296 | self.assertTrue(m, |
| 297 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 298 | |
| 299 | def test_subclassing_list(self): |
| 300 | 'Verify the pretty-printing of an instance of a list subclass' |
| 301 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 302 | class Foo(list): |
| 303 | pass |
| 304 | foo = Foo() |
| 305 | foo += [1, 2, 3] |
| 306 | foo.an_int = 42 |
| 307 | print foo''') |
| 308 | m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
| 309 | self.assertTrue(m, |
| 310 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 311 | |
| 312 | def test_subclassing_tuple(self): |
| 313 | 'Verify the pretty-printing of an instance of a tuple subclass' |
| 314 | # This should exercise the negative tp_dictoffset code in the |
| 315 | # new-style class support |
| 316 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 317 | class Foo(tuple): |
| 318 | pass |
| 319 | foo = Foo((1, 2, 3)) |
| 320 | foo.an_int = 42 |
| 321 | print foo''') |
| 322 | m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
| 323 | self.assertTrue(m, |
| 324 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 325 | |
Martin v. Löwis | 7f7765c | 2010-04-12 05:18:16 +0000 | [diff] [blame] | 326 | def assertSane(self, source, corruption, expvalue=None, exptype=None): |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 327 | '''Run Python under gdb, corrupting variables in the inferior process |
| 328 | immediately before taking a backtrace. |
| 329 | |
| 330 | Verify that the variable's representation is the expected failsafe |
| 331 | representation''' |
| 332 | if corruption: |
| 333 | cmds_after_breakpoint=[corruption, 'backtrace'] |
| 334 | else: |
| 335 | cmds_after_breakpoint=['backtrace'] |
| 336 | |
| 337 | gdb_repr, gdb_output = \ |
| 338 | self.get_gdb_repr(source, |
| 339 | cmds_after_breakpoint=cmds_after_breakpoint) |
Martin v. Löwis | 7f7765c | 2010-04-12 05:18:16 +0000 | [diff] [blame] | 340 | |
| 341 | if expvalue: |
| 342 | if gdb_repr == repr(expvalue): |
| 343 | # gdb managed to print the value in spite of the corruption; |
| 344 | # this is good (see http://bugs.python.org/issue8330) |
| 345 | return |
| 346 | |
| 347 | if exptype: |
| 348 | pattern = '<' + exptype + ' at remote 0x[0-9a-f]+>' |
| 349 | else: |
| 350 | # Match anything for the type name; 0xDEADBEEF could point to |
| 351 | # something arbitrary (see http://bugs.python.org/issue8330) |
| 352 | pattern = '<.* at remote 0x[0-9a-f]+>' |
| 353 | |
| 354 | m = re.match(pattern, gdb_repr) |
| 355 | if not m: |
| 356 | self.fail('Unexpected gdb representation: %r\n%s' % \ |
| 357 | (gdb_repr, gdb_output)) |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 358 | |
| 359 | def test_NULL_ptr(self): |
| 360 | 'Ensure that a NULL PyObject* is handled gracefully' |
| 361 | gdb_repr, gdb_output = ( |
| 362 | self.get_gdb_repr('print 42', |
| 363 | cmds_after_breakpoint=['set variable op=0', |
R. David Murray | 0c08009 | 2010-04-05 16:28:49 +0000 | [diff] [blame] | 364 | 'backtrace']) |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 365 | ) |
| 366 | |
| 367 | self.assertEquals(gdb_repr, '0x0') |
| 368 | |
| 369 | def test_NULL_ob_type(self): |
| 370 | 'Ensure that a PyObject* with NULL ob_type is handled gracefully' |
| 371 | self.assertSane('print 42', |
| 372 | 'set op->ob_type=0') |
| 373 | |
| 374 | def test_corrupt_ob_type(self): |
| 375 | 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully' |
| 376 | self.assertSane('print 42', |
Martin v. Löwis | 7f7765c | 2010-04-12 05:18:16 +0000 | [diff] [blame] | 377 | 'set op->ob_type=0xDEADBEEF', |
| 378 | expvalue=42) |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 379 | |
| 380 | def test_corrupt_tp_flags(self): |
| 381 | 'Ensure that a PyObject* with a type with corrupt tp_flags is handled' |
| 382 | self.assertSane('print 42', |
| 383 | 'set op->ob_type->tp_flags=0x0', |
Martin v. Löwis | 7f7765c | 2010-04-12 05:18:16 +0000 | [diff] [blame] | 384 | expvalue=42) |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 385 | |
| 386 | def test_corrupt_tp_name(self): |
| 387 | 'Ensure that a PyObject* with a type with corrupt tp_name is handled' |
| 388 | self.assertSane('print 42', |
Martin v. Löwis | 7f7765c | 2010-04-12 05:18:16 +0000 | [diff] [blame] | 389 | 'set op->ob_type->tp_name=0xDEADBEEF', |
| 390 | expvalue=42) |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 391 | |
| 392 | def test_NULL_instance_dict(self): |
| 393 | 'Ensure that a PyInstanceObject with with a NULL in_dict is handled' |
| 394 | self.assertSane(''' |
| 395 | class Foo: |
| 396 | pass |
| 397 | foo = Foo() |
| 398 | foo.an_int = 42 |
| 399 | print foo''', |
| 400 | 'set ((PyInstanceObject*)op)->in_dict = 0', |
Martin v. Löwis | 7f7765c | 2010-04-12 05:18:16 +0000 | [diff] [blame] | 401 | exptype='Foo') |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 402 | |
| 403 | def test_builtins_help(self): |
| 404 | 'Ensure that the new-style class _Helper in site.py can be handled' |
| 405 | # (this was the issue causing tracebacks in |
| 406 | # http://bugs.python.org/issue8032#msg100537 ) |
| 407 | |
| 408 | gdb_repr, gdb_output = self.get_gdb_repr('print __builtins__.help', import_site=True) |
| 409 | m = re.match(r'<_Helper at remote 0x[0-9a-f]+>', gdb_repr) |
| 410 | self.assertTrue(m, |
| 411 | msg='Unexpected rendering %r' % gdb_repr) |
| 412 | |
| 413 | def test_selfreferential_list(self): |
| 414 | '''Ensure that a reference loop involving a list doesn't lead proxyval |
| 415 | into an infinite loop:''' |
| 416 | gdb_repr, gdb_output = \ |
| 417 | self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; print a") |
| 418 | |
| 419 | self.assertEquals(gdb_repr, '[3, 4, 5, [...]]') |
| 420 | |
| 421 | gdb_repr, gdb_output = \ |
| 422 | self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; print a") |
| 423 | |
| 424 | self.assertEquals(gdb_repr, '[3, 4, 5, [[...]]]') |
| 425 | |
| 426 | def test_selfreferential_dict(self): |
| 427 | '''Ensure that a reference loop involving a dict doesn't lead proxyval |
| 428 | into an infinite loop:''' |
| 429 | gdb_repr, gdb_output = \ |
| 430 | self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; print a") |
| 431 | |
| 432 | self.assertEquals(gdb_repr, "{'foo': {'bar': {...}}}") |
| 433 | |
| 434 | def test_selfreferential_old_style_instance(self): |
| 435 | gdb_repr, gdb_output = \ |
| 436 | self.get_gdb_repr(''' |
| 437 | class Foo: |
| 438 | pass |
| 439 | foo = Foo() |
| 440 | foo.an_attr = foo |
| 441 | print foo''') |
| 442 | self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>', |
| 443 | gdb_repr), |
| 444 | 'Unexpected gdb representation: %r\n%s' % \ |
| 445 | (gdb_repr, gdb_output)) |
| 446 | |
| 447 | def test_selfreferential_new_style_instance(self): |
| 448 | gdb_repr, gdb_output = \ |
| 449 | self.get_gdb_repr(''' |
| 450 | class Foo(object): |
| 451 | pass |
| 452 | foo = Foo() |
| 453 | foo.an_attr = foo |
| 454 | print foo''') |
| 455 | self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>', |
| 456 | gdb_repr), |
| 457 | 'Unexpected gdb representation: %r\n%s' % \ |
| 458 | (gdb_repr, gdb_output)) |
| 459 | |
| 460 | gdb_repr, gdb_output = \ |
| 461 | self.get_gdb_repr(''' |
| 462 | class Foo(object): |
| 463 | pass |
| 464 | a = Foo() |
| 465 | b = Foo() |
| 466 | a.an_attr = b |
| 467 | b.an_attr = a |
| 468 | print a''') |
| 469 | self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>\) at remote 0x[0-9a-f]+>', |
| 470 | gdb_repr), |
| 471 | 'Unexpected gdb representation: %r\n%s' % \ |
| 472 | (gdb_repr, gdb_output)) |
| 473 | |
| 474 | def test_truncation(self): |
| 475 | 'Verify that very long output is truncated' |
| 476 | gdb_repr, gdb_output = self.get_gdb_repr('print range(1000)') |
| 477 | self.assertEquals(gdb_repr, |
R. David Murray | 0c08009 | 2010-04-05 16:28:49 +0000 | [diff] [blame] | 478 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, " |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 479 | "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, " |
| 480 | "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, " |
| 481 | "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, " |
| 482 | "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, " |
| 483 | "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, " |
| 484 | "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, " |
| 485 | "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, " |
| 486 | "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, " |
| 487 | "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, " |
| 488 | "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, " |
| 489 | "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, " |
| 490 | "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, " |
| 491 | "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, " |
| 492 | "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, " |
| 493 | "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, " |
| 494 | "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, " |
| 495 | "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, " |
| 496 | "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, " |
| 497 | "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, " |
| 498 | "224, 225, 226...(truncated)") |
| 499 | self.assertEquals(len(gdb_repr), |
R. David Murray | 0c08009 | 2010-04-05 16:28:49 +0000 | [diff] [blame] | 500 | 1024 + len('...(truncated)')) |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 501 | |
| 502 | def test_builtin_function(self): |
| 503 | gdb_repr, gdb_output = self.get_gdb_repr('print len') |
| 504 | self.assertEquals(gdb_repr, '<built-in function len>') |
| 505 | |
| 506 | def test_builtin_method(self): |
| 507 | gdb_repr, gdb_output = self.get_gdb_repr('import sys; print sys.stdout.readlines') |
| 508 | self.assertTrue(re.match('<built-in method readlines of file object at remote 0x[0-9a-f]+>', |
| 509 | gdb_repr), |
| 510 | 'Unexpected gdb representation: %r\n%s' % \ |
| 511 | (gdb_repr, gdb_output)) |
| 512 | |
| 513 | def test_frames(self): |
| 514 | gdb_output = self.get_stack_trace(''' |
| 515 | def foo(a, b, c): |
| 516 | pass |
| 517 | |
| 518 | foo(3, 4, 5) |
| 519 | print foo.__code__''', |
| 520 | breakpoint='PyObject_Print', |
| 521 | cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)op)->co_zombieframe)'] |
| 522 | ) |
R. David Murray | 0c08009 | 2010-04-05 16:28:49 +0000 | [diff] [blame] | 523 | self.assertTrue(re.match(r'.*\s+\$1 =\s+Frame 0x[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*', |
| 524 | gdb_output, |
| 525 | re.DOTALL), |
| 526 | 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output)) |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 527 | |
| 528 | class PyListTests(DebuggerTests): |
| 529 | def assertListing(self, expected, actual): |
| 530 | self.assertEndsWith(actual, expected) |
| 531 | |
| 532 | def test_basic_command(self): |
| 533 | 'Verify that the "py-list" command works' |
| 534 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 535 | cmds_after_breakpoint=['py-list']) |
| 536 | |
| 537 | self.assertListing(''' |
| 538 | 5 |
| 539 | 6 def bar(a, b, c): |
| 540 | 7 baz(a, b, c) |
| 541 | 8 |
| 542 | 9 def baz(*args): |
| 543 | >10 print(42) |
| 544 | 11 |
| 545 | 12 foo(1, 2, 3) |
| 546 | ''', |
| 547 | bt) |
| 548 | |
| 549 | def test_one_abs_arg(self): |
| 550 | 'Verify the "py-list" command with one absolute argument' |
| 551 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 552 | cmds_after_breakpoint=['py-list 9']) |
| 553 | |
| 554 | self.assertListing(''' |
| 555 | 9 def baz(*args): |
| 556 | >10 print(42) |
| 557 | 11 |
| 558 | 12 foo(1, 2, 3) |
| 559 | ''', |
| 560 | bt) |
| 561 | |
| 562 | def test_two_abs_args(self): |
| 563 | 'Verify the "py-list" command with two absolute arguments' |
| 564 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 565 | cmds_after_breakpoint=['py-list 1,3']) |
| 566 | |
| 567 | self.assertListing(''' |
| 568 | 1 # Sample script for use by test_gdb.py |
| 569 | 2 |
| 570 | 3 def foo(a, b, c): |
| 571 | ''', |
| 572 | bt) |
| 573 | |
| 574 | class StackNavigationTests(DebuggerTests): |
| 575 | def test_pyup_command(self): |
| 576 | 'Verify that the "py-up" command works' |
| 577 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 578 | cmds_after_breakpoint=['py-up']) |
| 579 | self.assertMultilineMatches(bt, |
| 580 | r'''^.* |
| 581 | #[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
| 582 | baz\(a, b, c\) |
| 583 | $''') |
| 584 | |
| 585 | def test_down_at_bottom(self): |
| 586 | 'Verify handling of "py-down" at the bottom of the stack' |
| 587 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 588 | cmds_after_breakpoint=['py-down']) |
| 589 | self.assertEndsWith(bt, |
| 590 | 'Unable to find a newer python frame\n') |
| 591 | |
| 592 | def test_up_at_top(self): |
| 593 | 'Verify handling of "py-up" at the top of the stack' |
| 594 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 595 | cmds_after_breakpoint=['py-up'] * 4) |
| 596 | self.assertEndsWith(bt, |
| 597 | 'Unable to find an older python frame\n') |
| 598 | |
| 599 | def test_up_then_down(self): |
| 600 | 'Verify "py-up" followed by "py-down"' |
| 601 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 602 | cmds_after_breakpoint=['py-up', 'py-down']) |
| 603 | self.assertMultilineMatches(bt, |
| 604 | r'''^.* |
| 605 | #[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
| 606 | baz\(a, b, c\) |
| 607 | #[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\) |
| 608 | print\(42\) |
| 609 | $''') |
| 610 | |
| 611 | class PyBtTests(DebuggerTests): |
| 612 | def test_basic_command(self): |
| 613 | 'Verify that the "py-bt" command works' |
| 614 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 615 | cmds_after_breakpoint=['py-bt']) |
| 616 | self.assertMultilineMatches(bt, |
| 617 | r'''^.* |
| 618 | #[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
| 619 | baz\(a, b, c\) |
| 620 | #[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\) |
| 621 | bar\(a, b, c\) |
| 622 | #[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 12, in <module> \(\) |
| 623 | foo\(1, 2, 3\) |
| 624 | ''') |
| 625 | |
| 626 | class PyPrintTests(DebuggerTests): |
| 627 | def test_basic_command(self): |
| 628 | 'Verify that the "py-print" command works' |
| 629 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 630 | cmds_after_breakpoint=['py-print args']) |
| 631 | self.assertMultilineMatches(bt, |
| 632 | r".*\nlocal 'args' = \(1, 2, 3\)\n.*") |
| 633 | |
| 634 | def test_print_after_up(self): |
| 635 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 636 | cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a']) |
| 637 | self.assertMultilineMatches(bt, |
| 638 | r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*") |
| 639 | |
| 640 | def test_printing_global(self): |
| 641 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 642 | cmds_after_breakpoint=['py-print __name__']) |
| 643 | self.assertMultilineMatches(bt, |
| 644 | r".*\nglobal '__name__' = '__main__'\n.*") |
| 645 | |
| 646 | def test_printing_builtin(self): |
| 647 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 648 | cmds_after_breakpoint=['py-print len']) |
| 649 | self.assertMultilineMatches(bt, |
| 650 | r".*\nbuiltin 'len' = <built-in function len>\n.*") |
| 651 | |
| 652 | class PyLocalsTests(DebuggerTests): |
| 653 | def test_basic_command(self): |
| 654 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 655 | cmds_after_breakpoint=['py-locals']) |
| 656 | self.assertMultilineMatches(bt, |
| 657 | r".*\nargs = \(1, 2, 3\)\n.*") |
| 658 | |
| 659 | def test_locals_after_up(self): |
| 660 | bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py', |
| 661 | cmds_after_breakpoint=['py-up', 'py-locals']) |
| 662 | self.assertMultilineMatches(bt, |
| 663 | r".*\na = 1\nb = 2\nc = 3\n.*") |
| 664 | |
| 665 | def test_main(): |
Martin v. Löwis | 5a96543 | 2010-04-12 05:22:25 +0000 | [diff] [blame^] | 666 | run_unittest(PrettyPrintTests, |
| 667 | PyListTests, |
| 668 | StackNavigationTests, |
| 669 | PyBtTests, |
| 670 | PyPrintTests, |
| 671 | PyLocalsTests |
Martin v. Löwis | bf0dfb3 | 2010-04-01 07:40:51 +0000 | [diff] [blame] | 672 | ) |
| 673 | |
| 674 | if __name__ == "__main__": |
| 675 | test_main() |