Support starting the Ipython prompt with `ipi`

This commit is contained in:
lebr0nli 2022-09-27 13:23:54 +08:00 committed by Disconnect3d
parent df26a11b57
commit 6a3faa0a17
2 changed files with 41 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import pwndbg.commands.got
import pwndbg.commands.heap
import pwndbg.commands.hexdump
import pwndbg.commands.ida
import pwndbg.commands.ipython_interactive
import pwndbg.commands.leakfind
import pwndbg.commands.memoize
import pwndbg.commands.misc

View File

@ -0,0 +1,40 @@
"""
Command to start an interactive IPython prompt.
"""
import sys
from contextlib import contextmanager
import gdb
import pwndbg.commands
@contextmanager
def switch_to_ipython_env():
"""We need to change stdout/stderr to the default ones, otherwise we can't use tab or autocomplete"""
# Save GDB's stdout and stderr
saved_stdout = sys.stdout
saved_stderr = sys.stderr
# Use Python's default stdout and stderr
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
yield
# Restore GDB's stdout and stderr
sys.stdout = saved_stdout
sys.stderr = saved_stderr
# Restore Python's default ps1 and ps2 for GDB's `pi` command
sys.ps1 = ">>> "
sys.ps2 = "... "
@pwndbg.commands.ArgparsedCommand("Start an interactive IPython prompt.")
def ipi():
with switch_to_ipython_env():
# Use `gdb.execute` to embed IPython into GDB's variable scope
code4ipython = """import IPython
import pwn
IPython.embed(colors='neutral',banner1='',confirm_exit=False,simple_prompt=False)
""".strip().replace(
"\n", ";"
)
gdb.execute(f"pi {code4ipython}")