[Lldb-commits] [lldb] r180201 - <rdar://problem/13665446>

Greg Clayton gclayton at apple.com
Wed Apr 24 10:53:59 PDT 2013


Author: gclayton
Date: Wed Apr 24 12:53:59 2013
New Revision: 180201

URL: http://llvm.org/viewvc/llvm-project?rev=180201&view=rev
Log:
<rdar://problem/13665446>

Pass the user set target.env-vars when launching through a terminal.


Modified:
    lldb/trunk/source/Host/macosx/Host.mm
    lldb/trunk/tools/darwin-debug/darwin-debug.cpp

Modified: lldb/trunk/source/Host/macosx/Host.mm
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/macosx/Host.mm?rev=180201&r1=180200&r2=180201&view=diff
==============================================================================
--- lldb/trunk/source/Host/macosx/Host.mm (original)
+++ lldb/trunk/source/Host/macosx/Host.mm Wed Apr 24 12:53:59 2013
@@ -527,6 +527,32 @@ LaunchInNewTerminalWithAppleScript (cons
     if (launch_info.GetFlags().Test (eLaunchFlagDisableASLR))
         command.PutCString(" --disable-aslr");
     
+    // We are launching on this host in a terminal. So compare the environemnt on the host
+    // to what is supplied in the launch_info. Any items that aren't in the host environemnt
+    // need to be sent to darwin-debug. If we send all environment entries, we might blow the
+    // max command line length, so we only send user modified entries.
+    const char **envp = launch_info.GetEnvironmentEntries().GetConstArgumentVector ();
+    StringList host_env;
+    const size_t host_env_count = Host::GetEnvironment (host_env);
+    const char *env_entry;
+    for (size_t env_idx = 0; (env_entry = envp[env_idx]) != NULL; ++env_idx)
+    {
+        bool add_entry = true;
+        for (size_t i=0; i<host_env_count; ++i)
+        {
+            const char *host_env_entry = host_env.GetStringAtIndex(i);
+            if (strcmp(env_entry, host_env_entry) == 0)
+            {
+                add_entry = false;
+                break;
+            }
+        }
+        if (add_entry)
+        {
+            command.Printf(" --env='%s'", env_entry);
+        }
+    }
+
     command.PutCString(" -- ");
 
     const char **argv = launch_info.GetArguments().GetConstArgumentVector ();

Modified: lldb/trunk/tools/darwin-debug/darwin-debug.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/darwin-debug/darwin-debug.cpp?rev=180201&r1=180200&r2=180201&view=diff
==============================================================================
--- lldb/trunk/tools/darwin-debug/darwin-debug.cpp (original)
+++ lldb/trunk/tools/darwin-debug/darwin-debug.cpp Wed Apr 24 12:53:59 2013
@@ -24,6 +24,7 @@
 //----------------------------------------------------------------------
 #if defined (__APPLE__)
 
+#include <crt_externs.h> // for _NSGetEnviron()
 #include <getopt.h>
 #include <limits.h>
 #include <mach/machine.h>
@@ -54,6 +55,7 @@ static struct option g_long_options[] =
 	{ "setsid",         no_argument,		NULL,           's'		},
 	{ "unix-socket",    required_argument,  NULL,           'u'		},
     { "working-dir",    required_argument,  NULL,           'w'     },
+    { "env",            required_argument,  NULL,           'E'     },
 	{ NULL,				0,					NULL,            0		}
 };
 
@@ -181,10 +183,10 @@ int main (int argc, char *const *argv, c
     bool show_usage = false;
     int ch;
     int disable_aslr = 0; // By default we disable ASLR
-    int pass_env = 1;
+    bool pass_env = true;
     std::string unix_socket_name;
     std::string working_dir;
-	while ((ch = getopt_long_only(argc, argv, "a:dehsu:?", g_long_options, NULL)) != -1)
+	while ((ch = getopt_long_only(argc, argv, "a:deE:hsu:?", g_long_options, NULL)) != -1)
 	{
 		switch (ch) 
 		{
@@ -213,9 +215,29 @@ int main (int argc, char *const *argv, c
             break;            
 
         case 'e':
-            pass_env = 0;
+            pass_env = false;
             break;
-        
+            
+        case 'E':
+            {
+                // Since we will exec this program into our new program, we can just set environment
+                // varaibles in this process and they will make it into the child process.
+                std::string name;
+                std::string value;
+                const char *equal_pos = strchr (optarg, '=');
+                if (equal_pos)
+                {
+                    name.assign (optarg, equal_pos - optarg);
+                    value.assign (equal_pos + 1);
+                }
+                else
+                {
+                    name = optarg;
+                }
+                ::setenv (name.c_str(), value.c_str(), 1);
+            }
+            break;
+            
         case 's':
             // Create a new session to avoid having control-C presses kill our current
             // terminal session when this program is launched from a .command file
@@ -309,8 +331,8 @@ int main (int argc, char *const *argv, c
 
     // Now we posix spawn to exec this process into the inferior that we want
     // to debug.
-    posix_spawn_for_debug (argv, 
-                           pass_env ? envp : NULL, 
+    posix_spawn_for_debug (argv,
+                           pass_env ? *_NSGetEnviron() : NULL, // Pass current environment as we may have modified it if "--env" options was used, do NOT pass "envp" here
                            working_dir.empty() ? NULL : working_dir.c_str(),
                            cpu_type, 
                            disable_aslr);





More information about the lldb-commits mailing list