<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.
git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@166137 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Commands/CommandObjectPlatform.cpp b/source/Commands/CommandObjectPlatform.cpp
index 64c05af..c97abf0 100644
--- a/source/Commands/CommandObjectPlatform.cpp
+++ b/source/Commands/CommandObjectPlatform.cpp
@@ -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);
}
}
diff --git a/source/Commands/CommandObjectProcess.cpp b/source/Commands/CommandObjectProcess.cpp
index 41e216e..441aee9 100644
--- a/source/Commands/CommandObjectProcess.cpp
+++ b/source/Commands/CommandObjectProcess.cpp
@@ -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())
diff --git a/source/Commands/CommandObjectTarget.cpp b/source/Commands/CommandObjectTarget.cpp
index 6f77106..8c46487 100644
--- a/source/Commands/CommandObjectTarget.cpp
+++ b/source/Commands/CommandObjectTarget.cpp
@@ -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)
{
diff --git a/source/Host/common/Host.cpp b/source/Host/common/Host.cpp
index 2b9ae59..b25f8bc 100644
--- a/source/Host/common/Host.cpp
+++ b/source/Host/common/Host.cpp
@@ -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)
diff --git a/source/Target/Process.cpp b/source/Target/Process.cpp
index e77a898..dd28cc1 100644
--- a/source/Target/Process.cpp
+++ b/source/Target/Process.cpp
@@ -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;
diff --git a/source/Target/Target.cpp b/source/Target/Target.cpp
index 0dd3463..3305373 100644
--- a/source/Target/Target.cpp
+++ b/source/Target/Target.cpp
@@ -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
{