[Lldb-commits] [lldb] r166137 - in /lldb/trunk: include/lldb/Target/Process.h include/lldb/Target/Target.h source/Commands/CommandObjectPlatform.cpp source/Commands/CommandObjectProcess.cpp source/Commands/CommandObjectTarget.cpp source/Host/common/Host.cpp source/Target/Process.cpp source/Target/Target.cpp test/expression_command/test/TestExprs.py

Greg Clayton gclayton at apple.com
Wed Oct 17 15:57:13 PDT 2012


Author: gclayton
Date: Wed Oct 17 17:57:12 2012
New Revision: 166137

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

LLDB changes argv[0] when debugging a symlink. Now we have the notion of argv0 in the target settings:

target.arg0 (string) = 

There is also the program argument that are separate from the first argument that have existed for a while:

target.run-args (arguments) =

When running "target create <exe>", we will place the untouched "<exe>" into target.arg0 to ensure when we run, we run with what the user typed. This has been added to the ProcessLaunchInfo and all other needed places so we always carry around the:
- resolved executable path
- argv0
- program args

Some systems may not support separating argv0 from the resolved executable path and the ProcessLaunchInfo needs to carry all of this information along so that each platform can make that decision.



Modified:
    lldb/trunk/include/lldb/Target/Process.h
    lldb/trunk/include/lldb/Target/Target.h
    lldb/trunk/source/Commands/CommandObjectPlatform.cpp
    lldb/trunk/source/Commands/CommandObjectProcess.cpp
    lldb/trunk/source/Commands/CommandObjectTarget.cpp
    lldb/trunk/source/Host/common/Host.cpp
    lldb/trunk/source/Target/Process.cpp
    lldb/trunk/source/Target/Target.cpp
    lldb/trunk/test/expression_command/test/TestExprs.py

Modified: lldb/trunk/include/lldb/Target/Process.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Process.h?rev=166137&r1=166136&r2=166137&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Process.h (original)
+++ lldb/trunk/include/lldb/Target/Process.h Wed Oct 17 17:57:12 2012
@@ -242,15 +242,28 @@
         return m_arguments;
     }
     
+    const char *
+    GetArg0 () const
+    {
+        if (m_arg0.empty())
+            return NULL;
+        return m_arg0.c_str();
+    }
+    
+    void
+    SetArg0 (const char *arg)
+    {
+        if (arg && arg[0])
+            m_arg0.clear();
+        else
+            m_arg0 = arg;
+    }
+    
     void
-    SetArguments (const Args& args, 
-                  bool first_arg_is_executable,
-                  bool first_arg_is_executable_and_argument);
+    SetArguments (const Args& args, bool first_arg_is_executable);
 
     void
-    SetArguments (char const **argv,
-                  bool first_arg_is_executable,
-                  bool first_arg_is_executable_and_argument);
+    SetArguments (char const **argv, bool first_arg_is_executable);
     
     Args &
     GetEnvironmentEntries ()
@@ -266,7 +279,11 @@
     
 protected:
     FileSpec m_executable;
-    Args m_arguments;
+    std::string m_arg0; // argv[0] if supported. If empty, then use m_executable.
+                        // Not all process plug-ins support specifying an argv[0]
+                        // that differs from the resolved platform executable
+                        // (which is in m_executable)
+    Args m_arguments;   // All program arguments except argv[0]
     Args m_environment;
     uint32_t m_uid;
     uint32_t m_gid;    

Modified: lldb/trunk/include/lldb/Target/Target.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Target.h?rev=166137&r1=166136&r2=166137&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Target.h (original)
+++ lldb/trunk/include/lldb/Target/Target.h Wed Oct 17 17:57:12 2012
@@ -83,6 +83,12 @@
     InlineStrategy
     GetInlineStrategy () const;
 
+    const char *
+    GetArg0 () const;
+    
+    void
+    SetArg0 (const char *arg);
+
     bool
     GetRunArguments (Args &args) const;
     

