mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2026-09-28 17:11:11 -09:00
GP-0: If pyghidra.sys.modules.restore is set, PyGhidra will restore
sys.modules each time a script is run (Closes #8529)
This commit is contained in:
@@ -558,8 +558,12 @@ import pdb_ # imports Ghidra's pdb
|
||||
```
|
||||
## Change History
|
||||
__3.0.0:__
|
||||
* Revised the the PyGhidra API. See the [API section](#api) for more details.
|
||||
* Revised the PyGhidra API. See the [API section](#api) for more details.
|
||||
* PyGhidra 3.0.0 requires Ghidra 12.0 or later to run.
|
||||
* If PyGhidra sees that the `pyghidra.sys.modules.restore` Java system property is set (typically
|
||||
via the `support/launch.properties` file), it will restore `sys.modules` to its prior state after
|
||||
a PyGhidra script is run so the next time the script is run, it freshly loads all of its imported
|
||||
modules again. This is experimental and should only be enabled if necessary.
|
||||
|
||||
__2.2.1:__
|
||||
* PyGhidra now launches with the current working directory removed from `sys.path` to prevent
|
||||
|
||||
@@ -219,6 +219,8 @@ class PyGhidraScript(dict):
|
||||
:param script_path: The path of the python script
|
||||
:param script_args: The arguments for the python script
|
||||
"""
|
||||
from java.lang import Boolean # type:ignore @UnresolvedImport
|
||||
|
||||
sf = self._script.getSourceFile()
|
||||
if sf is None and script_path is None:
|
||||
return
|
||||
@@ -232,6 +234,7 @@ class PyGhidraScript(dict):
|
||||
self._script.setScriptArgs(script_args)
|
||||
|
||||
orig_argv = sys.argv
|
||||
orig_modules = sys.modules.copy()
|
||||
script_root = str(Path(script_path).parent)
|
||||
|
||||
# honor the python safe_path flag introduced in 3.11
|
||||
@@ -267,6 +270,10 @@ class PyGhidraScript(dict):
|
||||
finally:
|
||||
sys.argv = orig_argv
|
||||
|
||||
if Boolean.getBoolean("pyghidra.sys.modules.restore"):
|
||||
for k in list(set(sys.modules) - set(orig_modules)):
|
||||
sys.modules.pop(k, None)
|
||||
|
||||
if not safe_path:
|
||||
sys.path.remove(script_root)
|
||||
|
||||
|
||||
@@ -151,3 +151,8 @@ VMARGS=--enable-native-access=ALL-UNNAMED
|
||||
|
||||
# Disables the loaders echoing their message log to the application.log file
|
||||
#VMARGS=-Ddisable.loader.logging=true
|
||||
|
||||
# Restores sys.modules to its prior state after a PyGhidra script is run so the next time the script
|
||||
# is run, it freshly reloads all of its imported modules again. This is experimental and should only
|
||||
# be enabled if necessary.
|
||||
#VMARGS=-Dpyghidra.sys.modules.restore=true
|
||||
|
||||
Reference in New Issue
Block a user