blob: fc65c2a0fc11265cfcea1757c7b930dceaa49142 [file] [log] [blame]
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +00001# 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
6import os
7import re
8import subprocess
9import sys
10import unittest
Antoine Pitrou22db7352010-07-08 18:54:04 +000011import sysconfig
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +000012
Victor Stinner3c5ce402015-09-03 09:51:59 +020013from test import test_support
Martin v. Löwis24f09fd2010-04-17 22:40:40 +000014from test.test_support import run_unittest, findfile
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +000015
Victor Stinnercc1db4b2015-09-03 10:17:28 +020016# Is this Python configured to support threads?
17try:
18 import thread
19except ImportError:
20 thread = None
21
Victor Stinner3c5ce402015-09-03 09:51:59 +020022def get_gdb_version():
23 try:
24 proc = subprocess.Popen(["gdb", "-nx", "--version"],
25 stdout=subprocess.PIPE,
26 universal_newlines=True)
27 version = proc.communicate()[0]
28 except OSError:
29 # This is what "no gdb" looks like. There may, however, be other
30 # errors that manifest this way too.
31 raise unittest.SkipTest("Couldn't find gdb on the path")
32
33 # Regex to parse:
34 # 'GNU gdb (GDB; SUSE Linux Enterprise 12) 7.7\n' -> 7.7
35 # 'GNU gdb (GDB) Fedora 7.9.1-17.fc22\n' -> 7.9
36 # 'GNU gdb 6.1.1 [FreeBSD]\n'
37 match = re.search("^GNU gdb.*? (\d+)\.(\d)", version)
38 if match is None:
39 raise Exception("unable to parse GDB version: %r" % version)
40 return (version, int(match.group(1)), int(match.group(2)))
41
42gdb_version, gdb_major_version, gdb_minor_version = get_gdb_version()
R David Murray3e66f0d2012-10-27 13:47:49 -040043if gdb_major_version < 7:
Victor Stinner3c5ce402015-09-03 09:51:59 +020044 raise unittest.SkipTest("gdb versions before 7.0 didn't support python "
45 "embedding. Saw %s.%s:\n%s"
46 % (gdb_major_version, gdb_minor_version,
47 gdb_version))
48
Benjamin Peterson51f461f2014-11-23 22:34:04 -060049if sys.platform.startswith("sunos"):
Benjamin Peterson0636a4b2014-11-23 22:02:47 -060050 raise unittest.SkipTest("test doesn't work very well on Solaris")
51
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +000052
R David Murray3e66f0d2012-10-27 13:47:49 -040053# Location of custom hooks file in a repository checkout.
54checkout_hook_path = os.path.join(os.path.dirname(sys.executable),
55 'python-gdb.py')
56
57def run_gdb(*args, **env_vars):
Victor Stinner8bd34152014-08-16 14:31:02 +020058 """Runs gdb in batch mode with the additional arguments given by *args.
R David Murray3e66f0d2012-10-27 13:47:49 -040059
60 Returns its (stdout, stderr)
61 """
62 if env_vars:
63 env = os.environ.copy()
64 env.update(env_vars)
65 else:
66 env = None
Victor Stinner8bd34152014-08-16 14:31:02 +020067 # -nx: Do not execute commands from any .gdbinit initialization files
68 # (issue #22188)
69 base_cmd = ('gdb', '--batch', '-nx')
R David Murray3e66f0d2012-10-27 13:47:49 -040070 if (gdb_major_version, gdb_minor_version) >= (7, 4):
71 base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path)
72 out, err = subprocess.Popen(base_cmd + args,
73 stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
74 ).communicate()
75 return out, err
76
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +000077# Verify that "gdb" was built with the embedded python support enabled:
Antoine Pitrou358da5b2013-11-23 17:40:36 +010078gdbpy_version, _ = run_gdb("--eval-command=python import sys; print(sys.version_info)")
R David Murray3e66f0d2012-10-27 13:47:49 -040079if not gdbpy_version:
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +000080 raise unittest.SkipTest("gdb not built with embedded python support")
81
Nick Coghlan254a3772013-09-22 19:36:09 +100082# Verify that "gdb" can load our custom hooks, as OS security settings may
83# disallow this without a customised .gdbinit.
R David Murray3e66f0d2012-10-27 13:47:49 -040084cmd = ['--args', sys.executable]
85_, gdbpy_errors = run_gdb('--args', sys.executable)
86if "auto-loading has been declined" in gdbpy_errors:
87 msg = "gdb security settings prevent use of custom hooks: "
Nick Coghlan254a3772013-09-22 19:36:09 +100088 raise unittest.SkipTest(msg + gdbpy_errors.rstrip())
Nick Coghlana0933122012-06-17 19:03:39 +100089
Victor Stinner99cff3f2011-12-19 13:59:58 +010090def python_is_optimized():
91 cflags = sysconfig.get_config_vars()['PY_CFLAGS']
92 final_opt = ""
93 for opt in cflags.split():
94 if opt.startswith('-O'):
95 final_opt = opt
Victor Stinner582265f2015-03-27 15:44:13 +010096 return final_opt not in ('', '-O0', '-Og')
Victor Stinner99cff3f2011-12-19 13:59:58 +010097
Victor Stinnera92e81b2010-04-20 22:28:31 +000098def gdb_has_frame_select():
99 # Does this build of gdb have gdb.Frame.select ?
R David Murray3e66f0d2012-10-27 13:47:49 -0400100 stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))")
Victor Stinnera92e81b2010-04-20 22:28:31 +0000101 m = re.match(r'.*\[(.*)\].*', stdout)
102 if not m:
103 raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test")
104 gdb_frame_dir = m.group(1).split(', ')
105 return "'select'" in gdb_frame_dir
106
107HAS_PYUP_PYDOWN = gdb_has_frame_select()
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000108
109class DebuggerTests(unittest.TestCase):
110
111 """Test that the debugger can debug Python."""
112
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000113 def get_stack_trace(self, source=None, script=None,
114 breakpoint='PyObject_Print',
115 cmds_after_breakpoint=None,
116 import_site=False):
117 '''
118 Run 'python -c SOURCE' under gdb with a breakpoint.
119
120 Support injecting commands after the breakpoint is reached
121
122 Returns the stdout from gdb
123
124 cmds_after_breakpoint: if provided, a list of strings: gdb commands
125 '''
126 # We use "set breakpoint pending yes" to avoid blocking with a:
127 # Function "foo" not defined.
128 # Make breakpoint pending on future shared library load? (y or [n])
129 # error, which typically happens python is dynamically linked (the
130 # breakpoints of interest are to be found in the shared library)
131 # When this happens, we still get:
132 # Function "PyObject_Print" not defined.
133 # emitted to stderr each time, alas.
134
135 # Initially I had "--eval-command=continue" here, but removed it to
136 # avoid repeated print breakpoints when traversing hierarchical data
137 # structures
138
139 # Generate a list of commands in gdb's language:
140 commands = ['set breakpoint pending yes',
141 'break %s' % breakpoint,
Serhiy Storchaka73bcde22015-01-31 11:48:36 +0200142
Serhiy Storchaka73bcde22015-01-31 11:48:36 +0200143 # The tests assume that the first frame of printed
144 # backtrace will not contain program counter,
145 # that is however not guaranteed by gdb
146 # therefore we need to use 'set print address off' to
147 # make sure the counter is not there. For example:
148 # #0 in PyObject_Print ...
149 # is assumed, but sometimes this can be e.g.
150 # #0 0x00003fffb7dd1798 in PyObject_Print ...
151 'set print address off',
152
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000153 'run']
Serhiy Storchakadd8430f2015-02-06 08:36:14 +0200154
155 # GDB as of 7.4 onwards can distinguish between the
156 # value of a variable at entry vs current value:
157 # http://sourceware.org/gdb/onlinedocs/gdb/Variables.html
158 # which leads to the selftests failing with errors like this:
159 # AssertionError: 'v@entry=()' != '()'
160 # Disable this:
161 if (gdb_major_version, gdb_minor_version) >= (7, 4):
162 commands += ['set print entry-values no']
163
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000164 if cmds_after_breakpoint:
165 commands += cmds_after_breakpoint
166 else:
167 commands += ['backtrace']
168
169 # print commands
170
171 # Use "commands" to generate the arguments with which to invoke "gdb":
Victor Stinner8bd34152014-08-16 14:31:02 +0200172 args = ["gdb", "--batch", "-nx"]
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000173 args += ['--eval-command=%s' % cmd for cmd in commands]
174 args += ["--args",
175 sys.executable]
176
177 if not import_site:
178 # -S suppresses the default 'import site'
179 args += ["-S"]
180
181 if source:
182 args += ["-c", source]
183 elif script:
184 args += [script]
185
186 # print args
187 # print ' '.join(args)
188
189 # Use "args" to invoke gdb, capturing stdout, stderr:
R David Murray3e66f0d2012-10-27 13:47:49 -0400190 out, err = run_gdb(*args, PYTHONHASHSEED='0')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000191
Antoine Pitroub996e042013-05-01 00:15:44 +0200192 errlines = err.splitlines()
193 unexpected_errlines = []
194
195 # Ignore some benign messages on stderr.
196 ignore_patterns = (
197 'Function "%s" not defined.' % breakpoint,
198 "warning: no loadable sections found in added symbol-file"
199 " system-supplied DSO",
200 "warning: Unable to find libthread_db matching"
201 " inferior's thread library, thread debugging will"
202 " not be available.",
203 "warning: Cannot initialize thread debugging"
204 " library: Debugger service failed",
205 'warning: Could not load shared library symbols for '
206 'linux-vdso.so',
207 'warning: Could not load shared library symbols for '
208 'linux-gate.so',
Serhiy Storchakab6b48e62015-02-14 22:44:35 +0200209 'warning: Could not load shared library symbols for '
210 'linux-vdso64.so',
Antoine Pitroub996e042013-05-01 00:15:44 +0200211 'Do you need "set solib-search-path" or '
212 '"set sysroot"?',
Victor Stinner57b00ed2014-11-05 15:07:18 +0100213 'warning: Source file is more recent than executable.',
214 # Issue #19753: missing symbols on System Z
215 'Missing separate debuginfo for ',
216 'Try: zypper install -C ',
Antoine Pitroub996e042013-05-01 00:15:44 +0200217 )
218 for line in errlines:
219 if not line.startswith(ignore_patterns):
220 unexpected_errlines.append(line)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000221
222 # Ensure no unexpected error messages:
Antoine Pitroub996e042013-05-01 00:15:44 +0200223 self.assertEqual(unexpected_errlines, [])
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000224 return out
225
226 def get_gdb_repr(self, source,
227 cmds_after_breakpoint=None,
228 import_site=False):
229 # Given an input python source representation of data,
230 # run "python -c'print DATA'" under gdb with a breakpoint on
231 # PyObject_Print and scrape out gdb's representation of the "op"
232 # parameter, and verify that the gdb displays the same string
233 #
234 # For a nested structure, the first time we hit the breakpoint will
235 # give us the top-level structure
236 gdb_output = self.get_stack_trace(source, breakpoint='PyObject_Print',
237 cmds_after_breakpoint=cmds_after_breakpoint,
238 import_site=import_site)
R. David Murray0c080092010-04-05 16:28:49 +0000239 # gdb can insert additional '\n' and space characters in various places
240 # in its output, depending on the width of the terminal it's connected
241 # to (using its "wrap_here" function)
242 m = re.match('.*#0\s+PyObject_Print\s+\(\s*op\=\s*(.*?),\s+fp=.*\).*',
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000243 gdb_output, re.DOTALL)
R. David Murray0c080092010-04-05 16:28:49 +0000244 if not m:
245 self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000246 return m.group(1), gdb_output
247
248 def assertEndsWith(self, actual, exp_end):
249 '''Ensure that the given "actual" string ends with "exp_end"'''
Ezio Melotti2623a372010-11-21 13:34:58 +0000250 self.assertTrue(actual.endswith(exp_end),
251 msg='%r did not end with %r' % (actual, exp_end))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000252
253 def assertMultilineMatches(self, actual, pattern):
254 m = re.match(pattern, actual, re.DOTALL)
Ezio Melotti2623a372010-11-21 13:34:58 +0000255 self.assertTrue(m, msg='%r did not match %r' % (actual, pattern))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000256
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000257 def get_sample_script(self):
258 return findfile('gdb_sample.py')
259
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000260class PrettyPrintTests(DebuggerTests):
261 def test_getting_backtrace(self):
262 gdb_output = self.get_stack_trace('print 42')
263 self.assertTrue('PyObject_Print' in gdb_output)
264
265 def assertGdbRepr(self, val, cmds_after_breakpoint=None):
266 # Ensure that gdb's rendering of the value in a debugged process
267 # matches repr(value) in this process:
268 gdb_repr, gdb_output = self.get_gdb_repr('print ' + repr(val),
269 cmds_after_breakpoint)
Antoine Pitrou358da5b2013-11-23 17:40:36 +0100270 self.assertEqual(gdb_repr, repr(val))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000271
272 def test_int(self):
273 'Verify the pretty-printing of various "int" values'
274 self.assertGdbRepr(42)
275 self.assertGdbRepr(0)
276 self.assertGdbRepr(-7)
277 self.assertGdbRepr(sys.maxint)
278 self.assertGdbRepr(-sys.maxint)
279
280 def test_long(self):
281 'Verify the pretty-printing of various "long" values'
282 self.assertGdbRepr(0L)
283 self.assertGdbRepr(1000000000000L)
284 self.assertGdbRepr(-1L)
285 self.assertGdbRepr(-1000000000000000L)
286
287 def test_singletons(self):
288 'Verify the pretty-printing of True, False and None'
289 self.assertGdbRepr(True)
290 self.assertGdbRepr(False)
291 self.assertGdbRepr(None)
292
293 def test_dicts(self):
294 'Verify the pretty-printing of dictionaries'
295 self.assertGdbRepr({})
296 self.assertGdbRepr({'foo': 'bar'})
Benjamin Peterson11fa11b2012-02-20 21:55:32 -0500297 self.assertGdbRepr("{'foo': 'bar', 'douglas':42}")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000298
299 def test_lists(self):
300 'Verify the pretty-printing of lists'
301 self.assertGdbRepr([])
302 self.assertGdbRepr(range(5))
303
304 def test_strings(self):
305 'Verify the pretty-printing of strings'
306 self.assertGdbRepr('')
307 self.assertGdbRepr('And now for something hopefully the same')
308 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
309 self.assertGdbRepr('this is byte 255:\xff and byte 128:\x80')
310
311 def test_tuples(self):
312 'Verify the pretty-printing of tuples'
313 self.assertGdbRepr(tuple())
314 self.assertGdbRepr((1,))
315 self.assertGdbRepr(('foo', 'bar', 'baz'))
316
317 def test_unicode(self):
318 'Verify the pretty-printing of unicode values'
319 # Test the empty unicode string:
320 self.assertGdbRepr(u'')
321
322 self.assertGdbRepr(u'hello world')
323
324 # Test printing a single character:
325 # U+2620 SKULL AND CROSSBONES
326 self.assertGdbRepr(u'\u2620')
327
328 # Test printing a Japanese unicode string
329 # (I believe this reads "mojibake", using 3 characters from the CJK
330 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
331 self.assertGdbRepr(u'\u6587\u5b57\u5316\u3051')
332
333 # Test a character outside the BMP:
334 # U+1D121 MUSICAL SYMBOL C CLEF
335 # This is:
336 # UTF-8: 0xF0 0x9D 0x84 0xA1
337 # UTF-16: 0xD834 0xDD21
Victor Stinnerb1556c52010-05-20 11:29:45 +0000338 # This will only work on wide-unicode builds:
339 self.assertGdbRepr(u"\U0001D121")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000340
341 def test_sets(self):
342 'Verify the pretty-printing of sets'
343 self.assertGdbRepr(set())
Benjamin Petersone39ccef2012-02-21 09:07:40 -0500344 rep = self.get_gdb_repr("print set(['a', 'b'])")[0]
345 self.assertTrue(rep.startswith("set(["))
346 self.assertTrue(rep.endswith("])"))
347 self.assertEqual(eval(rep), {'a', 'b'})
348 rep = self.get_gdb_repr("print set([4, 5])")[0]
349 self.assertTrue(rep.startswith("set(["))
350 self.assertTrue(rep.endswith("])"))
351 self.assertEqual(eval(rep), {4, 5})
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000352
353 # Ensure that we handled sets containing the "dummy" key value,
354 # which happens on deletion:
355 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
356s.pop()
357print s''')
Ezio Melotti2623a372010-11-21 13:34:58 +0000358 self.assertEqual(gdb_repr, "set(['b'])")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000359
360 def test_frozensets(self):
361 'Verify the pretty-printing of frozensets'
362 self.assertGdbRepr(frozenset())
Benjamin Petersone39ccef2012-02-21 09:07:40 -0500363 rep = self.get_gdb_repr("print frozenset(['a', 'b'])")[0]
364 self.assertTrue(rep.startswith("frozenset(["))
365 self.assertTrue(rep.endswith("])"))
366 self.assertEqual(eval(rep), {'a', 'b'})
367 rep = self.get_gdb_repr("print frozenset([4, 5])")[0]
368 self.assertTrue(rep.startswith("frozenset(["))
369 self.assertTrue(rep.endswith("])"))
370 self.assertEqual(eval(rep), {4, 5})
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000371
372 def test_exceptions(self):
373 # Test a RuntimeError
374 gdb_repr, gdb_output = self.get_gdb_repr('''
375try:
376 raise RuntimeError("I am an error")
377except RuntimeError, e:
378 print e
379''')
Ezio Melotti2623a372010-11-21 13:34:58 +0000380 self.assertEqual(gdb_repr,
381 "exceptions.RuntimeError('I am an error',)")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000382
383
384 # Test division by zero:
385 gdb_repr, gdb_output = self.get_gdb_repr('''
386try:
387 a = 1 / 0
388except ZeroDivisionError, e:
389 print e
390''')
Ezio Melotti2623a372010-11-21 13:34:58 +0000391 self.assertEqual(gdb_repr,
392 "exceptions.ZeroDivisionError('integer division or modulo by zero',)")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000393
394 def test_classic_class(self):
395 'Verify the pretty-printing of classic class instances'
396 gdb_repr, gdb_output = self.get_gdb_repr('''
397class Foo:
398 pass
399foo = Foo()
400foo.an_int = 42
401print foo''')
402 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
403 self.assertTrue(m,
404 msg='Unexpected classic-class rendering %r' % gdb_repr)
405
406 def test_modern_class(self):
407 'Verify the pretty-printing of new-style class instances'
408 gdb_repr, gdb_output = self.get_gdb_repr('''
409class Foo(object):
410 pass
411foo = Foo()
412foo.an_int = 42
413print foo''')
414 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
415 self.assertTrue(m,
416 msg='Unexpected new-style class rendering %r' % gdb_repr)
417
418 def test_subclassing_list(self):
419 'Verify the pretty-printing of an instance of a list subclass'
420 gdb_repr, gdb_output = self.get_gdb_repr('''
421class Foo(list):
422 pass
423foo = Foo()
424foo += [1, 2, 3]
425foo.an_int = 42
426print foo''')
427 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
428 self.assertTrue(m,
429 msg='Unexpected new-style class rendering %r' % gdb_repr)
430
431 def test_subclassing_tuple(self):
432 'Verify the pretty-printing of an instance of a tuple subclass'
433 # This should exercise the negative tp_dictoffset code in the
434 # new-style class support
435 gdb_repr, gdb_output = self.get_gdb_repr('''
436class Foo(tuple):
437 pass
438foo = Foo((1, 2, 3))
439foo.an_int = 42
440print foo''')
441 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
442 self.assertTrue(m,
443 msg='Unexpected new-style class rendering %r' % gdb_repr)
444
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000445 def assertSane(self, source, corruption, expvalue=None, exptype=None):
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000446 '''Run Python under gdb, corrupting variables in the inferior process
447 immediately before taking a backtrace.
448
449 Verify that the variable's representation is the expected failsafe
450 representation'''
451 if corruption:
452 cmds_after_breakpoint=[corruption, 'backtrace']
453 else:
454 cmds_after_breakpoint=['backtrace']
455
456 gdb_repr, gdb_output = \
457 self.get_gdb_repr(source,
458 cmds_after_breakpoint=cmds_after_breakpoint)
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000459
460 if expvalue:
461 if gdb_repr == repr(expvalue):
462 # gdb managed to print the value in spite of the corruption;
463 # this is good (see http://bugs.python.org/issue8330)
464 return
465
466 if exptype:
467 pattern = '<' + exptype + ' at remote 0x[0-9a-f]+>'
468 else:
469 # Match anything for the type name; 0xDEADBEEF could point to
470 # something arbitrary (see http://bugs.python.org/issue8330)
471 pattern = '<.* at remote 0x[0-9a-f]+>'
472
473 m = re.match(pattern, gdb_repr)
474 if not m:
475 self.fail('Unexpected gdb representation: %r\n%s' % \
476 (gdb_repr, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000477
478 def test_NULL_ptr(self):
479 'Ensure that a NULL PyObject* is handled gracefully'
480 gdb_repr, gdb_output = (
481 self.get_gdb_repr('print 42',
482 cmds_after_breakpoint=['set variable op=0',
R. David Murray0c080092010-04-05 16:28:49 +0000483 'backtrace'])
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000484 )
485
Ezio Melotti2623a372010-11-21 13:34:58 +0000486 self.assertEqual(gdb_repr, '0x0')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000487
488 def test_NULL_ob_type(self):
489 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
490 self.assertSane('print 42',
491 'set op->ob_type=0')
492
493 def test_corrupt_ob_type(self):
494 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
495 self.assertSane('print 42',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000496 'set op->ob_type=0xDEADBEEF',
497 expvalue=42)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000498
499 def test_corrupt_tp_flags(self):
500 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
501 self.assertSane('print 42',
502 'set op->ob_type->tp_flags=0x0',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000503 expvalue=42)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000504
505 def test_corrupt_tp_name(self):
506 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
507 self.assertSane('print 42',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000508 'set op->ob_type->tp_name=0xDEADBEEF',
509 expvalue=42)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000510
511 def test_NULL_instance_dict(self):
512 'Ensure that a PyInstanceObject with with a NULL in_dict is handled'
513 self.assertSane('''
514class Foo:
515 pass
516foo = Foo()
517foo.an_int = 42
518print foo''',
519 'set ((PyInstanceObject*)op)->in_dict = 0',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000520 exptype='Foo')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000521
522 def test_builtins_help(self):
523 'Ensure that the new-style class _Helper in site.py can be handled'
524 # (this was the issue causing tracebacks in
525 # http://bugs.python.org/issue8032#msg100537 )
526
527 gdb_repr, gdb_output = self.get_gdb_repr('print __builtins__.help', import_site=True)
528 m = re.match(r'<_Helper at remote 0x[0-9a-f]+>', gdb_repr)
529 self.assertTrue(m,
530 msg='Unexpected rendering %r' % gdb_repr)
531
532 def test_selfreferential_list(self):
533 '''Ensure that a reference loop involving a list doesn't lead proxyval
534 into an infinite loop:'''
535 gdb_repr, gdb_output = \
536 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; print a")
537
Ezio Melotti2623a372010-11-21 13:34:58 +0000538 self.assertEqual(gdb_repr, '[3, 4, 5, [...]]')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000539
540 gdb_repr, gdb_output = \
541 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; print a")
542
Ezio Melotti2623a372010-11-21 13:34:58 +0000543 self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000544
545 def test_selfreferential_dict(self):
546 '''Ensure that a reference loop involving a dict doesn't lead proxyval
547 into an infinite loop:'''
548 gdb_repr, gdb_output = \
549 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; print a")
550
Ezio Melotti2623a372010-11-21 13:34:58 +0000551 self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000552
553 def test_selfreferential_old_style_instance(self):
554 gdb_repr, gdb_output = \
555 self.get_gdb_repr('''
556class Foo:
557 pass
558foo = Foo()
559foo.an_attr = foo
560print foo''')
561 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
562 gdb_repr),
563 'Unexpected gdb representation: %r\n%s' % \
564 (gdb_repr, gdb_output))
565
566 def test_selfreferential_new_style_instance(self):
567 gdb_repr, gdb_output = \
568 self.get_gdb_repr('''
569class Foo(object):
570 pass
571foo = Foo()
572foo.an_attr = foo
573print foo''')
574 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
575 gdb_repr),
576 'Unexpected gdb representation: %r\n%s' % \
577 (gdb_repr, gdb_output))
578
579 gdb_repr, gdb_output = \
580 self.get_gdb_repr('''
581class Foo(object):
582 pass
583a = Foo()
584b = Foo()
585a.an_attr = b
586b.an_attr = a
587print a''')
588 self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>\) at remote 0x[0-9a-f]+>',
589 gdb_repr),
590 'Unexpected gdb representation: %r\n%s' % \
591 (gdb_repr, gdb_output))
592
593 def test_truncation(self):
594 'Verify that very long output is truncated'
595 gdb_repr, gdb_output = self.get_gdb_repr('print range(1000)')
Ezio Melotti2623a372010-11-21 13:34:58 +0000596 self.assertEqual(gdb_repr,
597 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
598 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
599 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
600 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
601 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
602 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
603 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
604 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
605 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
606 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
607 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
608 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
609 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
610 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
611 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
612 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
613 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
614 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
615 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
616 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
617 "224, 225, 226...(truncated)")
618 self.assertEqual(len(gdb_repr),
619 1024 + len('...(truncated)'))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000620
621 def test_builtin_function(self):
622 gdb_repr, gdb_output = self.get_gdb_repr('print len')
Ezio Melotti2623a372010-11-21 13:34:58 +0000623 self.assertEqual(gdb_repr, '<built-in function len>')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000624
625 def test_builtin_method(self):
626 gdb_repr, gdb_output = self.get_gdb_repr('import sys; print sys.stdout.readlines')
627 self.assertTrue(re.match('<built-in method readlines of file object at remote 0x[0-9a-f]+>',
628 gdb_repr),
629 'Unexpected gdb representation: %r\n%s' % \
630 (gdb_repr, gdb_output))
631
632 def test_frames(self):
633 gdb_output = self.get_stack_trace('''
634def foo(a, b, c):
635 pass
636
637foo(3, 4, 5)
638print foo.__code__''',
639 breakpoint='PyObject_Print',
640 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)op)->co_zombieframe)']
641 )
R. David Murray0c080092010-04-05 16:28:49 +0000642 self.assertTrue(re.match(r'.*\s+\$1 =\s+Frame 0x[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*',
643 gdb_output,
644 re.DOTALL),
645 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000646
Victor Stinner99cff3f2011-12-19 13:59:58 +0100647@unittest.skipIf(python_is_optimized(),
648 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000649class PyListTests(DebuggerTests):
650 def assertListing(self, expected, actual):
651 self.assertEndsWith(actual, expected)
652
653 def test_basic_command(self):
654 'Verify that the "py-list" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000655 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000656 cmds_after_breakpoint=['py-list'])
657
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000658 self.assertListing(' 5 \n'
659 ' 6 def bar(a, b, c):\n'
660 ' 7 baz(a, b, c)\n'
661 ' 8 \n'
662 ' 9 def baz(*args):\n'
663 ' >10 print(42)\n'
664 ' 11 \n'
665 ' 12 foo(1, 2, 3)\n',
666 bt)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000667
668 def test_one_abs_arg(self):
669 'Verify the "py-list" command with one absolute argument'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000670 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000671 cmds_after_breakpoint=['py-list 9'])
672
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000673 self.assertListing(' 9 def baz(*args):\n'
674 ' >10 print(42)\n'
675 ' 11 \n'
676 ' 12 foo(1, 2, 3)\n',
677 bt)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000678
679 def test_two_abs_args(self):
680 'Verify the "py-list" command with two absolute arguments'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000681 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000682 cmds_after_breakpoint=['py-list 1,3'])
683
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000684 self.assertListing(' 1 # Sample script for use by test_gdb.py\n'
685 ' 2 \n'
686 ' 3 def foo(a, b, c):\n',
687 bt)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000688
689class StackNavigationTests(DebuggerTests):
Victor Stinnera92e81b2010-04-20 22:28:31 +0000690 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinner99cff3f2011-12-19 13:59:58 +0100691 @unittest.skipIf(python_is_optimized(),
692 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000693 def test_pyup_command(self):
694 'Verify that the "py-up" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000695 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000696 cmds_after_breakpoint=['py-up'])
697 self.assertMultilineMatches(bt,
698 r'''^.*
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000699#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000700 baz\(a, b, c\)
701$''')
702
Victor Stinnera92e81b2010-04-20 22:28:31 +0000703 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000704 def test_down_at_bottom(self):
705 'Verify handling of "py-down" at the bottom of the stack'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000706 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000707 cmds_after_breakpoint=['py-down'])
708 self.assertEndsWith(bt,
709 'Unable to find a newer python frame\n')
710
Victor Stinnera92e81b2010-04-20 22:28:31 +0000711 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000712 def test_up_at_top(self):
713 'Verify handling of "py-up" at the top of the stack'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000714 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000715 cmds_after_breakpoint=['py-up'] * 4)
716 self.assertEndsWith(bt,
717 'Unable to find an older python frame\n')
718
Victor Stinnera92e81b2010-04-20 22:28:31 +0000719 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinner99cff3f2011-12-19 13:59:58 +0100720 @unittest.skipIf(python_is_optimized(),
721 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000722 def test_up_then_down(self):
723 'Verify "py-up" followed by "py-down"'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000724 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000725 cmds_after_breakpoint=['py-up', 'py-down'])
726 self.assertMultilineMatches(bt,
727 r'''^.*
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000728#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000729 baz\(a, b, c\)
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000730#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000731 print\(42\)
732$''')
733
734class PyBtTests(DebuggerTests):
Victor Stinner99cff3f2011-12-19 13:59:58 +0100735 @unittest.skipIf(python_is_optimized(),
736 "Python was compiled with optimizations")
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200737 def test_bt(self):
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000738 'Verify that the "py-bt" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000739 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000740 cmds_after_breakpoint=['py-bt'])
741 self.assertMultilineMatches(bt,
742 r'''^.*
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200743Traceback \(most recent call first\):
744 File ".*gdb_sample.py", line 10, in baz
745 print\(42\)
746 File ".*gdb_sample.py", line 7, in bar
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000747 baz\(a, b, c\)
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200748 File ".*gdb_sample.py", line 4, in foo
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000749 bar\(a, b, c\)
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200750 File ".*gdb_sample.py", line 12, in <module>
Victor Stinner99cff3f2011-12-19 13:59:58 +0100751 foo\(1, 2, 3\)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000752''')
753
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200754 @unittest.skipIf(python_is_optimized(),
755 "Python was compiled with optimizations")
756 def test_bt_full(self):
757 'Verify that the "py-bt-full" command works'
758 bt = self.get_stack_trace(script=self.get_sample_script(),
759 cmds_after_breakpoint=['py-bt-full'])
760 self.assertMultilineMatches(bt,
761 r'''^.*
762#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
763 baz\(a, b, c\)
764#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
765 bar\(a, b, c\)
766#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
767 foo\(1, 2, 3\)
768''')
769
770 @unittest.skipUnless(thread,
771 "Python was compiled without thread support")
772 def test_threads(self):
773 'Verify that "py-bt" indicates threads that are waiting for the GIL'
774 cmd = '''
775from threading import Thread
776
777class TestThread(Thread):
778 # These threads would run forever, but we'll interrupt things with the
779 # debugger
780 def run(self):
781 i = 0
782 while 1:
783 i += 1
784
785t = {}
786for i in range(4):
787 t[i] = TestThread()
788 t[i].start()
789
790# Trigger a breakpoint on the main thread
791print 42
792
793'''
794 # Verify with "py-bt":
795 gdb_output = self.get_stack_trace(cmd,
796 cmds_after_breakpoint=['thread apply all py-bt'])
797 self.assertIn('Waiting for the GIL', gdb_output)
798
799 # Verify with "py-bt-full":
800 gdb_output = self.get_stack_trace(cmd,
801 cmds_after_breakpoint=['thread apply all py-bt-full'])
802 self.assertIn('Waiting for the GIL', gdb_output)
803
804 @unittest.skipIf(python_is_optimized(),
805 "Python was compiled with optimizations")
806 # Some older versions of gdb will fail with
807 # "Cannot find new threads: generic error"
808 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
809 @unittest.skipUnless(thread,
810 "Python was compiled without thread support")
811 def test_gc(self):
812 'Verify that "py-bt" indicates if a thread is garbage-collecting'
813 cmd = ('from gc import collect\n'
814 'print 42\n'
815 'def foo():\n'
816 ' collect()\n'
817 'def bar():\n'
818 ' foo()\n'
819 'bar()\n')
820 # Verify with "py-bt":
821 gdb_output = self.get_stack_trace(cmd,
822 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
823 )
824 self.assertIn('Garbage-collecting', gdb_output)
825
826 # Verify with "py-bt-full":
827 gdb_output = self.get_stack_trace(cmd,
828 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
829 )
830 self.assertIn('Garbage-collecting', gdb_output)
831
832 @unittest.skipIf(python_is_optimized(),
833 "Python was compiled with optimizations")
834 # Some older versions of gdb will fail with
835 # "Cannot find new threads: generic error"
836 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
837 @unittest.skipUnless(thread,
838 "Python was compiled without thread support")
839 def test_pycfunction(self):
840 'Verify that "py-bt" displays invocations of PyCFunction instances'
841 # Tested function must not be defined with METH_NOARGS or METH_O,
842 # otherwise call_function() doesn't call PyCFunction_Call()
843 cmd = ('from time import gmtime\n'
844 'def foo():\n'
845 ' gmtime(1)\n'
846 'def bar():\n'
847 ' foo()\n'
848 'bar()\n')
849 # Verify with "py-bt":
850 gdb_output = self.get_stack_trace(cmd,
851 breakpoint='time_gmtime',
852 cmds_after_breakpoint=['bt', 'py-bt'],
853 )
854 self.assertIn('<built-in function gmtime', gdb_output)
855
856 # Verify with "py-bt-full":
857 gdb_output = self.get_stack_trace(cmd,
858 breakpoint='time_gmtime',
859 cmds_after_breakpoint=['py-bt-full'],
860 )
861 self.assertIn('#0 <built-in function gmtime', gdb_output)
862
863
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000864class PyPrintTests(DebuggerTests):
Victor Stinner99cff3f2011-12-19 13:59:58 +0100865 @unittest.skipIf(python_is_optimized(),
866 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000867 def test_basic_command(self):
868 'Verify that the "py-print" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000869 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000870 cmds_after_breakpoint=['py-print args'])
871 self.assertMultilineMatches(bt,
872 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
873
Victor Stinnera92e81b2010-04-20 22:28:31 +0000874 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinner99cff3f2011-12-19 13:59:58 +0100875 @unittest.skipIf(python_is_optimized(),
876 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000877 def test_print_after_up(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000878 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000879 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
880 self.assertMultilineMatches(bt,
881 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
882
Victor Stinner99cff3f2011-12-19 13:59:58 +0100883 @unittest.skipIf(python_is_optimized(),
884 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000885 def test_printing_global(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000886 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000887 cmds_after_breakpoint=['py-print __name__'])
888 self.assertMultilineMatches(bt,
889 r".*\nglobal '__name__' = '__main__'\n.*")
890
Victor Stinner99cff3f2011-12-19 13:59:58 +0100891 @unittest.skipIf(python_is_optimized(),
892 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000893 def test_printing_builtin(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000894 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000895 cmds_after_breakpoint=['py-print len'])
896 self.assertMultilineMatches(bt,
897 r".*\nbuiltin 'len' = <built-in function len>\n.*")
898
899class PyLocalsTests(DebuggerTests):
Victor Stinner99cff3f2011-12-19 13:59:58 +0100900 @unittest.skipIf(python_is_optimized(),
901 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000902 def test_basic_command(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000903 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000904 cmds_after_breakpoint=['py-locals'])
905 self.assertMultilineMatches(bt,
906 r".*\nargs = \(1, 2, 3\)\n.*")
907
Victor Stinnera92e81b2010-04-20 22:28:31 +0000908 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinner99cff3f2011-12-19 13:59:58 +0100909 @unittest.skipIf(python_is_optimized(),
910 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000911 def test_locals_after_up(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000912 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000913 cmds_after_breakpoint=['py-up', 'py-locals'])
914 self.assertMultilineMatches(bt,
915 r".*\na = 1\nb = 2\nc = 3\n.*")
916
917def test_main():
Victor Stinner3c5ce402015-09-03 09:51:59 +0200918 if test_support.verbose:
919 print("GDB version %s.%s:" % (gdb_major_version, gdb_minor_version))
920 for line in gdb_version.splitlines():
921 print(" " * 4 + line)
Martin v. Löwis5a965432010-04-12 05:22:25 +0000922 run_unittest(PrettyPrintTests,
923 PyListTests,
924 StackNavigationTests,
925 PyBtTests,
926 PyPrintTests,
927 PyLocalsTests
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000928 )
929
930if __name__ == "__main__":
931 test_main()