@ -4,11 +4,10 @@ import locale
import os
import os
import sys
import sys
import time
import time
from glob import glob
from os import environ
from os import environ
from os import path
from os import path
import pkg_resources
_profiler = cProfile . Profile ( )
_profiler = cProfile . Profile ( )
_start_time = None
_start_time = None
@ -16,73 +15,31 @@ if environ.get("PWNDBG_PROFILE") == "1":
_start_time = time . time ( )
_start_time = time . time ( )
_profiler . enable ( )
_profiler . enable ( )
# Allow users to use packages from a virtualenv
directory , file = path . split ( __file__ )
# That's not 100% supported, but they do it on their own,
directory = path . expanduser ( directory )
# so we will warn them if the GDB's Python is not virtualenv's Python
directory = path . abspath ( directory )
virtual_env = environ . get ( " VIRTUAL_ENV " )
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
possible_site_packages = glob . glob (
path . join ( virtual_env , " lib " , " python* " , " site-packages " )
)
if len ( possible_site_packages ) > 1 :
venv_warn ( " *** Found multiple site packages in virtualenv: " )
for site_pkg in possible_site_packages :
venv_warn ( f " - { site_pkg } " )
virtualenv_site_packages = possible_site_packages [ - 1 ]
venv_warn ( f " *** Using the last one: { virtualenv_site_packages } " )
elif len ( possible_site_packages ) == 1 :
virtualenv_site_packages = possible_site_packages [ - 1 ]
venv_warn ( f " *** Using the only site packages dir found: { virtualenv_site_packages } " )
else :
# Get virtualenv's site-packages path
guessed_python_directory = f " python { sys . version_info . major } . { sys . version_info . minor } "
venv_path = os . environ . get ( " PWNDBG_VENV_PATH " )
virtualenv_site_packages = path . join (
if not venv_path :
virtual_env , " lib " , guessed_python_directory , " site-packages "
venv_path = os . path . join ( directory , " .venv " )
)
venv_warn (
f " *** Not found site-packages in virtualenv, using guessed site packages Python dir: { virtualenv_site_packages } "
)
venv_warn ( " Added detected virtualenv ' s Python site packages to sys.path " )
if not os . path . exists ( venv_path ) :
venv_warn ( " " )
print ( f " Cannot find Pwndbg virtualenv directory: { venv_path } : please re-run setup.sh " )
sys . path . append ( virtualenv_site_packages )
sys . exit ( 1 )
site_pkgs_path = glob ( os . path . join ( venv_path , " lib/*/site-packages " ) ) [ 0 ]
directory , file = path . split ( __file__ )
# Set virtualenv's bin path (needed for utility tools like ropper, pwntools etc)
directory = path . expanduser ( directory )
bin_path = os . path . join ( venv_path , " bin " )
directory = path . abspath ( directory )
os . environ [ " PATH " ] = bin_path + os . pathsep + os . environ . get ( " PATH " )
# Add gdb-pt-dump directory to sys.path so it can be imported
# Add gdb-pt-dump directory to sys.path so it can be imported
gdbpt = path . join ( directory , " gdb-pt-dump " )
gdbpt = path . join ( directory , " gdb-pt-dump " )
sys . path . append ( directory )
sys . path . append ( directory )
sys . path . append ( site_pkgs_path )
sys . path . append ( gdbpt )
sys . path . append ( gdbpt )
# 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
# warn if the user has different encoding than utf-8
encoding = locale . getpreferredencoding ( )
encoding = locale . getpreferredencoding ( )