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