2
0

test_gdbstub.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """Helper functions for gdbstub testing
  2. """
  3. from __future__ import print_function
  4. import argparse
  5. import gdb
  6. import os
  7. import sys
  8. import traceback
  9. fail_count = 0
  10. def gdb_exit(status):
  11. gdb.execute(f"exit {status}")
  12. class arg_parser(argparse.ArgumentParser):
  13. def exit(self, status=None, message=""):
  14. print("Wrong GDB script test argument! " + message)
  15. gdb_exit(1)
  16. def report(cond, msg):
  17. """Report success/fail of a test"""
  18. if cond:
  19. print("PASS: {}".format(msg))
  20. else:
  21. print("FAIL: {}".format(msg))
  22. global fail_count
  23. fail_count += 1
  24. def main(test, expected_arch=None):
  25. """Run a test function
  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. gdb_exit(0)
  37. if gdb.parse_and_eval("$pc") == 0:
  38. print("SKIP: PC not set")
  39. gdb_exit(0)
  40. try:
  41. test()
  42. except:
  43. print("GDB Exception:")
  44. traceback.print_exc(file=sys.stdout)
  45. global fail_count
  46. fail_count += 1
  47. if "QEMU_TEST_INTERACTIVE" in os.environ:
  48. import code
  49. code.InteractiveConsole(locals=globals()).interact()
  50. raise
  51. try:
  52. gdb.execute("kill")
  53. except gdb.error:
  54. pass
  55. print("All tests complete: {} failures".format(fail_count))
  56. gdb_exit(fail_count)