blob: b01efcb6069451a474e3ed6c3962761f0cb2ca3e [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
11
Martin v. Löwis24f09fd2010-04-17 22:40:40 +000012from test.test_support import run_unittest, findfile
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +000013
14try:
15 gdb_version, _ = subprocess.Popen(["gdb", "--version"],
16 stdout=subprocess.PIPE).communicate()
17except OSError:
18 # This is what "no gdb" looks like. There may, however, be other
19 # errors that manifest this way too.
20 raise unittest.SkipTest("Couldn't find gdb on the path")
21gdb_version_number = re.search(r"^GNU gdb [^\d]*(\d+)\.", gdb_version)
22if int(gdb_version_number.group(1)) < 7:
23 raise unittest.SkipTest("gdb versions before 7.0 didn't support python embedding"
24 " Saw:\n" + gdb_version)
25
26# Verify that "gdb" was built with the embedded python support enabled:
27cmd = "--eval-command=python import sys; print sys.version_info"
28p = subprocess.Popen(["gdb", "--batch", cmd],
29 stdout=subprocess.PIPE)
30gdbpy_version, _ = p.communicate()
31if gdbpy_version == '':
32 raise unittest.SkipTest("gdb not built with embedded python support")
33
34
35class DebuggerTests(unittest.TestCase):
36
37 """Test that the debugger can debug Python."""
38
39 def run_gdb(self, *args):
40 """Runs gdb with the command line given by *args.
41
42 Returns its stdout, stderr
43 """
44 out, err = subprocess.Popen(
45 args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
46 ).communicate()
47 return out, err
48
49 def get_stack_trace(self, source=None, script=None,
50 breakpoint='PyObject_Print',
51 cmds_after_breakpoint=None,
52 import_site=False):
53 '''
54 Run 'python -c SOURCE' under gdb with a breakpoint.
55
56 Support injecting commands after the breakpoint is reached
57
58 Returns the stdout from gdb
59
60 cmds_after_breakpoint: if provided, a list of strings: gdb commands
61 '''
62 # We use "set breakpoint pending yes" to avoid blocking with a:
63 # Function "foo" not defined.
64 # Make breakpoint pending on future shared library load? (y or [n])
65 # error, which typically happens python is dynamically linked (the
66 # breakpoints of interest are to be found in the shared library)
67 # When this happens, we still get:
68 # Function "PyObject_Print" not defined.
69 # emitted to stderr each time, alas.
70
71 # Initially I had "--eval-command=continue" here, but removed it to
72 # avoid repeated print breakpoints when traversing hierarchical data
73 # structures
74
75 # Generate a list of commands in gdb's language:
76 commands = ['set breakpoint pending yes',
77 'break %s' % breakpoint,
78 'run']
79 if cmds_after_breakpoint:
80 commands += cmds_after_breakpoint
81 else:
82 commands += ['backtrace']
83
84 # print commands
85
86 # Use "commands" to generate the arguments with which to invoke "gdb":
87 args = ["gdb", "--batch"]
88 args += ['--eval-command=%s' % cmd for cmd in commands]
89 args += ["--args",
90 sys.executable]
91
92 if not import_site:
93 # -S suppresses the default 'import site'
94 args += ["-S"]
95
96 if source:
97 args += ["-c", source]
98 elif script:
99 args += [script]
100
101 # print args
102 # print ' '.join(args)
103
104 # Use "args" to invoke gdb, capturing stdout, stderr:
105 out, err = self.run_gdb(*args)
106
107 # Ignore some noise on stderr due to the pending breakpoint:
108 err = err.replace('Function "%s" not defined.\n' % breakpoint, '')
109
110 # Ensure no unexpected error messages:
111 self.assertEquals(err, '')
112
113 return out
114
115 def get_gdb_repr(self, source,
116 cmds_after_breakpoint=None,
117 import_site=False):
118 # Given an input python source representation of data,
119 # run "python -c'print DATA'" under gdb with a breakpoint on
120 # PyObject_Print and scrape out gdb's representation of the "op"
121 # parameter, and verify that the gdb displays the same string
122 #
123 # For a nested structure, the first time we hit the breakpoint will
124 # give us the top-level structure
125 gdb_output = self.get_stack_trace(source, breakpoint='PyObject_Print',
126 cmds_after_breakpoint=cmds_after_breakpoint,
127 import_site=import_site)
R. David Murray0c080092010-04-05 16:28:49 +0000128 # gdb can insert additional '\n' and space characters in various places
129 # in its output, depending on the width of the terminal it's connected
130 # to (using its "wrap_here" function)
131 m = re.match('.*#0\s+PyObject_Print\s+\(\s*op\=\s*(.*?),\s+fp=.*\).*',
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000132 gdb_output, re.DOTALL)
R. David Murray0c080092010-04-05 16:28:49 +0000133 if not m:
134 self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000135 return m.group(1), gdb_output
136
137 def assertEndsWith(self, actual, exp_end):
138 '''Ensure that the given "actual" string ends with "exp_end"'''
139 self.assert_(actual.endswith(exp_end),
140 msg='%r did not end with %r' % (actual, exp_end))
141
142 def assertMultilineMatches(self, actual, pattern):
143 m = re.match(pattern, actual, re.DOTALL)
144 self.assert_(m,
145 msg='%r did not match %r' % (actual, pattern))
146
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000147 def get_sample_script(self):
148 return findfile('gdb_sample.py')
149
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000150class PrettyPrintTests(DebuggerTests):
151 def test_getting_backtrace(self):
152 gdb_output = self.get_stack_trace('print 42')
153 self.assertTrue('PyObject_Print' in gdb_output)
154
155 def assertGdbRepr(self, val, cmds_after_breakpoint=None):
156 # Ensure that gdb's rendering of the value in a debugged process
157 # matches repr(value) in this process:
158 gdb_repr, gdb_output = self.get_gdb_repr('print ' + repr(val),
159 cmds_after_breakpoint)
160 self.assertEquals(gdb_repr, repr(val), gdb_output)
161
162 def test_int(self):
163 'Verify the pretty-printing of various "int" values'
164 self.assertGdbRepr(42)
165 self.assertGdbRepr(0)
166 self.assertGdbRepr(-7)
167 self.assertGdbRepr(sys.maxint)
168 self.assertGdbRepr(-sys.maxint)
169
170 def test_long(self):
171 'Verify the pretty-printing of various "long" values'
172 self.assertGdbRepr(0L)
173 self.assertGdbRepr(1000000000000L)
174 self.assertGdbRepr(-1L)
175 self.assertGdbRepr(-1000000000000000L)
176
177 def test_singletons(self):
178 'Verify the pretty-printing of True, False and None'
179 self.assertGdbRepr(True)
180 self.assertGdbRepr(False)
181 self.assertGdbRepr(None)
182
183 def test_dicts(self):
184 'Verify the pretty-printing of dictionaries'
185 self.assertGdbRepr({})
186 self.assertGdbRepr({'foo': 'bar'})
187 self.assertGdbRepr({'foo': 'bar', 'douglas':42})
188
189 def test_lists(self):
190 'Verify the pretty-printing of lists'
191 self.assertGdbRepr([])
192 self.assertGdbRepr(range(5))
193
194 def test_strings(self):
195 'Verify the pretty-printing of strings'
196 self.assertGdbRepr('')
197 self.assertGdbRepr('And now for something hopefully the same')
198 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
199 self.assertGdbRepr('this is byte 255:\xff and byte 128:\x80')
200
201 def test_tuples(self):
202 'Verify the pretty-printing of tuples'
203 self.assertGdbRepr(tuple())
204 self.assertGdbRepr((1,))
205 self.assertGdbRepr(('foo', 'bar', 'baz'))
206
207 def test_unicode(self):
208 'Verify the pretty-printing of unicode values'
209 # Test the empty unicode string:
210 self.assertGdbRepr(u'')
211
212 self.assertGdbRepr(u'hello world')
213
214 # Test printing a single character:
215 # U+2620 SKULL AND CROSSBONES
216 self.assertGdbRepr(u'\u2620')
217
218 # Test printing a Japanese unicode string
219 # (I believe this reads "mojibake", using 3 characters from the CJK
220 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
221 self.assertGdbRepr(u'\u6587\u5b57\u5316\u3051')
222
223 # Test a character outside the BMP:
224 # U+1D121 MUSICAL SYMBOL C CLEF
225 # This is:
226 # UTF-8: 0xF0 0x9D 0x84 0xA1
227 # UTF-16: 0xD834 0xDD21
228 try:
229 # This will only work on wide-unicode builds:
230 self.assertGdbRepr(unichr(0x1D121))
231 except ValueError, e:
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000232 # We're probably on a narrow-unicode build; if we're seeing a
233 # different problem, then re-raise it:
234 if e.args != ('unichr() arg not in range(0x10000) (narrow Python build)',):
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000235 raise e
236
237 def test_sets(self):
238 'Verify the pretty-printing of sets'
239 self.assertGdbRepr(set())
240 self.assertGdbRepr(set(['a', 'b']))
241 self.assertGdbRepr(set([4, 5, 6]))
242
243 # Ensure that we handled sets containing the "dummy" key value,
244 # which happens on deletion:
245 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
246s.pop()
247print s''')
248 self.assertEquals(gdb_repr, "set(['b'])")
249
250 def test_frozensets(self):
251 'Verify the pretty-printing of frozensets'
252 self.assertGdbRepr(frozenset())
253 self.assertGdbRepr(frozenset(['a', 'b']))
254 self.assertGdbRepr(frozenset([4, 5, 6]))
255
256 def test_exceptions(self):
257 # Test a RuntimeError
258 gdb_repr, gdb_output = self.get_gdb_repr('''
259try:
260 raise RuntimeError("I am an error")
261except RuntimeError, e:
262 print e
263''')
264 self.assertEquals(gdb_repr,
265 "exceptions.RuntimeError('I am an error',)")
266
267
268 # Test division by zero:
269 gdb_repr, gdb_output = self.get_gdb_repr('''
270try:
271 a = 1 / 0
272except ZeroDivisionError, e:
273 print e
274''')
275 self.assertEquals(gdb_repr,
276 "exceptions.ZeroDivisionError('integer division or modulo by zero',)")
277
278 def test_classic_class(self):
279 'Verify the pretty-printing of classic class instances'
280 gdb_repr, gdb_output = self.get_gdb_repr('''
281class Foo:
282 pass
283foo = Foo()
284foo.an_int = 42
285print foo''')
286 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
287 self.assertTrue(m,
288 msg='Unexpected classic-class rendering %r' % gdb_repr)
289
290 def test_modern_class(self):
291 'Verify the pretty-printing of new-style class instances'
292 gdb_repr, gdb_output = self.get_gdb_repr('''
293class Foo(object):
294 pass
295foo = Foo()
296foo.an_int = 42
297print foo''')
298 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
299 self.assertTrue(m,
300 msg='Unexpected new-style class rendering %r' % gdb_repr)
301
302 def test_subclassing_list(self):
303 'Verify the pretty-printing of an instance of a list subclass'
304 gdb_repr, gdb_output = self.get_gdb_repr('''
305class Foo(list):
306 pass
307foo = Foo()
308foo += [1, 2, 3]
309foo.an_int = 42
310print foo''')
311 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
312 self.assertTrue(m,
313 msg='Unexpected new-style class rendering %r' % gdb_repr)
314
315 def test_subclassing_tuple(self):
316 'Verify the pretty-printing of an instance of a tuple subclass'
317 # This should exercise the negative tp_dictoffset code in the
318 # new-style class support
319 gdb_repr, gdb_output = self.get_gdb_repr('''
320class Foo(tuple):
321 pass
322foo = Foo((1, 2, 3))
323foo.an_int = 42
324print foo''')
325 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
326 self.assertTrue(m,
327 msg='Unexpected new-style class rendering %r' % gdb_repr)
328
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000329 def assertSane(self, source, corruption, expvalue=None, exptype=None):
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000330 '''Run Python under gdb, corrupting variables in the inferior process
331 immediately before taking a backtrace.
332
333 Verify that the variable's representation is the expected failsafe
334 representation'''
335 if corruption:
336 cmds_after_breakpoint=[corruption, 'backtrace']
337 else:
338 cmds_after_breakpoint=['backtrace']
339
340 gdb_repr, gdb_output = \
341 self.get_gdb_repr(source,
342 cmds_after_breakpoint=cmds_after_breakpoint)
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000343
344 if expvalue:
345 if gdb_repr == repr(expvalue):
346 # gdb managed to print the value in spite of the corruption;
347 # this is good (see http://bugs.python.org/issue8330)
348 return
349
350 if exptype:
351 pattern = '<' + exptype + ' at remote 0x[0-9a-f]+>'
352 else:
353 # Match anything for the type name; 0xDEADBEEF could point to
354 # something arbitrary (see http://bugs.python.org/issue8330)
355 pattern = '<.* at remote 0x[0-9a-f]+>'
356
357 m = re.match(pattern, gdb_repr)
358 if not m:
359 self.fail('Unexpected gdb representation: %r\n%s' % \
360 (gdb_repr, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000361
362 def test_NULL_ptr(self):
363 'Ensure that a NULL PyObject* is handled gracefully'
364 gdb_repr, gdb_output = (
365 self.get_gdb_repr('print 42',
366 cmds_after_breakpoint=['set variable op=0',
R. David Murray0c080092010-04-05 16:28:49 +0000367 'backtrace'])
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000368 )
369
370 self.assertEquals(gdb_repr, '0x0')
371
372 def test_NULL_ob_type(self):
373 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
374 self.assertSane('print 42',
375 'set op->ob_type=0')
376
377 def test_corrupt_ob_type(self):
378 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
379 self.assertSane('print 42',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000380 'set op->ob_type=0xDEADBEEF',
381 expvalue=42)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000382
383 def test_corrupt_tp_flags(self):
384 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
385 self.assertSane('print 42',
386 'set op->ob_type->tp_flags=0x0',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000387 expvalue=42)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000388
389 def test_corrupt_tp_name(self):
390 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
391 self.assertSane('print 42',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000392 'set op->ob_type->tp_name=0xDEADBEEF',
393 expvalue=42)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000394
395 def test_NULL_instance_dict(self):
396 'Ensure that a PyInstanceObject with with a NULL in_dict is handled'
397 self.assertSane('''
398class Foo:
399 pass
400foo = Foo()
401foo.an_int = 42
402print foo''',
403 'set ((PyInstanceObject*)op)->in_dict = 0',
Martin v. Löwis7f7765c2010-04-12 05:18:16 +0000404 exptype='Foo')
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000405
406 def test_builtins_help(self):
407 'Ensure that the new-style class _Helper in site.py can be handled'
408 # (this was the issue causing tracebacks in
409 # http://bugs.python.org/issue8032#msg100537 )
410
411 gdb_repr, gdb_output = self.get_gdb_repr('print __builtins__.help', import_site=True)
412 m = re.match(r'<_Helper at remote 0x[0-9a-f]+>', gdb_repr)
413 self.assertTrue(m,
414 msg='Unexpected rendering %r' % gdb_repr)
415
416 def test_selfreferential_list(self):
417 '''Ensure that a reference loop involving a list doesn't lead proxyval
418 into an infinite loop:'''
419 gdb_repr, gdb_output = \
420 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; print a")
421
422 self.assertEquals(gdb_repr, '[3, 4, 5, [...]]')
423
424 gdb_repr, gdb_output = \
425 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; print a")
426
427 self.assertEquals(gdb_repr, '[3, 4, 5, [[...]]]')
428
429 def test_selfreferential_dict(self):
430 '''Ensure that a reference loop involving a dict doesn't lead proxyval
431 into an infinite loop:'''
432 gdb_repr, gdb_output = \
433 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; print a")
434
435 self.assertEquals(gdb_repr, "{'foo': {'bar': {...}}}")
436
437 def test_selfreferential_old_style_instance(self):
438 gdb_repr, gdb_output = \
439 self.get_gdb_repr('''
440class Foo:
441 pass
442foo = Foo()
443foo.an_attr = foo
444print foo''')
445 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
446 gdb_repr),
447 'Unexpected gdb representation: %r\n%s' % \
448 (gdb_repr, gdb_output))
449
450 def test_selfreferential_new_style_instance(self):
451 gdb_repr, gdb_output = \
452 self.get_gdb_repr('''
453class Foo(object):
454 pass
455foo = Foo()
456foo.an_attr = foo
457print foo''')
458 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
459 gdb_repr),
460 'Unexpected gdb representation: %r\n%s' % \
461 (gdb_repr, gdb_output))
462
463 gdb_repr, gdb_output = \
464 self.get_gdb_repr('''
465class Foo(object):
466 pass
467a = Foo()
468b = Foo()
469a.an_attr = b
470b.an_attr = a
471print a''')
472 self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>\) at remote 0x[0-9a-f]+>',
473 gdb_repr),
474 'Unexpected gdb representation: %r\n%s' % \
475 (gdb_repr, gdb_output))
476
477 def test_truncation(self):
478 'Verify that very long output is truncated'
479 gdb_repr, gdb_output = self.get_gdb_repr('print range(1000)')
480 self.assertEquals(gdb_repr,
R. David Murray0c080092010-04-05 16:28:49 +0000481 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000482 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
483 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
484 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
485 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
486 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
487 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
488 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
489 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
490 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
491 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
492 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
493 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
494 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
495 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
496 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
497 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
498 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
499 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
500 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
501 "224, 225, 226...(truncated)")
502 self.assertEquals(len(gdb_repr),
R. David Murray0c080092010-04-05 16:28:49 +0000503 1024 + len('...(truncated)'))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000504
505 def test_builtin_function(self):
506 gdb_repr, gdb_output = self.get_gdb_repr('print len')
507 self.assertEquals(gdb_repr, '<built-in function len>')
508
509 def test_builtin_method(self):
510 gdb_repr, gdb_output = self.get_gdb_repr('import sys; print sys.stdout.readlines')
511 self.assertTrue(re.match('<built-in method readlines of file object at remote 0x[0-9a-f]+>',
512 gdb_repr),
513 'Unexpected gdb representation: %r\n%s' % \
514 (gdb_repr, gdb_output))
515
516 def test_frames(self):
517 gdb_output = self.get_stack_trace('''
518def foo(a, b, c):
519 pass
520
521foo(3, 4, 5)
522print foo.__code__''',
523 breakpoint='PyObject_Print',
524 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)op)->co_zombieframe)']
525 )
R. David Murray0c080092010-04-05 16:28:49 +0000526 self.assertTrue(re.match(r'.*\s+\$1 =\s+Frame 0x[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*',
527 gdb_output,
528 re.DOTALL),
529 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000530
531class PyListTests(DebuggerTests):
532 def assertListing(self, expected, actual):
533 self.assertEndsWith(actual, expected)
534
535 def test_basic_command(self):
536 'Verify that the "py-list" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000537 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000538 cmds_after_breakpoint=['py-list'])
539
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000540 self.assertListing(' 5 \n'
541 ' 6 def bar(a, b, c):\n'
542 ' 7 baz(a, b, c)\n'
543 ' 8 \n'
544 ' 9 def baz(*args):\n'
545 ' >10 print(42)\n'
546 ' 11 \n'
547 ' 12 foo(1, 2, 3)\n',
548 bt)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000549
550 def test_one_abs_arg(self):
551 'Verify the "py-list" command with one absolute argument'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000552 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000553 cmds_after_breakpoint=['py-list 9'])
554
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000555 self.assertListing(' 9 def baz(*args):\n'
556 ' >10 print(42)\n'
557 ' 11 \n'
558 ' 12 foo(1, 2, 3)\n',
559 bt)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000560
561 def test_two_abs_args(self):
562 'Verify the "py-list" command with two absolute arguments'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000563 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000564 cmds_after_breakpoint=['py-list 1,3'])
565
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000566 self.assertListing(' 1 # Sample script for use by test_gdb.py\n'
567 ' 2 \n'
568 ' 3 def foo(a, b, c):\n',
569 bt)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000570
571class StackNavigationTests(DebuggerTests):
572 def test_pyup_command(self):
573 'Verify that the "py-up" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000574 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000575 cmds_after_breakpoint=['py-up'])
576 self.assertMultilineMatches(bt,
577 r'''^.*
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000578#[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 +0000579 baz\(a, b, c\)
580$''')
581
582 def test_down_at_bottom(self):
583 'Verify handling of "py-down" at the bottom of the stack'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000584 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000585 cmds_after_breakpoint=['py-down'])
586 self.assertEndsWith(bt,
587 'Unable to find a newer python frame\n')
588
589 def test_up_at_top(self):
590 'Verify handling of "py-up" at the top of the stack'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000591 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000592 cmds_after_breakpoint=['py-up'] * 4)
593 self.assertEndsWith(bt,
594 'Unable to find an older python frame\n')
595
596 def test_up_then_down(self):
597 'Verify "py-up" followed by "py-down"'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000598 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000599 cmds_after_breakpoint=['py-up', 'py-down'])
600 self.assertMultilineMatches(bt,
601 r'''^.*
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000602#[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 +0000603 baz\(a, b, c\)
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000604#[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 +0000605 print\(42\)
606$''')
607
608class PyBtTests(DebuggerTests):
609 def test_basic_command(self):
610 'Verify that the "py-bt" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000611 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000612 cmds_after_breakpoint=['py-bt'])
613 self.assertMultilineMatches(bt,
614 r'''^.*
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000615#[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 +0000616 baz\(a, b, c\)
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000617#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000618 bar\(a, b, c\)
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000619#[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000620foo\(1, 2, 3\)
621''')
622
623class PyPrintTests(DebuggerTests):
624 def test_basic_command(self):
625 'Verify that the "py-print" command works'
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000626 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000627 cmds_after_breakpoint=['py-print args'])
628 self.assertMultilineMatches(bt,
629 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
630
631 def test_print_after_up(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000632 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000633 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
634 self.assertMultilineMatches(bt,
635 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
636
637 def test_printing_global(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000638 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000639 cmds_after_breakpoint=['py-print __name__'])
640 self.assertMultilineMatches(bt,
641 r".*\nglobal '__name__' = '__main__'\n.*")
642
643 def test_printing_builtin(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000644 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000645 cmds_after_breakpoint=['py-print len'])
646 self.assertMultilineMatches(bt,
647 r".*\nbuiltin 'len' = <built-in function len>\n.*")
648
649class PyLocalsTests(DebuggerTests):
650 def test_basic_command(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000651 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000652 cmds_after_breakpoint=['py-locals'])
653 self.assertMultilineMatches(bt,
654 r".*\nargs = \(1, 2, 3\)\n.*")
655
656 def test_locals_after_up(self):
Martin v. Löwis24f09fd2010-04-17 22:40:40 +0000657 bt = self.get_stack_trace(script=self.get_sample_script(),
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000658 cmds_after_breakpoint=['py-up', 'py-locals'])
659 self.assertMultilineMatches(bt,
660 r".*\na = 1\nb = 2\nc = 3\n.*")
661
662def test_main():
Martin v. Löwis5a965432010-04-12 05:22:25 +0000663 run_unittest(PrettyPrintTests,
664 PyListTests,
665 StackNavigationTests,
666 PyBtTests,
667 PyPrintTests,
668 PyLocalsTests
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000669 )
670
671if __name__ == "__main__":
672 test_main()