From 2ea32b089ab77668b983c2065265c189f1eaa547 Mon Sep 17 00:00:00 2001 From: Gulshan Singh Date: Wed, 7 Sep 2022 05:15:07 -0700 Subject: [PATCH] Move qemu.py and remote.py to gdblib (#1130) --- docs/source/api/qemu.rst | 4 ++-- docs/source/api/remote.rst | 4 ++-- pwndbg/auxv.py | 4 ++-- pwndbg/commands/__init__.py | 2 +- pwndbg/commands/leakfind.py | 2 +- pwndbg/commands/vmmap.py | 2 +- pwndbg/elf.py | 2 +- pwndbg/file.py | 16 ++++++++-------- pwndbg/gdblib/android.py | 4 ++-- pwndbg/gdblib/memory.py | 4 ++-- pwndbg/{ => gdblib}/qemu.py | 12 +++++++----- pwndbg/gdblib/regs.py | 4 ++-- pwndbg/{ => gdblib}/remote.py | 0 pwndbg/proc.py | 10 +++++----- pwndbg/symbol.py | 10 +++++----- pwndbg/vmmap.py | 12 ++++++------ 16 files changed, 47 insertions(+), 45 deletions(-) rename pwndbg/{ => gdblib}/qemu.py (90%) rename pwndbg/{ => gdblib}/remote.py (100%) diff --git a/docs/source/api/qemu.rst b/docs/source/api/qemu.rst index ae93199dc..5242ca24a 100644 --- a/docs/source/api/qemu.rst +++ b/docs/source/api/qemu.rst @@ -1,5 +1,5 @@ -:mod:`pwndbg.qemu` --- pwndbg.qemu +:mod:`pwndbg.gdblib.qemu` --- pwndbg.gdblib.qemu ============================================= -.. automodule:: pwndbg.qemu +.. automodule:: pwndbg.gdblib.qemu :members: diff --git a/docs/source/api/remote.rst b/docs/source/api/remote.rst index e0d61cffd..ecd0c30fc 100644 --- a/docs/source/api/remote.rst +++ b/docs/source/api/remote.rst @@ -1,5 +1,5 @@ -:mod:`pwndbg.remote` --- pwndbg.remote +:mod:`pwndbg.gdblib.remote` --- pwndbg.gdblib.remote ============================================= -.. automodule:: pwndbg.remote +.. automodule:: pwndbg.gdblib.remote :members: diff --git a/pwndbg/auxv.py b/pwndbg/auxv.py index e23f2bc5e..55b6674b4 100644 --- a/pwndbg/auxv.py +++ b/pwndbg/auxv.py @@ -9,9 +9,9 @@ import pwndbg.gdblib.arch import pwndbg.gdblib.events import pwndbg.gdblib.info import pwndbg.gdblib.memory +import pwndbg.gdblib.qemu import pwndbg.gdblib.regs import pwndbg.gdblib.typeinfo -import pwndbg.qemu import pwndbg.stack example_info_auxv_linux = """ @@ -148,7 +148,7 @@ def find_stack_boundary(addr): def walk_stack(): if not pwndbg.gdblib.abi.linux: return None - if pwndbg.qemu.is_qemu_kernel(): + if pwndbg.gdblib.qemu.is_qemu_kernel(): return None auxv = walk_stack2(0) diff --git a/pwndbg/commands/__init__.py b/pwndbg/commands/__init__.py index 64cb854f6..3be323886 100644 --- a/pwndbg/commands/__init__.py +++ b/pwndbg/commands/__init__.py @@ -224,7 +224,7 @@ def OnlyWithFile(function): if pwndbg.proc.exe: return function(*a, **kw) else: - if pwndbg.qemu.is_qemu(): + if pwndbg.gdblib.qemu.is_qemu(): print(message.error("Could not determine the target binary on QEMU.")) else: print(message.error("%s: There is no file loaded." % function.__name__)) diff --git a/pwndbg/commands/leakfind.py b/pwndbg/commands/leakfind.py index 4e5847c7d..c7e7af14d 100644 --- a/pwndbg/commands/leakfind.py +++ b/pwndbg/commands/leakfind.py @@ -184,5 +184,5 @@ def leakfind( for line in output_map[chain_length]: print(line) - if pwndbg.qemu.is_qemu(): + if pwndbg.gdblib.qemu.is_qemu(): print("\n[QEMU target detected - leakfind result might not be accurate; see `help vmmap`]") diff --git a/pwndbg/commands/vmmap.py b/pwndbg/commands/vmmap.py index 8f47ad601..1edce17a4 100644 --- a/pwndbg/commands/vmmap.py +++ b/pwndbg/commands/vmmap.py @@ -74,7 +74,7 @@ def vmmap(gdbval_or_str=None, writable=False, executable=False): continue print(M.get(page.vaddr, text=str(page))) - if pwndbg.qemu.is_qemu(): + if pwndbg.gdblib.qemu.is_qemu(): print("\n[QEMU target detected - vmmap result might not be accurate; see `help vmmap`]") diff --git a/pwndbg/elf.py b/pwndbg/elf.py index ff17786cc..e6d8c4b60 100644 --- a/pwndbg/elf.py +++ b/pwndbg/elf.py @@ -238,7 +238,7 @@ def get_ehdr(pointer): """ # This just does not work :( - if pwndbg.qemu.is_qemu(): + if pwndbg.gdblib.qemu.is_qemu(): return None, None vmmap = pwndbg.vmmap.find(pointer) diff --git a/pwndbg/file.py b/pwndbg/file.py index f82185df6..7f68b2701 100755 --- a/pwndbg/file.py +++ b/pwndbg/file.py @@ -11,8 +11,8 @@ import tempfile import gdb import pwndbg.color.message as message -import pwndbg.qemu -import pwndbg.remote +import pwndbg.gdblib.qemu +import pwndbg.gdblib.remote import pwndbg.symbol @@ -33,13 +33,13 @@ def get_file(path): path = path[7:] # len('target:') == 7 local_path = path - qemu_root = pwndbg.qemu.root() + qemu_root = pwndbg.gdblib.qemu.root() if qemu_root: return os.path.join(qemu_root, path) - elif pwndbg.remote.is_remote(): - if not pwndbg.qemu.is_qemu(): + elif pwndbg.gdblib.remote.is_remote(): + if not pwndbg.gdblib.qemu.is_qemu(): local_path = tempfile.mktemp(dir=pwndbg.symbol.remote_files_dir) error = None try: @@ -84,13 +84,13 @@ def readlink(path): Handles local, qemu-usermode, and remote debugging cases. """ - is_qemu = pwndbg.qemu.is_qemu_usermode() + is_qemu = pwndbg.gdblib.qemu.is_qemu_usermode() if is_qemu: if not os.path.exists(path): - path = os.path.join(pwndbg.qemu.root(), path) + path = os.path.join(pwndbg.gdblib.qemu.root(), path) - if is_qemu or not pwndbg.remote.is_remote(): + if is_qemu or not pwndbg.gdblib.remote.is_remote(): try: return os.readlink(path) except Exception: diff --git a/pwndbg/gdblib/android.py b/pwndbg/gdblib/android.py index f141a68d6..fb73ce9b3 100644 --- a/pwndbg/gdblib/android.py +++ b/pwndbg/gdblib/android.py @@ -3,14 +3,14 @@ import gdb import pwndbg.color.message as message import pwndbg.file import pwndbg.gdblib.events +import pwndbg.gdblib.qemu import pwndbg.lib.memoize -import pwndbg.qemu @pwndbg.lib.memoize.reset_on_start @pwndbg.lib.memoize.reset_on_exit def is_android(): - if pwndbg.qemu.is_qemu(): + if pwndbg.gdblib.qemu.is_qemu(): return False try: diff --git a/pwndbg/gdblib/memory.py b/pwndbg/gdblib/memory.py index 33ad8b7c6..b7f2472c7 100644 --- a/pwndbg/gdblib/memory.py +++ b/pwndbg/gdblib/memory.py @@ -8,8 +8,8 @@ import gdb import pwndbg.gdblib.arch import pwndbg.gdblib.events +import pwndbg.gdblib.qemu import pwndbg.gdblib.typeinfo -import pwndbg.qemu from pwndbg.lib.memory import PAGE_MASK from pwndbg.lib.memory import PAGE_SIZE @@ -334,5 +334,5 @@ def find_lower_boundary(addr, max_pages=1024): @pwndbg.gdblib.events.start def update_min_addr(): global MMAP_MIN_ADDR - if pwndbg.qemu.is_qemu_kernel(): + if pwndbg.gdblib.qemu.is_qemu_kernel(): MMAP_MIN_ADDR = 0 diff --git a/pwndbg/qemu.py b/pwndbg/gdblib/qemu.py similarity index 90% rename from pwndbg/qemu.py rename to pwndbg/gdblib/qemu.py index e6bbc22de..70003ae93 100644 --- a/pwndbg/qemu.py +++ b/pwndbg/gdblib/qemu.py @@ -7,13 +7,15 @@ import os import gdb import psutil -import pwndbg.gdblib.events -import pwndbg.remote +import pwndbg.gdblib.remote + +# TODO: `import pwndbg.gdblib.events` leads to a circular import +from pwndbg.gdblib.events import start @pwndbg.lib.memoize.reset_on_stop def is_qemu(): - if not pwndbg.remote.is_remote(): + if not pwndbg.gdblib.remote.is_remote(): return False # Example: @@ -27,7 +29,7 @@ def is_qemu(): @pwndbg.lib.memoize.reset_on_stop def is_usermode(): - if not pwndbg.remote.is_remote(): + if not pwndbg.gdblib.remote.is_remote(): return False # There is also 'qAttached' - maybe we can use it too? @@ -53,7 +55,7 @@ def is_qemu_kernel(): return is_qemu() and not is_usermode() -# @pwndbg.gdblib.events.start +@start @pwndbg.lib.memoize.reset_on_stop def root(): global binfmt_root diff --git a/pwndbg/gdblib/regs.py b/pwndbg/gdblib/regs.py index 926a8cd3e..a4e232110 100644 --- a/pwndbg/gdblib/regs.py +++ b/pwndbg/gdblib/regs.py @@ -12,9 +12,9 @@ import gdb import pwndbg.gdblib.arch import pwndbg.gdblib.events +import pwndbg.gdblib.remote import pwndbg.lib.memoize import pwndbg.proc -import pwndbg.remote from pwndbg.lib.regs import reg_sets @@ -191,7 +191,7 @@ class module(ModuleType): return get_register(regname) # We can't really do anything if the process is remote. - if pwndbg.remote.is_remote(): + if pwndbg.gdblib.remote.is_remote(): return 0 # Use the lightweight process ID diff --git a/pwndbg/remote.py b/pwndbg/gdblib/remote.py similarity index 100% rename from pwndbg/remote.py rename to pwndbg/gdblib/remote.py diff --git a/pwndbg/proc.py b/pwndbg/proc.py index a3370ddc9..0314ff6f6 100644 --- a/pwndbg/proc.py +++ b/pwndbg/proc.py @@ -10,8 +10,8 @@ from types import ModuleType import gdb +import pwndbg.gdblib.qemu import pwndbg.lib.memoize -import pwndbg.qemu class module(ModuleType): @@ -19,8 +19,8 @@ class module(ModuleType): def pid(self): # QEMU usermode emualtion always returns 42000 for some reason. # In any case, we can't use the info. - if pwndbg.qemu.is_qemu_usermode(): - return pwndbg.qemu.pid() + if pwndbg.gdblib.qemu.is_qemu_usermode(): + return pwndbg.gdblib.qemu.pid() i = gdb.selected_inferior() if i is not None: @@ -29,8 +29,8 @@ class module(ModuleType): @property def tid(self): - if pwndbg.qemu.is_qemu_usermode(): - return pwndbg.qemu.pid() + if pwndbg.gdblib.qemu.is_qemu_usermode(): + return pwndbg.gdblib.qemu.pid() i = gdb.selected_thread() if i is not None: diff --git a/pwndbg/symbol.py b/pwndbg/symbol.py index 7c20336d1..4a7e039f4 100644 --- a/pwndbg/symbol.py +++ b/pwndbg/symbol.py @@ -22,10 +22,10 @@ import pwndbg.gdblib.android import pwndbg.gdblib.arch import pwndbg.gdblib.events import pwndbg.gdblib.memory +import pwndbg.gdblib.qemu +import pwndbg.gdblib.remote import pwndbg.ida import pwndbg.lib.memoize -import pwndbg.qemu -import pwndbg.remote import pwndbg.stack import pwndbg.vmmap @@ -78,10 +78,10 @@ def reset_remote_files(): def autofetch(): """ """ global remote_files_dir - if not pwndbg.remote.is_remote(): + if not pwndbg.gdblib.remote.is_remote(): return - if pwndbg.qemu.is_qemu_usermode(): + if pwndbg.gdblib.qemu.is_qemu_usermode(): return if pwndbg.gdblib.android.is_android(): @@ -238,7 +238,7 @@ def address(symbol, allow_unmapped=False): @pwndbg.gdblib.events.stop @pwndbg.lib.memoize.reset_on_start def add_main_exe_to_symbols(): - if not pwndbg.remote.is_remote(): + if not pwndbg.gdblib.remote.is_remote(): return if pwndbg.gdblib.android.is_android(): diff --git a/pwndbg/vmmap.py b/pwndbg/vmmap.py index 29fd724d1..8f8818a68 100644 --- a/pwndbg/vmmap.py +++ b/pwndbg/vmmap.py @@ -16,12 +16,12 @@ import pwndbg.file import pwndbg.gdblib.abi import pwndbg.gdblib.events import pwndbg.gdblib.memory +import pwndbg.gdblib.qemu import pwndbg.gdblib.regs +import pwndbg.gdblib.remote import pwndbg.gdblib.typeinfo import pwndbg.lib.memoize import pwndbg.proc -import pwndbg.qemu -import pwndbg.remote import pwndbg.stack # List of manually-explored pages which were discovered @@ -56,7 +56,7 @@ def get(): if ( not pages - and pwndbg.qemu.is_qemu_kernel() + and pwndbg.gdblib.qemu.is_qemu_kernel() and pwndbg.gdblib.arch.current in ("i386", "x86-64", "aarch64", "riscv:rv64") ): if kernel_vmmap_via_pt: @@ -81,7 +81,7 @@ def get(): if pages: pages.extend(info_sharedlibrary()) else: - if pwndbg.qemu.is_qemu(): + if pwndbg.gdblib.qemu.is_qemu(): return (pwndbg.lib.memory.Page(0, pwndbg.gdblib.arch.ptrmask, 7, 0, "[qemu]"),) pages.extend(info_files()) @@ -280,7 +280,7 @@ def proc_pid_maps(): # If we debug remotely a qemu-user or qemu-system target, # there is no point of hitting things further - if pwndbg.qemu.is_qemu(): + if pwndbg.gdblib.qemu.is_qemu(): return tuple() # Example /proc/$pid/maps @@ -594,7 +594,7 @@ def check_aslr(): None is returned when we can't detect ASLR. """ # QEMU does not support this concept. - if pwndbg.qemu.is_qemu(): + if pwndbg.gdblib.qemu.is_qemu(): return None, "Could not detect ASLR on QEMU targets" # Systemwide ASLR is disabled