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