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