1"""Test that gdbstub has access to proc mappings.
2
3This runs as a sourced script (via -x, via run-test.py)."""
4from __future__ import print_function
5import gdb
6import sys
7
8
9n_failures = 0
10
11
12def report(cond, msg):
13    """Report success/fail of a test"""
14    if cond:
15        print("PASS: {}".format(msg))
16    else:
17        print("FAIL: {}".format(msg))
18        global n_failures
19        n_failures += 1
20
21
22def run_test():
23    """Run through the tests one by one"""
24    try:
25        mappings = gdb.execute("info proc mappings", False, True)
26    except gdb.error as exc:
27        exc_str = str(exc)
28        if "Not supported on this target." in exc_str:
29            # Detect failures due to an outstanding issue with how GDB handles
30            # the x86_64 QEMU's target.xml, which does not contain the
31            # definition of orig_rax. Skip the test in this case.
32            print("SKIP: {}".format(exc_str))
33            return
34        raise
35    report(isinstance(mappings, str), "Fetched the mappings from the inferior")
36    # Broken with host page size > guest page size
37    # report("/sha1" in mappings, "Found the test binary name in the mappings")
38
39
40def main():
41    """Prepare the environment and run through the tests"""
42    try:
43        inferior = gdb.selected_inferior()
44        print("ATTACHED: {}".format(inferior.architecture().name()))
45    except (gdb.error, AttributeError):
46        print("SKIPPING (not connected)")
47        exit(0)
48
49    if gdb.parse_and_eval('$pc') == 0:
50        print("SKIP: PC not set")
51        exit(0)
52
53    try:
54        # Run the actual tests
55        run_test()
56    except gdb.error:
57        report(False, "GDB Exception: {}".format(sys.exc_info()[0]))
58    print("All tests complete: %d failures" % n_failures)
59    exit(n_failures)
60
61
62main()
63