| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 |
# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
# flake8: noqa: F821 |
| 6 |
|
| 7 |
import re |
| 8 |
import sys |
| 9 |
import traceback |
| 10 |
|
| 11 |
import gdb |
| 12 |
|
| 13 |
# testlibdir is set on the GDB command line, via --eval-command python testlibdir=... |
| 14 |
sys.path[0:0] = [testlibdir] |
| 15 |
|
| 16 |
active_fragment = None |
| 17 |
|
| 18 |
# Run the C++ fragment named |fragment|, stopping on entry to |function| |
| 19 |
# ('breakpoint', by default) and then select the calling frame. |
| 20 |
|
| 21 |
|
| 22 |
def run_fragment(fragment, function="gdb-tests.cpp:breakpoint"): |
| 23 |
# Arrange to stop at a reasonable place in the test program. |
| 24 |
bp = gdb.Breakpoint(function) |
| 25 |
try: |
| 26 |
gdb.execute("run %s" % (fragment,)) |
| 27 |
# Check that we did indeed stop by hitting the breakpoint we set. |
| 28 |
assert bp.hit_count == 1 |
| 29 |
finally: |
| 30 |
bp.delete() |
| 31 |
gdb.execute("frame 1") |
| 32 |
|
| 33 |
global active_fragment |
| 34 |
active_fragment = fragment |
| 35 |
|
| 36 |
|
| 37 |
# Assert that |actual| is equal to |expected|; if not, complain in a helpful way. |
| 38 |
|
| 39 |
|
| 40 |
def assert_eq(actual, expected): |
| 41 |
if actual != expected: |
| 42 |
raise AssertionError( |
| 43 |
"""Unexpected result: |
| 44 |
expected: %r |
| 45 |
actual: %r""" |
| 46 |
% (expected, actual) |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
# Assert that |expected| regex matches |actual| result; if not, complain in a helpful way. |
| 51 |
|
| 52 |
|
| 53 |
def assert_match(actual, expected): |
| 54 |
if re.match(expected, actual, re.MULTILINE) is None: |
| 55 |
raise AssertionError( |
| 56 |
"""Unexpected result: |
| 57 |
expected pattern: %r |
| 58 |
actual: %r""" |
| 59 |
% (expected, actual) |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
# Assert that |value|'s pretty-printed form is |form|. If |value| is a |
| 64 |
# string, then evaluate it with gdb.parse_and_eval to produce a value. |
| 65 |
|
| 66 |
|
| 67 |
def assert_pretty(value, form): |
| 68 |
if isinstance(value, str): |
| 69 |
value = gdb.parse_and_eval(value) |
| 70 |
assert_eq(str(value), form) |
| 71 |
|
| 72 |
|
| 73 |
# Assert that |value|'s pretty-printed form match the pattern |pattern|. If |
| 74 |
# |value| is a string, then evaluate it with gdb.parse_and_eval to produce a |
| 75 |
# value. |
| 76 |
|
| 77 |
|
| 78 |
def assert_regexp_pretty(value, form): |
| 79 |
if isinstance(value, str): |
| 80 |
value = gdb.parse_and_eval(value) |
| 81 |
assert_match(str(value), form) |
| 82 |
|
| 83 |
|
| 84 |
# Check that the list of registered pretty-printers includes one named |
| 85 |
# |printer|, with a subprinter named |subprinter|. |
| 86 |
|
| 87 |
|
| 88 |
def assert_subprinter_registered(printer, subprinter): |
| 89 |
# Match a line containing |printer| followed by a colon, and then a |
| 90 |
# series of more-indented lines containing |subprinter|. |
| 91 |
|
| 92 |
names = {"printer": re.escape(printer), "subprinter": re.escape(subprinter)} |
| 93 |
pat = r"^( +)%(printer)s *\n(\1 +.*\n)*\1 +%(subprinter)s *\n" % names |
| 94 |
output = gdb.execute("info pretty-printer", to_string=True) |
| 95 |
if not re.search(pat, output, re.MULTILINE): |
| 96 |
raise AssertionError( |
| 97 |
"assert_subprinter_registered failed to find pretty-printer:\n" |
| 98 |
" %s:%s\n" |
| 99 |
"'info pretty-printer' says:\n" |
| 100 |
"%s" % (printer, subprinter, output) |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
# Request full stack traces for Python errors. |
| 105 |
gdb.execute("set python print-stack full") |
| 106 |
|
| 107 |
# Tell GDB not to ask the user about the things we tell it to do. |
| 108 |
gdb.execute("set confirm off", False) |
| 109 |
|
| 110 |
# Some print settings that make testing easier. |
| 111 |
gdb.execute("set print static-members off") |
| 112 |
gdb.execute("set print address off") |
| 113 |
gdb.execute("set print pretty off") |
| 114 |
gdb.execute("set width 0") |
| 115 |
|
| 116 |
try: |
| 117 |
# testscript is set on the GDB command line, via: |
| 118 |
# --eval-command python testscript=... |
| 119 |
execfile(testscript, globals(), locals()) |
| 120 |
except AssertionError as err: |
| 121 |
header = "\nAssertion traceback" |
| 122 |
if active_fragment: |
| 123 |
header += " for " + active_fragment |
| 124 |
sys.stderr.write(header + ":\n") |
| 125 |
(t, v, tb) = sys.exc_info() |
| 126 |
traceback.print_tb(tb) |
| 127 |
sys.stderr.write("\nTest assertion failed:\n") |
| 128 |
sys.stderr.write(str(err)) |
| 129 |
sys.exit(1) |