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    report("/sha1" in mappings, "Found the test binary name in the mappings")
37
38
39def main():
40    """Prepare the environment and run through the tests"""
41    try:
42        inferior = gdb.selected_inferior()
43        print("ATTACHED: {}".format(inferior.architecture().name()))
44    except (gdb.error, AttributeError):
45        print("SKIPPING (not connected)")
46        exit(0)
47
48    if gdb.parse_and_eval('$pc') == 0:
49        print("SKIP: PC not set")
50        exit(0)
51
52    try:
53        # These are not very useful in scripts
54        gdb.execute("set pagination off")
55        gdb.execute("set confirm off")
56
57        # Run the actual tests
58        run_test()
59    except gdb.error:
60        report(False, "GDB Exception: {}".format(sys.exc_info()[0]))
61    print("All tests complete: %d failures" % n_failures)
62    exit(n_failures)
63
64
65main()
66