[Lldb-commits] [PATCH] D77123: [lldb] Inherit host environment when running shell commands

Pavel Labath via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Mar 31 02:42:49 PDT 2020


labath created this revision.
labath added reviewers: jingham, friss.
Herald added a project: LLDB.
labath added a comment.

This stems from D76835 <https://reviews.llvm.org/D76835> -- when the inherit fallback on windows gets removed some tests fail bacause the can't find `ls`. This is used in `lldbutil.wait_for_file_on_target`. The inheritance was not a problem because `ls` is normally in the hardcoded default path, but that is not the case on windows.


On most hosts we were running shell commands with an empty environment.
The only exception was windows, which was inheriting the host enviroment
mostly by accident.

Running the commands in an empty environment does not sound like a
sensible default, so this patch changes Host::RunShellCommand to inherit
the host environment.  This impacts both commands run via
SBPlatform::Run (in case of host platforms), as well as the "platform
shell" CLI command.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77123

Files:
  lldb/source/Host/common/Host.cpp
  lldb/test/API/python_api/sbplatform/Makefile
  lldb/test/API/python_api/sbplatform/TestSBPlatform.py
  lldb/test/API/python_api/sbplatform/main.cpp


Index: lldb/test/API/python_api/sbplatform/main.cpp
===================================================================
--- /dev/null
+++ lldb/test/API/python_api/sbplatform/main.cpp
@@ -0,0 +1,8 @@
+#include <cstdlib>
+#include <cstdio>
+
+int main() {
+  printf("MY_TEST_ENV_VAR=%s\n", getenv("MY_TEST_ENV_VAR"));
+
+  return 0;
+}
Index: lldb/test/API/python_api/sbplatform/TestSBPlatform.py
===================================================================
--- /dev/null
+++ lldb/test/API/python_api/sbplatform/TestSBPlatform.py
@@ -0,0 +1,22 @@
+"""Test the SBPlatform APIs."""
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+class SBPlatformAPICase(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+    NO_DEBUG_INFO_TESTCASE = True
+
+    @add_test_categories(['pyapi'])
+    def test_run(self):
+        self.build()
+        plat = lldb.SBPlatform.GetHostPlatform()
+
+        os.environ["MY_TEST_ENV_VAR"]="SBPlatformAPICase.test_run"
+        def cleanup():
+            del os.environ["MY_TEST_ENV_VAR"]
+        self.addTearDownHook(cleanup)
+        cmd = lldb.SBPlatformShellCommand(self.getBuildArtifact("a.out"))
+        self.assertTrue(plat.Run(cmd).Success())
+        self.assertIn("MY_TEST_ENV_VAR=SBPlatformAPICase.test_run", cmd.GetOutput())
Index: lldb/test/API/python_api/sbplatform/Makefile
===================================================================
--- /dev/null
+++ lldb/test/API/python_api/sbplatform/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
Index: lldb/source/Host/common/Host.cpp
===================================================================
--- lldb/source/Host/common/Host.cpp
+++ lldb/source/Host/common/Host.cpp
@@ -501,6 +501,8 @@
     launch_info.SetArguments(args, first_arg_is_executable);
   }
 
+  launch_info.GetEnvironment() = Host::GetEnvironment();
+
   if (working_dir)
     launch_info.SetWorkingDirectory(working_dir);
   llvm::SmallString<64> output_file_path;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77123.253814.patch
Type: text/x-patch
Size: 2010 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200331/def37149/attachment-0001.bin>


More information about the lldb-commits mailing list