| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  | #!/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 | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  | from subprocess import check_output, STDOUT, CalledProcessError | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  | # Mappings from gdb arch to QEMU target | 
					
						
							|  |  |  | MAP = { | 
					
						
							|  |  |  |     "alpha" : ["alpha"], | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  |     "aarch64" : ["aarch64", "aarch64_be"], | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |     "armv7": ["arm"], | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  |     "armv8-a" : ["aarch64", "aarch64_be"], | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |     "avr" : ["avr"], | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  |     # no hexagon in upstream gdb | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |     "hppa1.0" : ["hppa"], | 
					
						
							|  |  |  |     "i386" : ["i386"], | 
					
						
							|  |  |  |     "i386:x86-64" : ["x86_64"], | 
					
						
							|  |  |  |     "Loongarch64" : ["loongarch64"], | 
					
						
							|  |  |  |     "m68k" : ["m68k"], | 
					
						
							|  |  |  |     "MicroBlaze" : ["microblaze"], | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  |     "mips:isa64" : ["mips64", "mips64el"], | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |     "or1k" : ["or1k"], | 
					
						
							|  |  |  |     "powerpc:common" : ["ppc"], | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  |     "powerpc:common64" : ["ppc64", "ppc64le"], | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |     "riscv:rv32" : ["riscv32"], | 
					
						
							|  |  |  |     "riscv:rv64" : ["riscv64"], | 
					
						
							|  |  |  |     "s390:64-bit" : ["s390x"], | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  |     "sh4" : ["sh4", "sh4eb"], | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |     "sparc": ["sparc"], | 
					
						
							|  |  |  |     "sparc:v8plus": ["sparc32plus"], | 
					
						
							|  |  |  |     "sparc:v9a" : ["sparc64"], | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  |     # no tricore in upstream gdb | 
					
						
							|  |  |  |     "xtensa" : ["xtensa", "xtensaeb"] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  | def do_probe(gdb): | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |     try: | 
					
						
							|  |  |  |         gdb_out = check_output([gdb, | 
					
						
							|  |  |  |                                "-ex", "set architecture", | 
					
						
							|  |  |  |                                "-ex", "quit"], stderr=STDOUT, encoding="utf-8") | 
					
						
							|  |  |  |     except (OSError) as e: | 
					
						
							|  |  |  |         sys.exit(e) | 
					
						
							|  |  |  |     except CalledProcessError as e: | 
					
						
							|  |  |  |         sys.exit(f'{e}. Output:\n\n{e.output}') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     found_gdb_archs = re.search(r'Valid arguments are (.*)', gdb_out) | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |     targets = set() | 
					
						
							|  |  |  |     if found_gdb_archs: | 
					
						
							|  |  |  |         gdb_archs = found_gdb_archs.group(1).split(", ") | 
					
						
							|  |  |  |         mapped_gdb_archs = [arch for arch in gdb_archs if arch in MAP] | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |         targets = {target for arch in mapped_gdb_archs for target in MAP[arch]} | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-23 12:34:03 +01:00
										 |  |  |     # QEMU targets | 
					
						
							|  |  |  |     return targets | 
					
						
							| 
									
										
										
										
											2023-03-02 18:57:59 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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() |