Modified: lldb/trunk/source/Commands/CommandObjectPlatform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectPlatform.cpp?rev=166137&r1=166136&r2=166137&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectPlatform.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectPlatform.cpp Wed Oct 17 17:57:12 2012
@@ -416,9 +416,7 @@
                     // We don't have any file yet, so the first argument is our
                     // executable, and the rest are program arguments
                     const bool first_arg_is_executable = true;
-                    m_options.launch_info.SetArguments (args, 
-                                                        first_arg_is_executable, 
-                                                        first_arg_is_executable);
+                    m_options.launch_info.SetArguments (args, first_arg_is_executable);
                 }
             }
             

Modified: lldb/trunk/source/Commands/CommandObjectProcess.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectProcess.cpp?rev=166137&r1=166136&r2=166137&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectProcess.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectProcess.cpp Wed Oct 17 17:57:12 2012
@@ -127,11 +127,6 @@
             return false;
         }
         
-        exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
-
-        const bool add_exe_file_as_first_arg = true;
-        m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), add_exe_file_as_first_arg);
-        
         StateType state = eStateInvalid;
         Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
         if (process)
@@ -167,18 +162,32 @@
             }
         }
         
+        const char *target_settings_argv0 = target->GetArg0();
+        
+        exe_module->GetFileSpec().GetPath (filename, sizeof(filename));
+        
+        if (target_settings_argv0)
+        {
+            m_options.launch_info.GetArguments().AppendArgument (target_settings_argv0);
+            m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), false);
+        }
+        else
+        {
+            m_options.launch_info.SetExecutableFile(exe_module->GetPlatformFileSpec(), true);
+        }
+
         if (launch_args.GetArgumentCount() == 0)
         {
             Args target_setting_args;
-            if (target->GetRunArguments(target_setting_args) > 0)
+            if (target->GetRunArguments(target_setting_args))
                 m_options.launch_info.GetArguments().AppendArguments (target_setting_args);
         }
         else
         {
+            m_options.launch_info.GetArguments().AppendArguments (launch_args);
+
             // Save the arguments for subsequent runs in the current target.
             target->SetRunArguments (launch_args);
-
-            m_options.launch_info.GetArguments().AppendArguments (launch_args);
         }
         
         if (target->GetDisableASLR())

Modified: lldb/trunk/source/Commands/CommandObjectTarget.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectTarget.cpp?rev=166137&r1=166136&r2=166137&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectTarget.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectTarget.cpp Wed Oct 17 17:57:12 2012
@@ -239,6 +239,13 @@
 
             if (target_sp)
             {
+                if (file_path)
+                {
+                    // Use exactly what the user typed as the first argument
+                    // when we exec or posix_spawn
+                    target_sp->SetArg0 (file_path);
+                }
+
                 debugger.GetTargetList().SetSelectedTarget(target_sp.get());
                 if (core_file)
                 {

Modified: lldb/trunk/source/Host/common/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Host.cpp?rev=166137&r1=166136&r2=166137&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Host.cpp (original)
+++ lldb/trunk/source/Host/common/Host.cpp Wed Oct 17 17:57:12 2012
@@ -1328,8 +1328,7 @@
         // No shell, just run it
         Args args (command);
         const bool first_arg_is_executable = true;
-        const bool first_arg_is_executable_and_argument = true;
-        launch_info.SetArguments(args, first_arg_is_executable, first_arg_is_executable_and_argument);
+        launch_info.SetArguments(args, first_arg_is_executable);
     }
     
     if (working_dir)

Modified: lldb/trunk/source/Target/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Process.cpp?rev=166137&r1=166136&r2=166137&view=diff
==============================================================================
--- lldb/trunk/source/Target/Process.cpp (original)
+++ lldb/trunk/source/Target/Process.cpp Wed Oct 17 17:57:12 2012
@@ -302,9 +302,7 @@
 
 
 void
-ProcessInfo::SetArguments (char const **argv,
-                           bool first_arg_is_executable,
-                           bool first_arg_is_executable_and_argument)
+ProcessInfo::SetArguments (char const **argv, bool first_arg_is_executable)
 {
     m_arguments.SetArguments (argv);
         
@@ -319,18 +317,11 @@
             // could be a remote platform path
             const bool resolve = false;
             m_executable.SetFile(first_arg, resolve); 
-            
-            // If argument zero is an executable and shouldn't be included
-            // in the arguments, remove it from the front of the arguments
-            if (first_arg_is_executable_and_argument == false)
-                m_arguments.DeleteArgumentAtIndex (0);
         }
     }
 }
 void
