Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 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 | |
| 20 | import argparse |
| 21 | import re |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 22 | from subprocess import check_output, STDOUT, CalledProcessError |
| 23 | import sys |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 24 | |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 25 | # Mappings from gdb arch to QEMU target |
| 26 | MAP = { |
| 27 | "alpha" : ["alpha"], |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 28 | "aarch64" : ["aarch64", "aarch64_be"], |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 29 | "armv7": ["arm"], |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 30 | "armv8-a" : ["aarch64", "aarch64_be"], |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 31 | "avr" : ["avr"], |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 32 | # no hexagon in upstream gdb |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 33 | "hppa1.0" : ["hppa"], |
| 34 | "i386" : ["i386"], |
| 35 | "i386:x86-64" : ["x86_64"], |
| 36 | "Loongarch64" : ["loongarch64"], |
| 37 | "m68k" : ["m68k"], |
| 38 | "MicroBlaze" : ["microblaze"], |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 39 | "mips:isa64" : ["mips64", "mips64el"], |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 40 | "or1k" : ["or1k"], |
| 41 | "powerpc:common" : ["ppc"], |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 42 | "powerpc:common64" : ["ppc64", "ppc64le"], |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 43 | "riscv:rv32" : ["riscv32"], |
| 44 | "riscv:rv64" : ["riscv64"], |
| 45 | "s390:64-bit" : ["s390x"], |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 46 | "sh4" : ["sh4", "sh4eb"], |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 47 | "sparc": ["sparc"], |
| 48 | "sparc:v8plus": ["sparc32plus"], |
| 49 | "sparc:v9a" : ["sparc64"], |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 50 | # no tricore in upstream gdb |
| 51 | "xtensa" : ["xtensa", "xtensaeb"] |
| 52 | } |
| 53 | |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 54 | |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 55 | def do_probe(gdb): |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 56 | try: |
| 57 | gdb_out = check_output([gdb, |
| 58 | "-ex", "set architecture", |
| 59 | "-ex", "quit"], stderr=STDOUT, encoding="utf-8") |
| 60 | except (OSError) as e: |
| 61 | sys.exit(e) |
| 62 | except CalledProcessError as e: |
| 63 | sys.exit(f'{e}. Output:\n\n{e.output}') |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 64 | |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 65 | found_gdb_archs = re.search(r'Valid arguments are (.*)', gdb_out) |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 66 | |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 67 | targets = set() |
| 68 | if found_gdb_archs: |
| 69 | gdb_archs = found_gdb_archs.group(1).split(", ") |
| 70 | mapped_gdb_archs = [arch for arch in gdb_archs if arch in MAP] |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 71 | |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 72 | targets = {target for arch in mapped_gdb_archs for target in MAP[arch]} |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 73 | |
Gustavo Romero | 345dedb | 2024-10-23 12:34:03 +0100 | [diff] [blame] | 74 | # QEMU targets |
| 75 | return targets |
| 76 | |
Alex Bennée | bcbc36a | 2023-03-02 18:57:59 -0800 | [diff] [blame] | 77 | |
| 78 | def main() -> None: |
| 79 | parser = argparse.ArgumentParser(description='Probe GDB Architectures') |
| 80 | parser.add_argument('gdb', help='Path to GDB binary.') |
| 81 | |
| 82 | args = parser.parse_args() |
| 83 | |
| 84 | supported = do_probe(args.gdb) |
| 85 | |
| 86 | print(" ".join(supported)) |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | main() |