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