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