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