pwndbg/gdbinit.py

116 lines
3.9 KiB
Python
Raw Normal View History

import cProfile
2022-07-16 23:40:44 +08:00
import glob
import locale
import os
2017-02-11 03:00:07 +08:00
import sys
import time
from os import environ
from os import path
2022-07-16 23:40:44 +08:00
import pkg_resources
_profiler = cProfile.Profile()
_start_time = None
if environ.get("PWNDBG_PROFILE") == "1":
_start_time = time.time()
_profiler.enable()
2022-07-17 00:24:55 +08:00
# Allow users to use packages from a virtualenv
# That's not 100% supported, but they do it on their own,
# so we will warn them if the GDB's Python is not virtualenv's Python
2022-08-30 08:53:00 +08:00
virtual_env = environ.get("VIRTUAL_ENV")
2022-07-16 23:40:44 +08:00
if virtual_env:
py_exe_matches = sys.executable.startswith(virtual_env)
if not py_exe_matches:
venv_warning = int(environ.get("PWNDBG_VENV_WARNING", 1))
if venv_warning:
print(
f"""WARNING: Pwndbg/GDB run in virtualenv with which it may not work correctly ***
Detected Python virtual environment: VIRTUAL_ENV='{virtual_env}'
while GDB is built with different Python binary: {sys.executable}
Assuming that you installed Pwndbg dependencies into the virtual environment
If this is not true, this may cause import errors or other issues in Pwndbg
If all works for you, you can suppress this warning and all further prints
by setting `export PWNDBG_VENV_WARNING=0` (e.g. in ~/.bashrc or ~/.zshrc etc.)"""
)
venv_warn = print
else:
venv_warn = lambda *a, **kw: None
2022-07-17 00:24:55 +08:00
2022-08-30 08:53:00 +08:00
possible_site_packages = glob.glob(
path.join(virtual_env, "lib", "python*", "site-packages")
)
2022-07-17 00:24:55 +08:00
if len(possible_site_packages) > 1:
venv_warn("*** Found multiple site packages in virtualenv:")
for site_pkg in possible_site_packages:
venv_warn(" - %s" % site_pkg)
virtualenv_site_packages = possible_site_packages[-1]
venv_warn("*** Using the last one: %s" % virtualenv_site_packages)
elif len(possible_site_packages) == 1:
virtualenv_site_packages = possible_site_packages[-1]
venv_warn("*** Using the only site packages dir found: %s" % virtualenv_site_packages)
else:
2022-08-30 08:53:00 +08:00
guessed_python_directory = "python%s.%s" % (
sys.version_info.major,
sys.version_info.minor,
)
virtualenv_site_packages = path.join(
virtual_env, "lib", guessed_python_directory, "site-packages"
)
venv_warn(
"*** Not found site-packages in virtualenv, using guessed site packages Python dir: %s"
% virtualenv_site_packages
)
2022-07-17 00:24:55 +08:00
venv_warn(" Added detected virtualenv's Python site packages to sys.path")
venv_warn("")
2022-07-17 00:24:55 +08:00
sys.path.append(virtualenv_site_packages)
2015-02-14 00:08:56 +08:00
directory, file = path.split(__file__)
2022-08-30 08:53:00 +08:00
directory = path.expanduser(directory)
directory = path.abspath(directory)
2015-02-14 00:08:56 +08:00
# Add gdb-pt-dump directory to sys.path so it can be imported
2022-08-30 08:53:00 +08:00
gdbpt = path.join(directory, "gdb-pt-dump")
2015-02-14 00:08:56 +08:00
sys.path.append(directory)
2021-10-23 21:21:52 +08:00
sys.path.append(gdbpt)
2015-02-14 00:08:56 +08:00
# Add the dir where Pwntools binaries might be into PATH
pwntools_bin_dir = os.path.join(pkg_resources.get_distribution("pwntools").location, "bin")
os.environ["PATH"] = os.environ.get("PATH") + os.pathsep + pwntools_bin_dir
# warn if the user has different encoding than utf-8
encoding = locale.getpreferredencoding()
2022-08-30 08:53:00 +08:00
if encoding != "UTF-8":
print("******")
print(
"Your encoding ({}) is different than UTF-8. pwndbg might not work properly.".format(
encoding
)
)
gdbinit.py: fix message when locales are wrong (#1573) * gdbinit.py: fix message when locales are wrong Apparently the suggested solution is not great: ``` nix@33843c903468:~$ locale -a C C.UTF-8 POSIX en_US.utf8 nix@33843c903468:~$ LC_ALL=en_US.UTF-8 PYTHONIOENCODING=UTF-8 pwndbg /nix/store/qqa28hmysc23yy081d178jfd9a1yk8aw-bash-5.2-p15/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8) ****** Your encoding (ANSI_X3.4-1968) is different than UTF-8. pwndbg might not work properly. You might try launching gdb with: LC_ALL=en_US.UTF-8 PYTHONIOENCODING=UTF-8 gdb Make sure that en_US.UTF-8 is activated in /etc/locale.gen and you called locale-gen ****** pwndbg: loaded 164 pwndbg commands and 42 shell commands. Type pwndbg [--shell | --all] [filter] for a list. pwndbg: created $rebase, $ida gdb functions (can be used with print/break) Traceback (most recent call last): File "/nix/store/x2yncb885vd33dgigwfwc6qamjxs4d7h-pwndbg-2022.12.19/share/pwndbg/gdbinit.py", line 84, in <module> import pwndbg # noqa: F401 File "/nix/store/x2yncb885vd33dgigwfwc6qamjxs4d7h-pwndbg-2022.12.19/share/pwndbg/pwndbg/__init__.py", line 113, in <module> config_mod.init_params() File "/nix/store/x2yncb885vd33dgigwfwc6qamjxs4d7h-pwndbg-2022.12.19/share/pwndbg/pwndbg/gdblib/config.py", line 114, in init_params Parameter(p) File "/nix/store/x2yncb885vd33dgigwfwc6qamjxs4d7h-pwndbg-2022.12.19/share/pwndbg/pwndbg/gdblib/config.py", line 46, in __init__ self.value = param.value UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) ------- tip of the day (disable with set show-tips off) ------- Use the procinfo command for better process introspection (than the GDB's info proc command) pwndbg> quit nix@33843c903468:~$ LC_ALL=C.UTF-8 PYTHONIOENCODING=UTF-8 pwndbg pwndbg: loaded 164 pwndbg commands and 42 shell commands. Type pwndbg [--shell | --all] [filter] for a list. pwndbg: created $rebase, $ida gdb functions (can be used with print/break) ------- tip of the day (disable with set show-tips off) ------- Want to display each context panel in a separate tmux window? See https://github.com/pwndbg/pwndbg/blob/dev/FEATURES.md#splitting--layouting-context pwndbg> quit nix@33843c903468:~$ LC_ALL=C.UTF-8 pwndbg pwndbg: loaded 164 pwndbg commands and 42 shell commands. Type pwndbg [--shell | --all] [filter] for a list. pwndbg: created $rebase, $ida gdb functions (can be used with print/break) ------- tip of the day (disable with set show-tips off) ------- Use the telescope command to dereference a given address/pointer multiple times (if the dereferenced value is a valid ptr; see config telescope to configure its behavior) pwndbg> quit ``` * fix lint
2023-02-09 07:51:28 +08:00
print("You might try launching GDB with:")
print(" LC_CTYPE=C.UTF-8 gdb")
print(
"If that does not work, make sure that en_US.UTF-8 is uncommented in /etc/locale.gen and that you called `locale-gen` command"
)
2022-08-30 08:53:00 +08:00
print("******")
environ["PWNLIB_NOTERM"] = "1"
2022-09-09 10:56:26 +08:00
import pwndbg # noqa: F401
import pwndbg.profiling
pwndbg.profiling.init(_profiler, _start_time)
if environ.get("PWNDBG_PROFILE") == "1":
pwndbg.profiling.profiler.stop("pwndbg-load.pstats")
pwndbg.profiling.profiler.start()