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