-ProcessInfo::SetArguments (const Args& args, 
-                           bool first_arg_is_executable,
-                           bool first_arg_is_executable_and_argument)
+ProcessInfo::SetArguments (const Args& args, bool first_arg_is_executable)
 {
     // Copy all arguments
     m_arguments = args;
@@ -346,11 +337,6 @@
             // could be a remote platform path
             const bool resolve = false;
             m_executable.SetFile(first_arg, resolve); 
-    
-            // If argument zero is an executable and shouldn't be included
-            // in the arguments, remove it from the front of the arguments
-            if (first_arg_is_executable_and_argument == false)
-                m_arguments.DeleteArgumentAtIndex (0);
         }
     }
 }
@@ -443,19 +429,57 @@
                 shell_executable = shell_resolved_path;
             }
             
+            const char **argv = GetArguments().GetConstArgumentVector ();
+            if (argv == NULL || argv[0] == NULL)
+                return false;
             Args shell_arguments;
             std::string safe_arg;
             shell_arguments.AppendArgument (shell_executable);
             shell_arguments.AppendArgument ("-c");
-
             StreamString shell_command;
             if (will_debug)
             {
+                // Add a modified PATH environment variable in case argv[0]
+                // is a relative path
+                const char *argv0 = argv[0];
+                if (argv0 && (argv0[0] != '/' && argv0[0] != '~'))
+                {
+                    // We have a relative path to our executable which may not work if
+                    // we just try to run "a.out" (without it being converted to "./a.out")
+                    const char *working_dir = GetWorkingDirectory();
+                    std::string new_path("PATH=");
+                    const size_t empty_path_len = new_path.size();
+                    
+                    if (working_dir && working_dir[0])
+                    {
+                        new_path += working_dir;
+                    }
+                    else
+                    {
+                        char current_working_dir[PATH_MAX];
+                        const char *cwd = getcwd(current_working_dir, sizeof(current_working_dir));
+                        if (cwd && cwd[0])
+                            new_path += cwd;
+                    }
+                    const char *curr_path = getenv("PATH");
+                    if (curr_path)
+                    {
+                        if (new_path.size() > empty_path_len)
+                            new_path += ':';
+                        new_path += curr_path;
+                    }
+                    new_path += ' ';
+                    shell_command.PutCString(new_path.c_str());
+                }
+
                 shell_command.PutCString ("exec");
+
+#if defined(__APPLE__)
+                // Only Apple supports /usr/bin/arch being able to specify the architecture
                 if (GetArchitecture().IsValid())
                 {
                     shell_command.Printf(" /usr/bin/arch -arch %s", GetArchitecture().GetArchitectureName());
-                    // Set the resume count to 2: 
+                    // Set the resume count to 2:
                     // 1 - stop in shell
                     // 2 - stop in /usr/bin/arch
                     // 3 - then we will stop in our program
@@ -463,39 +487,36 @@
                 }
                 else
                 {
-                    // Set the resume count to 1: 
+                    // Set the resume count to 1:
                     // 1 - stop in shell
                     // 2 - then we will stop in our program
                     SetResumeCount(1);
                 }
+#else
+                // Set the resume count to 1:
+                // 1 - stop in shell
+                // 2 - then we will stop in our program
+                SetResumeCount(1);
+#endif
             }
