|
1 import gdb |
|
2 import os |
|
3 import re |
|
4 import sys |
|
5 import traceback |
|
6 |
|
7 # testlibdir is set on the GDB command line, via --eval-command python testlibdir=... |
|
8 sys.path[0:0] = [testlibdir] |
|
9 |
|
10 # Run the C++ fragment named |fragment|, stopping on entry to |function| |
|
11 # ('breakpoint', by default) and then select the calling frame. |
|
12 def run_fragment(fragment, function='breakpoint'): |
|
13 # Arrange to stop at a reasonable place in the test program. |
|
14 bp = gdb.Breakpoint(function); |
|
15 try: |
|
16 gdb.execute("run %s" % (fragment,)) |
|
17 # Check that we did indeed stop by hitting the breakpoint we set. |
|
18 assert bp.hit_count == 1 |
|
19 finally: |
|
20 bp.delete() |
|
21 gdb.execute('frame 1') |
|
22 |
|
23 # Assert that |actual| is equal to |expected|; if not, complain in a helpful way. |
|
24 def assert_eq(actual, expected): |
|
25 if actual != expected: |
|
26 raise AssertionError, """Unexpected result: |
|
27 expected: %r |
|
28 actual: %r""" % (expected, actual) |
|
29 |
|
30 # Assert that |value|'s pretty-printed form is |form|. If |value| is a |
|
31 # string, then evaluate it with gdb.parse_and_eval to produce a value. |
|
32 def assert_pretty(value, form): |
|
33 if isinstance(value, str): |
|
34 value = gdb.parse_and_eval(value) |
|
35 assert_eq(str(value), form) |
|
36 |
|
37 # Check that the list of registered pretty-printers includes one named |
|
38 # |printer|, with a subprinter named |subprinter|. |
|
39 def assert_subprinter_registered(printer, subprinter): |
|
40 # Match a line containing |printer| followed by a colon, and then a |
|
41 # series of more-indented lines containing |subprinter|. |
|
42 |
|
43 names = { 'printer': re.escape(printer), 'subprinter': re.escape(subprinter) } |
|
44 pat = r'^( +)%(printer)s *\n(\1 +.*\n)*\1 +%(subprinter)s *\n' % names |
|
45 output = gdb.execute('info pretty-printer', to_string=True) |
|
46 if not re.search(pat, output, re.MULTILINE): |
|
47 raise AssertionError, ("assert_subprinter_registered failed to find pretty-printer:\n" |
|
48 " %s:%s\n" |
|
49 "'info pretty-printer' says:\n" |
|
50 "%s" % (printer, subprinter, output)) |
|
51 |
|
52 # Request full stack traces for Python errors. |
|
53 gdb.execute('set python print-stack full') |
|
54 |
|
55 # Tell GDB not to ask the user about the things we tell it to do. |
|
56 gdb.execute('set confirm off', False) |
|
57 |
|
58 # Some print settings that make testing easier. |
|
59 gdb.execute('set print static-members off') |
|
60 gdb.execute('set print address off') |
|
61 gdb.execute('set print pretty off') |
|
62 gdb.execute('set width 0') |
|
63 |
|
64 try: |
|
65 # testscript is set on the GDB command line, via: |
|
66 # --eval-command python testscript=... |
|
67 execfile(testscript) |
|
68 except AssertionError as err: |
|
69 sys.stderr.write('\nAssertion traceback:\n') |
|
70 (t, v, tb) = sys.exc_info() |
|
71 traceback.print_tb(tb) |
|
72 sys.stderr.write('\nTest assertion failed:\n') |
|
73 sys.stderr.write(str(err)) |
|
74 sys.exit(1) |
|
75 |