Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 1 | //===-- ProcessLaunchInfo.cpp -----------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 9 | #include <climits> |
Todd Fiala | 2850b1b | 2014-06-30 23:51:35 +0000 | [diff] [blame] | 10 | |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 11 | #include "lldb/Host/Config.h" |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 12 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 13 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 14 | #include "lldb/Target/FileAction.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 15 | #include "lldb/Target/ProcessLaunchInfo.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 16 | #include "lldb/Utility/Log.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/StreamString.h" |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 18 | |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ConvertUTF.h" |
Pavel Labath | 1d5855b | 2017-01-23 15:56:45 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FileSystem.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 21 | |
| 22 | #if !defined(_WIN32) |
| 23 | #include <limits.h> |
| 24 | #endif |
| 25 | |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | |
| 29 | //---------------------------------------------------------------------------- |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 30 | // ProcessLaunchInfo member functions |
| 31 | //---------------------------------------------------------------------------- |
| 32 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | ProcessLaunchInfo::ProcessLaunchInfo() |
| 34 | : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(0), |
Pavel Labath | 07d6f88 | 2017-12-11 10:09:14 +0000 | [diff] [blame] | 35 | m_file_actions(), m_pty(new PseudoTerminal), m_resume_count(0), |
| 36 | m_monitor_callback(nullptr), m_monitor_callback_baton(nullptr), |
| 37 | m_monitor_signals(false), m_listener_sp(), m_hijack_listener_sp() {} |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 38 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 39 | ProcessLaunchInfo::ProcessLaunchInfo(const FileSpec &stdin_file_spec, |
| 40 | const FileSpec &stdout_file_spec, |
| 41 | const FileSpec &stderr_file_spec, |
| 42 | const FileSpec &working_directory, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 43 | uint32_t launch_flags) |
| 44 | : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(launch_flags), |
Pavel Labath | 07d6f88 | 2017-12-11 10:09:14 +0000 | [diff] [blame] | 45 | m_file_actions(), m_pty(new PseudoTerminal), m_resume_count(0), |
| 46 | m_monitor_callback(nullptr), m_monitor_callback_baton(nullptr), |
| 47 | m_monitor_signals(false), m_listener_sp(), m_hijack_listener_sp() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | if (stdin_file_spec) { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 49 | FileAction file_action; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | const bool read = true; |
| 51 | const bool write = false; |
| 52 | if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write)) |
| 53 | AppendFileAction(file_action); |
| 54 | } |
| 55 | if (stdout_file_spec) { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 56 | FileAction file_action; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | const bool read = false; |
| 58 | const bool write = true; |
| 59 | if (file_action.Open(STDOUT_FILENO, stdout_file_spec, read, write)) |
| 60 | AppendFileAction(file_action); |
| 61 | } |
| 62 | if (stderr_file_spec) { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 63 | FileAction file_action; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | const bool read = false; |
| 65 | const bool write = true; |
| 66 | if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write)) |
| 67 | AppendFileAction(file_action); |
| 68 | } |
| 69 | if (working_directory) |
| 70 | SetWorkingDirectory(working_directory); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | bool ProcessLaunchInfo::AppendCloseFileAction(int fd) { |
| 74 | FileAction file_action; |
| 75 | if (file_action.Close(fd)) { |
| 76 | AppendFileAction(file_action); |
| 77 | return true; |
| 78 | } |
| 79 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | bool ProcessLaunchInfo::AppendDuplicateFileAction(int fd, int dup_fd) { |
| 83 | FileAction file_action; |
| 84 | if (file_action.Duplicate(fd, dup_fd)) { |
| 85 | AppendFileAction(file_action); |
| 86 | return true; |
| 87 | } |
| 88 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | bool ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec, |
| 92 | bool read, bool write) { |
| 93 | FileAction file_action; |
| 94 | if (file_action.Open(fd, file_spec, read, write)) { |
| 95 | AppendFileAction(file_action); |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | bool ProcessLaunchInfo::AppendSuppressFileAction(int fd, bool read, |
| 102 | bool write) { |
| 103 | FileAction file_action; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 104 | if (file_action.Open(fd, FileSpec(FileSystem::DEV_NULL), read, write)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | AppendFileAction(file_action); |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | const FileAction *ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const { |
| 112 | if (idx < m_file_actions.size()) |
| 113 | return &m_file_actions[idx]; |
| 114 | return nullptr; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | const FileAction *ProcessLaunchInfo::GetFileActionForFD(int fd) const { |
| 118 | for (size_t idx = 0, count = m_file_actions.size(); idx < count; ++idx) { |
| 119 | if (m_file_actions[idx].GetFD() == fd) |
| 120 | return &m_file_actions[idx]; |
| 121 | } |
| 122 | return nullptr; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | const FileSpec &ProcessLaunchInfo::GetWorkingDirectory() const { |
| 126 | return m_working_dir; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | void ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir) { |
| 130 | m_working_dir = working_dir; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | const char *ProcessLaunchInfo::GetProcessPluginName() const { |
| 134 | return (m_plugin_name.empty() ? nullptr : m_plugin_name.c_str()); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Zachary Turner | fe11483 | 2016-11-12 16:56:47 +0000 | [diff] [blame] | 137 | void ProcessLaunchInfo::SetProcessPluginName(llvm::StringRef plugin) { |
| 138 | m_plugin_name = plugin; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | const FileSpec &ProcessLaunchInfo::GetShell() const { return m_shell; } |
| 142 | |
| 143 | void ProcessLaunchInfo::SetShell(const FileSpec &shell) { |
| 144 | m_shell = shell; |
| 145 | if (m_shell) { |
Jonas Devlieghere | 2c22c80 | 2018-11-01 17:09:22 +0000 | [diff] [blame] | 146 | FileSystem::Instance().ResolveExecutableLocation(m_shell); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | m_flags.Set(lldb::eLaunchFlagLaunchInShell); |
| 148 | } else |
| 149 | m_flags.Clear(lldb::eLaunchFlagLaunchInShell); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | void ProcessLaunchInfo::SetLaunchInSeparateProcessGroup(bool separate) { |
| 153 | if (separate) |
| 154 | m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup); |
| 155 | else |
| 156 | m_flags.Clear(lldb::eLaunchFlagLaunchInSeparateProcessGroup); |
| 157 | } |
| 158 | |
| 159 | void ProcessLaunchInfo::SetShellExpandArguments(bool expand) { |
| 160 | if (expand) |
| 161 | m_flags.Set(lldb::eLaunchFlagShellExpandArguments); |
| 162 | else |
| 163 | m_flags.Clear(lldb::eLaunchFlagShellExpandArguments); |
| 164 | } |
| 165 | |
| 166 | void ProcessLaunchInfo::Clear() { |
| 167 | ProcessInfo::Clear(); |
| 168 | m_working_dir.Clear(); |
| 169 | m_plugin_name.clear(); |
| 170 | m_shell.Clear(); |
| 171 | m_flags.Clear(); |
| 172 | m_file_actions.clear(); |
| 173 | m_resume_count = 0; |
| 174 | m_listener_sp.reset(); |
| 175 | m_hijack_listener_sp.reset(); |
| 176 | } |
| 177 | |
| 178 | void ProcessLaunchInfo::SetMonitorProcessCallback( |
| 179 | const Host::MonitorChildProcessCallback &callback, bool monitor_signals) { |
| 180 | m_monitor_callback = callback; |
| 181 | m_monitor_signals = monitor_signals; |
| 182 | } |
| 183 | |
Pavel Labath | 245dd2e | 2018-05-15 13:42:26 +0000 | [diff] [blame] | 184 | bool ProcessLaunchInfo::NoOpMonitorCallback(lldb::pid_t pid, bool exited, int signal, int status) { |
| 185 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS); |
| 186 | LLDB_LOG(log, "pid = {0}, exited = {1}, signal = {2}, status = {3}", pid, |
| 187 | exited, signal, status); |
| 188 | return true; |
| 189 | } |
| 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | bool ProcessLaunchInfo::MonitorProcess() const { |
| 192 | if (m_monitor_callback && ProcessIDIsValid()) { |
| 193 | Host::StartMonitoringChildProcess(m_monitor_callback, GetProcessID(), |
| 194 | m_monitor_signals); |
| 195 | return true; |
| 196 | } |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | void ProcessLaunchInfo::SetDetachOnError(bool enable) { |
| 201 | if (enable) |
| 202 | m_flags.Set(lldb::eLaunchFlagDetachOnError); |
| 203 | else |
| 204 | m_flags.Clear(lldb::eLaunchFlagDetachOnError); |
| 205 | } |
| 206 | |
Pavel Labath | 8e55dde | 2019-01-08 11:55:19 +0000 | [diff] [blame] | 207 | llvm::Error ProcessLaunchInfo::SetUpPtyRedirection() { |
| 208 | Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS); |
| 209 | LLDB_LOG(log, "Generating a pty to use for stdin/out/err"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 210 | |
Pavel Labath | 8e55dde | 2019-01-08 11:55:19 +0000 | [diff] [blame] | 211 | int open_flags = O_RDWR | O_NOCTTY; |
Hafiz Abid Qadeer | f6ee79c | 2016-12-15 15:00:41 +0000 | [diff] [blame] | 212 | #if !defined(_WIN32) |
Pavel Labath | 8e55dde | 2019-01-08 11:55:19 +0000 | [diff] [blame] | 213 | // We really shouldn't be specifying platform specific flags that are |
| 214 | // intended for a system call in generic code. But this will have to |
| 215 | // do for now. |
| 216 | open_flags |= O_CLOEXEC; |
Zachary Turner | 362a813 | 2015-02-04 19:11:48 +0000 | [diff] [blame] | 217 | #endif |
Pavel Labath | 8e55dde | 2019-01-08 11:55:19 +0000 | [diff] [blame] | 218 | if (!m_pty->OpenFirstAvailableMaster(open_flags, nullptr, 0)) { |
| 219 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 220 | "PTY::OpenFirstAvailableMaster failed"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | } |
Pavel Labath | 8e55dde | 2019-01-08 11:55:19 +0000 | [diff] [blame] | 222 | const FileSpec slave_file_spec(m_pty->GetSlaveName(nullptr, 0)); |
| 223 | |
| 224 | // Only use the slave tty if we don't have anything specified for |
| 225 | // input and don't have an action for stdin |
| 226 | if (GetFileActionForFD(STDIN_FILENO) == nullptr) |
| 227 | AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false); |
| 228 | |
| 229 | // Only use the slave tty if we don't have anything specified for |
| 230 | // output and don't have an action for stdout |
| 231 | if (GetFileActionForFD(STDOUT_FILENO) == nullptr) |
| 232 | AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true); |
| 233 | |
| 234 | // Only use the slave tty if we don't have anything specified for |
| 235 | // error and don't have an action for stderr |
| 236 | if (GetFileActionForFD(STDERR_FILENO) == nullptr) |
| 237 | AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true); |
| 238 | return llvm::Error::success(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | bool ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell( |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 242 | Status &error, bool localhost, bool will_debug, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | bool first_arg_is_full_shell_command, int32_t num_resumes) { |
| 244 | error.Clear(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 245 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 246 | if (GetFlags().Test(eLaunchFlagLaunchInShell)) { |
| 247 | if (m_shell) { |
| 248 | std::string shell_executable = m_shell.GetPath(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 249 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | const char **argv = GetArguments().GetConstArgumentVector(); |
| 251 | if (argv == nullptr || argv[0] == nullptr) |
| 252 | return false; |
| 253 | Args shell_arguments; |
| 254 | std::string safe_arg; |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 255 | shell_arguments.AppendArgument(shell_executable); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | const llvm::Triple &triple = GetArchitecture().GetTriple(); |
| 257 | if (triple.getOS() == llvm::Triple::Win32 && |
| 258 | !triple.isWindowsCygwinEnvironment()) |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 259 | shell_arguments.AppendArgument(llvm::StringRef("/C")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 260 | else |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 261 | shell_arguments.AppendArgument(llvm::StringRef("-c")); |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 262 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 263 | StreamString shell_command; |
| 264 | if (will_debug) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 265 | // Add a modified PATH environment variable in case argv[0] is a |
| 266 | // relative path. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | const char *argv0 = argv[0]; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 268 | FileSpec arg_spec(argv0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | if (arg_spec.IsRelative()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 270 | // We have a relative path to our executable which may not work if we |
| 271 | // just try to run "a.out" (without it being converted to "./a.out") |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | FileSpec working_dir = GetWorkingDirectory(); |
| 273 | // Be sure to put quotes around PATH's value in case any paths have |
| 274 | // spaces... |
| 275 | std::string new_path("PATH=\""); |
| 276 | const size_t empty_path_len = new_path.size(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | if (working_dir) { |
| 279 | new_path += working_dir.GetPath(); |
| 280 | } else { |
Pavel Labath | 1d5855b | 2017-01-23 15:56:45 +0000 | [diff] [blame] | 281 | llvm::SmallString<64> cwd; |
| 282 | if (! llvm::sys::fs::current_path(cwd)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 283 | new_path += cwd; |
| 284 | } |
| 285 | std::string curr_path; |
| 286 | if (HostInfo::GetEnvironmentVar("PATH", curr_path)) { |
| 287 | if (new_path.size() > empty_path_len) |
| 288 | new_path += ':'; |
| 289 | new_path += curr_path; |
| 290 | } |
| 291 | new_path += "\" "; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 292 | shell_command.PutCString(new_path); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 293 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | |
| 295 | if (triple.getOS() != llvm::Triple::Win32 || |
| 296 | triple.isWindowsCygwinEnvironment()) |
| 297 | shell_command.PutCString("exec"); |
| 298 | |
| 299 | // Only Apple supports /usr/bin/arch being able to specify the |
| 300 | // architecture |
| 301 | if (GetArchitecture().IsValid() && // Valid architecture |
| 302 | GetArchitecture().GetTriple().getVendor() == |
| 303 | llvm::Triple::Apple && // Apple only |
| 304 | GetArchitecture().GetCore() != |
| 305 | ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h |
| 306 | { |
| 307 | shell_command.Printf(" /usr/bin/arch -arch %s", |
| 308 | GetArchitecture().GetArchitectureName()); |
| 309 | // Set the resume count to 2: |
| 310 | // 1 - stop in shell |
| 311 | // 2 - stop in /usr/bin/arch |
| 312 | // 3 - then we will stop in our program |
| 313 | SetResumeCount(num_resumes + 1); |
| 314 | } else { |
| 315 | // Set the resume count to 1: |
| 316 | // 1 - stop in shell |
| 317 | // 2 - then we will stop in our program |
| 318 | SetResumeCount(num_resumes); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (first_arg_is_full_shell_command) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 323 | // There should only be one argument that is the shell command itself |
| 324 | // to be used as is |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 325 | if (argv[0] && !argv[1]) |
| 326 | shell_command.Printf("%s", argv[0]); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 327 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 328 | return false; |
| 329 | } else { |
| 330 | for (size_t i = 0; argv[i] != nullptr; ++i) { |
| 331 | const char *arg = |
| 332 | Args::GetShellSafeArgument(m_shell, argv[i], safe_arg); |
| 333 | shell_command.Printf(" %s", arg); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 334 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 335 | } |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 336 | shell_arguments.AppendArgument(shell_command.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 337 | m_executable = m_shell; |
| 338 | m_arguments = shell_arguments; |
| 339 | return true; |
| 340 | } else { |
| 341 | error.SetErrorString("invalid shell path"); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 342 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 343 | } else { |
| 344 | error.SetErrorString("not launching in shell"); |
| 345 | } |
| 346 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 347 | } |