-            
-            const char **argv = GetArguments().GetConstArgumentVector ();
-            if (argv)
+        
+            if (first_arg_is_full_shell_command)
             {
-                if (first_arg_is_full_shell_command)
-                {
-                    // There should only be one argument that is the shell command itself to be used as is
-                    if (argv[0] && !argv[1])
-                        shell_command.Printf("%s", argv[0]);
-                    else
-                        return false;
-                }
+                // There should only be one argument that is the shell command itself to be used as is
+                if (argv[0] && !argv[1])
+                    shell_command.Printf("%s", argv[0]);
                 else
-                {
-                    for (size_t i=0; argv[i] != NULL; ++i)
-                    {
-                        const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
-                        shell_command.Printf(" %s", arg);
-                    }
-                }
-                shell_arguments.AppendArgument (shell_command.GetString().c_str());
+                    return false;
             }
             else
             {
-                return false;
+                for (size_t i=0; argv[i] != NULL; ++i)
+                {
+                    const char *arg = Args::GetShellSafeArgument (argv[i], safe_arg);
+                    shell_command.Printf(" %s", arg);
+                }
             }
-
+            shell_arguments.AppendArgument (shell_command.GetString().c_str());
             m_executable.SetFile(shell_executable, false);
             m_arguments = shell_arguments;
             return true;

Modified: lldb/trunk/source/Target/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Target.cpp?rev=166137&r1=166136&r2=166137&view=diff
==============================================================================
--- lldb/trunk/source/Target/Target.cpp (original)
+++ lldb/trunk/source/Target/Target.cpp Wed Oct 17 17:57:12 2012
@@ -2151,7 +2151,8 @@
     { "max-children-count"                 , OptionValue::eTypeSInt64    , false, 256                       , NULL, NULL, "Maximum number of children to expand in any level of depth." },
     { "max-string-summary-length"          , OptionValue::eTypeSInt64    , false, 1024                      , NULL, NULL, "Maximum number of characters to show when using %s in summary strings." },
     { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean   , false, true                      , NULL, NULL, "Consult the platform module avoid list when setting non-module specific breakpoints." },
-    { "run-args"                           , OptionValue::eTypeArgs      , false, 0                         , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run." },
+    { "arg0"                               , OptionValue::eTypeString    , false, 0                         , NULL, NULL, "The first argument passed to the program in the argument array which can be different from the executable itself." },
+    { "run-args"                           , OptionValue::eTypeArgs      , false, 0                         , NULL, NULL, "A list containing all the arguments to be passed to the executable when it is run. Note that this does NOT include the argv[0] which is in target.arg0." },
     { "env-vars"                           , OptionValue::eTypeDictionary, false, OptionValue::eTypeString  , NULL, NULL, "A list of all the environment variables to be passed to the executable's environment, and their values." },
     { "inherit-env"                        , OptionValue::eTypeBoolean   , false, true                      , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
     { "input-path"                         , OptionValue::eTypeFileSpec  , false, 0                         , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
@@ -2180,6 +2181,7 @@
     ePropertyMaxChildrenCount,
     ePropertyMaxSummaryLength,
     ePropertyBreakpointUseAvoidList,
+    ePropertyArg0,
     ePropertyRunArgs,
     ePropertyEnvVars,
     ePropertyInheritEnv,
@@ -2371,6 +2373,20 @@
     return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
 }
 
+const char *
+TargetProperties::GetArg0 () const
+{
+    const uint32_t idx = ePropertyArg0;
+    return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
+}
+
+void
+TargetProperties::SetArg0 (const char *arg)
+{
+    const uint32_t idx = ePropertyArg0;
+    m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
+}
+
 bool
 TargetProperties::GetRunArguments (Args &args) const
 {

Modified: lldb/trunk/test/expression_command/test/TestExprs.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/expression_command/test/TestExprs.py?rev=166137&r1=166136&r2=166137&view=diff
==============================================================================
--- lldb/trunk/test/expression_command/test/TestExprs.py (original)
+++ lldb/trunk/test/expression_command/test/TestExprs.py Wed Oct 17 17:57:12 2012
@@ -71,8 +71,8 @@
         # (const char *) $7 = ...
 
         self.expect("expression argv[0]",
-            substrs = ["(const char *)",
-                       os.path.join(self.mydir, "a.out")])
+            substrs = ["(const char *)", 
+                       "a.out"])
         # (const char *) $8 = 0x... "/Volumes/data/lldb/svn/trunk/test/expression_command/test/a.out"
 
     @python_api_test





More information about the lldb-commits mailing list