test_gdbstub.py 1.4 KB

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