1"""Helper functions for gdbstub testing
2
3"""
4from __future__ import print_function
5import gdb
6import os
7import sys
8import traceback
9
10fail_count = 0
11
12
13def report(cond, msg):
14    """Report success/fail of a test"""
15    if cond:
16        print("PASS: {}".format(msg))
17    else:
18        print("FAIL: {}".format(msg))
19        global fail_count
20        fail_count += 1
21
22
23def main(test, expected_arch=None):
24    """Run a test function
25
26    This runs as the script it sourced (via -x, via run-test.py)."""
27    try:
28        inferior = gdb.selected_inferior()
29        arch = inferior.architecture()
30        print("ATTACHED: {}".format(arch.name()))
31        if expected_arch is not None:
32            report(arch.name() == expected_arch,
33                   "connected to {}".format(expected_arch))
34    except (gdb.error, AttributeError):
35        print("SKIP: not connected")
36        exit(0)
37
38    if gdb.parse_and_eval("$pc") == 0:
39        print("SKIP: PC not set")
40        exit(0)
41
42    try:
43        test()
44    except:
45        print("GDB Exception:")
46        traceback.print_exc(file=sys.stdout)
47        global fail_count
48        fail_count += 1
49        if "QEMU_TEST_INTERACTIVE" in os.environ:
50            import code
51            code.InteractiveConsole(locals=globals()).interact()
52        raise
53
54    try:
55        gdb.execute("kill")
56    except gdb.error:
57        pass
58
59    print("All tests complete: {} failures".format(fail_count))
60    exit(fail_count)
61