run-test.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. #
  3. # Run a gdbstub test case
  4. #
  5. # Copyright (c) 2019 Linaro
  6. #
  7. # Author: Alex Bennée <alex.bennee@linaro.org>
  8. #
  9. # This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. # See the COPYING file in the top-level directory.
  11. #
  12. # SPDX-License-Identifier: GPL-2.0-or-later
  13. import argparse
  14. import subprocess
  15. import shutil
  16. import shlex
  17. import os
  18. from time import sleep
  19. from tempfile import TemporaryDirectory
  20. def get_args():
  21. parser = argparse.ArgumentParser(description="A gdbstub test runner")
  22. parser.add_argument("--qemu", help="Qemu binary for test",
  23. required=True)
  24. parser.add_argument("--qargs", help="Qemu arguments for test")
  25. parser.add_argument("--binary", help="Binary to debug",
  26. required=True)
  27. parser.add_argument("--test", help="GDB test script",
  28. required=True)
  29. parser.add_argument("--gdb", help="The gdb binary to use",
  30. default=None)
  31. parser.add_argument("--output", help="A file to redirect output to")
  32. return parser.parse_args()
  33. def log(output, msg):
  34. if output:
  35. output.write(msg + "\n")
  36. output.flush()
  37. else:
  38. print(msg)
  39. if __name__ == '__main__':
  40. args = get_args()
  41. # Search for a gdb we can use
  42. if not args.gdb:
  43. args.gdb = shutil.which("gdb-multiarch")
  44. if not args.gdb:
  45. args.gdb = shutil.which("gdb")
  46. if not args.gdb:
  47. print("We need gdb to run the test")
  48. exit(-1)
  49. if args.output:
  50. output = open(args.output, "w")
  51. else:
  52. output = None
  53. socket_dir = TemporaryDirectory("qemu-gdbstub")
  54. socket_name = os.path.join(socket_dir.name, "gdbstub.socket")
  55. # Launch QEMU with binary
  56. if "system" in args.qemu:
  57. cmd = "%s %s %s -gdb unix:path=%s,server=on" % (args.qemu,
  58. args.qargs,
  59. args.binary,
  60. socket_name)
  61. else:
  62. cmd = "%s %s -g %s %s" % (args.qemu, args.qargs, socket_name,
  63. args.binary)
  64. log(output, "QEMU CMD: %s" % (cmd))
  65. inferior = subprocess.Popen(shlex.split(cmd))
  66. # Now launch gdb with our test and collect the result
  67. gdb_cmd = "%s %s" % (args.gdb, args.binary)
  68. # run quietly and ignore .gdbinit
  69. gdb_cmd += " -q -n -batch"
  70. # disable prompts in case of crash
  71. gdb_cmd += " -ex 'set confirm off'"
  72. # connect to remote
  73. gdb_cmd += " -ex 'target remote %s'" % (socket_name)
  74. # finally the test script itself
  75. gdb_cmd += " -x %s" % (args.test)
  76. sleep(1)
  77. log(output, "GDB CMD: %s" % (gdb_cmd))
  78. result = subprocess.call(gdb_cmd, shell=True, stdout=output)
  79. # A result of greater than 128 indicates a fatal signal (likely a
  80. # crash due to gdb internal failure). That's a problem for GDB and
  81. # not the test so we force a return of 0 so we don't fail the test on
  82. # account of broken external tools.
  83. if result > 128:
  84. log(output, "GDB crashed? (%d, %d) SKIPPING" % (result, result - 128))
  85. exit(0)
  86. try:
  87. inferior.wait(2)
  88. except subprocess.TimeoutExpired:
  89. log(output, "GDB never connected? Killed guest")
  90. inferior.kill()
  91. exit(result)