blob: f2ab58add1a28a8be7544ef35b4a32ec201eebdf [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
12from test.test_support import run_unittest
13
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
147class PrettyPrintTests(DebuggerTests):
148 def test_getting_backtrace(self):
149 gdb_output = self.get_stack_trace('print 42')
150 self.assertTrue('PyObject_Print' in gdb_output)
151
152 def assertGdbRepr(self, val, cmds_after_breakpoint=None):
153 # Ensure that gdb's rendering of the value in a debugged process
154 # matches repr(value) in this process:
155 gdb_repr, gdb_output = self.get_gdb_repr('print ' + repr(val),
156 cmds_after_breakpoint)
157 self.assertEquals(gdb_repr, repr(val), gdb_output)
158
159 def test_int(self):
160 'Verify the pretty-printing of various "int" values'
161 self.assertGdbRepr(42)
162 self.assertGdbRepr(0)
163 self.assertGdbRepr(-7)
164 self.assertGdbRepr(sys.maxint)
165 self.assertGdbRepr(-sys.maxint)
166
167 def test_long(self):
168 'Verify the pretty-printing of various "long" values'
169 self.assertGdbRepr(0L)
170 self.assertGdbRepr(1000000000000L)
171 self.assertGdbRepr(-1L)
172 self.assertGdbRepr(-1000000000000000L)
173
174 def test_singletons(self):
175 'Verify the pretty-printing of True, False and None'
176 self.assertGdbRepr(True)
177 self.assertGdbRepr(False)
178 self.assertGdbRepr(None)
179
180 def test_dicts(self):
181 'Verify the pretty-printing of dictionaries'
182 self.assertGdbRepr({})
183 self.assertGdbRepr({'foo': 'bar'})
184 self.assertGdbRepr({'foo': 'bar', 'douglas':42})
185
186 def test_lists(self):
187 'Verify the pretty-printing of lists'
188 self.assertGdbRepr([])
189 self.assertGdbRepr(range(5))
190
191 def test_strings(self):
192 'Verify the pretty-printing of strings'
193 self.assertGdbRepr('')
194 self.assertGdbRepr('And now for something hopefully the same')
195 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
196 self.assertGdbRepr('this is byte 255:\xff and byte 128:\x80')
197
198 def test_tuples(self):
199 'Verify the pretty-printing of tuples'
200 self.assertGdbRepr(tuple())
201 self.assertGdbRepr((1,))
202 self.assertGdbRepr(('foo', 'bar', 'baz'))
203
204 def test_unicode(self):
205 'Verify the pretty-printing of unicode values'
206 # Test the empty unicode string:
207 self.assertGdbRepr(u'')
208
209 self.assertGdbRepr(u'hello world')
210
211 # Test printing a single character:
212 # U+2620 SKULL AND CROSSBONES
213 self.assertGdbRepr(u'\u2620')
214
215 # Test printing a Japanese unicode string
216 # (I believe this reads "mojibake", using 3 characters from the CJK
217 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
218 self.assertGdbRepr(u'\u6587\u5b57\u5316\u3051')
219
220 # Test a character outside the BMP:
221 # U+1D121 MUSICAL SYMBOL C CLEF
222 # This is:
223 # UTF-8: 0xF0 0x9D 0x84 0xA1
224 # UTF-16: 0xD834 0xDD21
225 try:
226 # This will only work on wide-unicode builds:
227 self.assertGdbRepr(unichr(0x1D121))
228 except ValueError, e:
229 if e.message != 'unichr() arg not in range(0x10000) (narrow Python build)':
230 raise e
231
232 def test_sets(self):
233 'Verify the pretty-printing of sets'
234 self.assertGdbRepr(set())
235 self.assertGdbRepr(set(['a', 'b']))
236 self.assertGdbRepr(set([4, 5, 6]))
237
238 # Ensure that we handled sets containing the "dummy" key value,
239 # which happens on deletion:
240 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
241s.pop()
242print s''')
243 self.assertEquals(gdb_repr, "set(['b'])")
244
245 def test_frozensets(self):
246 'Verify the pretty-printing of frozensets'
247 self.assertGdbRepr(frozenset())
248 self.assertGdbRepr(frozenset(['a', 'b']))
249 self.assertGdbRepr(frozenset([4, 5, 6]))
250
251 def test_exceptions(self):
252 # Test a RuntimeError
253 gdb_repr, gdb_output = self.get_gdb_repr('''
254try:
255 raise RuntimeError("I am an error")
256except RuntimeError, e:
257 print e
258''')
259 self.assertEquals(gdb_repr,
260 "exceptions.RuntimeError('I am an error',)")
261
262
263 # Test division by zero:
264 gdb_repr, gdb_output = self.get_gdb_repr('''
265try:
266 a = 1 / 0
267except ZeroDivisionError, e:
268 print e
269''')
270 self.assertEquals(gdb_repr,
271 "exceptions.ZeroDivisionError('integer division or modulo by zero',)")
272
273 def test_classic_class(self):
274 'Verify the pretty-printing of classic class instances'
275 gdb_repr, gdb_output = self.get_gdb_repr('''
276class Foo:
277 pass
278foo = Foo()
279foo.an_int = 42
280print foo''')
281 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
282 self.assertTrue(m,
283 msg='Unexpected classic-class rendering %r' % gdb_repr)
284
285 def test_modern_class(self):
286 'Verify the pretty-printing of new-style class instances'
287 gdb_repr, gdb_output = self.get_gdb_repr('''
288class Foo(object):
289 pass
290foo = Foo()
291foo.an_int = 42
292print foo''')
293 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
294 self.assertTrue(m,
295 msg='Unexpected new-style class rendering %r' % gdb_repr)
296
297 def test_subclassing_list(self):
298 'Verify the pretty-printing of an instance of a list subclass'
299 gdb_repr, gdb_output = self.get_gdb_repr('''
300class Foo(list):
301 pass
302foo = Foo()
303foo += [1, 2, 3]
304foo.an_int = 42
305print foo''')
306 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
307 self.assertTrue(m,
308 msg='Unexpected new-style class rendering %r' % gdb_repr)
309
310 def test_subclassing_tuple(self):
311 'Verify the pretty-printing of an instance of a tuple subclass'
312 # This should exercise the negative tp_dictoffset code in the
313 # new-style class support
314 gdb_repr, gdb_output = self.get_gdb_repr('''
315class Foo(tuple):
316 pass
317foo = Foo((1, 2, 3))
318foo.an_int = 42
319print foo''')
320 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
321 self.assertTrue(m,
322 msg='Unexpected new-style class rendering %r' % gdb_repr)
323
324 def assertSane(self, source, corruption, exp_type='unknown'):
325 '''Run Python under gdb, corrupting variables in the inferior process
326 immediately before taking a backtrace.
327
328 Verify that the variable's representation is the expected failsafe
329 representation'''
330 if corruption:
331 cmds_after_breakpoint=[corruption, 'backtrace']
332 else:
333 cmds_after_breakpoint=['backtrace']
334
335 gdb_repr, gdb_output = \
336 self.get_gdb_repr(source,
337 cmds_after_breakpoint=cmds_after_breakpoint)
338 self.assertTrue(re.match('<%s at remote 0x[0-9a-f]+>' % exp_type,
339 gdb_repr),
340 'Unexpected gdb representation: %r\n%s' % \
341 (gdb_repr, gdb_output))
342
343 def test_NULL_ptr(self):
344 'Ensure that a NULL PyObject* is handled gracefully'
345 gdb_repr, gdb_output = (
346 self.get_gdb_repr('print 42',
347 cmds_after_breakpoint=['set variable op=0',
R. David Murray0c080092010-04-05 16:28:49 +0000348 'backtrace'])
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000349 )
350
351 self.assertEquals(gdb_repr, '0x0')
352
353 def test_NULL_ob_type(self):
354 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
355 self.assertSane('print 42',
356 'set op->ob_type=0')
357
358 def test_corrupt_ob_type(self):
359 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
360 self.assertSane('print 42',
361 'set op->ob_type=0xDEADBEEF')
362
363 def test_corrupt_tp_flags(self):
364 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
365 self.assertSane('print 42',
366 'set op->ob_type->tp_flags=0x0',
367 exp_type='int')
368
369 def test_corrupt_tp_name(self):
370 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
371 self.assertSane('print 42',
372 'set op->ob_type->tp_name=0xDEADBEEF')
373
374 def test_NULL_instance_dict(self):
375 'Ensure that a PyInstanceObject with with a NULL in_dict is handled'
376 self.assertSane('''
377class Foo:
378 pass
379foo = Foo()
380foo.an_int = 42
381print foo''',
382 'set ((PyInstanceObject*)op)->in_dict = 0',
383 exp_type='Foo')
384
385 def test_builtins_help(self):
386 'Ensure that the new-style class _Helper in site.py can be handled'
387 # (this was the issue causing tracebacks in
388 # http://bugs.python.org/issue8032#msg100537 )
389
390 gdb_repr, gdb_output = self.get_gdb_repr('print __builtins__.help', import_site=True)
391 m = re.match(r'<_Helper at remote 0x[0-9a-f]+>', gdb_repr)
392 self.assertTrue(m,
393 msg='Unexpected rendering %r' % gdb_repr)
394
395 def test_selfreferential_list(self):
396 '''Ensure that a reference loop involving a list doesn't lead proxyval
397 into an infinite loop:'''
398 gdb_repr, gdb_output = \
399 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; print a")
400
401 self.assertEquals(gdb_repr, '[3, 4, 5, [...]]')
402
403 gdb_repr, gdb_output = \
404 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; print a")
405
406 self.assertEquals(gdb_repr, '[3, 4, 5, [[...]]]')
407
408 def test_selfreferential_dict(self):
409 '''Ensure that a reference loop involving a dict doesn't lead proxyval
410 into an infinite loop:'''
411 gdb_repr, gdb_output = \
412 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; print a")
413
414 self.assertEquals(gdb_repr, "{'foo': {'bar': {...}}}")
415
416 def test_selfreferential_old_style_instance(self):
417 gdb_repr, gdb_output = \
418 self.get_gdb_repr('''
419class Foo:
420 pass
421foo = Foo()
422foo.an_attr = foo
423print foo''')
424 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
425 gdb_repr),
426 'Unexpected gdb representation: %r\n%s' % \
427 (gdb_repr, gdb_output))
428
429 def test_selfreferential_new_style_instance(self):
430 gdb_repr, gdb_output = \
431 self.get_gdb_repr('''
432class Foo(object):
433 pass
434foo = Foo()
435foo.an_attr = foo
436print foo''')
437 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
438 gdb_repr),
439 'Unexpected gdb representation: %r\n%s' % \
440 (gdb_repr, gdb_output))
441
442 gdb_repr, gdb_output = \
443 self.get_gdb_repr('''
444class Foo(object):
445 pass
446a = Foo()
447b = Foo()
448a.an_attr = b
449b.an_attr = a
450print a''')
451 self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>\) at remote 0x[0-9a-f]+>',
452 gdb_repr),
453 'Unexpected gdb representation: %r\n%s' % \
454 (gdb_repr, gdb_output))
455
456 def test_truncation(self):
457 'Verify that very long output is truncated'
458 gdb_repr, gdb_output = self.get_gdb_repr('print range(1000)')
459 self.assertEquals(gdb_repr,
R. David Murray0c080092010-04-05 16:28:49 +0000460 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000461 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
462 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
463 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
464 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
465 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
466 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
467 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
468 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
469 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
470 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
471 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
472 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
473 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
474 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
475 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
476 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
477 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
478 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
479 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
480 "224, 225, 226...(truncated)")
481 self.assertEquals(len(gdb_repr),
R. David Murray0c080092010-04-05 16:28:49 +0000482 1024 + len('...(truncated)'))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000483
484 def test_builtin_function(self):
485 gdb_repr, gdb_output = self.get_gdb_repr('print len')
486 self.assertEquals(gdb_repr, '<built-in function len>')
487
488 def test_builtin_method(self):
489 gdb_repr, gdb_output = self.get_gdb_repr('import sys; print sys.stdout.readlines')
490 self.assertTrue(re.match('<built-in method readlines of file object at remote 0x[0-9a-f]+>',
491 gdb_repr),
492 'Unexpected gdb representation: %r\n%s' % \
493 (gdb_repr, gdb_output))
494
495 def test_frames(self):
496 gdb_output = self.get_stack_trace('''
497def foo(a, b, c):
498 pass
499
500foo(3, 4, 5)
501print foo.__code__''',
502 breakpoint='PyObject_Print',
503 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)op)->co_zombieframe)']
504 )
R. David Murray0c080092010-04-05 16:28:49 +0000505 self.assertTrue(re.match(r'.*\s+\$1 =\s+Frame 0x[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*',
506 gdb_output,
507 re.DOTALL),
508 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +0000509
510class PyListTests(DebuggerTests):
511 def assertListing(self, expected, actual):
512 self.assertEndsWith(actual, expected)
513
514 def test_basic_command(self):
515 'Verify that the "py-list" command works'
516 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
517 cmds_after_breakpoint=['py-list'])
518
519 self.assertListing('''
520 5
521 6 def bar(a, b, c):
522 7 baz(a, b, c)
523 8
524 9 def baz(*args):
525 >10 print(42)
526 11
527 12 foo(1, 2, 3)
528''',
529 bt)
530
531 def test_one_abs_arg(self):
532 'Verify the "py-list" command with one absolute argument'
533 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
534 cmds_after_breakpoint=['py-list 9'])
535
536 self.assertListing('''
537 9 def baz(*args):
538 >10 print(42)
539 11
540 12 foo(1, 2, 3)
541''',
542 bt)
543
544 def test_two_abs_args(self):
545 'Verify the "py-list" command with two absolute arguments'
546 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
547 cmds_after_breakpoint=['py-list 1,3'])
548
549 self.assertListing('''
550 1 # Sample script for use by test_gdb.py
551 2
552 3 def foo(a, b, c):
553''',
554 bt)
555
556class StackNavigationTests(DebuggerTests):
557 def test_pyup_command(self):
558 'Verify that the "py-up" command works'
559 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
560 cmds_after_breakpoint=['py-up'])
561 self.assertMultilineMatches(bt,
562 r'''^.*
563#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
564 baz\(a, b, c\)
565$''')
566
567 def test_down_at_bottom(self):
568 'Verify handling of "py-down" at the bottom of the stack'
569 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
570 cmds_after_breakpoint=['py-down'])
571 self.assertEndsWith(bt,
572 'Unable to find a newer python frame\n')
573
574 def test_up_at_top(self):
575 'Verify handling of "py-up" at the top of the stack'
576 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
577 cmds_after_breakpoint=['py-up'] * 4)
578 self.assertEndsWith(bt,
579 'Unable to find an older python frame\n')
580
581 def test_up_then_down(self):
582 'Verify "py-up" followed by "py-down"'
583 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
584 cmds_after_breakpoint=['py-up', 'py-down'])
585 self.assertMultilineMatches(bt,
586 r'''^.*
587#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
588 baz\(a, b, c\)
589#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\)
590 print\(42\)
591$''')
592
593class PyBtTests(DebuggerTests):
594 def test_basic_command(self):
595 'Verify that the "py-bt" command works'
596 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
597 cmds_after_breakpoint=['py-bt'])
598 self.assertMultilineMatches(bt,
599 r'''^.*
600#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
601 baz\(a, b, c\)
602#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
603 bar\(a, b, c\)
604#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 12, in <module> \(\)
605foo\(1, 2, 3\)
606''')
607
608class PyPrintTests(DebuggerTests):
609 def test_basic_command(self):
610 'Verify that the "py-print" command works'
611 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
612 cmds_after_breakpoint=['py-print args'])
613 self.assertMultilineMatches(bt,
614 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
615
616 def test_print_after_up(self):
617 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
618 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
619 self.assertMultilineMatches(bt,
620 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
621
622 def test_printing_global(self):
623 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
624 cmds_after_breakpoint=['py-print __name__'])
625 self.assertMultilineMatches(bt,
626 r".*\nglobal '__name__' = '__main__'\n.*")
627
628 def test_printing_builtin(self):
629 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
630 cmds_after_breakpoint=['py-print len'])
631 self.assertMultilineMatches(bt,
632 r".*\nbuiltin 'len' = <built-in function len>\n.*")
633
634class PyLocalsTests(DebuggerTests):
635 def test_basic_command(self):
636 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
637 cmds_after_breakpoint=['py-locals'])
638 self.assertMultilineMatches(bt,
639 r".*\nargs = \(1, 2, 3\)\n.*")
640
641 def test_locals_after_up(self):
642 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
643 cmds_after_breakpoint=['py-up', 'py-locals'])
644 self.assertMultilineMatches(bt,
645 r".*\na = 1\nb = 2\nc = 3\n.*")
646
647def test_main():
648 run_unittest(PrettyPrintTests,
649 #PyListTests,
650 #StackNavigationTests,
651 #PyBtTests,
652 #PyPrintTests,
653 #PyLocalsTests
654 )
655
656if __name__ == "__main__":
657 test_main()