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