Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp =================================================================== --- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -537,6 +537,8 @@ PyRun_SimpleString(run_string.GetData()); run_string.Clear(); + SetPythonEnvironment(m_debugger); + run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from " "lldb.embedded_interpreter import run_python_interpreter; " "from lldb.embedded_interpreter import run_one_line')", @@ -3269,6 +3271,26 @@ "from lldb.embedded_interpreter import run_one_line"); } +void ScriptInterpreterPythonImpl::SetPythonEnvironment(Debugger &debugger) { + StreamString run_string; + lldb::TargetSP target_get = debugger.GetTargetList().GetSelectedTarget(); + const Environment &env = target_get->GetGlobalProperties()->GetEnvironment(); + + PyRun_SimpleString("import os"); + for (const auto &KV : env) { + if (strcmp(KV.getKey().data(), "PYTHONPATH") == 0 || + strcmp(KV.getKey().data(), "PATH") == 0) { + run_string.Clear(); + run_string.Printf( + "os.environ[\"%s\"] = os.environ.get(\"%s\",\"\")+ os.pathsep +" + "\"%s\"", + KV.getKey().data(), KV.getKey().data(), KV.getValue().data()); + PyRun_SimpleString(run_string.GetData()); + AddToSysPath(AddLocation::End, KV.getValue().data()); + } + } +} + void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation location, std::string path) { std::string path_copy; Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h =================================================================== --- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h +++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h @@ -341,6 +341,8 @@ lldb::user_id_t watch_id); static void InitializePrivate(); + static void SetPythonEnvironment(Debugger &debugger); + class SynchronicityHandler { private: lldb::DebuggerSP m_debugger_sp; Index: lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py =================================================================== --- /dev/null +++ lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py @@ -0,0 +1,10 @@ +import os +import sys + +def test(debugger, command, result, internal_dict): + print(os.environ["PYTHONPATH"]) + print(sys.path) + +def __lldb_init_module(debugger, internal_dict): + debugger.HandleCommand('command script add -f environ.test test') + Index: lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test =================================================================== --- /dev/null +++ lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test @@ -0,0 +1,10 @@ +# REQUIRES: python + +# RUN: %lldb --batch \ +# RUN: --script-language python \ +# RUN: -O 'settings append target.env-vars PYTHONPATH=/tmp' \ +# RUN: -O 'command script import %S/Inputs/environ.py' \ +# RUN: -O 'test' 2>&1 | FileCheck %s + +# CHECK: :/tmp +# CHECK: '/tmp'