xref: /openbmc/qemu/scripts/probe-gdb-support.py (revision 1abdde1ad42d0ebccc5e8bc574ebe805cd650102)
1#!/usr/bin/env python3
2#
3# Probe gdb for supported architectures.
4#
5# This is required to support testing of the gdbstub as its hard to
6# handle errors gracefully during the test. Instead this script when
7# passed a GDB binary will probe its architecture support and return a
8# string of supported arches, stripped of guff.
9#
10# Copyright 2023 Linaro Ltd
11#
12# Author: Alex Bennée <alex.bennee@linaro.org>
13#
14# This work is licensed under the terms of the GNU GPL, version 2 or later.
15# See the COPYING file in the top-level directory.
16#
17# SPDX-License-Identifier: GPL-2.0-or-later
18
19import argparse
20import re
21from subprocess import check_output, STDOUT, CalledProcessError
22import sys
23
24# Mappings from gdb arch to QEMU target
25MAP = {
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
53
54def do_probe(gdb):
55    try:
56        gdb_out = check_output([gdb,
57                               "-ex", "set architecture",
58                               "-ex", "quit"], stderr=STDOUT, encoding="utf-8")
59    except (OSError) as e:
60        sys.exit(e)
61    except CalledProcessError as e:
62        sys.exit(f'{e}. Output:\n\n{e.output}')
63
64    found_gdb_archs = re.search(r'Valid arguments are (.*)', gdb_out)
65
66    targets = set()
67    if found_gdb_archs:
68        gdb_archs = found_gdb_archs.group(1).split(", ")
69        mapped_gdb_archs = [arch for arch in gdb_archs if arch in MAP]
70
71        targets = {target for arch in mapped_gdb_archs for target in MAP[arch]}
72
73    # QEMU targets
74    return targets
75
76
77def main() -> None:
78    parser = argparse.ArgumentParser(description='Probe GDB Architectures')
79    parser.add_argument('gdb', help='Path to GDB binary.')
80
81    args = parser.parse_args()
82
83    supported = do_probe(args.gdb)
84
85    print(" ".join(supported))
86
87if __name__ == '__main__':
88    main()
89