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 20import argparse 21import re 22from subprocess import check_output, STDOUT 23 24# mappings from gdb arch to QEMU target 25mappings = { 26 "alpha" : "alpha", 27 "aarch64" : ["aarch64", "aarch64_be"], 28 "armv7": "arm", 29 "armv8-a" : ["aarch64", "aarch64_be"], 30 "avr" : "avr", 31 # no hexagon in upstream gdb 32 "hppa1.0" : "hppa", 33 "i386" : "i386", 34 "i386:x86-64" : "x86_64", 35 "Loongarch64" : "loongarch64", 36 "m68k" : "m68k", 37 "MicroBlaze" : "microblaze", 38 "mips:isa64" : ["mips64", "mips64el"], 39 "or1k" : "or1k", 40 "powerpc:common" : "ppc", 41 "powerpc:common64" : ["ppc64", "ppc64le"], 42 "riscv:rv32" : "riscv32", 43 "riscv:rv64" : "riscv64", 44 "s390:64-bit" : "s390x", 45 "sh4" : ["sh4", "sh4eb"], 46 "sparc": "sparc", 47 "sparc:v8plus": "sparc32plus", 48 "sparc:v9a" : "sparc64", 49 # no tricore in upstream gdb 50 "xtensa" : ["xtensa", "xtensaeb"] 51} 52 53def do_probe(gdb): 54 gdb_out = check_output([gdb, 55 "-ex", "set architecture", 56 "-ex", "quit"], stderr=STDOUT) 57 58 m = re.search(r"Valid arguments are (.*)", 59 gdb_out.decode("utf-8")) 60 61 valid_arches = set() 62 63 if m.group(1): 64 for arch in m.group(1).split(", "): 65 if arch in mappings: 66 mapping = mappings[arch] 67 if isinstance(mapping, str): 68 valid_arches.add(mapping) 69 else: 70 for entry in mapping: 71 valid_arches.add(entry) 72 73 return valid_arches 74 75def main() -> None: 76 parser = argparse.ArgumentParser(description='Probe GDB Architectures') 77 parser.add_argument('gdb', help='Path to GDB binary.') 78 79 args = parser.parse_args() 80 81 supported = do_probe(args.gdb) 82 83 print(" ".join(supported)) 84 85if __name__ == '__main__': 86 main() 87