[Lldb-commits] [PATCH] D76105: [lldb/settings] Reset the inferior environment when target.inherit-env is toggled

Frederic Riss via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Mar 12 15:45:27 PDT 2020


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

Allow the target.env-vars property to be recomputed when the
target.inherit-env property changes. This allows to change
the value of the property between runs and get a meaningful
behavior.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76105

Files:
  lldb/source/Target/Target.cpp
  lldb/test/API/commands/settings/TestSettings.py


Index: lldb/test/API/commands/settings/TestSettings.py
===================================================================
--- lldb/test/API/commands/settings/TestSettings.py
+++ lldb/test/API/commands/settings/TestSettings.py
@@ -293,6 +293,23 @@
                 "The host environment variable 'MY_HOST_ENV_VAR1' successfully passed.",
                 "The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])
 
+        self.runCmd('settings set target.inherit-env false')
+
+        self.addTearDownHook(unset_env_variables)
+        self.runCmd("process launch --working-dir '{0}'".format(self.get_process_working_directory()),
+                RUN_SUCCEEDED)
+
+        # Read the output file produced by running the program.
+        output = lldbutil.read_file_from_process_wd(self, "output1.txt")
+
+        self.expect(
+            output,
+            matching=False,
+            exe=False,
+            substrs=[
+                "The host environment variable 'MY_HOST_ENV_VAR1' successfully passed.",
+                "The host environment variable 'MY_HOST_ENV_VAR2' successfully passed."])
+
     @skipIfDarwinEmbedded   # <rdar://problem/34446098> debugserver on ios etc can't write files
     def test_set_error_output_path(self):
         """Test that setting target.error/output-path for the launched process works."""
Index: lldb/source/Target/Target.cpp
===================================================================
--- lldb/source/Target/Target.cpp
+++ lldb/source/Target/Target.cpp
@@ -3394,36 +3394,43 @@
 
 protected:
   void GetHostEnvironmentIfNeeded() const {
-    if (!m_got_host_env) {
-      if (m_target) {
-        m_got_host_env = true;
-        const uint32_t idx = ePropertyInheritEnv;
-        if (GetPropertyAtIndexAsBoolean(
-                nullptr, idx, g_target_properties[idx].default_uint_value != 0)) {
-          PlatformSP platform_sp(m_target->GetPlatform());
-          if (platform_sp) {
-            Environment env = platform_sp->GetEnvironment();
-            OptionValueDictionary *env_dict =
-                GetPropertyAtIndexAsOptionValueDictionary(nullptr,
-                                                          ePropertyEnvVars);
-            if (env_dict) {
-              const bool can_replace = false;
-              for (const auto &KV : env) {
-                // Don't allow existing keys to be replaced with ones we get
-                // from the platform environment
-                env_dict->SetValueForKey(
-                    ConstString(KV.first()),
-                    OptionValueSP(new OptionValueString(KV.second.c_str())),
-                    can_replace);
-              }
-            }
-          }
+    if (!m_target)
+      return;
+
+    const uint32_t idx = ePropertyInheritEnv;
+    bool should_inherit = GetPropertyAtIndexAsBoolean(
+        nullptr, idx, g_target_properties[idx].default_uint_value != 0);
+
+    if (!m_got_host_env || should_inherit != m_host_env_inherited) {
+      m_got_host_env = true;
+      PlatformSP platform_sp(m_target->GetPlatform());
+      if (platform_sp) {
+        m_host_env_inherited = should_inherit;
+
+        Environment env = platform_sp->GetEnvironment();
+        OptionValueDictionary *env_dict =
+            GetPropertyAtIndexAsOptionValueDictionary(nullptr,
+                                                      ePropertyEnvVars);
+        assert(env_dict && "The target.env-vars property doesn't exist.");
+        if (should_inherit) {
+          // Don't allow existing keys to be replaced with ones we get
+          // from the platform environment
+          const bool can_replace = false;
+          for (const auto &KV : env)
+            env_dict->SetValueForKey(
+                ConstString(KV.first()),
+                OptionValueSP(new OptionValueString(KV.second.c_str())),
+                can_replace);
+        } else {
+          for (const auto &KV : env)
+            env_dict->DeleteValueForKey(ConstString(KV.first()));
         }
       }
     }
   }
   Target *m_target;
   mutable bool m_got_host_env;
+  mutable bool m_host_env_inherited;
 };
 
 // TargetProperties


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76105.250076.patch
Type: text/x-patch
Size: 4164 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200312/13674a9e/attachment.bin>


More information about the lldb-commits mailing list