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