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