aboutsummaryrefslogtreecommitdiff
path: root/scripts/probe-gdb-support.py
blob: 6dc58d06c78003142cc7207622649e086dbcbb89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
# coding: utf-8
#
# Probe gdb for supported architectures.
#
# This is required to support testing of the gdbstub as its hard to
# handle errors gracefully during the test. Instead this script when
# passed a GDB binary will probe its architecture support and return a
# string of supported arches, stripped of guff.
#
# Copyright 2023 Linaro Ltd
#
# Author: Alex Bennée <alex.bennee@linaro.org>
#
# This work is licensed under the terms of the GNU GPL, version 2 or later.
# See the COPYING file in the top-level directory.
#
# SPDX-License-Identifier: GPL-2.0-or-later

import argparse
import re
from subprocess import check_output, STDOUT

# mappings from gdb arch to QEMU target
mappings = {
    "alpha" : "alpha",
    "aarch64" : ["aarch64", "aarch64_be"],
    "armv7": "arm",
    "armv8-a" : ["aarch64", "aarch64_be"],
    "avr" : "avr",
    # no hexagon in upstream gdb
    "hppa1.0" : "hppa",
    "i386" : "i386",
    "i386:x86-64" : "x86_64",
    "Loongarch64" : "loongarch64",
    "m68k" : "m68k",
    "MicroBlaze" : "microblaze",
    "mips:isa64" : ["mips64", "mips64el"],
    "or1k" : "or1k",
    "powerpc:common" : "ppc",
    "powerpc:common64" : ["ppc64", "ppc64le"],
    "riscv:rv32" : "riscv32",
    "riscv:rv64" : "riscv64",
    "s390:64-bit" : "s390x",
    "sh4" : ["sh4", "sh4eb"],
    "sparc": "sparc",
    "sparc:v8plus": "sparc32plus",
    "sparc:v9a" : "sparc64",
    # no tricore in upstream gdb
    "xtensa" : ["xtensa", "xtensaeb"]
}

def do_probe(gdb):
    gdb_out = check_output([gdb,
                            "-ex", "set architecture",
                            "-ex", "quit"], stderr=STDOUT)

    m = re.search(r"Valid arguments are (.*)",
                  gdb_out.decode("utf-8"))

    valid_arches = set()

    if m.group(1):
        for arch in m.group(1).split(", "):
            if arch in mappings:
                mapping = mappings[arch]
                if isinstance(mapping, str):
                    valid_arches.add(mapping)
                else:
                    for entry in mapping:
                        valid_arches.add(entry)

    return valid_arches

def main() -> None:
    parser = argparse.ArgumentParser(description='Probe GDB Architectures')
    parser.add_argument('gdb', help='Path to GDB binary.')

    args = parser.parse_args()

    supported = do_probe(args.gdb)

    print(" ".join(supported))

if __name__ == '__main__':
    main()