[PATCH v2 15/20] testing: Enhance gdb probe script

Alex Bennée posted 20 patches 11 hours ago
[PATCH v2 15/20] testing: Enhance gdb probe script
Posted by Alex Bennée 11 hours ago
From: Gustavo Romero <gustavo.romero@linaro.org>

Use list and set comprehension to simplify code. Also, gently handle
invalid gdb filenames.

Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
Message-Id: <20241015145848.387281-1-gustavo.romero@linaro.org>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 scripts/probe-gdb-support.py | 75 +++++++++++++++++++-----------------
 1 file changed, 39 insertions(+), 36 deletions(-)

diff --git a/scripts/probe-gdb-support.py b/scripts/probe-gdb-support.py
index 6dc58d06c7..6bcadce150 100644
--- a/scripts/probe-gdb-support.py
+++ b/scripts/probe-gdb-support.py
@@ -19,58 +19,61 @@
 
 import argparse
 import re
-from subprocess import check_output, STDOUT
+from subprocess import check_output, STDOUT, CalledProcessError
+import sys
 
-# mappings from gdb arch to QEMU target
-mappings = {
-    "alpha" : "alpha",
+# Mappings from gdb arch to QEMU target
+MAP = {
+    "alpha" : ["alpha"],
     "aarch64" : ["aarch64", "aarch64_be"],
-    "armv7": "arm",
+    "armv7": ["arm"],
     "armv8-a" : ["aarch64", "aarch64_be"],
-    "avr" : "avr",
+    "avr" : ["avr"],
     # no hexagon in upstream gdb
-    "hppa1.0" : "hppa",
-    "i386" : "i386",
-    "i386:x86-64" : "x86_64",
-    "Loongarch64" : "loongarch64",
-    "m68k" : "m68k",
-    "MicroBlaze" : "microblaze",
+    "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",
+    "or1k" : ["or1k"],
+    "powerpc:common" : ["ppc"],
     "powerpc:common64" : ["ppc64", "ppc64le"],
-    "riscv:rv32" : "riscv32",
-    "riscv:rv64" : "riscv64",
-    "s390:64-bit" : "s390x",
+    "riscv:rv32" : ["riscv32"],
+    "riscv:rv64" : ["riscv64"],
+    "s390:64-bit" : ["s390x"],
     "sh4" : ["sh4", "sh4eb"],
-    "sparc": "sparc",
-    "sparc:v8plus": "sparc32plus",
-    "sparc:v9a" : "sparc64",
+    "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)
+    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)
 
-    m = re.search(r"Valid arguments are (.*)",
-                  gdb_out.decode("utf-8"))
+    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]
 
-    valid_arches = set()
+        targets = {target for arch in mapped_gdb_archs for target in MAP[arch]}
 
-    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)
+    # QEMU targets
+    return targets
 
-    return valid_arches
 
 def main() -> None:
     parser = argparse.ArgumentParser(description='Probe GDB Architectures')
-- 
2.39.5


Re: [PATCH v2 15/20] testing: Enhance gdb probe script
Posted by Pierrick Bouvier an hour ago
On 10/22/24 03:56, Alex Bennée wrote:
> From: Gustavo Romero <gustavo.romero@linaro.org>
> 
> Use list and set comprehension to simplify code. Also, gently handle
> invalid gdb filenames.
> 
> Signed-off-by: Gustavo Romero <gustavo.romero@linaro.org>
> Message-Id: <20241015145848.387281-1-gustavo.romero@linaro.org>
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>   scripts/probe-gdb-support.py | 75 +++++++++++++++++++-----------------
>   1 file changed, 39 insertions(+), 36 deletions(-)
> 
> diff --git a/scripts/probe-gdb-support.py b/scripts/probe-gdb-support.py
> index 6dc58d06c7..6bcadce150 100644
> --- a/scripts/probe-gdb-support.py
> +++ b/scripts/probe-gdb-support.py
> @@ -19,58 +19,61 @@
>   
>   import argparse
>   import re
> -from subprocess import check_output, STDOUT
> +from subprocess import check_output, STDOUT, CalledProcessError
> +import sys
>   
> -# mappings from gdb arch to QEMU target
> -mappings = {
> -    "alpha" : "alpha",
> +# Mappings from gdb arch to QEMU target
> +MAP = {
> +    "alpha" : ["alpha"],
>       "aarch64" : ["aarch64", "aarch64_be"],
> -    "armv7": "arm",
> +    "armv7": ["arm"],
>       "armv8-a" : ["aarch64", "aarch64_be"],
> -    "avr" : "avr",
> +    "avr" : ["avr"],
>       # no hexagon in upstream gdb
> -    "hppa1.0" : "hppa",
> -    "i386" : "i386",
> -    "i386:x86-64" : "x86_64",
> -    "Loongarch64" : "loongarch64",
> -    "m68k" : "m68k",
> -    "MicroBlaze" : "microblaze",
> +    "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",
> +    "or1k" : ["or1k"],
> +    "powerpc:common" : ["ppc"],
>       "powerpc:common64" : ["ppc64", "ppc64le"],
> -    "riscv:rv32" : "riscv32",
> -    "riscv:rv64" : "riscv64",
> -    "s390:64-bit" : "s390x",
> +    "riscv:rv32" : ["riscv32"],
> +    "riscv:rv64" : ["riscv64"],
> +    "s390:64-bit" : ["s390x"],
>       "sh4" : ["sh4", "sh4eb"],
> -    "sparc": "sparc",
> -    "sparc:v8plus": "sparc32plus",
> -    "sparc:v9a" : "sparc64",
> +    "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)
> +    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)
>   
> -    m = re.search(r"Valid arguments are (.*)",
> -                  gdb_out.decode("utf-8"))
> +    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]
>   
> -    valid_arches = set()
> +        targets = {target for arch in mapped_gdb_archs for target in MAP[arch]}
>   
> -    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)
> +    # QEMU targets
> +    return targets
>   
> -    return valid_arches
>   
>   def main() -> None:
>       parser = argparse.ArgumentParser(description='Probe GDB Architectures')

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>