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