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