From 295cbf7afa5fd59a88c066e478bd3ef22760a6de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Di=20Biase?= Date: Wed, 29 Nov 2023 19:42:19 -0300 Subject: [PATCH] Reorder `reset_arch`: parameter forced, elf header, gdb conf (#1004) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reorder how on reset_architecture fn is going to find arch. Now: - Param forced - Elf header - gdb conf It is based on chat on #947 with @Grazfather This change is needed because if first check for gdb conf if some has another language active would raise an exception making gef not work at all (some code match only english sentences). This pattch would help non englihs users of gdb to not have many problems to use gef. Signed-off-by: José Luis Di Biase --- gef.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/gef.py b/gef.py index 49be5eae7..7782d2e35 100644 --- a/gef.py +++ b/gef.py @@ -2150,7 +2150,7 @@ def checksec(filename: str) -> Dict[str, bool]: return Elf(filename).checksec -@lru_cache() +@deprecated("Use `gef.arch` instead") def get_arch() -> str: """Return the binary's architecture.""" if is_alive(): @@ -3679,20 +3679,22 @@ def reset_architecture(arch: Optional[str] = None) -> None: raise OSError(f"Specified arch {arch.upper()} is not supported") return - gdb_arch = get_arch() - - preciser_arch = next((a for a in arches.values() if a.supports_gdb_arch(gdb_arch)), None) - if preciser_arch: - gef.arch = preciser_arch() - return + # check for bin running + if is_alive(): + arch = gdb.selected_frame().architecture() + gdb_arch = arch.name() + preciser_arch = next((a for a in arches.values() if a.supports_gdb_arch(gdb_arch)), None) + if preciser_arch: + gef.arch = preciser_arch() + return # last resort, use the info from elf header to find it from the known architectures - try: - arch_name = gef.binary.e_machine if gef.binary else gdb_arch - gef.arch = arches[arch_name]() - except KeyError: - raise OSError(f"CPU type is currently not supported: {get_arch()}") - return + if gef.binary.e_machine: + try: + gef.arch = arches[gef.binary.e_machine]() + except KeyError: + raise OSError(f"CPU type is currently not supported: {gef.binary.e_machine}") + return @lru_cache()