run-test.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. parser.add_argument("--gdb", help="The gdb binary to use",
  29. default=None)
  30. parser.add_argument("--gdb-args", help="Additional gdb arguments")
  31. parser.add_argument("--output", help="A file to redirect output to")
  32. parser.add_argument("--stderr", help="A file to redirect stderr to")
  33. return parser.parse_args()
  34. def log(output, msg):
  35. if output:
  36. output.write(msg + "\n")
  37. output.flush()
  38. else:
  39. print(msg)
  40. if __name__ == '__main__':
  41. args = get_args()
  42. # Search for a gdb we can use
  43. if not args.gdb:
  44. args.gdb = shutil.which("gdb-multiarch")
  45. if not args.gdb:
  46. args.gdb = shutil.which("gdb")
  47. if not args.gdb:
  48. print("We need gdb to run the test")
  49. exit(-1)
  50. if args.output:
  51. output = open(args.output, "w")
  52. else:
  53. output = None
  54. if args.stderr:
  55. stderr = open(args.stderr, "w")
  56. else:
  57. stderr = None
  58. socket_dir = TemporaryDirectory("qemu-gdbstub")
  59. socket_name = os.path.join(socket_dir.name, "gdbstub.socket")
  60. # Launch QEMU with binary
  61. if "system" in args.qemu:
  62. cmd = f'{args.qemu} {args.qargs} {args.binary}' \
  63. f' -S -gdb unix:path={socket_name},server=on'
  64. else:
  65. cmd = f'{args.qemu} {args.qargs} -g {socket_name} {args.binary}'
  66. log(output, "QEMU CMD: %s" % (cmd))
  67. inferior = subprocess.Popen(shlex.split(cmd))
  68. # Now launch gdb with our test and collect the result
  69. gdb_cmd = "%s %s" % (args.gdb, args.binary)
  70. if args.gdb_args:
  71. gdb_cmd += " %s" % (args.gdb_args)
  72. # run quietly and ignore .gdbinit
  73. gdb_cmd += " -q -n -batch"
  74. # disable prompts in case of crash
  75. gdb_cmd += " -ex 'set confirm off'"
  76. # connect to remote
  77. gdb_cmd += " -ex 'target remote %s'" % (socket_name)
  78. # finally the test script itself
  79. if args.test:
  80. gdb_cmd += " -x %s" % (args.test)
  81. sleep(1)
  82. log(output, "GDB CMD: %s" % (gdb_cmd))
  83. result = subprocess.call(gdb_cmd, shell=True, stdout=output, stderr=stderr)
  84. # A result of greater than 128 indicates a fatal signal (likely a
  85. # crash due to gdb internal failure). That's a problem for GDB and
  86. # not the test so we force a return of 0 so we don't fail the test on
  87. # account of broken external tools.
  88. if result > 128:
  89. log(output, "GDB crashed? (%d, %d) SKIPPING" % (result, result - 128))
  90. exit(0)
  91. try:
  92. inferior.wait(2)
  93. except subprocess.TimeoutExpired:
  94. log(output, "GDB never connected? Killed guest")
  95. inferior.kill()
  96. exit(result)