Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 1 | //===-- ProcessLaunchInfo.cpp -----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | #include <climits> |
Todd Fiala | 2850b1b | 2014-06-30 23:51:35 +0000 | [diff] [blame] | 13 | |
Eugene Zelenko | 9394d772 | 2016-02-18 00:10:17 +0000 | [diff] [blame] | 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Debugger.h" |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Log.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 18 | #include "lldb/Host/Config.h" |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame] | 19 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 20 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 696b528 | 2014-08-14 16:01:25 +0000 | [diff] [blame] | 21 | #include "lldb/Target/FileAction.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 22 | #include "lldb/Target/ProcessLaunchInfo.h" |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Target.h" |
| 24 | |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ConvertUTF.h" |
| 26 | |
| 27 | #if !defined(_WIN32) |
| 28 | #include <limits.h> |
| 29 | #endif |
| 30 | |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
| 34 | //---------------------------------------------------------------------------- |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 35 | // ProcessLaunchInfo member functions |
| 36 | //---------------------------------------------------------------------------- |
| 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | ProcessLaunchInfo::ProcessLaunchInfo() |
| 39 | : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(0), |
| 40 | m_file_actions(), m_pty(new lldb_utility::PseudoTerminal), |
| 41 | m_resume_count(0), m_monitor_callback(nullptr), |
| 42 | m_monitor_callback_baton(nullptr), m_monitor_signals(false), |
| 43 | m_listener_sp(), m_hijack_listener_sp() {} |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 44 | |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 45 | ProcessLaunchInfo::ProcessLaunchInfo(const FileSpec &stdin_file_spec, |
| 46 | const FileSpec &stdout_file_spec, |
| 47 | const FileSpec &stderr_file_spec, |
| 48 | const FileSpec &working_directory, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | uint32_t launch_flags) |
| 50 | : ProcessInfo(), m_working_dir(), m_plugin_name(), m_flags(launch_flags), |
| 51 | m_file_actions(), m_pty(new lldb_utility::PseudoTerminal), |
| 52 | m_resume_count(0), m_monitor_callback(nullptr), |
| 53 | m_monitor_callback_baton(nullptr), m_monitor_signals(false), |
| 54 | m_listener_sp(), m_hijack_listener_sp() { |
| 55 | if (stdin_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 = true; |
| 58 | const bool write = false; |
| 59 | if (file_action.Open(STDIN_FILENO, stdin_file_spec, read, write)) |
| 60 | AppendFileAction(file_action); |
| 61 | } |
| 62 | if (stdout_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(STDOUT_FILENO, stdout_file_spec, read, write)) |
| 67 | AppendFileAction(file_action); |
| 68 | } |
| 69 | if (stderr_file_spec) { |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 70 | FileAction file_action; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | const bool read = false; |
| 72 | const bool write = true; |
| 73 | if (file_action.Open(STDERR_FILENO, stderr_file_spec, read, write)) |
| 74 | AppendFileAction(file_action); |
| 75 | } |
| 76 | if (working_directory) |
| 77 | SetWorkingDirectory(working_directory); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | bool ProcessLaunchInfo::AppendCloseFileAction(int fd) { |
| 81 | FileAction file_action; |
| 82 | if (file_action.Close(fd)) { |
| 83 | AppendFileAction(file_action); |
| 84 | return true; |
| 85 | } |
| 86 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | bool ProcessLaunchInfo::AppendDuplicateFileAction(int fd, int dup_fd) { |
| 90 | FileAction file_action; |
| 91 | if (file_action.Duplicate(fd, dup_fd)) { |
| 92 | AppendFileAction(file_action); |
| 93 | return true; |
| 94 | } |
| 95 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | bool ProcessLaunchInfo::AppendOpenFileAction(int fd, const FileSpec &file_spec, |
| 99 | bool read, bool write) { |
| 100 | FileAction file_action; |
| 101 | if (file_action.Open(fd, file_spec, read, write)) { |
| 102 | AppendFileAction(file_action); |
| 103 | return true; |
| 104 | } |
| 105 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | bool ProcessLaunchInfo::AppendSuppressFileAction(int fd, bool read, |
| 109 | bool write) { |
| 110 | FileAction file_action; |
| 111 | if (file_action.Open(fd, FileSpec{FileSystem::DEV_NULL, false}, read, |
| 112 | write)) { |
| 113 | AppendFileAction(file_action); |
| 114 | return true; |
| 115 | } |
| 116 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | const FileAction *ProcessLaunchInfo::GetFileActionAtIndex(size_t idx) const { |
| 120 | if (idx < m_file_actions.size()) |
| 121 | return &m_file_actions[idx]; |
| 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 FileAction *ProcessLaunchInfo::GetFileActionForFD(int fd) const { |
| 126 | for (size_t idx = 0, count = m_file_actions.size(); idx < count; ++idx) { |
| 127 | if (m_file_actions[idx].GetFD() == fd) |
| 128 | return &m_file_actions[idx]; |
| 129 | } |
| 130 | return nullptr; |
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 FileSpec &ProcessLaunchInfo::GetWorkingDirectory() const { |
| 134 | return m_working_dir; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | void ProcessLaunchInfo::SetWorkingDirectory(const FileSpec &working_dir) { |
| 138 | m_working_dir = working_dir; |
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 char *ProcessLaunchInfo::GetProcessPluginName() const { |
| 142 | return (m_plugin_name.empty() ? nullptr : m_plugin_name.c_str()); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | void ProcessLaunchInfo::SetProcessPluginName(const char *plugin) { |
| 146 | if (plugin && plugin[0]) |
| 147 | m_plugin_name.assign(plugin); |
| 148 | else |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 149 | m_plugin_name.clear(); |
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 | const FileSpec &ProcessLaunchInfo::GetShell() const { return m_shell; } |
| 153 | |
| 154 | void ProcessLaunchInfo::SetShell(const FileSpec &shell) { |
| 155 | m_shell = shell; |
| 156 | if (m_shell) { |
| 157 | m_shell.ResolveExecutableLocation(); |
| 158 | m_flags.Set(lldb::eLaunchFlagLaunchInShell); |
| 159 | } else |
| 160 | m_flags.Clear(lldb::eLaunchFlagLaunchInShell); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | void ProcessLaunchInfo::SetLaunchInSeparateProcessGroup(bool separate) { |
| 164 | if (separate) |
| 165 | m_flags.Set(lldb::eLaunchFlagLaunchInSeparateProcessGroup); |
| 166 | else |
| 167 | m_flags.Clear(lldb::eLaunchFlagLaunchInSeparateProcessGroup); |
| 168 | } |
| 169 | |
| 170 | void ProcessLaunchInfo::SetShellExpandArguments(bool expand) { |
| 171 | if (expand) |
| 172 | m_flags.Set(lldb::eLaunchFlagShellExpandArguments); |
| 173 | else |
| 174 | m_flags.Clear(lldb::eLaunchFlagShellExpandArguments); |
| 175 | } |
| 176 | |
| 177 | void ProcessLaunchInfo::Clear() { |
| 178 | ProcessInfo::Clear(); |
| 179 | m_working_dir.Clear(); |
| 180 | m_plugin_name.clear(); |
| 181 | m_shell.Clear(); |
| 182 | m_flags.Clear(); |
| 183 | m_file_actions.clear(); |
| 184 | m_resume_count = 0; |
| 185 | m_listener_sp.reset(); |
| 186 | m_hijack_listener_sp.reset(); |
| 187 | } |
| 188 | |
| 189 | void ProcessLaunchInfo::SetMonitorProcessCallback( |
| 190 | const Host::MonitorChildProcessCallback &callback, bool monitor_signals) { |
| 191 | m_monitor_callback = callback; |
| 192 | m_monitor_signals = monitor_signals; |
| 193 | } |
| 194 | |
| 195 | bool ProcessLaunchInfo::MonitorProcess() const { |
| 196 | if (m_monitor_callback && ProcessIDIsValid()) { |
| 197 | Host::StartMonitoringChildProcess(m_monitor_callback, GetProcessID(), |
| 198 | m_monitor_signals); |
| 199 | return true; |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | void ProcessLaunchInfo::SetDetachOnError(bool enable) { |
| 205 | if (enable) |
| 206 | m_flags.Set(lldb::eLaunchFlagDetachOnError); |
| 207 | else |
| 208 | m_flags.Clear(lldb::eLaunchFlagDetachOnError); |
| 209 | } |
| 210 | |
| 211 | void ProcessLaunchInfo::FinalizeFileActions(Target *target, |
| 212 | bool default_to_use_pty) { |
| 213 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)); |
| 214 | |
| 215 | // If nothing for stdin or stdout or stderr was specified, then check the |
| 216 | // process for any default |
| 217 | // settings that were set with "settings set" |
| 218 | if (GetFileActionForFD(STDIN_FILENO) == nullptr || |
| 219 | GetFileActionForFD(STDOUT_FILENO) == nullptr || |
| 220 | GetFileActionForFD(STDERR_FILENO) == nullptr) { |
| 221 | if (log) |
| 222 | log->Printf("ProcessLaunchInfo::%s at least one of stdin/stdout/stderr " |
| 223 | "was not set, evaluating default handling", |
| 224 | __FUNCTION__); |
| 225 | |
| 226 | if (m_flags.Test(eLaunchFlagLaunchInTTY)) { |
| 227 | // Do nothing, if we are launching in a remote terminal |
| 228 | // no file actions should be done at all. |
| 229 | return; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 230 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 231 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 232 | if (m_flags.Test(eLaunchFlagDisableSTDIO)) { |
| 233 | if (log) |
| 234 | log->Printf("ProcessLaunchInfo::%s eLaunchFlagDisableSTDIO set, adding " |
| 235 | "suppression action for stdin, stdout and stderr", |
| 236 | __FUNCTION__); |
| 237 | AppendSuppressFileAction(STDIN_FILENO, true, false); |
| 238 | AppendSuppressFileAction(STDOUT_FILENO, false, true); |
| 239 | AppendSuppressFileAction(STDERR_FILENO, false, true); |
| 240 | } else { |
| 241 | // Check for any values that might have gotten set with any of: |
| 242 | // (lldb) settings set target.input-path |
| 243 | // (lldb) settings set target.output-path |
| 244 | // (lldb) settings set target.error-path |
| 245 | FileSpec in_file_spec; |
| 246 | FileSpec out_file_spec; |
| 247 | FileSpec err_file_spec; |
| 248 | if (target) { |
| 249 | // Only override with the target settings if we don't already have |
| 250 | // an action for in, out or error |
| 251 | if (GetFileActionForFD(STDIN_FILENO) == nullptr) |
| 252 | in_file_spec = target->GetStandardInputPath(); |
| 253 | if (GetFileActionForFD(STDOUT_FILENO) == nullptr) |
| 254 | out_file_spec = target->GetStandardOutputPath(); |
| 255 | if (GetFileActionForFD(STDERR_FILENO) == nullptr) |
| 256 | err_file_spec = target->GetStandardErrorPath(); |
| 257 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 258 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 259 | if (log) |
| 260 | log->Printf("ProcessLaunchInfo::%s target stdin='%s', target " |
| 261 | "stdout='%s', stderr='%s'", |
| 262 | __FUNCTION__, |
| 263 | in_file_spec ? in_file_spec.GetCString() : "<null>", |
| 264 | out_file_spec ? out_file_spec.GetCString() : "<null>", |
| 265 | err_file_spec ? err_file_spec.GetCString() : "<null>"); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 266 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | if (in_file_spec) { |
| 268 | AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 269 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | log->Printf( |
| 271 | "ProcessLaunchInfo::%s appended stdin open file action for %s", |
| 272 | __FUNCTION__, in_file_spec.GetCString()); |
| 273 | } |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 274 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 275 | if (out_file_spec) { |
| 276 | AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true); |
| 277 | if (log) |
| 278 | log->Printf( |
| 279 | "ProcessLaunchInfo::%s appended stdout open file action for %s", |
| 280 | __FUNCTION__, out_file_spec.GetCString()); |
| 281 | } |
Greg Clayton | bf91f71 | 2015-07-10 18:04:46 +0000 | [diff] [blame] | 282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 283 | if (err_file_spec) { |
| 284 | AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true); |
| 285 | if (log) |
| 286 | log->Printf( |
| 287 | "ProcessLaunchInfo::%s appended stderr open file action for %s", |
| 288 | __FUNCTION__, err_file_spec.GetCString()); |
| 289 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 291 | if (default_to_use_pty && |
| 292 | (!in_file_spec || !out_file_spec || !err_file_spec)) { |
| 293 | if (log) |
| 294 | log->Printf("ProcessLaunchInfo::%s default_to_use_pty is set, and at " |
| 295 | "least one stdin/stderr/stdout is unset, so generating a " |
| 296 | "pty to use for it", |
| 297 | __FUNCTION__); |
Todd Fiala | 75f47c3 | 2014-10-11 21:42:09 +0000 | [diff] [blame] | 298 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 299 | int open_flags = O_RDWR | O_NOCTTY; |
Zachary Turner | 362a813 | 2015-02-04 19:11:48 +0000 | [diff] [blame] | 300 | #if !defined(_MSC_VER) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 301 | // We really shouldn't be specifying platform specific flags |
| 302 | // that are intended for a system call in generic code. But |
| 303 | // this will have to do for now. |
| 304 | open_flags |= O_CLOEXEC; |
Zachary Turner | 362a813 | 2015-02-04 19:11:48 +0000 | [diff] [blame] | 305 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 306 | if (m_pty->OpenFirstAvailableMaster(open_flags, nullptr, 0)) { |
| 307 | const FileSpec slave_file_spec{m_pty->GetSlaveName(nullptr, 0), |
| 308 | false}; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 309 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 310 | // Only use the slave tty if we don't have anything specified for |
| 311 | // input and don't have an action for stdin |
| 312 | if (!in_file_spec && GetFileActionForFD(STDIN_FILENO) == nullptr) { |
| 313 | AppendOpenFileAction(STDIN_FILENO, slave_file_spec, true, false); |
| 314 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 315 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 316 | // Only use the slave tty if we don't have anything specified for |
| 317 | // output and don't have an action for stdout |
| 318 | if (!out_file_spec && GetFileActionForFD(STDOUT_FILENO) == nullptr) { |
| 319 | AppendOpenFileAction(STDOUT_FILENO, slave_file_spec, false, true); |
| 320 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 321 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 322 | // Only use the slave tty if we don't have anything specified for |
| 323 | // error and don't have an action for stderr |
| 324 | if (!err_file_spec && GetFileActionForFD(STDERR_FILENO) == nullptr) { |
| 325 | AppendOpenFileAction(STDERR_FILENO, slave_file_spec, false, true); |
| 326 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 327 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 328 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 329 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 330 | } |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 333 | bool ProcessLaunchInfo::ConvertArgumentsForLaunchingInShell( |
| 334 | Error &error, bool localhost, bool will_debug, |
| 335 | bool first_arg_is_full_shell_command, int32_t num_resumes) { |
| 336 | error.Clear(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 337 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 338 | if (GetFlags().Test(eLaunchFlagLaunchInShell)) { |
| 339 | if (m_shell) { |
| 340 | std::string shell_executable = m_shell.GetPath(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | const char **argv = GetArguments().GetConstArgumentVector(); |
| 343 | if (argv == nullptr || argv[0] == nullptr) |
| 344 | return false; |
| 345 | Args shell_arguments; |
| 346 | std::string safe_arg; |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 347 | shell_arguments.AppendArgument(shell_executable); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | const llvm::Triple &triple = GetArchitecture().GetTriple(); |
| 349 | if (triple.getOS() == llvm::Triple::Win32 && |
| 350 | !triple.isWindowsCygwinEnvironment()) |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 351 | shell_arguments.AppendArgument(llvm::StringRef("/C")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 352 | else |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 353 | shell_arguments.AppendArgument(llvm::StringRef("-c")); |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 354 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 355 | StreamString shell_command; |
| 356 | if (will_debug) { |
| 357 | // Add a modified PATH environment variable in case argv[0] |
| 358 | // is a relative path. |
| 359 | const char *argv0 = argv[0]; |
| 360 | FileSpec arg_spec(argv0, false); |
| 361 | if (arg_spec.IsRelative()) { |
| 362 | // We have a relative path to our executable which may not work if |
| 363 | // we just try to run "a.out" (without it being converted to |
| 364 | // "./a.out") |
| 365 | FileSpec working_dir = GetWorkingDirectory(); |
| 366 | // Be sure to put quotes around PATH's value in case any paths have |
| 367 | // spaces... |
| 368 | std::string new_path("PATH=\""); |
| 369 | const size_t empty_path_len = new_path.size(); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 370 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 371 | if (working_dir) { |
| 372 | new_path += working_dir.GetPath(); |
| 373 | } else { |
| 374 | char current_working_dir[PATH_MAX]; |
| 375 | const char *cwd = |
| 376 | getcwd(current_working_dir, sizeof(current_working_dir)); |
| 377 | if (cwd && cwd[0]) |
| 378 | new_path += cwd; |
| 379 | } |
| 380 | std::string curr_path; |
| 381 | if (HostInfo::GetEnvironmentVar("PATH", curr_path)) { |
| 382 | if (new_path.size() > empty_path_len) |
| 383 | new_path += ':'; |
| 384 | new_path += curr_path; |
| 385 | } |
| 386 | new_path += "\" "; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame^] | 387 | shell_command.PutCString(new_path); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 388 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 389 | |
| 390 | if (triple.getOS() != llvm::Triple::Win32 || |
| 391 | triple.isWindowsCygwinEnvironment()) |
| 392 | shell_command.PutCString("exec"); |
| 393 | |
| 394 | // Only Apple supports /usr/bin/arch being able to specify the |
| 395 | // architecture |
| 396 | if (GetArchitecture().IsValid() && // Valid architecture |
| 397 | GetArchitecture().GetTriple().getVendor() == |
| 398 | llvm::Triple::Apple && // Apple only |
| 399 | GetArchitecture().GetCore() != |
| 400 | ArchSpec::eCore_x86_64_x86_64h) // Don't do this for x86_64h |
| 401 | { |
| 402 | shell_command.Printf(" /usr/bin/arch -arch %s", |
| 403 | GetArchitecture().GetArchitectureName()); |
| 404 | // Set the resume count to 2: |
| 405 | // 1 - stop in shell |
| 406 | // 2 - stop in /usr/bin/arch |
| 407 | // 3 - then we will stop in our program |
| 408 | SetResumeCount(num_resumes + 1); |
| 409 | } else { |
| 410 | // Set the resume count to 1: |
| 411 | // 1 - stop in shell |
| 412 | // 2 - then we will stop in our program |
| 413 | SetResumeCount(num_resumes); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (first_arg_is_full_shell_command) { |
| 418 | // There should only be one argument that is the shell command itself to |
| 419 | // be used as is |
| 420 | if (argv[0] && !argv[1]) |
| 421 | shell_command.Printf("%s", argv[0]); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 422 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | return false; |
| 424 | } else { |
| 425 | for (size_t i = 0; argv[i] != nullptr; ++i) { |
| 426 | const char *arg = |
| 427 | Args::GetShellSafeArgument(m_shell, argv[i], safe_arg); |
| 428 | shell_command.Printf(" %s", arg); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 429 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 430 | } |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 431 | shell_arguments.AppendArgument(shell_command.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 432 | m_executable = m_shell; |
| 433 | m_arguments = shell_arguments; |
| 434 | return true; |
| 435 | } else { |
| 436 | error.SetErrorString("invalid shell path"); |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 437 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 438 | } else { |
| 439 | error.SetErrorString("not launching in shell"); |
| 440 | } |
| 441 | return false; |
Todd Fiala | 6d6b55d | 2014-06-30 00:30:53 +0000 | [diff] [blame] | 442 | } |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 443 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | ListenerSP ProcessLaunchInfo::GetListenerForProcess(Debugger &debugger) { |
| 445 | if (m_listener_sp) |
| 446 | return m_listener_sp; |
| 447 | else |
| 448 | return debugger.GetListener(); |
Greg Clayton | 8012cad | 2014-11-17 19:39:20 +0000 | [diff] [blame] | 449 | } |