2
0

probe-gdb-support.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. # coding: utf-8
  3. #
  4. # Probe gdb for supported architectures.
  5. #
  6. # This is required to support testing of the gdbstub as its hard to
  7. # handle errors gracefully during the test. Instead this script when
  8. # passed a GDB binary will probe its architecture support and return a
  9. # string of supported arches, stripped of guff.
  10. #
  11. # Copyright 2023 Linaro Ltd
  12. #
  13. # Author: Alex Bennée <alex.bennee@linaro.org>
  14. #
  15. # This work is licensed under the terms of the GNU GPL, version 2 or later.
  16. # See the COPYING file in the top-level directory.
  17. #
  18. # SPDX-License-Identifier: GPL-2.0-or-later
  19. import argparse
  20. import re
  21. from subprocess import check_output, STDOUT, CalledProcessError
  22. import sys
  23. # Mappings from gdb arch to QEMU target
  24. MAP = {
  25. "alpha" : ["alpha"],
  26. "aarch64" : ["aarch64", "aarch64_be"],
  27. "armv7": ["arm"],
  28. "armv8-a" : ["aarch64", "aarch64_be"],
  29. "avr" : ["avr"],
  30. # no hexagon in upstream gdb
  31. "hppa1.0" : ["hppa"],
  32. "i386" : ["i386"],
  33. "i386:x86-64" : ["x86_64"],
  34. "Loongarch64" : ["loongarch64"],
  35. "m68k" : ["m68k"],
  36. "MicroBlaze" : ["microblaze"],
  37. "mips:isa64" : ["mips64", "mips64el"],
  38. "or1k" : ["or1k"],
  39. "powerpc:common" : ["ppc"],
  40. "powerpc:common64" : ["ppc64", "ppc64le"],
  41. "riscv:rv32" : ["riscv32"],
  42. "riscv:rv64" : ["riscv64"],
  43. "s390:64-bit" : ["s390x"],
  44. "sh4" : ["sh4", "sh4eb"],
  45. "sparc": ["sparc"],
  46. "sparc:v8plus": ["sparc32plus"],
  47. "sparc:v9a" : ["sparc64"],
  48. # no tricore in upstream gdb
  49. "xtensa" : ["xtensa", "xtensaeb"]
  50. }
  51. def do_probe(gdb):
  52. try:
  53. gdb_out = check_output([gdb,
  54. "-ex", "set architecture",
  55. "-ex", "quit"], stderr=STDOUT, encoding="utf-8")
  56. except (OSError) as e:
  57. sys.exit(e)
  58. except CalledProcessError as e:
  59. sys.exit(f'{e}. Output:\n\n{e.output}')
  60. found_gdb_archs = re.search(r'Valid arguments are (.*)', gdb_out)
  61. targets = set()
  62. if found_gdb_archs:
  63. gdb_archs = found_gdb_archs.group(1).split(", ")
  64. mapped_gdb_archs = [arch for arch in gdb_archs if arch in MAP]
  65. targets = {target for arch in mapped_gdb_archs for target in MAP[arch]}
  66. # QEMU targets
  67. return targets
  68. def main() -> None:
  69. parser = argparse.ArgumentParser(description='Probe GDB Architectures')
  70. parser.add_argument('gdb', help='Path to GDB binary.')
  71. args = parser.parse_args()
  72. supported = do_probe(args.gdb)
  73. print(" ".join(supported))
  74. if __name__ == '__main__':
  75. main()