blob: 8a56118f963e561644fcaad74cd991082c11c6a0 [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
Victor Stinnerdf11d7c2015-09-15 00:19:47 +020036 # 'GNU gdb 6.1.1 [FreeBSD]\n' -> 6.1
37 # 'GNU gdb (GDB) Fedora (7.5.1-37.fc18)\n' -> 7.5
38 match = re.search(r"^GNU gdb.*?\b(\d+)\.(\d+)", version)
Victor Stinner3c5ce402015-09-03 09:51:59 +020039 if match is None:
40 raise Exception("unable to parse GDB version: %r" % version)
41 return (version, int(match.group(1)), int(match.group(2)))
42
43gdb_version, gdb_major_version, gdb_minor_version = get_gdb_version()
R David Murray3e66f0d2012-10-27 13:47:49 -040044if gdb_major_version < 7:
Victor Stinner3c5ce402015-09-03 09:51:59 +020045 raise unittest.SkipTest("gdb versions before 7.0 didn't support python "
46 "embedding. Saw %s.%s:\n%s"
47 % (gdb_major_version, gdb_minor_version,
48 gdb_version))
49
Benjamin Peterson51f461f2014-11-23 22:34:04 -060050if sys.platform.startswith("sunos"):
Benjamin Peterson0636a4b2014-11-23 22:02:47 -060051 raise unittest.SkipTest("test doesn't work very well on Solaris")
52
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +000053
R David Murray3e66f0d2012-10-27 13:47:49 -040054# Location of custom hooks file in a repository checkout.
55checkout_hook_path = os.path.join(os.path.dirname(sys.executable),
56 'python-gdb.py')
57
58def run_gdb(*args, **env_vars):
Victor Stinner8bd34152014-08-16 14:31:02 +020059 """Runs gdb in batch mode with the additional arguments given by *args.
R David Murray3e66f0d2012-10-27 13:47:49 -040060
61 Returns its (stdout, stderr)
62 """
63 if env_vars:
64 env = os.environ.copy()
65 env.update(env_vars)
66 else:
67 env = None
Victor Stinner8bd34152014-08-16 14:31:02 +020068 # -nx: Do not execute commands from any .gdbinit initialization files
69 # (issue #22188)
70 base_cmd = ('gdb', '--batch', '-nx')
R David Murray3e66f0d2012-10-27 13:47:49 -040071 if (gdb_major_version, gdb_minor_version) >= (7, 4):
72 base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path)
73 out, err = subprocess.Popen(base_cmd + args,
74 stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
75 ).communicate()
76 return out, err
77
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +000078# Verify that "gdb" was built with the embedded python support enabled:
Antoine Pitrou358da5b2013-11-23 17:40:36 +010079gdbpy_version, _ = run_gdb("--eval-command=python import sys; print(sys.version_info)")
R David Murray3e66f0d2012-10-27 13:47:49 -040080if not gdbpy_version:
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +000081 raise unittest.SkipTest("gdb not built with embedded python support")
82
Nick Coghlan254a3772013-09-22 19:36:09 +100083# Verify that "gdb" can load our custom hooks, as OS security settings may
84# disallow this without a customised .gdbinit.
R David Murray3e66f0d2012-10-27 13:47:49 -040085cmd = ['--args', sys.executable]
86_, gdbpy_errors = run_gdb('--args', sys.executable)
87if "auto-loading has been declined" in gdbpy_errors:
88 msg = "gdb security settings prevent use of custom hooks: "
Nick Coghlan254a3772013-09-22 19:36:09 +100089 raise unittest.SkipTest(msg + gdbpy_errors.rstrip())
Nick Coghlana0933122012-06-17 19:03:39 +100090
Victor Stinner99cff3f2011-12-19 13:59:58 +010091def python_is_optimized():
92 cflags = sysconfig.get_config_vars()['PY_CFLAGS']
93 final_opt = ""
94 for opt in cflags.split():
95 if opt.startswith('-O'):
96 final_opt = opt
Victor Stinner582265f2015-03-27 15:44:13 +010097 return final_opt not in ('', '-O0', '-Og')
Victor Stinner99cff3f2011-12-19 13:59:58 +010098
Victor Stinnera92e81b2010-04-20 22:28:31 +000099def gdb_has_frame_select():
100 # Does this build of gdb have gdb.Frame.select ?
R David Murray3e66f0d2012-10-27 13:47:49 -0400101 stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))")
Victor Stinnera92e81b2010-04-20 22:28:31 +0000102 m = re.match(r'.*\[(.*)\].*', stdout)
103 if not m:
104 raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test")
105 gdb_frame_dir = m.group(1).split(', ')
106 return "'select'" in gdb_frame_dir
107
108HAS_PYUP_PYDOWN = gdb_has_frame_select()
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000109
110class DebuggerTests(unittest.TestCase):
111
112 """Test that the debugger can debug Python."""
113
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000114 def get_stack_trace(self, source=None, script=None,
115 breakpoint='PyObject_Print',
116 cmds_after_breakpoint=None,
117 import_site=False):
118 '''
119 Run 'python -c SOURCE' under gdb with a breakpoint.
120
121 Support injecting commands after the breakpoint is reached
122
123 Returns the stdout from gdb
124
125 cmds_after_breakpoint: if provided, a list of strings: gdb commands
126 '''
127 # We use "set breakpoint pending yes" to avoid blocking with a:
128 # Function "foo" not defined.
129 # Make breakpoint pending on future shared library load? (y or [n])
130 # error, which typically happens python is dynamically linked (the
131 # breakpoints of interest are to be found in the shared library)
132 # When this happens, we still get:
133 # Function "PyObject_Print" not defined.
134 # emitted to stderr each time, alas.
135
136 # Initially I had "--eval-command=continue" here, but removed it to
137 # avoid repeated print breakpoints when traversing hierarchical data
138 # structures
139
140 # Generate a list of commands in gdb's language:
141 commands = ['set breakpoint pending yes',
142 'break %s' % breakpoint,
Serhiy Storchaka73bcde22015-01-31 11:48:36 +0200143
Serhiy Storchaka73bcde22015-01-31 11:48:36 +0200144 # The tests assume that the first frame of printed
145 # backtrace will not contain program counter,
146 # that is however not guaranteed by gdb
147 # therefore we need to use 'set print address off' to
148 # make sure the counter is not there. For example:
149 # #0 in PyObject_Print ...
150 # is assumed, but sometimes this can be e.g.
151 # #0 0x00003fffb7dd1798 in PyObject_Print ...
152 'set print address off',
153
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000154 'run']
Serhiy Storchakadd8430f2015-02-06 08:36:14 +0200155
156 # GDB as of 7.4 onwards can distinguish between the
157 # value of a variable at entry vs current value:
158 # http://sourceware.org/gdb/onlinedocs/gdb/Variables.html
159 # which leads to the selftests failing with errors like this:
160 # AssertionError: 'v@entry=()' != '()'
161 # Disable this:
162 if (gdb_major_version, gdb_minor_version) >= (7, 4):
163 commands += ['set print entry-values no']
164
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000165 if cmds_after_breakpoint:
166 commands += cmds_after_breakpoint
167 else:
168 commands += ['backtrace']
169
170 # print commands
171
172 # Use "commands" to generate the arguments with which to invoke "gdb":
Victor Stinner8bd34152014-08-16 14:31:02 +0200173 args = ["gdb", "--batch", "-nx"]
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000174 args += ['--eval-command=%s' % cmd for cmd in commands]
175 args += ["--args",
176 sys.executable]
177
178 if not import_site:
179 # -S suppresses the default 'import site'
180 args += ["-S"]
181
182 if source:
183 args += ["-c", source]
184 elif script:
185 args += [script]
186
187 # print args
188 # print ' '.join(args)
189
190 # Use "args" to invoke gdb, capturing stdout, stderr:
R David Murray3e66f0d2012-10-27 13:47:49 -0400191 out, err = run_gdb(*args, PYTHONHASHSEED='0')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000192
Antoine Pitroub996e042013-05-01 00:15:44 +0200193 errlines = err.splitlines()
194 unexpected_errlines = []
195
196 # Ignore some benign messages on stderr.
197 ignore_patterns = (
198 'Function "%s" not defined.' % breakpoint,
199 "warning: no loadable sections found in added symbol-file"
200 " system-supplied DSO",
201 "warning: Unable to find libthread_db matching"
202 " inferior's thread library, thread debugging will"
203 " not be available.",
204 "warning: Cannot initialize thread debugging"
205 " library: Debugger service failed",
206 'warning: Could not load shared library symbols for '
207 'linux-vdso.so',
208 'warning: Could not load shared library symbols for '
209 'linux-gate.so',
Serhiy Storchakab6b48e62015-02-14 22:44:35 +0200210 'warning: Could not load shared library symbols for '
211 'linux-vdso64.so',
Antoine Pitroub996e042013-05-01 00:15:44 +0200212 'Do you need "set solib-search-path" or '
213 '"set sysroot"?',
Victor Stinner57b00ed2014-11-05 15:07:18 +0100214 'warning: Source file is more recent than executable.',
215 # Issue #19753: missing symbols on System Z
216 'Missing separate debuginfo for ',
217 'Try: zypper install -C ',
Antoine Pitroub996e042013-05-01 00:15:44 +0200218 )
219 for line in errlines:
220 if not line.startswith(ignore_patterns):
221 unexpected_errlines.append(line)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000222
223 # Ensure no unexpected error messages:
Antoine Pitroub996e042013-05-01 00:15:44 +0200224 self.assertEqual(unexpected_errlines, [])
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000225 return out
226
227 def get_gdb_repr(self, source,
228 cmds_after_breakpoint=None,
229 import_site=False):
230 # Given an input python source representation of data,
231 # run "python -c'print DATA'" under gdb with a breakpoint on
232 # PyObject_Print and scrape out gdb's representation of the "op"
233 # parameter, and verify that the gdb displays the same string
234 #
235 # For a nested structure, the first time we hit the breakpoint will
236 # give us the top-level structure
237 gdb_output = self.get_stack_trace(source, breakpoint='PyObject_Print',
238 cmds_after_breakpoint=cmds_after_breakpoint,
239 import_site=import_site)
R. David Murray0c080092010-04-05 16:28:49 +0000240 # gdb can insert additional '\n' and space characters in various places
241 # in its output, depending on the width of the terminal it's connected
242 # to (using its "wrap_here" function)
243 m = re.match('.*#0\s+PyObject_Print\s+\(\s*op\=\s*(.*?),\s+fp=.*\).*',
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000244 gdb_output, re.DOTALL)
R. David Murray0c080092010-04-05 16:28:49 +0000245 if not m:
246 self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000247 return m.group(1), gdb_output
248
249 def assertEndsWith(self, actual, exp_end):
250 '''Ensure that the given "actual" string ends with "exp_end"'''
Ezio Melotti2623a372010-11-21 13:34:58 +0000251 self.assertTrue(actual.endswith(exp_end),
252 msg='%r did not end with %r' % (actual, exp_end))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000253
254 def assertMultilineMatches(self, actual, pattern):
255 m = re.match(pattern, actual, re.DOTALL)
Ezio Melotti2623a372010-11-21 13:34:58 +0000256 self.assertTrue(m, msg='%r did not match %r' % (actual, pattern))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000257
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000258 def get_sample_script(self):
259 return findfile('gdb_sample.py')
260
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000261class PrettyPrintTests(DebuggerTests):
262 def test_getting_backtrace(self):
263 gdb_output = self.get_stack_trace('print 42')
264 self.assertTrue('PyObject_Print' in gdb_output)
265
266 def assertGdbRepr(self, val, cmds_after_breakpoint=None):
267 # Ensure that gdb's rendering of the value in a debugged process
268 # matches repr(value) in this process:
269 gdb_repr, gdb_output = self.get_gdb_repr('print ' + repr(val),
270 cmds_after_breakpoint)
Antoine Pitrou358da5b2013-11-23 17:40:36 +0100271 self.assertEqual(gdb_repr, repr(val))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000272
273 def test_int(self):
274 'Verify the pretty-printing of various "int" values'
275 self.assertGdbRepr(42)
276 self.assertGdbRepr(0)
277 self.assertGdbRepr(-7)
278 self.assertGdbRepr(sys.maxint)
279 self.assertGdbRepr(-sys.maxint)
280
281 def test_long(self):
282 'Verify the pretty-printing of various "long" values'
283 self.assertGdbRepr(0L)
284 self.assertGdbRepr(1000000000000L)
285 self.assertGdbRepr(-1L)
286 self.assertGdbRepr(-1000000000000000L)
287
288 def test_singletons(self):
289 'Verify the pretty-printing of True, False and None'
290 self.assertGdbRepr(True)
291 self.assertGdbRepr(False)
292 self.assertGdbRepr(None)
293
294 def test_dicts(self):
295 'Verify the pretty-printing of dictionaries'
296 self.assertGdbRepr({})
297 self.assertGdbRepr({'foo': 'bar'})
Benjamin Peterson11fa11b2012-02-20 21:55:32 -0500298 self.assertGdbRepr("{'foo': 'bar', 'douglas':42}")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000299
300 def test_lists(self):
301 'Verify the pretty-printing of lists'
302 self.assertGdbRepr([])
303 self.assertGdbRepr(range(5))
304
305 def test_strings(self):
306 'Verify the pretty-printing of strings'
307 self.assertGdbRepr('')
308 self.assertGdbRepr('And now for something hopefully the same')
309 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
310 self.assertGdbRepr('this is byte 255:\xff and byte 128:\x80')
311
312 def test_tuples(self):
313 'Verify the pretty-printing of tuples'
314 self.assertGdbRepr(tuple())
315 self.assertGdbRepr((1,))
316 self.assertGdbRepr(('foo', 'bar', 'baz'))
317
318 def test_unicode(self):
319 'Verify the pretty-printing of unicode values'
320 # Test the empty unicode string:
321 self.assertGdbRepr(u'')
322
323 self.assertGdbRepr(u'hello world')
324
325 # Test printing a single character:
326 # U+2620 SKULL AND CROSSBONES
327 self.assertGdbRepr(u'\u2620')
328
329 # Test printing a Japanese unicode string
330 # (I believe this reads "mojibake", using 3 characters from the CJK
331 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
332 self.assertGdbRepr(u'\u6587\u5b57\u5316\u3051')
333
334 # Test a character outside the BMP:
335 # U+1D121 MUSICAL SYMBOL C CLEF
336 # This is:
337 # UTF-8: 0xF0 0x9D 0x84 0xA1
338 # UTF-16: 0xD834 0xDD21
Victor Stinnerb1556c52010-05-20 11:29:45 +0000339 # This will only work on wide-unicode builds:
340 self.assertGdbRepr(u"\U0001D121")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000341
342 def test_sets(self):
343 'Verify the pretty-printing of sets'
344 self.assertGdbRepr(set())
Benjamin Petersone39ccef2012-02-21 09:07:40 -0500345 rep = self.get_gdb_repr("print set(['a', 'b'])")[0]
346 self.assertTrue(rep.startswith("set(["))
347 self.assertTrue(rep.endswith("])"))
348 self.assertEqual(eval(rep), {'a', 'b'})
349 rep = self.get_gdb_repr("print set([4, 5])")[0]
350 self.assertTrue(rep.startswith("set(["))
351 self.assertTrue(rep.endswith("])"))
352 self.assertEqual(eval(rep), {4, 5})
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000353
354 # Ensure that we handled sets containing the "dummy" key value,
355 # which happens on deletion:
356 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
357s.pop()
358print s''')
Ezio Melotti2623a372010-11-21 13:34:58 +0000359 self.assertEqual(gdb_repr, "set(['b'])")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000360
361 def test_frozensets(self):
362 'Verify the pretty-printing of frozensets'
363 self.assertGdbRepr(frozenset())
Benjamin Petersone39ccef2012-02-21 09:07:40 -0500364 rep = self.get_gdb_repr("print frozenset(['a', 'b'])")[0]
365 self.assertTrue(rep.startswith("frozenset(["))
366 self.assertTrue(rep.endswith("])"))
367 self.assertEqual(eval(rep), {'a', 'b'})
368 rep = self.get_gdb_repr("print frozenset([4, 5])")[0]
369 self.assertTrue(rep.startswith("frozenset(["))
370 self.assertTrue(rep.endswith("])"))
371 self.assertEqual(eval(rep), {4, 5})
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000372
373 def test_exceptions(self):
374 # Test a RuntimeError
375 gdb_repr, gdb_output = self.get_gdb_repr('''
376try:
377 raise RuntimeError("I am an error")
378except RuntimeError, e:
379 print e
380''')
Ezio Melotti2623a372010-11-21 13:34:58 +0000381 self.assertEqual(gdb_repr,
382 "exceptions.RuntimeError('I am an error',)")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000383
384
385 # Test division by zero:
386 gdb_repr, gdb_output = self.get_gdb_repr('''
387try:
388 a = 1 / 0
389except ZeroDivisionError, e:
390 print e
391''')
Ezio Melotti2623a372010-11-21 13:34:58 +0000392 self.assertEqual(gdb_repr,
393 "exceptions.ZeroDivisionError('integer division or modulo by zero',)")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000394
395 def test_classic_class(self):
396 'Verify the pretty-printing of classic class instances'
397 gdb_repr, gdb_output = self.get_gdb_repr('''
398class Foo:
399 pass
400foo = Foo()
401foo.an_int = 42
402print foo''')
403 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
404 self.assertTrue(m,
405 msg='Unexpected classic-class rendering %r' % gdb_repr)
406
407 def test_modern_class(self):
408 'Verify the pretty-printing of new-style class instances'
409 gdb_repr, gdb_output = self.get_gdb_repr('''
410class Foo(object):
411 pass
412foo = Foo()
413foo.an_int = 42
414print foo''')
415 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
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('''
422class Foo(list):
423 pass
424foo = Foo()
425foo += [1, 2, 3]
426foo.an_int = 42
427print foo''')
428 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
429 self.assertTrue(m,
430 msg='Unexpected new-style class rendering %r' % gdb_repr)
431
432 def test_subclassing_tuple(self):
433 'Verify the pretty-printing of an instance of a tuple subclass'
434 # This should exercise the negative tp_dictoffset code in the
435 # new-style class support
436 gdb_repr, gdb_output = self.get_gdb_repr('''
437class Foo(tuple):
438 pass
439foo = Foo((1, 2, 3))
440foo.an_int = 42
441print foo''')
442 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
443 self.assertTrue(m,
444 msg='Unexpected new-style class rendering %r' % gdb_repr)
445
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000446 def assertSane(self, source, corruption, expvalue=None, exptype=None):
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000447 '''Run Python under gdb, corrupting variables in the inferior process
448 immediately before taking a backtrace.
449
450 Verify that the variable's representation is the expected failsafe
451 representation'''
452 if corruption:
453 cmds_after_breakpoint=[corruption, 'backtrace']
454 else:
455 cmds_after_breakpoint=['backtrace']
456
457 gdb_repr, gdb_output = \
458 self.get_gdb_repr(source,
459 cmds_after_breakpoint=cmds_after_breakpoint)
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000460
461 if expvalue:
462 if gdb_repr == repr(expvalue):
463 # gdb managed to print the value in spite of the corruption;
464 # this is good (see http://bugs.python.org/issue8330)
465 return
466
467 if exptype:
468 pattern = '<' + exptype + ' at remote 0x[0-9a-f]+>'
469 else:
470 # Match anything for the type name; 0xDEADBEEF could point to
471 # something arbitrary (see http://bugs.python.org/issue8330)
472 pattern = '<.* at remote 0x[0-9a-f]+>'
473
474 m = re.match(pattern, gdb_repr)
475 if not m:
476 self.fail('Unexpected gdb representation: %r\n%s' % \
477 (gdb_repr, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000478
479 def test_NULL_ptr(self):
480 'Ensure that a NULL PyObject* is handled gracefully'
481 gdb_repr, gdb_output = (
482 self.get_gdb_repr('print 42',
483 cmds_after_breakpoint=['set variable op=0',
R. David Murray0c080092010-04-05 16:28:49 +0000484 'backtrace'])
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000485 )
486
Ezio Melotti2623a372010-11-21 13:34:58 +0000487 self.assertEqual(gdb_repr, '0x0')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000488
489 def test_NULL_ob_type(self):
490 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
491 self.assertSane('print 42',
492 'set op->ob_type=0')
493
494 def test_corrupt_ob_type(self):
495 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
496 self.assertSane('print 42',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000497 'set op->ob_type=0xDEADBEEF',
498 expvalue=42)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000499
500 def test_corrupt_tp_flags(self):
501 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
502 self.assertSane('print 42',
503 'set op->ob_type->tp_flags=0x0',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000504 expvalue=42)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000505
506 def test_corrupt_tp_name(self):
507 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
508 self.assertSane('print 42',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000509 'set op->ob_type->tp_name=0xDEADBEEF',
510 expvalue=42)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000511
512 def test_NULL_instance_dict(self):
513 'Ensure that a PyInstanceObject with with a NULL in_dict is handled'
514 self.assertSane('''
515class Foo:
516 pass
517foo = Foo()
518foo.an_int = 42
519print foo''',
520 'set ((PyInstanceObject*)op)->in_dict = 0',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000521 exptype='Foo')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000522
523 def test_builtins_help(self):
524 'Ensure that the new-style class _Helper in site.py can be handled'
525 # (this was the issue causing tracebacks in
526 # http://bugs.python.org/issue8032#msg100537 )
527
528 gdb_repr, gdb_output = self.get_gdb_repr('print __builtins__.help', import_site=True)
529 m = re.match(r'<_Helper at remote 0x[0-9a-f]+>', gdb_repr)
530 self.assertTrue(m,
531 msg='Unexpected rendering %r' % gdb_repr)
532
533 def test_selfreferential_list(self):
534 '''Ensure that a reference loop involving a list doesn't lead proxyval
535 into an infinite loop:'''
536 gdb_repr, gdb_output = \
537 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; print a")
538
Ezio Melotti2623a372010-11-21 13:34:58 +0000539 self.assertEqual(gdb_repr, '[3, 4, 5, [...]]')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000540
541 gdb_repr, gdb_output = \
542 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; print a")
543
Ezio Melotti2623a372010-11-21 13:34:58 +0000544 self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000545
546 def test_selfreferential_dict(self):
547 '''Ensure that a reference loop involving a dict doesn't lead proxyval
548 into an infinite loop:'''
549 gdb_repr, gdb_output = \
550 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; print a")
551
Ezio Melotti2623a372010-11-21 13:34:58 +0000552 self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000553
554 def test_selfreferential_old_style_instance(self):
555 gdb_repr, gdb_output = \
556 self.get_gdb_repr('''
557class Foo:
558 pass
559foo = Foo()
560foo.an_attr = foo
561print foo''')
562 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
563 gdb_repr),
564 'Unexpected gdb representation: %r\n%s' % \
565 (gdb_repr, gdb_output))
566
567 def test_selfreferential_new_style_instance(self):
568 gdb_repr, gdb_output = \
569 self.get_gdb_repr('''
570class Foo(object):
571 pass
572foo = Foo()
573foo.an_attr = foo
574print foo''')
575 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
576 gdb_repr),
577 'Unexpected gdb representation: %r\n%s' % \
578 (gdb_repr, gdb_output))
579
580 gdb_repr, gdb_output = \
581 self.get_gdb_repr('''
582class Foo(object):
583 pass
584a = Foo()
585b = Foo()
586a.an_attr = b
587b.an_attr = a
588print a''')
589 self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>\) at remote 0x[0-9a-f]+>',
590 gdb_repr),
591 'Unexpected gdb representation: %r\n%s' % \
592 (gdb_repr, gdb_output))
593
594 def test_truncation(self):
595 'Verify that very long output is truncated'
596 gdb_repr, gdb_output = self.get_gdb_repr('print range(1000)')
Ezio Melotti2623a372010-11-21 13:34:58 +0000597 self.assertEqual(gdb_repr,
598 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
599 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
600 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
601 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
602 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
603 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
604 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
605 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
606 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
607 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
608 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
609 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
610 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
611 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
612 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
613 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
614 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
615 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
616 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
617 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
618 "224, 225, 226...(truncated)")
619 self.assertEqual(len(gdb_repr),
620 1024 + len('...(truncated)'))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000621
622 def test_builtin_function(self):
623 gdb_repr, gdb_output = self.get_gdb_repr('print len')
Ezio Melotti2623a372010-11-21 13:34:58 +0000624 self.assertEqual(gdb_repr, '<built-in function len>')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000625
626 def test_builtin_method(self):
627 gdb_repr, gdb_output = self.get_gdb_repr('import sys; print sys.stdout.readlines')
628 self.assertTrue(re.match('<built-in method readlines of file object at remote 0x[0-9a-f]+>',
629 gdb_repr),
630 'Unexpected gdb representation: %r\n%s' % \
631 (gdb_repr, gdb_output))
632
633 def test_frames(self):
634 gdb_output = self.get_stack_trace('''
635def foo(a, b, c):
636 pass
637
638foo(3, 4, 5)
639print foo.__code__''',
640 breakpoint='PyObject_Print',
641 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)op)->co_zombieframe)']
642 )
R. David Murray0c080092010-04-05 16:28:49 +0000643 self.assertTrue(re.match(r'.*\s+\$1 =\s+Frame 0x[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*',
644 gdb_output,
645 re.DOTALL),
646 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000647
Victor Stinner99cff3f2011-12-19 13:59:58 +0100648@unittest.skipIf(python_is_optimized(),
649 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000650class PyListTests(DebuggerTests):
651 def assertListing(self, expected, actual):
652 self.assertEndsWith(actual, expected)
653
654 def test_basic_command(self):
655 'Verify that the "py-list" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000656 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000657 cmds_after_breakpoint=['py-list'])
658
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000659 self.assertListing(' 5 \n'
660 ' 6 def bar(a, b, c):\n'
661 ' 7 baz(a, b, c)\n'
662 ' 8 \n'
663 ' 9 def baz(*args):\n'
664 ' >10 print(42)\n'
665 ' 11 \n'
666 ' 12 foo(1, 2, 3)\n',
667 bt)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000668
669 def test_one_abs_arg(self):
670 'Verify the "py-list" command with one absolute argument'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000671 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000672 cmds_after_breakpoint=['py-list 9'])
673
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000674 self.assertListing(' 9 def baz(*args):\n'
675 ' >10 print(42)\n'
676 ' 11 \n'
677 ' 12 foo(1, 2, 3)\n',
678 bt)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000679
680 def test_two_abs_args(self):
681 'Verify the "py-list" command with two absolute arguments'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000682 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000683 cmds_after_breakpoint=['py-list 1,3'])
684
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000685 self.assertListing(' 1 # Sample script for use by test_gdb.py\n'
686 ' 2 \n'
687 ' 3 def foo(a, b, c):\n',
688 bt)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000689
690class StackNavigationTests(DebuggerTests):
Victor Stinnera92e81b2010-04-20 22:28:31 +0000691 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinner99cff3f2011-12-19 13:59:58 +0100692 @unittest.skipIf(python_is_optimized(),
693 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000694 def test_pyup_command(self):
695 'Verify that the "py-up" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000696 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000697 cmds_after_breakpoint=['py-up'])
698 self.assertMultilineMatches(bt,
699 r'''^.*
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000700#[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 +0000701 baz\(a, b, c\)
702$''')
703
Victor Stinnera92e81b2010-04-20 22:28:31 +0000704 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000705 def test_down_at_bottom(self):
706 'Verify handling of "py-down" at the bottom of the stack'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000707 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000708 cmds_after_breakpoint=['py-down'])
709 self.assertEndsWith(bt,
710 'Unable to find a newer python frame\n')
711
Victor Stinnera92e81b2010-04-20 22:28:31 +0000712 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000713 def test_up_at_top(self):
714 'Verify handling of "py-up" at the top of the stack'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000715 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000716 cmds_after_breakpoint=['py-up'] * 4)
717 self.assertEndsWith(bt,
718 'Unable to find an older python frame\n')
719
Victor Stinnera92e81b2010-04-20 22:28:31 +0000720 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinner99cff3f2011-12-19 13:59:58 +0100721 @unittest.skipIf(python_is_optimized(),
722 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000723 def test_up_then_down(self):
724 'Verify "py-up" followed by "py-down"'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000725 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000726 cmds_after_breakpoint=['py-up', 'py-down'])
727 self.assertMultilineMatches(bt,
728 r'''^.*
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000729#[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 +0000730 baz\(a, b, c\)
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000731#[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 +0000732 print\(42\)
733$''')
734
735class PyBtTests(DebuggerTests):
Victor Stinner99cff3f2011-12-19 13:59:58 +0100736 @unittest.skipIf(python_is_optimized(),
737 "Python was compiled with optimizations")
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200738 def test_bt(self):
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000739 'Verify that the "py-bt" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000740 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000741 cmds_after_breakpoint=['py-bt'])
742 self.assertMultilineMatches(bt,
743 r'''^.*
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200744Traceback \(most recent call first\):
745 File ".*gdb_sample.py", line 10, in baz
746 print\(42\)
747 File ".*gdb_sample.py", line 7, in bar
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000748 baz\(a, b, c\)
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200749 File ".*gdb_sample.py", line 4, in foo
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000750 bar\(a, b, c\)
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200751 File ".*gdb_sample.py", line 12, in <module>
Victor Stinner99cff3f2011-12-19 13:59:58 +0100752 foo\(1, 2, 3\)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000753''')
754
Victor Stinnercc1db4b2015-09-03 10:17:28 +0200755 @unittest.skipIf(python_is_optimized(),
756 "Python was compiled with optimizations")
757 def test_bt_full(self):
758 'Verify that the "py-bt-full" command works'
759 bt = self.get_stack_trace(script=self.get_sample_script(),
760 cmds_after_breakpoint=['py-bt-full'])
761 self.assertMultilineMatches(bt,
762 r'''^.*
763#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
764 baz\(a, b, c\)
765#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
766 bar\(a, b, c\)
767#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
768 foo\(1, 2, 3\)
769''')
770
771 @unittest.skipUnless(thread,
772 "Python was compiled without thread support")
773 def test_threads(self):
774 'Verify that "py-bt" indicates threads that are waiting for the GIL'
775 cmd = '''
776from threading import Thread
777
778class TestThread(Thread):
779 # These threads would run forever, but we'll interrupt things with the
780 # debugger
781 def run(self):
782 i = 0
783 while 1:
784 i += 1
785
786t = {}
787for i in range(4):
788 t[i] = TestThread()
789 t[i].start()
790
791# Trigger a breakpoint on the main thread
792print 42
793
794'''
795 # Verify with "py-bt":
796 gdb_output = self.get_stack_trace(cmd,
797 cmds_after_breakpoint=['thread apply all py-bt'])
798 self.assertIn('Waiting for the GIL', gdb_output)
799
800 # Verify with "py-bt-full":
801 gdb_output = self.get_stack_trace(cmd,
802 cmds_after_breakpoint=['thread apply all py-bt-full'])
803 self.assertIn('Waiting for the GIL', gdb_output)
804
805 @unittest.skipIf(python_is_optimized(),
806 "Python was compiled with optimizations")
807 # Some older versions of gdb will fail with
808 # "Cannot find new threads: generic error"
809 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
810 @unittest.skipUnless(thread,
811 "Python was compiled without thread support")
812 def test_gc(self):
813 'Verify that "py-bt" indicates if a thread is garbage-collecting'
814 cmd = ('from gc import collect\n'
815 'print 42\n'
816 'def foo():\n'
817 ' collect()\n'
818 'def bar():\n'
819 ' foo()\n'
820 'bar()\n')
821 # Verify with "py-bt":
822 gdb_output = self.get_stack_trace(cmd,
823 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
824 )
825 self.assertIn('Garbage-collecting', gdb_output)
826
827 # Verify with "py-bt-full":
828 gdb_output = self.get_stack_trace(cmd,
829 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
830 )
831 self.assertIn('Garbage-collecting', gdb_output)
832
833 @unittest.skipIf(python_is_optimized(),
834 "Python was compiled with optimizations")
835 # Some older versions of gdb will fail with
836 # "Cannot find new threads: generic error"
837 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
838 @unittest.skipUnless(thread,
839 "Python was compiled without thread support")
840 def test_pycfunction(self):
841 'Verify that "py-bt" displays invocations of PyCFunction instances'
842 # Tested function must not be defined with METH_NOARGS or METH_O,
843 # otherwise call_function() doesn't call PyCFunction_Call()
844 cmd = ('from time import gmtime\n'
845 'def foo():\n'
846 ' gmtime(1)\n'
847 'def bar():\n'
848 ' foo()\n'
849 'bar()\n')
850 # Verify with "py-bt":
851 gdb_output = self.get_stack_trace(cmd,
852 breakpoint='time_gmtime',
853 cmds_after_breakpoint=['bt', 'py-bt'],
854 )
855 self.assertIn('<built-in function gmtime', gdb_output)
856
857 # Verify with "py-bt-full":
858 gdb_output = self.get_stack_trace(cmd,
859 breakpoint='time_gmtime',
860 cmds_after_breakpoint=['py-bt-full'],
861 )
862 self.assertIn('#0 <built-in function gmtime', gdb_output)
863
864
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000865class PyPrintTests(DebuggerTests):
Victor Stinner99cff3f2011-12-19 13:59:58 +0100866 @unittest.skipIf(python_is_optimized(),
867 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000868 def test_basic_command(self):
869 'Verify that the "py-print" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000870 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000871 cmds_after_breakpoint=['py-print args'])
872 self.assertMultilineMatches(bt,
873 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
874
Victor Stinnera92e81b2010-04-20 22:28:31 +0000875 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinner99cff3f2011-12-19 13:59:58 +0100876 @unittest.skipIf(python_is_optimized(),
877 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000878 def test_print_after_up(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000879 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000880 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
881 self.assertMultilineMatches(bt,
882 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
883
Victor Stinner99cff3f2011-12-19 13:59:58 +0100884 @unittest.skipIf(python_is_optimized(),
885 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000886 def test_printing_global(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000887 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000888 cmds_after_breakpoint=['py-print __name__'])
889 self.assertMultilineMatches(bt,
890 r".*\nglobal '__name__' = '__main__'\n.*")
891
Victor Stinner99cff3f2011-12-19 13:59:58 +0100892 @unittest.skipIf(python_is_optimized(),
893 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000894 def test_printing_builtin(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000895 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000896 cmds_after_breakpoint=['py-print len'])
897 self.assertMultilineMatches(bt,
898 r".*\nbuiltin 'len' = <built-in function len>\n.*")
899
900class PyLocalsTests(DebuggerTests):
Victor Stinner99cff3f2011-12-19 13:59:58 +0100901 @unittest.skipIf(python_is_optimized(),
902 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000903 def test_basic_command(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000904 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000905 cmds_after_breakpoint=['py-locals'])
906 self.assertMultilineMatches(bt,
907 r".*\nargs = \(1, 2, 3\)\n.*")
908
Victor Stinnera92e81b2010-04-20 22:28:31 +0000909 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinner99cff3f2011-12-19 13:59:58 +0100910 @unittest.skipIf(python_is_optimized(),
911 "Python was compiled with optimizations")
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000912 def test_locals_after_up(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000913 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000914 cmds_after_breakpoint=['py-up', 'py-locals'])
915 self.assertMultilineMatches(bt,
916 r".*\na = 1\nb = 2\nc = 3\n.*")
917
918def test_main():
Victor Stinner3c5ce402015-09-03 09:51:59 +0200919 if test_support.verbose:
920 print("GDB version %s.%s:" % (gdb_major_version, gdb_minor_version))
921 for line in gdb_version.splitlines():
922 print(" " * 4 + line)
Martin v. Löwis5a965432010-04-12 05:22:25 +0000923 run_unittest(PrettyPrintTests,
924 PyListTests,
925 StackNavigationTests,
926 PyBtTests,
927 PyPrintTests,
928 PyLocalsTests
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000929 )
930
931if __name__ == "__main__":
932 test_main()