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